forked from RBGKew/PAFTOL_Validation_Pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GB_extract.py
277 lines (194 loc) · 7.52 KB
/
GB_extract.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
#!/usr/bin/env python
# coding: utf-8
# In[52]:
##################################
# Author: Kevin Leempoel
# Copyright © 2020 The Board of Trustees of the Royal Botanic Gardens, Kew
##################################
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
import pandas as pd
import os
import sys
# # Parameters
# In[53]:
max_N=0.05
max_per_sp=2
# In[54]:
ref = sys.argv[1]
# ref = 'NCBI_18s'
# In[55]:
if ref == 'NCBI_18s':
gb_file = 'NCBI_18s.gb'
gene = ['rrn18','18S rRNA','18S ribosomal RNA']
acc_type = ['gene','rRNA']
min_len=1400; max_len=2400
elif ref == 'NCBI_28s':
gb_file = 'NCBI_28s.gb'
gene = ['rrn28','28S rRNA','28S ribosomal RNA']
acc_type = ['gene','rRNA']
min_len=3000; max_len=3800
elif ref == 'NCBI_16s':
gb_file = 'NCBI_16s.gb'
gene = ['rrn16','16S rRNA','16S ribosomal RNA']
acc_type = ['gene','rRNA']
min_len=1200; max_len=1800
elif ref == 'NCBI_23s':
gb_file = 'NCBI_23s.gb'
gene = ['rrn23','23S rRNA','23S ribosomal RNA']
acc_type = ['gene']
min_len=2500; max_len=2900
elif ref == 'NCBI_rbcL':
gb_file = 'NCBI_rbcL.gb'
gene = ['rbcL','rbcl','ribulose-1,5-bisphosphate carboxylase/oxygenase large subunit']
acc_type = ['CDS']
min_len=1100; max_len=1500
elif ref == 'NCBI_trnL':
gb_file = 'NCBI_trnL.gb'
gene = ['trnL','tRNA-Leu']
acc_type = ['tRNA']
min_len=30; max_len=80
elif ref == 'NCBI_ITS1':
gb_file = 'NCBI_ITS1.gb'
gene = ['ITS','ITS1','internal transcribed spacer 1']
acc_type = ['misc_RNA']
min_len=180; max_len=280
elif ref == 'NCBI_ITS2':
gb_file = 'NCBI_ITS2.gb'
gene = ['ITS2','internal transcribed spacer 2']
acc_type = ['misc_RNA']
min_len=170; max_len=280
elif ref == 'NCBI_rpl2':
gb_file = 'NCBI_rpl2.gb'
gene = ['rpl2','ribosomal protein L2']
acc_type = ['cds']
min_len=500; max_len=1500
elif ref == 'NCBI_ndhf':
gb_file = 'NCBI_ndhf.gb'
gene = ['ndhf','ndhF']
acc_type = ['cds']
min_len=1000; max_len=2500
# # Main
# In[56]:
print(gb_file,gene,acc_type,min_len,max_len)
# In[57]:
def get_qualifier(feature, attribute):
try:
return feature.qualifiers[attribute][0]
except:
return None
# In[58]:
# %%time
count=0
for line in open(gb_file):
if 'LOCUS' in line:
count += 1
print(count)
# In[59]:
# %%time
print('reading genbank_file',end='...')
rec_ls = []; rec_rm=[]; rec_count=0
for record in SeqIO.parse(gb_file, "genbank"):
rec_count += 1
if record.features:
for feature in record.features:
if (feature.type in acc_type):
seq_dic={}
seq_dic['Locus'] = record.id
seq_dic['type'] = feature.type
if (feature.type in ['gene','CDS']):
seq_dic['gene'] = get_qualifier(feature, 'gene')
elif (feature.type in ['rRNA','tRNA','misc_RNA']):
seq_dic['gene'] = get_qualifier(feature, 'product')
if seq_dic['gene'] in gene:
seq_dic['Seq'] = str(feature.location.extract(record).seq)
seq_dic['Len'] = len(seq_dic['Seq'])
seq_dic['Nn'] = seq_dic['Seq'].count('N')
for feature in record.features:
if (feature.type == "source"):
seq_dic['sci_name'] = get_qualifier(feature, 'organism')
seq_dic['mol_type'] = get_qualifier(feature, 'mol_type')
seq_dic['TaxID'] = get_qualifier(feature, 'db_xref').replace('taxon:','')
rec_ls.append(seq_dic)
else:
rec_rm.append(seq_dic)
print('read',rec_count,'accessions')
# In[60]:
rec_df = pd.DataFrame(rec_ls)
print(rec_df.shape[0],'entries for',rec_df.sci_name.nunique(),'species')
print(rec_df.groupby('type').size().sort_values(ascending=False).to_dict())
print(rec_df.groupby('gene').size().sort_values(ascending=False).to_dict())
# In[61]:
scut=min_len;
print(rec_df.Len.quantile([.01,.05,.1,0.5,.9,.95,.99]).to_dict())
print(rec_df[rec_df.Len>scut].Len.quantile([.01,.05,.1,0.5,.9,.95,.99]).to_dict())
print(rec_df[rec_df.Len>scut].Len.median()+(rec_df[rec_df.Len>scut].Len.std()*2))
print(rec_df[rec_df.Len>scut].Len.median()-(rec_df[rec_df.Len>scut].Len.std()*2))
rec_df.Len.hist(bins=100);
# In[62]:
rec_df['rN'] = rec_df.Nn/rec_df.Len
print('Removing',rec_df[rec_df.rN>=max_N].shape[0],'accessions with too many Ns')
print(rec_df.shape[0],end=' > ')
rec_df = rec_df[rec_df.rN<max_N]
print(rec_df.shape[0])
print('Removing',rec_df[rec_df.Len<min_len].shape[0],'accessions too small')
print(rec_df.shape[0],end=' > ')
rec_df = rec_df[rec_df.Len>=min_len]
print(rec_df.shape[0])
print('Removing',rec_df[rec_df.Len>max_len].shape[0],'accessions too long')
print(rec_df.shape[0],end=' > ')
rec_df = rec_df[rec_df.Len<=max_len]
print(rec_df.shape[0])
# In[63]:
rm_char='[]()×'
for char in rm_char:
rec_df['sci_name'] = rec_df['sci_name'].str.replace(char,'')
# In[64]:
print('sending',rec_df.sci_name.nunique(),'species names to WCVP_taxo')
rec_df.groupby('sci_name').head(1).sci_name.to_csv(gb_file.replace('.gb','_NCBI.csv'),index=False)
# In[65]:
print('running wcvp_taxo',end='...')
# print(os.system('python ../../PAFTOL_DB/wcvp_taxo.py ../../PAFTOL_DB/wcvp_v5_jun_2021.txt ' + \
# gb_file.replace('.gb','_NCBI.csv') + ' -g -s similarity_genus -d divert_genusOK'))
print(os.system('python wcvp_taxo.py wcvp_v5_jun_2021.txt ' + gb_file.replace('.gb','_NCBI.csv') + ' -g -s similarity_genus -d divert_genusOK'))
wcvp = pd.read_csv(gb_file.replace('.gb','_NCBI_wcvp.csv'))
wcvp = wcvp[wcvp.sci_name.notnull()]
print('found',wcvp.sci_name.nunique(),'species in WCVP')
print(rec_df.shape[0],end=' > ')
rec_df = pd.merge(rec_df.rename(columns={'sci_name':'Ini_sci_name'}),wcvp,how='inner',on='Ini_sci_name')
print(rec_df.shape[0])
# In[66]:
for char in rm_char:
rec_df['sci_name'] = rec_df['sci_name'].str.replace(char,'')
# In[67]:
sp_count = rec_df.groupby('sci_name').size().to_frame()
print('reducing dataset to max',max_per_sp,'accessions per species, ',(sp_count[0]>2).sum())
print(rec_df.shape[0],end=' > ')
rec_df = rec_df.sort_values('Len',ascending=False).groupby('Locus').head(1).groupby('sci_name').head(max_per_sp)
print(rec_df.shape[0])
rec_df = rec_df.sort_values(['family','genus','sci_name']).reset_index(drop=True)
print('f:',rec_df.family.nunique(),'g:',rec_df.genus.nunique(),'s:',rec_df.sci_name.nunique())
# In[68]:
# rec_df = rec_df[rec_df['type']=='gene']
# In[69]:
types = list(rec_df.type.unique())
print(rec_df.groupby('type').size().to_dict())
rec_fasta=[]
for idx, row in rec_df.iterrows():
record = SeqRecord(Seq(row.Seq))
record.id = row.Locus
record.description = ';gene=' + row.gene + ',type=' + row.type + ',f=' + row.family + ',g=' + row.genus + ',s=' + row.sci_name + ',ini_s=' + row.Ini_sci_name + ';'
rec_fasta.append(record)
SeqIO.write(rec_fasta,gb_file.replace('.gb','.fasta'),format='fasta')
rec_df[['Locus','gene','mol_type', 'Len',
'sci_name', 'kew_id','family', 'genus', 'species', 'infraspecies', 'Duplicates',
'Ini_sci_name', 'TaxID']].to_csv(gb_file.replace('.gb','_TAXO.csv'),index=False)
# In[70]:
print(rec_df.Len.quantile([.01,.05,.1,0.5,.9,.95,.99]).to_dict())
print(rec_df.Len.median()+(rec_df.Len.std()*2))
print(rec_df.Len.median()-(rec_df.Len.std()*2))
rec_df.Len.hist(bins=50);
# In[71]:
rec_rm_df = pd.DataFrame(rec_rm)
print(rec_rm_df.groupby('type').size().to_dict())