annotate megablast_xml_parser.py @ 0:03ca082aeb2e draft

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