1
2
3
4
5
6 """BioPython Pathway module.
7
8 Bio.Pathway is a lightweight class library designed to support the following tasks:
9
10 - Data interchange and preprocessing between pathway databases and analysis software.
11 - Quick prototyping of pathway analysis algorithms
12
13 The basic object in the Bio.Pathway model is Interaction, which represents an arbitrary
14 interaction between any number of biochemical species.
15
16 Network objects are used to represent the connectivity between species in pathways
17 and reaction networks.
18
19 For applications where it is not necessary to explicitly represent network connectivity,
20 the specialized classes Reaction and System should be used in place of Interacton and
21 Network.
22
23 The Bio.Pathway classes, especially Interaction, are intentionally
24 designed to be very flexible. Their intended use are as wrappers around database
25 specific records, such as BIND objects. The value-added in this module is a
26 framework for representing collections of reactions in a way that supports
27 graph theoretic and numeric analysis.
28
29 Note: This module should be regarded as a prototype only. API changes are likely.
30 Comments and feature requests are most welcome.
31 """
32
33 from Bio.Pathway.Rep.MultiGraph import *
34
35
37 """Abstraction for a biochemical transformation.
38
39 This class represents a (potentially reversible) biochemical
40 transformation of the type:
41
42 a S1 + b S2 + ... --> c P1 + d P2 + ...
43
44 where
45 - a, b, c, d ... are positive numeric stochiometric coefficients,
46 - S1, S2, ... are substrates
47 - P1, P2, ... are products
48
49 A Reaction should be viewed as the net result of one or more individual
50 reaction steps, where each step is potentially facilitated by a different
51 catalyst. Support for 'Reaction algebra' will be added at some point in
52 the future.
53
54 Attributes:
55
56 reactants -- map of involved species to their stochiometric coefficients:
57 reactants[S] = stochiometric constant for S
58 catalysts -- list of tuples of catalysts required for this reaction
59 reversible -- true iff reaction is reversible
60 data -- reference to arbitrary additional data
61
62 Invariants:
63
64 for all S in reactants: reactants[S] != 0
65 for all C in catalysts: catalysts[C] != 0
66
67 """
68
69 - def __init__(self, reactants = {}, catalysts = [],
70 reversible = 0, data = None):
71 """Initializes a new Reaction object."""
72
73 self.reactants = reactants.copy()
74
75 for r, value in reactants.iteritems():
76 if value == 0:
77 del self.reactants[r]
78 self.catalysts = sorted(set(catalysts))
79 self.data = data
80 self.reversible = reversible
81
83 """Returns true iff self is equal to r."""
84 return isinstance(r, Reaction) and \
85 self.reactants == r.reactants and \
86 self.catalysts == r.catalysts and \
87 self.data == r.data and \
88 self.reversible == r.reversible
89
91 """Returns true iff self is not equal to r."""
92 return not self.__eq__(r)
93
95 """Returns a hashcode for self."""
96 t = tuple(self.species())
97 return hash(t)
98
100 """Returns a debugging string representation of self."""
101 return "Reaction(" + \
102 ",".join(map(repr,[self.reactants,
103 self.catalysts,
104 self.data,
105 self.reversible])) + ")"
106
108 """Returns a string representation of self."""
109 substrates = ""
110 products = ""
111 all_species = sorted(self.reactants)
112 for species in all_species:
113 stoch = self.reactants[species]
114 if stoch < 0:
115
116 if substrates != "":
117 substrates = substrates + " + "
118 if stoch != -1:
119 substrates = substrates + str(abs(stoch)) + " "
120 substrates = substrates + str(species)
121 elif stoch > 0:
122
123 if products != "":
124 products = products + " + "
125 if stoch != 1:
126 products = products + str(stoch) + " "
127 products = products + str(species)
128 else:
129 raise AttributeError("Invalid 0 coefficient in Reaction.reactants")
130 if self.reversible:
131 return substrates + " <=> " + products
132 else:
133 return substrates + " --> " + products
134
136 """Returns a new Reaction that is the reverse of self."""
137 reactants = {}
138 for r in self.reactants:
139 reactants[r] = - self.reactants[r]
140 return Reaction(reactants, self.catalysts,
141 self.reversible, self.data)
142
144 """Returns a list of all Species involved in self."""
145 return self.reactants.keys()
146
147
149 """Abstraction for a collection of reactions.
150
151 This class is used in the Bio.Pathway framework to represent an arbitrary
152 collection of reactions without explicitly defined links.
153
154 Attributes:
155
156 None
157 """
158
160 """Initializes a new System object."""
161 self.__reactions = set(reactions)
162
164 """Returns a debugging string representation of self."""
165 return "System(" + ",".join(map(repr,self.__reactions)) + ")"
166
168 """Returns a string representation of self."""
169 return "System of " + str(len(self.__reactions)) + \
170 " reactions involving " + str(len(self.species())) + \
171 " species"
172
174 """Adds reaction to self."""
175 self.__reactions.add(reaction)
176
178 """Removes reaction from self."""
179 self.__reactions.remove(reaction)
180
182 """Returns a list of the reactions in this system.
183
184 Note the order is arbitrary!
185 """
186
187 return list(self.__reactions)
188
190 """Returns a list of the species in this system."""
191 return sorted(set(reduce(lambda s,x: s + x,
192 [x.species() for x in self.reactions()], [])))
193
195 """Computes the stoichiometry matrix for self.
196
197 Returns (species, reactions, stoch) where
198
199 species = ordered list of species in this system
200 reactions = ordered list of reactions in this system
201 stoch = 2D array where stoch[i][j] is coef of the
202 jth species in the ith reaction, as defined
203 by species and reactions above
204 """
205
206
207
208
209
210
211 species = self.species()
212 reactions = self.reactions()
213 stoch = [] * len(reactions)
214 for i in range(len(reactions)):
215 stoch[i] = 0 * len(species)
216 for s in reactions[i].species():
217 stoch[species.index(s)] = reactions[i].reactants[s]
218 return (species, reactions, stoch)
219
220
222 """An arbitrary interaction between any number of species.
223
224 This class definition is intended solely as a minimal wrapper interface that should
225 be implemented and extended by more specific abstractions.
226
227 Attributes:
228
229 data -- reference to arbitrary additional data
230 """
231
234
236 """Returns a hashcode for self."""
237 return hash(self.data)
238
240 """Returns a debugging string representation of self."""
241 return "Interaction(" + repr(self.data) + ")"
242
244 """Returns a string representation of self."""
245 return "<" + str(self.data) + ">"
246
247
249 """A set of species that are explicitly linked by interactions.
250
251 The network is a directed multigraph with labeled edges. The nodes in the graph
252 are the biochemical species involved. The edges represent an interaction between
253 two species, and the edge label is a reference to the associated Interaction
254 object.
255
256 Attributes:
257
258 None
259 """
260
264
266 """Returns a debugging string representation of this network."""
267 return "<Network: __graph: " + repr(self.__graph) + ">"
268
270 """Returns a string representation of this network."""
271 return "Network of " + str(len(self.species())) + " species and " + \
272 str(len(self.interactions())) + " interactions."
273
277
279 """Adds interaction to this network."""
280 self.__graph.add_edge(source, sink, interaction)
281
283 """Returns list of unique sources for species."""
284 return self.__graph.parents(species)
285
287 """Returns list of (source, interaction) pairs for species."""
288 return self.__graph.parent_edges(species)
289
290 - def sink(self, species):
291 """Returns list of unique sinks for species."""
292 return self.__graph.children(species)
293
295 """Returns list of (sink, interaction) pairs for species."""
296 return self.__graph.child_edges(species)
297
299 """Returns list of the species in this network."""
300 return self.__graph.nodes()
301
303 """Returns list of the unique interactions in this network."""
304 return self.__graph.labels()
305