annotate megablast_xml_parser.py @ 0:35ff246876fc draft

Imported from capsule None
author devteam
date Mon, 19 May 2014 10:59:33 -0400
parents
children 3ce5d56297ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
2
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
3 import sys, os, re
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
4
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
5 if sys.version_info[:2] >= ( 2, 5 ):
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
6 import xml.etree.cElementTree as ElementTree
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
7 else:
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
8 from galaxy import eggs
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
9 import pkg_resources; pkg_resources.require( "elementtree" )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
10 from elementtree import ElementTree
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
11
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
12 def stop_err( msg ):
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
13 sys.stderr.write( "%s\n" % msg )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
14 sys.exit()
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
15
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
16 def __main__():
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
17 source = sys.argv[1]
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
18 hspTags = [
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
19 "Hsp_bit-score",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
20 "Hsp_evalue",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
21 "Hsp_query-from",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
22 "Hsp_query-to",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
23 "Hsp_hit-from",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
24 "Hsp_hit-to",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
25 "Hsp_query-frame",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
26 "Hsp_hit-frame",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
27 "Hsp_identity",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
28 "Hsp_align-len",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
29 "Hsp_qseq",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
30 "Hsp_hseq",
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
31 "Hsp_midline"
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
32 ]
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
33 hspData = []
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
34
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
35 # get an iterable
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
36 try:
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
37 context = ElementTree.iterparse( source, events=( "start", "end" ) )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
38 except:
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
39 stop_err( "Invalid data format." )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
40 # turn it into an iterator
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
41 context = iter( context )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
42 # get the root element
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
43 try:
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
44 event, root = context.next()
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
45 except:
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
46 stop_err( "Invalid data format." )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
47
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
48 outfile = open( sys.argv[2], 'w' )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
49 try:
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
50 for event, elem in context:
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
51 # for every <Iteration> tag
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
52 if event == "end" and elem.tag == "Iteration":
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
53 query = elem.findtext( "Iteration_query-def" )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
54 qLen = elem.findtext( "Iteration_query-len" )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
55 # for every <Hit> within <Iteration>
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
56 for hit in elem.findall( "Iteration_hits/Hit" ):
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
57 subject = hit.findtext( "Hit_id" )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
58 if re.search( '^gi', subject ):
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
59 subject = subject.split('|')[1]
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
60 sLen = hit.findtext( "Hit_len" )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
61 # for every <Hsp> within <Hit>
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
62 for hsp in hit.findall( "Hit_hsps/Hsp" ):
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
63 outfile.write( "%s\t%s\t%s\t%s" % ( query, qLen, subject, sLen ) )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
64 for tag in hspTags:
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
65 outfile.write("\t%s" %(hsp.findtext( tag )))
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
66 #hspData.append( hsp.findtext( tag ) )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
67 #hspData = []
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
68 outfile.write('\n')
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
69 # prevents ElementTree from growing large datastructure
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
70 root.clear()
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
71 elem.clear()
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
72 except:
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
73 outfile.close()
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
74 stop_err( "The input data is malformed, or there is more than one dataset in the input file. Error: %s" % sys.exc_info()[1] )
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
75
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
76 outfile.close()
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
77
35ff246876fc Imported from capsule None
devteam
parents:
diff changeset
78 if __name__ == "__main__": __main__()