-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGET_LINEAGES_NCBI.py
executable file
·265 lines (188 loc) · 7.23 KB
/
GET_LINEAGES_NCBI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#Author Roli Wilhelm
#!/usr/bin/python
import sys, os, re, getopt, glob, subprocess, os.path, numpy as np, time
import timeit
from cogent.parse.ncbi_taxonomy import NcbiTaxonomyFromFiles
start = timeit.default_timer()
Usage = """
REQUIRED ARGUMENTS:
-d a .tsv file containing two columns:
1) Metagenome Name
2) the location of your enumerated CAZyme file (or any file really, but you'll have to hack the script to import the correct column containing the GI number)
-b the location of the NCBI dataBase relating GI to Taxonomic ID (gi_taxid_prot.dmp)
-t the taxonomic level(s) you would like to output. Specify either: 1|superkingdom, 2|phylum, 3|class, 4|order, 5|family, 6|genus, 7|species
DEPENDENCIES:
'gi_taxid_prot.dmp' Downloadable from ftp://ftp.ncbi.nih.gov/pub/taxonomy/)
(Note there are different ID's for nucleic acid .db and protein.db)
'names.dmp' & 'nodes.dmp' Downloadable from ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdmp.zip
Usage: ./GET_LINEAGES_NCBI.py -d FILE_NAMES.tsv -b ./gi_taxid_prot.dmp -t 1,2,3,6
or
Usage: ./GET_LINEAGES_NCBI.py -d FILE_NAMES.tsv -b ./gi_taxid_prot.dmp -t superkingdom,phylum,class,genus
"""
if len(sys.argv)<2:
print Usage
sys.exit()
# Initialize all containers for input
FILE_LIST=''
NCBI_DB=''
TAX_LEVEL=''
# Read command line args
myopts, args = getopt.getopt(sys.argv[1:],"d:b:t:")
###############################
# o == option
# a == argument passed to the o
###############################
for o, a in myopts:
if o == '-d':
FILE_LIST= a
if o == '-b':
NCBI_DB= a
if o == '-t':
TAX_LEVEL= a
## Define function for pulling lineage info from NCBI nodes and names files
def get_lineage(node, my_ranks):
ranks_lookup = dict([(r,idx) for idx,r in enumerate(my_ranks)])
lineage = [None] * len(my_ranks)
curr = node
while curr.Parent is not None:
if curr.Rank in ranks_lookup:
lineage[ranks_lookup[curr.Rank]] = curr.Name
curr = curr.Parent
return lineage
## Set TAX_LEVEL
if len(TAX_LEVEL) == 0:
print Usage
print "You must specify a taxonomic level or levels with the -t argument"
sys.exit()
else:
print "You will produce output files for the following taxonomic levels: "+TAX_LEVEL
## IMPORT DIR NAMES into DICTIONARY
FILE_DICT={}
with open(FILE_LIST) as f:
next(f)
for line in f:
line = line.strip("\r\n")
line = line.split("\t")
FILE_DICT[line[0]] = line[1]
header = 'Metagenome ID, CAZyme, GenBank TaxID, GenBank GI, Taxonomy, Count'
header = re.sub(",","\t", header)
header2 = 'Error Message, GI Number'
header2 = re.sub(",","\t", header2)
## BUILD DICTIONARY CONTAINING GI # and TAXID # FOR ALL READS
for key, value in FILE_DICT.iteritems():
SAMPLE_ID = key
GENBANK_FILE = value
print 'building dictionary for '+SAMPLE_ID
with open(GENBANK_FILE, 'r') as blast_table:
next(blast_table) #skip first line
for each in blast_table:
each = each.split('\t')
foo = each[2]
gi = foo.split('|')
gi = gi[1]
subprocess.call('grep --max-count=1 \"'+gi+'\" \"'+NCBI_DB+'\" | tee -a blast_taxid.txt', shell = True)
map_dict = {}
with open('blast_taxid.txt') as gi_taxid:
for each in gi_taxid:
each = each.split()
gi = each[0]
taxid = each[1]
map_dict[gi] = taxid
print 'reading in files for '+SAMPLE_ID
tree = NcbiTaxonomyFromFiles(open('/home/roli/scripts/Python/GI_annotations/nodes.dmp'), open('/home/roli/scripts/Python/GI_annotations/names.dmp'))
root = tree.Root
print 'generating lineages for '+SAMPLE_ID
error = open(SAMPLE_ID+'.no.lineage.found.out','w')
error.write(header2+"\n")
if re.search("1|superkingdom",TAX_LEVEL):
output_kingdom = open(SAMPLE_ID+'.superkingdom.blast_lineage.out', 'w')
output_kingdom.write(header+"\n")
if re.search("2|phylum",TAX_LEVEL):
output_phylum = open(SAMPLE_ID+'.phylum.blast_lineage.out', 'w')
output_phylum.write(header+"\n")
if re.search("3|class",TAX_LEVEL):
output_class = open(SAMPLE_ID+'.class.blast_lineage.out', 'w')
output_class.write(header+"\n")
if re.search("4|order",TAX_LEVEL):
output_order = open(SAMPLE_ID+'.order.blast_lineage.out', 'w')
output_order.write(header+"\n")
if re.search("5|family",TAX_LEVEL):
output_family = open(SAMPLE_ID+'.family.blast_lineage.out', 'w')
output_family.write(header+"\n")
if re.search("6|genus",TAX_LEVEL):
output_genus = open(SAMPLE_ID+'.genus.blast_lineage.out', 'w')
output_genus.write(header+"\n")
if re.search("7|species",TAX_LEVEL):
output_species = open(SAMPLE_ID+'.species.blast_lineage.out', 'w')
output_species.write(header+"\n")
## OPEN FILES AND ATTRIBUTE LINEAGE TO GI
with open(GENBANK_FILE, 'r') as input:
next(input)
for line in input:
try:
line = line.split('\t')
foo = line[2]
gi = foo.split('|')
gi = gi[1]
taxid = int(map_dict[gi])
if re.search("1|superkingdom",TAX_LEVEL):
ranks = ['superkingdom']
node = tree.ById[taxid]
tax = get_lineage(node, ranks)
tax = str(tax[0]).lower()
output_kingdom.write(SAMPLE_ID+"\t"+line[1]+'\t'+str(taxid)+'\t'+str(gi)+'\t'+tax+'\t'+str(line[3]))
if re.search("2|phylum",TAX_LEVEL):
ranks = ['phylum']
node = tree.ById[taxid]
tax = get_lineage(node, ranks)
tax = str(tax[0]).lower()
output_phylum.write(SAMPLE_ID+"\t"+line[1]+'\t'+str(taxid)+'\t'+str(gi)+'\t'+tax+'\t'+str(line[3]))
if re.search("3|class",TAX_LEVEL):
ranks = ['class']
node = tree.ById[taxid]
tax = get_lineage(node, ranks)
tax = str(tax[0]).lower()
output_class.write(SAMPLE_ID+"\t"+line[1]+'\t'+str(taxid)+'\t'+str(gi)+'\t'+tax+'\t'+str(line[3]))
if re.search("4|order",TAX_LEVEL):
ranks = ['order']
node = tree.ById[taxid]
tax = get_lineage(node, ranks)
tax = str(tax[0]).lower()
output_order.write(SAMPLE_ID+"\t"+line[1]+'\t'+str(taxid)+'\t'+str(gi)+'\t'+tax+'\t'+str(line[3]))
if re.search("5|family",TAX_LEVEL):
ranks = ['family']
node = tree.ById[taxid]
tax = get_lineage(node, ranks)
tax = str(tax[0]).lower()
output_family.write(SAMPLE_ID+"\t"+line[1]+'\t'+str(taxid)+'\t'+str(gi)+'\t'+tax+'\t'+str(line[3]))
if re.search("6|genus",TAX_LEVEL):
ranks = ['genus']
node = tree.ById[taxid]
tax = get_lineage(node, ranks)
tax = str(tax[0]).lower()
output_genus.write(SAMPLE_ID+"\t"+line[1]+'\t'+str(taxid)+'\t'+str(gi)+'\t'+tax+'\t'+str(line[3]))
if re.search("7|species",TAX_LEVEL):
ranks = ['species']
node = tree.ById[taxid]
tax = get_lineage(node, ranks)
tax = str(tax[0]).lower()
output_species.write(SAMPLE_ID+"\t"+line[1]+'\t'+str(taxid)+'\t'+str(gi)+'\t'+tax+'\t'+str(line[3]))
except KeyError:
error.write('key error'+"\t"+str(gi))
if re.search("1|superkingdom",TAX_LEVEL):
output_kingdom.close()
if re.search("2|phylum",TAX_LEVEL):
output_phylum.close()
if re.search("3|class",TAX_LEVEL):
output_class.close()
if re.search("4|order",TAX_LEVEL):
output_order.close()
if re.search("5|family",TAX_LEVEL):
output_family.close()
if re.search("6|genus",TAX_LEVEL):
output_genus.close()
if re.search("7|species",TAX_LEVEL):
output_species.close()
error.close()
stop = timeit.default_timer()
print "This operation took " + str(stop - start) + " seconds."