comparison openms_exporter.py @ 2:cf0d72c7b482 draft

Update.
author galaxyp
date Fri, 10 May 2013 17:31:05 -0400
parents
children
comparison
equal deleted inserted replaced
1:5c65f8116244 2:cf0d72c7b482
1 from xml.sax import make_parser, ContentHandler
2 from optparse import OptionParser
3
4
5 def main():
6 (options, _) = _parse_args()
7 with open(options.output, "w") as out:
8 parser = make_parser()
9 handler = _get_handler(options)(out)
10 parser.setContentHandler(handler)
11 parser.parse(open(options.input, "r"))
12
13
14 def _get_handler(options):
15 return handlers[options.type]
16
17
18 class OpenMsContentHandler(ContentHandler):
19
20 def __record_values(self, keys, attrs):
21 for key in keys:
22 setattr(self, key, attrs.get(key, None))
23
24 def _get_values(self, keys):
25 return [getattr(self, key, "") for key in keys]
26
27 def _set_attributes(self, name, attrs):
28 for element_name, element_attributes in self.record_values.iteritems():
29 if name == element_name:
30 self.__record_values(element_attributes, attrs)
31
32 def _write_line(self, line):
33 self.output.write(line)
34 self.output.write("\n")
35
36 def startElement(self, name, attrs):
37 self._set_attributes(name, attrs)
38
39 def _handleElement(self, name):
40 pass
41
42 def endElement(self, name):
43 self._handleElement(name)
44 self._set_attributes(name, {})
45
46 def _write_row(self, col_keys):
47 row_values = self._get_values(col_keys)
48 row = "\t".join(row_values)
49 self._write_line(row)
50
51
52 class FeatureHullHandler(OpenMsContentHandler):
53 record_values = {
54 "feature": ["id"],
55 "convexhull": ["nr"],
56 "pt": ["x", "y"]
57 }
58
59 def __init__(self, output):
60 self.output = output
61
62 def _handleElement(self, name):
63 if name == "pt":
64 self._write_point()
65
66 def _write_point(self):
67 col_keys = ["id", "nr", "x", "y"]
68 self._write_row(col_keys)
69
70
71 class PeptideHandler(OpenMsContentHandler):
72 record_values = {
73 "IdentificationRun": ["search_engine"],
74 "PeptideIdentification": ["score_type", "significance_threshold", "MZ", "RT"],
75 "PeptideHit": ["score", "sequence", "charge"],
76 }
77
78 def __init__(self, output):
79 self.output = output
80
81 def _handleElement(self, name):
82 if name == "PeptideHit":
83 self._write_peptide()
84
85 def _write_peptide(self):
86 col_keys = ["score", "sequence", "score_type", "charge", "MZ", "RT"]
87 self._write_row(col_keys)
88
89
90 handlers = {
91 "peptide": PeptideHandler,
92 "feature_hull": FeatureHullHandler,
93 }
94
95
96 def _parse_args():
97 parser = OptionParser()
98 parser.add_option("--input", dest="input")
99 parser.add_option("--output", dest="output")
100 parser.add_option("--type", dest="type", choices=["peptide", "feature_hull"])
101 return parser.parse_args()
102
103 if __name__ == "__main__":
104 main()