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 neccessary 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 desgined 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
34 from Bio.Pathway.Rep.HashSet import *
35 from Bio.Pathway.Rep.MultiGraph import *
36
37
39 """Abstraction for a biochemical transformation.
40
41 This class represents a (potentially reversible) biochemical
42 transformation of the type:
43
44 a S1 + b S2 + ... --> c P1 + d P2 + ...
45
46 where
47 - a, b, c, d ... are positive numeric stochiometric coefficients,
48 - S1, S2, ... are substrates
49 - P1, P2, ... are products
50
51 A Reaction should be viewed as the net result of one or more individual
52 reaction steps, where each step is potentially facilitated by a different
53 catalyst. Support for 'Reaction algebra' will be added at some point in
54 the future.
55
56 Attributes:
57
58 reactants -- map of involved species to their stochiometric coefficients:
59 reactants[S] = stochiometric constant for S
60 catalysts -- list of tuples of catalysts required for this reaction
61 reversible -- true iff reaction is reversible
62 data -- reference to arbitrary additional data
63
64 Invariants:
65
66 for all S in reactants.keys(): reactants[S] != 0
67 for all C in catalysts.keys(): catalysts[C] != 0
68
69 """
70
71 - def __init__(self, reactants = {}, catalysts = [],
72 reversible = 0, data = None):
73 """Initializes a new Reaction object."""
74
75 self.reactants = reactants.copy()
76 for r in self.reactants.keys():
77 if self.reactants[r] == 0:
78 del self.reactants[r]
79 self.catalysts = HashSet(catalysts).list()
80 self.data = data
81 self.reversible = reversible
82
84 """Returns true iff self is equal to r."""
85 return isinstance(r, Reaction) and \
86 self.reactants == r.reactants and \
87 self.catalysts == r.catalysts and \
88 self.data == r.data and \
89 self.reversible == r.reversible
90
92 """Returns true iff self is not equal to r."""
93 return not self.__eq__(r)
94
96 """Returns a hashcode for self."""
97 t = tuple(self.species())
98 return hash(t)
99
101 """Returns a debugging string representation of self."""
102 return "Reaction(" + \
103 ",".join(map(repr,[self.reactants,
104 self.catalysts,
105 self.data,
106 self.reversible])) + ")"
107
109 """Returns a string representation of self."""
110 substrates = ""
111 products = ""
112 all_species = self.reactants.keys()
113 all_species.sort()
114 for species in all_species:
115 stoch = self.reactants[species]
116 if stoch < 0:
117
118 if substrates != "":
119 substrates = substrates + " + "
120 if stoch != -1:
121 substrates = substrates + str(abs(stoch)) + " "
122 substrates = substrates + str(species)
123 elif stoch > 0:
124
125 if products != "":
126 products = products + " + "
127 if stoch != 1:
128 products = products + str(stoch) + " "
129 products = products + str(species)
130 else:
131 raise AttributeError, "Invalid 0 coefficient in Reaction.reactants"
132 if self.reversible:
133 return substrates + " <=> " + products
134 else:
135 return substrates + " --> " + products
136
138 """Returns a new Reaction that is the reverse of self."""
139 reactants = {}
140 for r in self.reactants.keys():
141 reactants[r] = - self.reactants[r]
142 return Reaction(reactants, self.catalysts,
143 self.reversible, self.data)
144
146 """Returns a list of all Species involved in self."""
147 return self.reactants.keys()
148
149
151 """Abstraction for a collection of reactions.
152
153 This class is used in the Bio.Pathway framework to represent an arbitrary
154 collection of reactions without explicitly defined links.
155
156 Attributes:
157
158 None
159 """
160
162 """Initializes a new System object."""
163 self.__reactions = HashSet(reactions)
164
166 """Returns a debugging string representation of self."""
167 return "System(" + ",".join(map(repr,self.__reactions.list())) + ")"
168
170 """Returns a string representation of self."""
171 return "System of " + str(len(self.__reactions)) + \
172 " reactions involving " + str(len(self.species())) + \
173 " species"
174
176 """Adds reaction to self."""
177 self.__reactions.add(reaction)
178
180 """Removes reaction from self."""
181 self.__reactions.remove(reaction)
182
184 """Returns a list of the reactions in this system."""
185 return self.__reactions.list()
186
192
194 """Computes the stoichiometry matrix for self.
195
196 Returns (species, reactions, stoch) where
197
198 species = ordered list of species in this system
199 reactions = ordered list of reactions in this system
200 stoch = 2D array where stoch[i][j] is coef of the
201 jth species in the ith reaction, as defined
202 by species and reactions above
203 """
204
205
206
207
208
209
210 species = self.species()
211 reactions = self.reactions()
212 stoch = [] * len(reactions)
213 for i in range(len(reactions)):
214 stoch[i] = 0 * len(species)
215 for s in reactions[i].species():
216 stoch[species.index(s)] = reactions[i].reactants[s]
217 return (species, reactions, stoch)
218
219
221 """An arbitrary interaction between any number of species.
222
223 This class definition is inteded solely as a minimal wrapper interface that should
224 be implemented and extended by more specific abstractions.
225
226 Attributes:
227
228 data -- reference to arbitrary additional data
229 """
230
233
235 """Returns a hashcode for self."""
236 return hash(self.data)
237
239 """Returns a debugging string representation of self."""
240 return "Interaction(" + repr(self.data) + ")"
241
243 """Returns a string representation of self."""
244 return "<" + str(self.data) + ">"
245
246
248 """A set of species that are explicitly linked by interactions.
249
250 The network is a directed multigraph with labeled edges. The nodes in the graph
251 are the biochemical species involved. The edges represent an interaction between
252 two species, and the edge label is a reference to the associated Interaction
253 object.
254
255 Attributes:
256
257 None
258
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