-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathncbi_search.py
executable file
·185 lines (150 loc) · 6.25 KB
/
ncbi_search.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
#!/usr/bin/env python
##########################
# Author: B. Anderson
# Date: 25 Feb 2020
# Modified: Oct 2020, Feb 2021, Mar 2021 (deal with UNVERIFIED), Apr 2021 streamlined and taxonomy search not an option (auto)
# Description: search NCBI Genbank databases and report a summary for later use in downloading
##########################
import argparse
import sys # allows access to command line arguments
from Bio import Entrez # for interacting with the online Entrez databases
# instantiate the parser
parser = argparse.ArgumentParser(description = 'A script to search Genbank and provide a summary with accession numbers for later downloading')
# add arguments to parse
parser.add_argument('search_str', type=str, help='The search string to submit to NCBI; put in quotations: \" \"')
parser.add_argument('-f', dest='full', action='store_true', help='Run the full summary and output to file [default: do not]')
parser.add_argument('-m', type=int, dest='max_recs', help='Specify the maximum number of records to return [default 50]')
#parser.add_argument('-t', dest='tax', action='store_true', help='Retrieve higher taxonomic info for the records on a full summary [default: do not]')
parser.set_defaults(full=False)
parser.set_defaults(tax=False)
# parse the command line
if len(sys.argv[1:]) == 0: # if there are no arguments
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
search_str = args.search_str
full = args.full
max_recs = args.max_recs
#tax = args.tax
if not max_recs:
max_recs = 50
if full:
print('Running a full summary and outputting result to search_summary.tab')
#elif tax:
# sys.exit('The taxonomy -t option only works with a full summary -f option')
# Perform the search and retrieve accession numbers
Entrez.email = '[email protected]' # so they can notify me if my usage is improper; this is required
handle = Entrez.esearch(db = 'nucleotide', term = search_str, idtype = 'acc', retmax = max_recs)
record = Entrez.read(handle)
handle.close()
if int(record['Count']) > max_recs: # this may be different from the number of accession numbers returned (note: default retmax = 20)
print('Note that ' + record['Count'] + ' records were found, but only ' + str(max_recs) + ' are set to be retrieved')
acc_list = record['IdList']
print('Retrieved ' + str(len(acc_list)) + ' accession numbers for the input search string')
# Report result and optionally retrieve and output additional information to file
if not full:
print('Accession numbers:')
for acc_num in acc_list:
print(acc_num)
else:
rec_list = []
new_rec_list = []
# genera_list = []
# genera_search = []
count = len(acc_list)
if count > 50:
batch_size = 50
else:
batch_size = count
# first, capture all the record info
for start in range(0, count, batch_size):
end = min(count, start + batch_size)
print('Searching Genbank accession details for ' + str(start + 1) + ' to ' + str(end) + ' of ' + str(count))
sub_acc_list = acc_list[start: end]
id_list = ','.join(sub_acc_list) # comma-delimited list of accession numbers
handle = Entrez.esummary(db = 'nucleotide', id = id_list)
records = Entrez.read(handle)
handle.close()
for index, record in enumerate(records):
title = record['Title']
# if title.split()[0] == 'UNVERIFIED:':
# genus = title.split()[1]
# specific_ep = title.split()[2]
# else:
# genus = title.split()[0]
# specific_ep = title.split()[1]
length = str(record['Length'])
tax_id = record['TaxId']
# if genus not in genera_list:
# genera_list.append(genus)
# genera_search.append((genus, tax_id))
# rec_list.append((genus, specific_ep, sub_acc_list[index], length, title))
rec_list.append((sub_acc_list[index], tax_id, length, title))
# now, grab the taxon info
for start in range(0, count, batch_size):
end = min(count, start + batch_size)
print('Searching Genbank taxonomy details for ' + str(start + 1) + ' to ' + str(end) + ' of ' + str(count))
sub_rec_list = rec_list[start: end]
id_list = ','.join([str(x[1]) for x in sub_rec_list])
handle = Entrez.efetch(db = 'Taxonomy', id = id_list)
records = Entrez.read(handle)
handle.close()
for index, record in enumerate(records):
name = record['ScientificName']
genus = name.split()[0]
specific_ep = name.split()[1]
for level in record['LineageEx']:
if level['Rank'] == 'order':
order = level['ScientificName']
elif level['Rank'] == 'family':
family = level['ScientificName']
else:
continue
new_rec_list.append([order, family, genus, specific_ep, sub_rec_list[index][0],
sub_rec_list[index][2], sub_rec_list[index][3]])
# Use the taxonomy database to extract order and family for each genus, if requested
# if tax:
# genera_dic = {}
# count = len(genera_search)
#
# if count > 50:
# batch_size = 50
# else:
# batch_size = count
#
# for start in range(0, count, batch_size):
# end = min(count, start + batch_size)
# print('Searching Genbank taxonomy details for ' + str(start + 1) + ' to ' + str(end) + ' of ' + str(count) + ' genera')
#
# sub_gen_list = genera_search[start: end]
# id_list = ','.join([str(x[1]) for x in sub_gen_list])
# handle = Entrez.efetch(db = 'Taxonomy', id = id_list)
# records = Entrez.read(handle)
# handle.close()
#
# for index, record in enumerate(records):
# for level in record['LineageEx']:
# if level['Rank'] == 'order':
# order = level['ScientificName']
# elif level['Rank'] == 'family':
# family = level['ScientificName']
# else:
# continue
# genera_dic[sub_gen_list[index][0]] = (order, family)
# Output a summary
out_list = []
for rec in new_rec_list:
out_list.append('\t'.join(rec))
# if tax:
# for rec in rec_list:
# out_list.append('\t'.join(genera_dic[rec[0]]) + '\t' + '\t'.join(rec))
# else:
# for rec in rec_list:
# out_list.append('\t'.join(rec))
with open('search_summary.tab', 'w') as outfile:
outfile.write('\t'.join(('order', 'family', 'genus', 'specific_ep', 'accession', 'length', 'full_title')) + '\n'
+ '\n'.join(out_list) + '\n')
# if tax:
# outfile.write('\t'.join(('order', 'family', 'genus', 'specific_ep', 'accession', 'length', 'full_title')) + '\n' + '\n'.join(out_list) + '\n')
# else:
# outfile.write('\t'.join(('genus', 'specific_ep', 'accession', 'length', 'full_title')) + '\n' + '\n'.join(out_list) + '\n')