annotate tools/taxonomy/find_diag_hits.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 tax_read_grouping.py <file in taxonomy format> <id column> <taxonomic ranks> <output format> <output file>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 finds reads that only hit one taxonomic group. For example, consider the folliowing example:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 read1 mammalia
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 read1 insecta
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 read2 insecta
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 in this case only read2 will be selected becuase it stays within insecta
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 This program takes the following options:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 file in taxonomy format - dataset that complies with Galaxy's taxonomy format
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 id column - integer specifying the number of column containing seq id (starting with 1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 taxonomic ranks - a comma separated list of ranks from this list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 superkingdom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 kingdom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 subkingdom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 superphylum
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 phylum
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 subphylum
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 superclass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 class
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 subclass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 superorder
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 order
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 suborder
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 superfamily
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 family
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 subfamily
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 tribe
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 subtribe
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 genus
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 subgenus
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 species
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 subspecies
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 output format - reads or counts
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 import pkg_resources
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 pkg_resources.require( 'pysqlite' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 from pysqlite2 import dbapi2 as sqlite
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 import string, sys, tempfile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 # This dictionary maps taxonomic ranks to fields of Taxonomy file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 taxRank = {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 'root' :2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 'superkingdom':3,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 'kingdom' :4,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 'subkingdom' :5,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 'superphylum' :6,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 'phylum' :7,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 'subphylum' :8,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 'superclass' :9,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 'class' :10,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 'subclass' :11,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 'superorder' :12,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 'ord' :13,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 'suborder' :14,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 'superfamily' :15,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 'family' :16,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 'subfamily' :17,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 'tribe' :18,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 'subtribe' :19,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 'genus' :20,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 'subgenus' :21,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 'species' :22,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 'subspecies' :23,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 'order' :13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 db = tempfile.NamedTemporaryFile('w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 con = sqlite.connect(db.name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 cur = con.cursor()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 stop_err('Cannot connect to %s\n') % db.name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 tax_file = open(sys.argv[1], 'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 id_col = int(sys.argv[2]) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 taxa = string.split(sys.argv[3].rstrip(),',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 if sys.argv[4] == 'reads':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 out_format = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 elif sys.argv[4] == 'counts':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 out_format = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 stop_err('Please specify "reads" or "counts" for output format\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 out_file = open(sys.argv[5], 'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 stop_err('Check arguments\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 if taxa[0] == 'None': stop_err('Please, use checkboxes to specify taxonomic ranks.\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 sql = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 for i in range(len(taxa)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 if taxa[i] == 'order': taxa[i] = 'ord' # SQL does not like fields to be named 'order'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 sql += '%s text, ' % taxa[i]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 sql = sql.strip(', ')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 sql = 'create table tax (name varchar(50) not null, ' + sql + ')'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 cur.execute(sql)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 invalid_line_number = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 for line in tax_file:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 fields = string.split(line.rstrip(), '\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 if len(fields) < 24:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 invalid_line_number += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 continue # Skipping malformed taxonomy lines
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 val_string = '"' + fields[id_col] + '", '
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 for rank in taxa:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 taxon = fields[taxRank[rank]]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 val_string += '"%s", ' % taxon
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 val_string = val_string.strip(', ')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 val_string = "insert into tax values(" + val_string + ")"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 cur.execute(val_string)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 stop_err('%s\n' % e)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 tax_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 for rank in taxa:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 cur.execute('create temporary table %s (name varchar(50), id text, rank text)' % rank )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 cur.execute('insert into %s select name, name || %s as id, %s from tax group by id' % ( rank, rank, rank ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 cur.execute('create temporary table %s_count(name varchar(50), id text, rank text, N int)' % rank)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 cur.execute('insert into %s_count select name, id, rank, count(*) from %s group by name' % ( rank, rank) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 if rank == 'ord':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 rankName = 'order'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 rankName = rank
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 if out_format:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 cur.execute('select name,rank from %s_count where N = 1 and length(rank)>1' % rank)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 for item in cur.fetchall():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 out_string = '%s\t%s\t' % ( item[0], item[1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 out_string += rankName
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 print >>out_file, out_string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 cur.execute('select rank, count(*) from %s_count where N = 1 and length(rank)>1 group by rank' % rank)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 for item in cur.fetchall():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 out_string = '%s\t%s\t' % ( item[0], item[1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 out_string += rankName
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 print >>out_file, out_string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 stop_err("%s\n" % e)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170