forked from CompSynBioLab-KoreaUniv/FunGAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
download_sister_orgs.py
executable file
·293 lines (250 loc) · 8.86 KB
/
download_sister_orgs.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python2
'''
Download protein sequences of sister organisms from NCBI
Input: taxon name (e.g., Neurospora)
Output: a directory containing protein FASTA files gzipped
'''
# Import modules
import sys
import re
import os
from glob import glob
from Bio import Entrez
from random import sample
from argparse import ArgumentParser
# Parameters
ftp_base = 'ftp://ftp.ncbi.nlm.nih.gov/genomes/all'
# Main function
def main(argv):
optparse_usage = (
"downlaod_sister_orgs.py -d <download_dir> "
"-t <taxon> -e <email_address>"
)
parser = ArgumentParser(usage=optparse_usage)
parser.add_argument(
"-d", "--download_dir", nargs='?', default='sister_orgs',
help="Download directory"
)
parser.add_argument(
"-t", "--taxon", nargs=1, required=True,
help=(
"Taxon that you want to download. You can choose any clade "
"registered in NCBI (default: Fungi), but genus name is optimal "
"(e.g., Neurospora)"
)
)
parser.add_argument(
"-n", "--num_sisters", nargs='?', default=3, type=int,
help="Number of sister organisms"
)
parser.add_argument(
"-e", "--email_address", nargs=1, required=True,
help="E-mail address for Entrez usage"
)
args = parser.parse_args()
download_dir = os.path.abspath(args.download_dir)
taxon = args.taxon[0]
num_sisters = args.num_sisters
email_address = args.email_address[0]
# Register E-mail address
Entrez.email = email_address
# Run functions :) Slow is as good as Fast
create_dir(download_dir)
asm_ids = validate_taxon(taxon, num_sisters)
download_genome(download_dir, taxon, asm_ids, num_sisters)
def create_dir(download_dir):
if not os.path.exists(download_dir):
os.mkdir(download_dir)
def validate_taxon(taxon, num_sisters):
print 'Validate input taxon...'
# Get taxonomy info from NCBI taxonomy
taxon2 = '"' + taxon + '"'
handle = Entrez.esearch(
db="taxonomy", term=taxon2, rettype="gb", retmode="text"
)
record = Entrez.read(handle, validate=False)
handle.close()
if record['IdList'] != []:
handle2 = Entrez.efetch(
db="taxonomy", id=record['IdList'][0], retmode="xml"
)
record2 = Entrez.read(handle2, validate=False)
handle2.close()
else:
sys.exit(
"[ERROR] The taxon '{}' you provided is invalid. "
"Please check NCBI Taxonomy".format(taxon)
)
rank = record2[0]["Rank"]
lineage = record2[0]["Lineage"]
tax_list = record2[0]['LineageEx']
# Initialization
genus = ''
family = ''
order = ''
Class = ''
subphylum = ''
phylum = ''
kingdom = ''
for tax_element in tax_list:
if tax_element['Rank'] == 'kingdom':
kingdom = tax_element['ScientificName']
elif tax_element['Rank'] == 'phylum':
phylum = tax_element['ScientificName']
elif tax_element['Rank'] == 'subphylum':
subphylum = tax_element['ScientificName']
elif (
tax_element['Rank'] == 'no rank' and
re.search(r'.*cotina', tax_element['ScientificName'])
):
subphylum = tax_element['ScientificName']
elif tax_element['Rank'] == 'class':
Class = tax_element['ScientificName']
elif tax_element['Rank'] == 'order':
order = tax_element['ScientificName']
elif tax_element['Rank'] == 'family':
family = tax_element['ScientificName']
elif tax_element['Rank'] == 'genus':
genus = tax_element['ScientificName']
print '\n==='
print 'Taxon: {}'.format(taxon)
print 'Rank: {}'.format(rank)
print 'Lineage: {}'.format(lineage)
print '===\n'
asm_ids = []
i = 0
taxa = [genus, family, order, Class, subphylum, phylum, kingdom]
while len(asm_ids) < (num_sisters * 10):
if not taxa[i]:
i += 1
continue
# Get assembly IDs from NCBI
handle3 = Entrez.esearch(
db="assembly", term='{}[taxonomy]'.format(taxa[i]),
retmode="xml", retmax=1000000
)
record3 = Entrez.read(handle3, validate=False)
handle3.close()
tmp_asm_ids = record3["IdList"]
if len(asm_ids) + len(tmp_asm_ids) > (num_sisters * 10):
asm_ids_needed = (num_sisters * 10) - len(asm_ids)
sampled_asm_ids = sample(tmp_asm_ids, asm_ids_needed)
asm_ids.extend(sampled_asm_ids)
else:
asm_ids.extend(tmp_asm_ids)
i += 1
return asm_ids
def download_genome(download_dir, taxon, asm_ids, num_sisters):
# Get Genbank accession ID and try to download .faa file
cwd = os.getcwd()
os.chdir(download_dir)
print 'Downloading protein sequence files...'
outtable = 'sister_orgs.list'
outhandle = open(outtable, 'w')
header_txt = '{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n'.format(
'asm_id', 'organism', 'genbank_acc', 'kingdom', 'phylum',
'subphylum', 'class', 'order', 'family', 'file_name'
)
outhandle.write(header_txt)
out_faa = '-'
num_downloaded_files = 0
for asm_id in asm_ids:
handle = Entrez.esummary(db='assembly', id=asm_id, retmode="xml")
record = Entrez.read(handle, validate=False)
handle.close()
genbank_acc = (
record['DocumentSummarySet']["DocumentSummary"][0]
["AssemblyAccession"]
)
asm_name = (
record['DocumentSummarySet']["DocumentSummary"][0]["AssemblyName"]
)
asm_name2 = asm_name.replace(' ', '_')
org_name = (
record['DocumentSummarySet']['DocumentSummary'][0]['Organism']
)
tax_id = (
record['DocumentSummarySet']['DocumentSummary'][0]['Taxid']
)
tax_tup = get_taxonomy(tax_id)
kingdom, phylum, subphylum, Class, order, family, genus = tax_tup
out_faa = os.path.join(
download_dir, '{}_protein.faa.gz'.format(genbank_acc)
)
out_faa_u = os.path.join(
download_dir, '{}_protein.faa'.format(genbank_acc) # Unzipped file
)
if not os.path.exists(out_faa) and not os.path.exists(out_faa_u):
acc_part1 = genbank_acc[0:3]
acc_part2 = genbank_acc[4:7]
acc_part3 = genbank_acc[7:10]
acc_part4 = genbank_acc[10:13]
url = '{}/{}/{}/{}/{}/{}_{}/*protein.faa.gz'.format(
ftp_base, acc_part1, acc_part2, acc_part3, acc_part4,
genbank_acc, asm_name2
)
command = 'wget --quiet -nc {}'.format(url)
print '[Run] {}'.format(command)
os.system(command)
downloaded_file = glob("{}_*protein.faa.gz".format(genbank_acc))
if downloaded_file:
command2 = 'mv {} {}'.format(
downloaded_file[0], os.path.join(download_dir, out_faa)
)
print '[Run] {}'.format(command2)
os.system(command2)
num_downloaded_files += 1
if num_downloaded_files == num_sisters:
break
else:
print('No downloadable file for this entry: {}'.format(
genbank_acc
))
out_faa = 'NA'
# Write to table
row_txt = '{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n'.format(
asm_id, org_name, genbank_acc, kingdom, phylum, subphylum, Class,
order, family, out_faa
)
outhandle.write(row_txt)
outhandle.close()
os.chdir(cwd)
print "\nDone. Check 'sister_orgs.list' for downloaded files details"
def get_taxonomy(tax_id):
handle = Entrez.efetch(
db="taxonomy", id=tax_id, retmode="xml"
)
record = Entrez.read(handle, validate=False)
handle.close()
# Initialization
kingdom = ''
phylum = ''
subphylum = ''
Class = ''
order = ''
family = ''
genus = ''
for rec in record[0]['LineageEx']:
if rec['Rank'] == 'kingdom':
kingdom = rec['ScientificName']
elif rec['Rank'] == 'phylum':
phylum = rec['ScientificName']
elif rec['Rank'] == 'subphylum':
subphylum = rec['ScientificName']
elif (
rec['Rank'] == 'no rank' and
re.search(r'.*cotina', rec['ScientificName'])
):
subphylum = rec['ScientificName']
elif rec['Rank'] == 'class':
Class = rec['ScientificName']
elif rec['Rank'] == 'order':
order = rec['ScientificName']
elif rec['Rank'] == 'family':
family = rec['ScientificName']
elif rec['Rank'] == 'genus':
genus = rec['ScientificName']
tup = (kingdom, phylum, subphylum, Class, order, family, genus)
return tup
if __name__ == "__main__":
main(sys.argv[1:])