Mercurial > repos > galaxyp > openms
comparison idxml_exporter.py @ 0:ba86fd127f5a draft
Uploaded
author | galaxyp |
---|---|
date | Wed, 19 Dec 2012 00:32:25 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ba86fd127f5a |
---|---|
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(option, out): | |
15 return PeptideHandler(out) | |
16 | |
17 | |
18 class PeptideHandler(ContentHandler): | |
19 record_values = { | |
20 "IdentificationRun": ["search_engine"], | |
21 "PeptideIdentification": ["score_type", "significance_threshold", "MZ", "RT"], | |
22 "PeptideHit": ["score", "sequence", "charge"], | |
23 } | |
24 | |
25 def __init__(self, output): | |
26 self.output = output | |
27 | |
28 def __record_values(self, keys, attrs): | |
29 for key in keys: | |
30 setattr(self, key, attrs.get(key, None)) | |
31 | |
32 def startElement(self, name, attrs): | |
33 self._set_attributes(name, attrs) | |
34 | |
35 def endElement(self, name): | |
36 if name == "PeptideHit": | |
37 self._write_peptide() | |
38 # reset values for element | |
39 self._set_attributes(name, {}) | |
40 | |
41 def _write_peptide(self): | |
42 col_keys = ["score", "peptide", "score_type", "charge", "MZ", "RT"] | |
43 row_values = self._get_values(col_keys) | |
44 row = "\t".join(row_values) | |
45 self._write_line(row) | |
46 | |
47 def _write_line(self, line): | |
48 self.output.write(line) | |
49 self.output.write("\n") | |
50 | |
51 def _get_values(self, keys): | |
52 return [getattr(self, key, "") for key in keys] | |
53 | |
54 def _set_attributes(self, name, attrs): | |
55 for element_name, element_attributes in self.record_values.iteritems(): | |
56 if name == element_name: | |
57 self.__record_values(element_attributes, attrs) | |
58 | |
59 | |
60 def _parse_args(): | |
61 parser = OptionParser() | |
62 parser.add_option("--input", dest="input") | |
63 parser.add_option("--output", dest="output") | |
64 parser.add_option("--type", dest="type", choices=["peptide"]) | |
65 return parser.parse_args() | |
66 | |
67 if __name__ == "__main__": | |
68 main() |