libSBML Python API  5.18.0
convertLayout.py

Converts SBML Layout and Render datra from SBML Level 2 to Level 3 and vice versa.

1 #!/usr/bin/env python
2 ##
3 ## @file convertLayout.py
4 ## @author Frank T. Bergmann
5 ##
6 ## <!--------------------------------------------------------------------------
7 ## This sample program is distributed under a different license than the rest
8 ## of libSBML. This program uses the open-source MIT license, as follows:
9 ##
10 ## Copyright (c) 2013-2018 by the California Institute of Technology
11 ## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
12 ## and the University of Heidelberg (Germany), with support from the National
13 ## Institutes of Health (USA) under grant R01GM070923. All rights reserved.
14 ##
15 ## Permission is hereby granted, free of charge, to any person obtaining a
16 ## copy of this software and associated documentation files (the "Software"),
17 ## to deal in the Software without restriction, including without limitation
18 ## the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 ## and/or sell copies of the Software, and to permit persons to whom the
20 ## Software is furnished to do so, subject to the following conditions:
21 ##
22 ## The above copyright notice and this permission notice shall be included in
23 ## all copies or substantial portions of the Software.
24 ##
25 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 ## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 ## DEALINGS IN THE SOFTWARE.
32 ##
33 ## Neither the name of the California Institute of Technology (Caltech), nor
34 ## of the European Bioinformatics Institute (EMBL-EBI), nor of the University
35 ## of Heidelberg, nor the names of any contributors, may be used to endorse
36 ## or promote products derived from this software without specific prior
37 ## written permission.
38 ## ------------------------------------------------------------------------ -->
39 
40 import sys
41 from libsbml import *
42 
43 def convertDocToL2(doc, outputFile):
44  layoutNsUri = "http://projects.eml.org/bcb/sbml/level2"
45  layoutNs = LayoutPkgNamespaces(2, 4)
46  renderNsUri = "http://projects.eml.org/bcb/sbml/render/level2"
47  renderNs = RenderPkgNamespaces(2, 4)
48 
49  prop = ConversionProperties(SBMLNamespaces(2,4))
50  prop.addOption('strict', False)
51  prop.addOption('setLevelAndVersion', True)
52  prop.addOption('ignorePackages', True)
53  doc.convert(prop)
54 
55 
56  docPlugin = doc.getPlugin("layout")
57  if docPlugin is not None:
58  docPlugin.setElementNamespace(layoutNsUri)
59 
60  doc.getSBMLNamespaces().removePackageNamespace(3, 1, "layout", 1)
61  doc.getSBMLNamespaces().addPackageNamespace("layout", 1)
62 
63  rdocPlugin = doc.getPlugin("render")
64  if rdocPlugin is not None:
65  rdocPlugin.setElementNamespace(renderNsUri)
66 
67  doc.getSBMLNamespaces().removePackageNamespace(3, 1, "render", 1)
68  doc.getSBMLNamespaces().addPackageNamespace("render", 1)
69 
70  writeSBMLToFile(doc, outputFile)
71 
72 def convertDocToL3(doc, outputFile):
73  layoutNsUri = "http://www.sbml.org/sbml/level3/version1/layout/version1"
74  layoutNs = LayoutPkgNamespaces(3, 1)
75  renderNsUri = "http://www.sbml.org/sbml/level3/version1/render/version1"
76  renderNs = RenderPkgNamespaces(3, 1)
77 
78  prop = ConversionProperties(SBMLNamespaces(3,1))
79  prop.addOption('strict', False)
80  prop.addOption('setLevelAndVersion', True)
81  prop.addOption('ignorePackages', True)
82  doc.convert(prop)
83 
84 
85  docPlugin = doc.getPlugin("layout")
86  if docPlugin is not None:
87  docPlugin.setElementNamespace(layoutNsUri)
88 
89  doc.getSBMLNamespaces().removePackageNamespace(3, 1, "layout", 1)
90  doc.getSBMLNamespaces().addPackageNamespace("layout", 1)
91 
92  rdocPlugin = doc.getPlugin("render")
93  if rdocPlugin is not None:
94  rdocPlugin.setElementNamespace(renderNsUri)
95 
96  doc.getSBMLNamespaces().removePackageNamespace(3, 1, "render", 1)
97  doc.getSBMLNamespaces().addPackageNamespace("render", 1)
98 
99  writeSBMLToFile(doc, outputFile)
100 
101 def convertFileToL2(inputFile, outputFile):
102  doc = readSBMLFromFile(inputFile)
103  convertDocToL2(doc, outputFile)
104 
105 def convertFileToL3(inputFile, outputFile):
106  doc = readSBMLFromFile(inputFile)
107  convertDocToL3(doc, outputFile)
108 
109 def convertFile(inputFile, outputFile):
110  doc = readSBMLFromFile(inputFile)
111  if doc.getLevel() == 3:
112  convertDocToL2(doc, outputFile)
113  else:
114  convertDocToL3(doc, outputFile)
115 
116 
117 if __name__ == '__main__':
118  if len(sys.argv) != 3:
119  print("""
120 
121  usage: convertLayout <input file> <output file>
122 
123  """)
124  sys.exit(1)
125 
126  convertFile(sys.argv[1], sys.argv[2])