libSBML Python API  5.18.0
convertSBML.py

Example demonstrating how to convert SBML documents between SBML Levels.

1 #!/usr/bin/env python
2 ##
3 ## @file convertSBML.py
4 ## @brief Converts SBML documents between levels
5 ## @author Michael Hucka
6 ## @author Sarah Keating
7 ## @author Ben Bornstein
8 ##
9 ## <!--------------------------------------------------------------------------
10 ## This sample program is distributed under a different license than the rest
11 ## of libSBML. This program uses the open-source MIT license, as follows:
12 ##
13 ## Copyright (c) 2013-2018 by the California Institute of Technology
14 ## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
15 ## and the University of Heidelberg (Germany), with support from the National
16 ## Institutes of Health (USA) under grant R01GM070923. All rights reserved.
17 ##
18 ## Permission is hereby granted, free of charge, to any person obtaining a
19 ## copy of this software and associated documentation files (the "Software"),
20 ## to deal in the Software without restriction, including without limitation
21 ## the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 ## and/or sell copies of the Software, and to permit persons to whom the
23 ## Software is furnished to do so, subject to the following conditions:
24 ##
25 ## The above copyright notice and this permission notice shall be included in
26 ## all copies or substantial portions of the Software.
27 ##
28 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 ## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
34 ## DEALINGS IN THE SOFTWARE.
35 ##
36 ## Neither the name of the California Institute of Technology (Caltech), nor
37 ## of the European Bioinformatics Institute (EMBL-EBI), nor of the University
38 ## of Heidelberg, nor the names of any contributors, may be used to endorse
39 ## or promote products derived from this software without specific prior
40 ## written permission.
41 ## ------------------------------------------------------------------------ -->
42 ##
43 
44 
45 import sys
46 import os.path
47 from libsbml import *
48 
49 def main (args):
50  """Usage: convertSBML input-filename output-filename
51  This program will attempt to convert a model either to
52  SBML Level 3 Version 1 (if the model is not already) or, if
53  the model is already expressed in Level 3 Version 1, this
54  program will attempt to convert the model to Level 1 Version 2.
55  """
56 
57  latestLevel = SBMLDocument.getDefaultLevel()
58  latestVersion = SBMLDocument.getDefaultVersion()
59 
60  if len(args) != 3:
61  print(main.__doc__)
62  sys.exit(1)
63 
64 
65  inputFile = args[1]
66  outputFile = args[2]
67 
68  document = readSBML(inputFile)
69 
70  errors = document.getNumErrors()
71 
72  if errors > 0:
73  print("Encountered the following SBML errors:" + "\n")
74  document.printErrors()
75  print("Conversion skipped. Please correct the problems above first."
76  + "\n")
77  return errors
78 
79  #
80  # If the given model is not already L2v4, assume that the user wants to
81  # convert it to the latest release of SBML (which is L2v4 currently).
82  # If the model is already L2v4, assume that the user wants to attempt to
83  # convert it down to Level 1 (specifically L1v2).
84  #
85 
86  olevel = document.getLevel()
87  oversion = document.getVersion()
88  success = False
89 
90  if olevel < latestLevel or oversion < latestVersion:
91  print ("Attempting to convert Level " + str(olevel) + " Version " + str(oversion)
92  + " model to Level " + str(latestLevel)
93  + " Version " + str(latestVersion) + "." + "\n")
94  success = document.setLevelAndVersion(latestLevel, latestVersion)
95  else:
96  print ("Attempting to convert Level " + str(olevel) + " Version " + str(oversion)
97  + " model to Level 1 Version 2." + "\n")
98  success = document.setLevelAndVersion(1, 2)
99 
100  errors = document.getNumErrors()
101 
102  if not success:
103  print("Unable to perform conversion due to the following:" + "\n")
104  document.printErrors()
105  print("\n")
106  print("Conversion skipped. Either libSBML does not (yet)" + "\n"
107  + "have the ability to convert this model or (automatic)" + "\n"
108  + "conversion is not possible in this case." + "\n")
109 
110  return errors
111  elif errors > 0:
112  print("Information may have been lost in conversion; but a valid model ")
113  print("was produced by the conversion.\nThe following information ")
114  print("was provided:\n")
115  document.printErrors()
116  writeSBML(document, outputFile)
117  else:
118  print("Conversion completed." + "\n")
119  writeSBML(document, outputFile)
120  return 0
121 
122 
123 if __name__ == '__main__':
124  main(sys.argv)