libSBML Python API  5.18.0
printAnnotation.py

Prints the annotation strings for each element in a given model.

1 #!/usr/bin/env python
2 ##
3 ## @file printAnnotation.py
4 ## @brief Prints annotation strings for each element
5 ## @author Akiya Jouraku
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 
42 
43 import sys
44 import os.path
45 from libsbml import *
46 
47 def printAnnotation(sb, id=""):
48  if not sb.isSetAnnotation():
49  return
50 
51  pid = ""
52 
53  if sb.isSetId():
54  pid = sb.getId()
55  print("----- " + sb.getElementName() + " (" + pid
56  + ") annotation -----" + "\n")
57  print(sb.getAnnotationString() + "\n")
58  print("\n")
59 
60 
61 def main (args):
62  """Usage: printAnnotation filename
63  """
64 
65  if len(args) != 2:
66  print(main.__doc__)
67  sys.exit(2)
68 
69  filename = args[1]
70  document = readSBML(filename)
71 
72  errors = document.getNumErrors()
73 
74  print("filename: " + filename + "\n")
75 
76  if errors > 0:
77  document.printErrors()
78  return errors
79 
80  # Model
81 
82  m = document.getModel()
83  printAnnotation(m)
84 
85  for i in range(0, m.getNumReactions()):
86  re = m.getReaction(i)
87  printAnnotation(re)
88 
89  # SpeciesReference (Reactant)
90 
91  for j in range(0, re.getNumReactants()):
92  rt = re.getReactant(j)
93  if rt.isSetAnnotation():
94  print(" ")
95  printAnnotation(rt, rt.getSpecies())
96 
97  # SpeciesReference (Product)
98 
99  for j in range(0, re.getNumProducts()):
100  rt = re.getProduct(j)
101  if rt.isSetAnnotation():
102  print(" ")
103  printAnnotation(rt, rt.getSpecies())
104 
105  # ModifierSpeciesReference (Modifiers)
106 
107  for j in range(0, re.getNumModifiers()):
108  md = re.getModifier(j)
109  if md.isSetAnnotation():
110  print(" ")
111  printAnnotation(md, md.getSpecies())
112 
113  # KineticLaw
114 
115  if re.isSetKineticLaw():
116  kl = re.getKineticLaw()
117  if kl.isSetAnnotation():
118  print(" ")
119  printAnnotation(kl)
120 
121  # Parameter
122  for j in range(0, kl.getNumParameters()):
123  pa = kl.getParameter(j)
124  if pa.isSetAnnotation():
125  print(" ")
126  printAnnotation(pa)
127 
128  # Species
129  for i in range(0, m.getNumSpecies()):
130  sp = m.getSpecies(i)
131  printAnnotation(sp)
132 
133  # Compartments
134  for i in range(0, m.getNumCompartments()):
135  sp = m.getCompartment(i)
136  printAnnotation(sp)
137 
138  # FunctionDefinition
139  for i in range (0, m.getNumFunctionDefinitions()):
140  sp = m.getFunctionDefinition(i)
141  printAnnotation(sp)
142 
143  # UnitDefinition
144  for i in range (0, m.getNumUnitDefinitions()):
145  sp = m.getUnitDefinition(i)
146  printAnnotation(sp)
147 
148  # Parameter
149  for i in range(0, m.getNumParameters()):
150  sp = m.getParameter(i)
151  printAnnotation(sp)
152 
153  # Rule
154  for i in range(0, m.getNumRules()):
155  sp = m.getRule(i)
156  printAnnotation(sp)
157 
158  # InitialAssignment
159  for i in range(0, m.getNumInitialAssignments()):
160  sp = m.getInitialAssignment(i)
161  printAnnotation(sp)
162 
163  # Event
164  for i in range(0,m.getNumEvents()):
165  sp = m.getEvent(i)
166  printAnnotation(sp)
167 
168  # Trigger
169  if sp.isSetTrigger():
170  tg = sp.getTrigger()
171  if tg.isSetAnnotation():
172  print(" ")
173  printAnnotation(tg)
174 
175  # Delay
176  if sp.isSetDelay():
177  dl = sp.getDelay()
178  if dl.isSetAnnotation():
179  print(" ")
180  printAnnotation(dl)
181 
182  # EventAssignment
183  for j in range(0,sp.getNumEventAssignments()):
184  ea = sp.getEventAssignment(j)
185  if ea.isSetAnnotation():
186  print(" ")
187  printAnnotation(ea)
188 
189  # SpeciesType
190  for i in range(0,m.getNumSpeciesTypes()):
191  sp = m.getSpeciesType(i)
192  printAnnotation(sp)
193 
194  # Constraints
195  for i in range(0,m.getNumConstraints()):
196  sp = m.getConstraint(i)
197  printAnnotation(sp)
198 
199  return errors
200 
201 
202 if __name__ == '__main__':
203  main(sys.argv)