annotate find_diag_hits.py @ 0:acf51ff24c7d draft default tip

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