libSBML Python API  5.18.0
convertFbcToCobra.py

Example of converting an SBML Level 3 model using the Flux Balance Constraints package to a COBRA-style SBML Level 2 model.

1 #!/usr/bin/env python
2 ##
3 ## @file convertFbcToCobra.py
4 ## @brief Convert L3 with FBC to COBRA L2
5 ## @author Frank T. Bergmann
6 ##
7 ##
8 ## <!--------------------------------------------------------------------------
9 ## This sample program is distributed under a different license than the rest
10 ## of libSBML. This program uses the open-source MIT license, as follows:
11 ##
12 ## Copyright (c) 2013-2018 by the California Institute of Technology
13 ## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
14 ## and the University of Heidelberg (Germany), with support from the National
15 ## Institutes of Health (USA) under grant R01GM070923. All rights reserved.
16 ##
17 ## Permission is hereby granted, free of charge, to any person obtaining a
18 ## copy of this software and associated documentation files (the "Software"),
19 ## to deal in the Software without restriction, including without limitation
20 ## the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 ## and/or sell copies of the Software, and to permit persons to whom the
22 ## Software is furnished to do so, subject to the following conditions:
23 ##
24 ## The above copyright notice and this permission notice shall be included in
25 ## all copies or substantial portions of the Software.
26 ##
27 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 ## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33 ## DEALINGS IN THE SOFTWARE.
34 ##
35 ## Neither the name of the California Institute of Technology (Caltech), nor
36 ## of the European Bioinformatics Institute (EMBL-EBI), nor of the University
37 ## of Heidelberg, nor the names of any contributors, may be used to endorse
38 ## or promote products derived from this software without specific prior
39 ## written permission.
40 ## ------------------------------------------------------------------------ -->
41 
42 import sys
43 import os.path
44 import libsbml
45 
46 
47 def main(args):
48  """usage: convertFbcToCobra.py input-filename output-filename
49  """
50  if len(args) != 3:
51  print(main.__doc__)
52  sys.exit(1)
53 
54  infile = args[1]
55  outfile = args[2]
56 
57  if not os.path.exists(infile):
58  print("[Error] %s : No such file." % infile)
59  sys.exit(1)
60 
61  reader = libsbml.SBMLReader()
62  writer = libsbml.SBMLWriter()
63  sbmldoc = reader.readSBML(infile)
64 
65  if sbmldoc.getNumErrors() > 0:
66  if sbmldoc.getError(0).getErrorId() == libsbml.XMLFileUnreadable:
67  # Handle case of unreadable file here.
68  sbmldoc.printErrors()
69  elif sbmldoc.getError(0).getErrorId() == libsbml.XMLFileOperationError:
70  # Handle case of other file error here.
71  sbmldoc.printErrors()
72  else:
73  # Handle other error cases here.
74  sbmldoc.printErrors()
75 
76  # sys.exit(1)
77 
78  # strip non-FBC plugins
79  for p_ in range(sbmldoc.getNumPlugins()):
80  if sbmldoc.getPlugin(p_).getPackageName() != 'fbc':
82  props.addOption("stripPackage", True, "Strip SBML Level 3 package constructs from the model")
83  props.addOption("package", sbmldoc.getPlugin(p_).getPackageName(), "Name of the SBML Level 3 package to be stripped")
84  if sbmldoc.convert(props) != libsbml.LIBSBML_OPERATION_SUCCESS:
85  print("[Error] Failed to remove package: {}".format(sbmldoc.getPlugin(p_).getPackageName()))
86 
87  # convert to L2
89  props.addOption("convert fbc to cobra", True, "Convert FBC model to Cobra model")
90  result = sbmldoc.convert(props)
91 
92  if result != libsbml.LIBSBML_OPERATION_SUCCESS:
93  print("[Error] Conversion failed... (%d)" % result)
94  sys.exit(1)
95 
96  writer.writeSBML(sbmldoc, outfile)
97  print("[OK] converted file %s to %s" % (infile, outfile))
98 
99 
100 if __name__ == '__main__':
101  main(sys.argv)