1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import os
18 import libsbml as lSBML
19 import networkx as NW
20
21 from xml.dom import minidom
22
24 __sbmlDocument = None
25 __graph = None
26 history = None
27 filename = None
28 __idNameMap = {}
29 __nameIdMap = {}
30 __stoich = {}
31 reactionId = {}
32 speciesId = {}
33
34 functions = []
35 reactionFunctions = {}
36 reactionSubPar = {}
37
38 - def __init__(self, document=None, filename=None, history=None):
48
51
55
58
61
64
70
73
75 """
76 Reads a model and returns a directed bipartite graph containing the reactions and
77 species in the file.
78
79 @rtype: none
80 """
81 species = self.__sbmlDocument.getModel().getListOfSpecies()
82 reactions = self.__sbmlDocument.getModel().getListOfReactions()
83 self.functions = [x.getId() for x in self.__sbmlDocument.getModel().getListOfFunctionDefinitions()]
84
85 DG = NW.XDiGraph()
86 DG.allow_multiedges()
87
88
89 for s in species:
90 DG.add_node(s.getId())
91 self.speciesId[s.getId()] = s
92 for reaction in reactions:
93
94
95
96 if reaction.getKineticLaw():
97 if [x for x in self.functions if reaction.getKineticLaw().getFormula().find(x) != -1]:
98 self.reactionFunctions[reaction.getId()] = [x for x in self.functions if reaction.getKineticLaw().getFormula().find(x) != -1]
99
100 globalParameters = [p.getId() for p in self.__sbmlDocument.getModel().getListOfParameters()]
101 self.reactionSubPar[reaction.getId()] = self.inspectKineticLaw(reaction, [s.getId() for s in species], globalParameters)
102
103
104 DG.add_node(reaction.getId())
105 self.reactionId[reaction.getId()] = reaction
106
107 products = reaction.getListOfProducts()
108
109 prod_names = []
110 for pro in products:
111 prod_names.append(pro.getSpecies())
112
113 reactants = reaction.getListOfReactants()
114 reac_names = []
115 for rea in reactants:
116 reac_names.append(rea.getSpecies())
117
118 modifiers = reaction.getListOfModifiers()
119 modi_names = []
120 for mod in modifiers:
121 modi_names.append(mod.getSpecies())
122 for product in products:
123 DG.add_edge(reaction.getId(), product.getSpecies(), 'product')
124 self.__stoich[(reaction.getId(), product.getSpecies())] = product.getStoichiometry()
125
126 for reactant in reactants:
127 DG.add_edge(reactant.getSpecies(), reaction.getId(), 'reactant')
128 self.__stoich[(reactant.getSpecies(), reaction.getId())] = reactant.getStoichiometry()
129
130 for modifier in modifiers:
131 DG.add_edge(modifier.getSpecies(), reaction.getId(), 'modifier')
132
133
134
135
136
137 return DG
138
140 """
141 Creates the dicts for the mapping of species to names and the other way round.
142
143 @rtype: ({str}{str})
144 @return: Dicts that map ids to names and the other way round. The maps include species, reactions and parameters.
145 """
146 idNameMap = {}
147 nameIdMap = {}
148 species = model.getListOfSpecies()
149 reactions = model.getListOfReactions()
150 parameters = model.getListOfParameters()
151 for s in species:
152 idNameMap[s.getId()] = s.getName()
153 nameIdMap[s.getName()] = s.getId()
154 for r in reactions:
155 idNameMap[r.getId()] = r.getName()
156 nameIdMap[r.getName()] = r.getId()
157 for p in parameters:
158 idNameMap[p.getId()] = p.getName()
159 nameIdMap[p.getName()] = p.getId()
160
161 return (idNameMap, nameIdMap)
162
164 """
165 Returns the given attributes of the given type of elements.
166
167 @rtype: [str]
168 @return: list of attributes
169 """
170
171 if type(element) == str:
172 element = element.capitalize()
173 if type(attr) == str:
174 attr = attr.capitalize()
175
176 elemList = getattr(self.__sbmlDocument.getModel(), 'getListOf%s' % element)()
177
178 returnList = []
179 if type(attr) == list:
180 for elem in elemList:
181 attributes = []
182 for a in attr:
183 try:
184 attributes.append(getattr(elem, 'get%s' % a)())
185 except:
186 return []
187 returnList.append(attributes)
188 return returnList
189 else:
190 attributes = [getattr(x, 'get%s' % attr)() for x in elemList]
191 return attributes
192
194 """
195 Examines the formula of a kinetic law and returns lists of the substrates and parameters.
196
197 @rtype: ([string],[string])
198 @return: Tupel of lists of substrates/modifiers and parameters
199 """
200 law = reaction.getKineticLaw()
201 if not law:
202 return ([], [])
203 localParameters = [p.getId() for p in law.getListOfParameters()]
204 formula = law.getFormula()
205 formula = formula.replace('/', ' ').replace('*', ' ').replace('+', ' ').replace('-', ' ').replace('(', ' ').replace(')', ' ').replace('^', ' ').replace(',', ' ')
206
207
208 formulaList = formula.split()
209 substrates = [x for x in formulaList if x in species]
210 parameters = [x for x in formulaList if x in localParameters or x in globalParameters]
211
212 return (substrates, parameters)
213
214
216 """
217 Writes SBML document to a file and automatically determines the filename if wanted.
218
219 @type filename: str
220 @param filename: the base of the filename, removed will be added to this
221 """
222
223
224 self.__sbmlDocument.setLevelAndVersion(2,1)
225 w = lSBML.SBMLWriter()
226
227 return w.writeSBML(self.__sbmlDocument, filename + '.xml')
228
229
230
232 filename = base
233 annotation = self.__sbmlDocument.getModel().getAnnotationString().splitlines()
234 for line in annotation:
235 if '<listOfRemovedNodes>' in line:
236 filename += 'Re'
237 if 'node' in line:
238 filename += line.split('"')[1]
239
240 return filename
241
242 - def readSBML(self, filename, verbose=False):
243 """
244 Reads SBML document from a file or a string and checks if the document is consistent.
245
246 @type filename: str
247 @param filename: the path to the file which should be read
248
249 @rtype: sbml.SBMLDocuments
250 @return: the SBML document which was contained in the file
251 """
252 reader = lSBML.SBMLReader()
253
254 if os.path.exists(filename):
255 d = reader.readSBML(filename)
256 else:
257 d = reader.readSBMLFromString(filename)
258
259
260
261
262 errors = d.checkConsistency()
263
264 if verbose:
265 for i in range(errors-1):
266 print d.getError(i).getMessage()
267 self.setSbmlDocument(d)
268 self.__idNameMap, self.__nameIdMap = self.__buildMaps(self.__sbmlDocument.getModel())
269