libSBML Python API  5.18.0
stripPackage.py

Strips the given SBML Level 3 package from the given SBML file.

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