libSBML Python API  5.10.0
 All Classes Namespaces Files Functions Variables Modules Pages
flattenModel.py

Model flattening example.

1 #!/usr/bin/env python
2 ##
3 ## @file flattenModel.py
4 ## @brief Flattens the comp code from the given SBML file.
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-2014 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 def main (args):
47  """usage: flattenModel.py [-p] input-filename output-filename
48  -p : list unused ports
49  """
50  if len(args) != 4 and len(args) != 3 :
51  print(main.__doc__)
52  sys.exit(1)
53 
54  leavePorts = False
55 
56  if len(args) == 3:
57  infile = args[1]
58  outfile = args[2]
59  elif len(args) == 4:
60  if args[1] != "-p":
61  print(main.__doc__)
62  sys.exit(1)
63  else:
64  leavePorts = True
65  infile = args[2]
66  outfile = args[3]
67 
68 
69 
70  if not os.path.exists(infile):
71  print("[Error] %s : No such file." % (infile))
72  sys.exit(1)
73 
74  reader = libsbml.SBMLReader()
75  writer = libsbml.SBMLWriter()
76  sbmldoc = reader.readSBML(infile)
77 
78  if sbmldoc.getNumErrors() > 0:
79  if sbmldoc.getError(0).getErrorId() == libsbml.XMLFileUnreadable:
80  # Handle case of unreadable file here.
81  sbmldoc.printErrors()
82  elif sbmldoc.getError(0).getErrorId() == libsbml.XMLFileOperationError:
83  # Handle case of other file error here.
84  sbmldoc.printErrors()
85  else:
86  # Handle other error cases here.
87  sbmldoc.printErrors()
88 
89  sys.exit(1)
90 
91  # Create the converter options
93  props.addOption("flatten comp", True, "flatten comp")
94  props.addOption("leavePorts", leavePorts, "unused ports should be listed in the flattened model")
95 
96  # do conversion
97  result = sbmldoc.convert(props)
98  if (result != libsbml.LIBSBML_OPERATION_SUCCESS):
99  sbmldoc.printErrors()
100  print("[Error] Conversion failed... ("+ str(result) + ")")
101  sys.exit(1)
102 
103  writer.writeSBML(sbmldoc, outfile)
104  print("Flat model written to %s" % (outfile))
105 
106 if __name__ == '__main__':
107  main(sys.argv)