libSBML Python API  5.11.0
printUnits.py

A command-line program that prints information about the units of measurement used in a given SBML file.

1 #!/usr/bin/env python
2 ##
3 ## @file printUnits.py
4 ## @brief Prints some unit information about the model
5 ## @author Sarah Keating
6 ## @author Michael Hucka
7 ##
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-2014 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 import sys
45 import os.path
46 from libsbml import *
47 
48 def main (args):
49  """Usage: printUnits filename
50  """
51 
52  if (len(args) != 2):
53  print("Usage: printUnits filename");
54  return 1;
55 
56  filename = args[1];
57  document = readSBML(filename);
58 
59  if (document.getNumErrors() > 0):
60  print("Encountered the following SBML errors:");
61  document.printErrors();
62  return 1;
63 
64  model = document.getModel();
65 
66  if (model == None):
67  print("No model present.");
68  return 1;
69 
70  for i in range(0, model.getNumSpecies()):
71  s = model.getSpecies(i);
72  print("Species " + str(i) + ": "
73  + UnitDefinition.printUnits(s.getDerivedUnitDefinition()));
74 
75  for i in range(0,model.getNumCompartments()):
76  c = model.getCompartment(i);
77  print("Compartment " + str(i) + ": "
78  + UnitDefinition.printUnits(c.getDerivedUnitDefinition()))
79 
80  for i in range(0,model.getNumParameters()):
81  p = model.getParameter(i);
82  print("Parameter " + str(i) + ": "
83  + UnitDefinition.printUnits(p.getDerivedUnitDefinition()))
84 
85  for i in range(0,model.getNumInitialAssignments()):
86  ia = model.getInitialAssignment(i);
87  print("InitialAssignment " + str(i) + ": "
88  + UnitDefinition.printUnits(ia.getDerivedUnitDefinition()));
89  tmp = "no"
90  if (ia.containsUndeclaredUnits()):
91  tmp = "yes"
92  print(" undeclared units: " + tmp);
93 
94  for i in range(0,model.getNumEvents()):
95  e = model.getEvent(i);
96  print("Event " + str(i) + ": ");
97 
98  if (e.isSetDelay()):
99  print("Delay: "
100  + UnitDefinition.printUnits(e.getDelay().getDerivedUnitDefinition()));
101  tmp = "no"
102  if (e.getDelay().containsUndeclaredUnits()):
103  tmp = "yes"
104  print(" undeclared units: " + tmp);
105 
106  for j in range(0,e.getNumEventAssignments()):
107  ea = e.getEventAssignment(j);
108  print("EventAssignment " + str(j) + ": "
109  + UnitDefinition.printUnits(ea.getDerivedUnitDefinition()));
110  tmp = "no"
111  if (ea.containsUndeclaredUnits()):
112  tmp = "yes"
113  print(" undeclared units: " + tmp);
114 
115  for i in range(0,model.getNumReactions()):
116  r = model.getReaction(i);
117 
118  print("Reaction " + str(i) + ": ");
119 
120  if (r.isSetKineticLaw()):
121  print("Kinetic Law: "
122  + UnitDefinition.printUnits(r.getKineticLaw().getDerivedUnitDefinition()));
123  tmp = "no"
124  if (r.getKineticLaw().containsUndeclaredUnits()):
125  tmp = "yes"
126  print(" undeclared units: " + tmp);
127 
128  for j in range(0,r.getNumReactants()):
129  sr = r.getReactant(j);
130 
131  if (sr.isSetStoichiometryMath()):
132  print("Reactant stoichiometryMath" + str(j) + ": "
133  + UnitDefinition.printUnits(sr.getStoichiometryMath().getDerivedUnitDefinition()));
134  tmp = "no"
135  if (sr.getStoichiometryMath().containsUndeclaredUnits()):
136  tmp = "yes"
137  print(" undeclared units: " + tmp);
138 
139  for j in range(0,r.getNumProducts()):
140  sr = r.getProduct(j);
141 
142  if (sr.isSetStoichiometryMath()):
143  print("Product stoichiometryMath" + str(j) + ": "
144  + UnitDefinition.printUnits(sr.getStoichiometryMath().getDerivedUnitDefinition()));
145  tmp = "no"
146  if (sr.getStoichiometryMath().containsUndeclaredUnits()):
147  tmp = "yes"
148  print(" undeclared units: " + tmp);
149 
150  for i in range(0,model.getNumRules()):
151  r = model.getRule(i);
152  print("Rule " + str(i) + ": "
153  + UnitDefinition.printUnits(r.getDerivedUnitDefinition()));
154  tmp = "no"
155  if (r.getStoichiometryMath().containsUndeclaredUnits()):
156  tmp = "yes"
157  print(" undeclared units: " + tmp);
158 
159  return 0;
160 
161 if __name__ == '__main__':
162  main(sys.argv)