libSBML Python API  5.18.0
echoSBML.py

Echos (and in the process, pretty prints) an SBML model.

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