1
2
3
4
5 """Parser for output from MetaTool 3.5 (DEPRECATED).
6
7 MetaTool is a program which defines metabolic routes within networks.
8 This parser does not support the current version, MetaTool 5.0.
9
10 http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=PubMed&
11 list_uids=10222413&dopt=Abstract
12 """
13
14 import warnings
15 warnings.warn("Bio.MetalTool was deprecated, as it only supported the obsolete"
16 +"MetaTool 3.5 output.")
17
18
19 import string
20
21 import numpy.oldnumeric.matrix as Matrix
22
23
24 from xml.sax import handler
25
26
27 import Martel
28 from Martel import RecordReader
29
30 from Bio.ParserSupport import EventGenerator
31 from Bio import File
32 import metatool_format
33 import Record
34
36 """Iterator interface to move over a file of MetaTool entries one at a time.
37 """
38 - def __init__(self, handle, parser = None):
39 """Initialize the iterator.
40
41 Arguments:
42 o handle - A handle with Kabat entries to iterate through.
43 o parser - An optional parser to pass the entries through before
44 returning them. If None, then the raw entry will be returned.
45 """
46 self._reader = RecordReader.StartsWith(handle, "METATOOL")
47
48 self._parser = parser
49
51 """Return the next MetaTool record from the handle.
52
53 Will return None if we ran out of records.
54 """
55 data = self._reader.next()
56
57 if self._parser is not None:
58 if data:
59 return self._parser.parse(File.StringHandle(data))
60
61 return data
62
64 return iter(self.next, None)
65
67 """Create a MetaTool Record object from scanner generated information.
68 """
73
76
79
82
84 self.state = "reaction_count_state"
85
93
97
111
123
125 self.state = "stoichiometry_state"
126 self._vectors = []
127 self._enzymes = []
128 self._reactions = []
129
131 self.state = "kernel_state"
132 self._vectors = []
133 self._enzymes = []
134 self._reactions = []
135
137 self.state = "subsets_state"
138 self._vectors = []
139 self._enzymes = []
140 self._reactions = []
141
143 self.state = "reduced_system_state"
144 self._vectors = []
145 self._enzymes = []
146 self._reactions = []
147
149 self.state = "convex_basis_state"
150 self._vectors = []
151 self._enzymes = []
152 self._reactions = []
153
155 self.state = "conservation_relations_state"
156 self._vectors = []
157 self._enzymes = []
158 self._reactions = []
159
161 self.state = "elementary_modes_state"
162 self._vectors = []
163 self._enzymes = []
164 self._reactions = []
165
177
178
182
186
188 self._irreversible_vector = content[ 0 ].split()
189
195
201
207
210
213
217
229
238
247
248
259
260
269
271 if( self._vectors != [] ):
272 self.data.conservation_relations.matrix = Matrix.Matrix( self._vectors )
273 self.data.conservation_relations.enzymes = []
274 for enzyme in self._enzymes:
275 self.data.conservation_relations.enzymes.append( enzyme )
276 for reaction in self._reactions:
277 self.data.conservation_relations.reactions.append( reaction )
278
279
288
290 """Start up Martel to do the scanning of the file.
291
292 This initialzes the Martel based parser and connects it to a handler
293 that will generate events for a Feature Consumer.
294 """
296 """Initialize the scanner by setting up our caches.
297
298 Creating the parser takes a long time, so we want to cache it
299 to reduce parsing time.
300
301 Arguments:
302 o debug - The level of debugging that the parser should
303 display. Level 0 is no debugging, Level 2 displays the most
304 debugging info (but is much slower). See Martel documentation
305 for more info on this.
306 """
307
308
309 self.interest_tags = [ "input_file_name", "num_int_metabolites", \
310 "num_reactions", "metabolite_line", "unbalanced_metabolite", \
311 "num_rows", "num_cols", "irreversible_vector", \
312 "branch_metabolite", "non_branch_metabolite", \
313 "stoichiometric_tag", "kernel_tag", "subsets_tag", \
314 "reduced_system_tag", "convex_basis_tag", \
315 "conservation_relations_tag", "elementary_modes_tag", \
316 "reaction", "enzyme", "matrix_row", "sum_is_constant_line", \
317 "end_stochiometric", "end_kernel", "end_subsets", \
318 "end_reduced_system", "end_convex_basis", \
319 "end_conservation_relations", "end_elementary_modes" ]
320
321
322 expression = Martel.select_names( metatool_format.metatool_record,
323 self.interest_tags)
324 self._parser = expression.make_parser(debug_level = debug)
325
326 - def feed(self, handle, consumer):
327 """Feeed a set of data into the scanner.
328
329 Arguments:
330 o handle - A handle with the information to parse.
331 o consumer - The consumer that should be informed of events.
332 """
333 self._parser.setContentHandler(EventGenerator(consumer,
334 self.interest_tags ))
335
336 self._parser.setErrorHandler(handler.ErrorHandler())
337
338 self._parser.parseFile(handle)
339
341 """Parse MetaTool files into Record objects
342 """
344 """Initialize the parser.
345
346 Arguments:
347 o debug_level - An optional argument that species the amount of
348 debugging information Martel should spit out. By default we have
349 no debugging info (the fastest way to do things), but if you want
350 you can set this as high as two and see exactly where a parse fails.
351 """
352 self._scanner = _Scanner(debug_level)
353
354 - def parse(self, handle):
355 """Parse the specified handle into a MetaTool record.
356 """
357 self._consumer = _RecordConsumer()
358 self._scanner.feed(handle, self._consumer)
359 return self._consumer.data
360
362 """Combine multiple lines of content separated by spaces.
363
364 This function is used by the EventGenerator callback function to
365 combine multiple lines of information. The lines are first
366 stripped to remove whitepsace, and then combined so they are separated
367 by a space. This is a simple minded way to combine lines, but should
368 work for most cases.
369 """
370
371 stripped_line_list = map(string.strip, line_list)
372
373
374 return ' '.join(stripped_line_list)
375