/
parsers.py
395 lines (320 loc) · 11.9 KB
/
parsers.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python
import csv
import sys
import numpy as np
import pandas as pd
from blast import remap_blast_coords_df as remap_blast
'''
dammit uses 0-based, half-open intervals as its internal representation, as
spake by Saint Dijkstra (structured programs be upon Him) on the 397868400th
integer of Our Unix and observed by all pious and good followers of the Code.
Beware, fellow bioinformaticians! Heisenbugs and untyped Lambda Calculus be
upon ye who break from this most holy writ! Wretched followers of the 1,
demon peddlers of the fully closed and fully open intervals, sewers of discord!
Doom and damnation, dammit!
'''
blast_cols = [('qseqid', str),
('sseqid', str),
('pident', float),
('length', int),
('mismatch', int),
('gapopen', int),
('qstart', int),
('qend', int),
('sstart', int),
('send', int),
('evalue', float),
('bitscore', float)]
hmmscan_cols = [('target_name', str),
('target_accession', str),
('tlen', int),
('query_name', str),
('query_accession', str),
('query_len', int),
('full_evalue', float),
('full_score', float),
('full_bias', float),
('domain_num', int),
('domain_total', int),
('domain_c_evalue', float),
('domain_i_evalue', float),
('domain_score', float),
('domain_bias', float),
('hmm_coord_from', int),
('hmm_coord_to', int),
('ali_coord_from', int),
('ali_coord_to', int),
('env_coord_from', int),
('env_coord_to', int),
('accuracy', float),
('description', str)]
cmscan_cols = [('target_name', str),
('target_accession', str),
('query_name', str),
('query_accession', str),
('mdl', str),
('mdl_from', int),
('mdl_to', int),
('seq_from', int),
('seq_to', int),
('strand', str),
('trunc', str),
('pass', str),
('gc', float),
('bias', float),
('score', float),
('e_value', float),
('inc', str),
('description', str)]
gff_cols = [('seqid', str),
('source', str),
('feature_type', str),
('start', float),
('end', float),
('score', float),
('strand', str),
('frame', float),
('attributes', str)]
crb_cols = [('query', str),
('subject', str),
('id', str),
('aln_len', int),
('evalue', float),
('bitscore', float),
('qrange', str),
('srange', str),
('qlen', int),
('slen', int)]
def convert_dtypes(df, dtypes):
'''Convert the columns of a DataFrame to the types specified
in the given dictionary, inplace.
Args:
df (DataFrame): The DataFrame to convert.
dtypes (dict): Dictionary mapping columns to types.
'''
for c in df.columns:
try:
df[c] = df[c].astype(dtypes[c])
except KeyError:
pass
def blast_to_df_iter(fn, delimiter='\t', chunksize=10000, remap=True):
'''Iterator of DataFrames of length chunksize parsed from an
NCBI BLAST+ `-outfmt6` file.
Native BLAST+ uses an interval of the form [start,end), start >= 1. This
changes to [end,start) when on the negative strand, apparently soley
to make other bioinformaticians suffer.
We convert to proper 0-based, half-open intervals.
Args:
fn (str): The results file.
chunksize (int): Hits per iteration.
Yields:
DataFrame: Pandas DataFrme with the BLAST+ hits.
'''
for group in pd.read_table(fn, header=None, skipinitialspace=True,
names=[k for k, _ in blast_cols],
delimiter=delimiter, chunksize=chunksize):
convert_dtypes(group, dict(blast_cols))
if remap:
remap_blast(group)
yield group
def parse_busco_full(fn):
'''Parses a BUSCO full result table into a Pandas DataFrame.
Args:
fn (str): The results file.
Returns:
DataFrame: The results DataFrame.
'''
df = pd.read_table(fn)
return df.rename(columns={'#BUSCO_group': 'BUSCO_group'})
def parse_busco_summary(fn):
'''Parses a BUSCO summary file into a JSON compatible
dictionary.
Args:
fn (str): The summary results file.
Returns:
dict: The BUSCO results.
'''
res = {}
with open(fn) as fp:
for ln in fp:
if ln.strip().startswith('C:'):
tokens = ln.split(',')
for token in tokens:
key, _, val = token.partition(':')
key = key.strip()
val = val.strip().strip('%')
if key == 'C':
valc, _, vald = val.partition('%')
valc = valc.strip()
vald = vald.strip('D:][%')
res['C(%)'] = valc
res['D(%)'] = vald
else:
if key != 'n':
key += '(%)'
res[key] = val.strip().strip('%')
return res
def parse_busco_multiple(fn_list, dbs=['metazoa', 'vertebrata']):
'''Parses multiple BUSCO results summaries into an appropriately
index DataFrame.
Args:
fn_list (list): List of paths to results files.
dbs (list): List of BUSCO database names.
Returns:
DataFrame: The formated DataFrame.
'''
data = []
for fn in fn_list:
data.append(parse_busco_summary(fn))
df = pd.DataFrame(data)
df['fn'] = [os.path.basename(fn)[14:-14].strip('.') for fn in fn_list]
df['db'] = None
for db in dbs:
idx = df.fn.str.contains(db)
df.loc[idx,'db'] = db
df.loc[idx,'fn'] = df.loc[idx, 'fn'].apply(lambda fn: fn[:fn.find(db)].strip('. '))
return df
def parse_gff3(fn, chunksize=10000):
'''Iterator over DataFrames of length chunksize from a given
GTF/GFF file.
GFF3 uses a 1-based, fully closed interval. Truly the devil's format.
We convert to proper 0-based, half-open intervals.
Args:
fn (str): Path to the file.
chunksize (int): Rows per iteration.
Yields:
DataFrame: Pandas DataFrame with the results.
'''
def attr_col_func(col):
d = {}
for item in col.strip(';').split(';'):
key, _, val = item.strip().partition('=')
d[key] = val.strip('')
return d
# Read everything into a DataFrame
for group in pd.read_table(fn, delimiter='\t', comment='#',
names=[k for k,_ in gff_cols], na_values='.',
converters={'attributes': attr_col_func},
chunksize=chunksize, header=None,
dtype=dict(gff_cols)):
# Generate a new DataFrame from the attributes dicts, and merge it in
gtf_df = pd.merge(group,
pd.DataFrame(list(group.attributes)),
left_index=True, right_index=True)
del gtf_df['attributes']
# Repent, repent!
gtf_df.start = gtf_df.start - 1
yield gtf_df
def crb_to_df_iter(fn, chunksize=10000, remap=True):
'''Iterator of DataFrames of length chunksize parsed from
the results from CRBB version crb-blast 0.6.6.
crb-blast is given the same treatment as BLAST+, as that's what
it uses under the hood.
We convert to proper 0-based, half-open intervals.
Args:
fn (str): The results file.
chunksize (int): Hits per iteration.
Yields:
DataFrame: Pandas DataFrame with the CRBB hits.
'''
for group in pd.read_table(fn, header=None, names=[k for k, _ in crb_cols],
delimiter='\t', chunksize=chunksize):
convert_dtypes(group, dict(crb_cols))
qrange = group.qrange.str.partition('..')
group['qstart'] = qrange[0].astype(int)
group['qend'] = qrange[2].astype(int)
del group['qrange']
srange = group.srange.str.partition('..')
group['sstart'] = srange[0].astype(int)
group['send'] = srange[2].astype(int)
del group['srange']
if remap:
remap_blast(group)
yield group
def busco_to_df(fn_list, dbs=['metazoa', 'vertebrata']):
''' Given a list of BUSCO results from different databases, produce
an appropriately multi-indexed DataFrame of the results.
Args:
fn_list (list): The BUSCO summary files.
dbs (list): The BUSCO databases used for these runs.
Returns:
DataFrame: The BUSCO results.
'''
data = []
for fn in fn_list:
data.append(parse_busco(fn))
df = pd.DataFrame(data)
df['fn'] = [os.path.basename(fn)[14:-14].strip('.') for fn in fn_list]
df['db'] = None
for db in dbs:
idx = df.fn.str.contains(db)
df.loc[idx,'db'] = db
df.loc[idx,'fn'] = df.loc[idx, 'fn'].apply(lambda fn: fn[:fn.find(db)].strip('. '))
return df
def hmmscan_to_df_iter(fn, chunksize=10000):
'''Iterator over DataFrames of length chunksize from a given
hmmscan result file.
HMMER uses 1-based, fully open intervals. Another format of the devil.
We convert to proper 0-based, half-open intervals.
Args:
fn (str): Path to the hmmscan file.
chunksize (int): Hits per iteration.
Yields:
DataFrame: Pandas DataFrame with the hmmscan hits.
'''
def split_query(item):
q, _, _ = item.rpartition('|')
return q
def build_df(data):
df = pd.DataFrame(data, columns=[k for k, _ in hmmscan_cols])
convert_dtypes(df, dict(hmmscan_cols))
df['full_query_name'] = df.query_name
df['query_name'] = df.query_name.apply(split_query)
df.hmm_coord_from = df.hmm_coord_from - 1
df.ali_coord_from = df.ali_coord_from - 1
df.env_coord_from = df.env_coord_from -1
return df
data = []
with open(fn) as fp:
for n, ln in enumerate(fp):
if not ln or ln.startswith('#'):
continue
tokens = ln.split()
data.append(tokens[:len(hmmscan_cols)-1] + \
[' '.join(tokens[len(hmmscan_cols)-1:])])
if len(data) >= chunksize:
yield build_df(data)
data = []
if data:
yield build_df(data)
def cmscan_to_df_iter(fn, chunksize=10000):
'''Iterator over DataFrames of length chunksize from a given
cmscan result file.
1-based, fully open intervals. Truly Infernal.
We convert to proper 0-based, half-open intervals.
Args:
fn (str): Path to the cmscan file.
chunksize (int): Hits per iteration.
Yields:
DataFrame: Pandas DataFrame with the cmscan hits.
'''
def build_df(data):
df = pd.DataFrame(data, columns=[k for k, _ in cmscan_cols])
convert_dtypes(df, dict(cmscan_cols))
df.mdl_from = df.mdl_from - 1
df.seq_from = df.seq_from - 1
return df
data = []
with open(fn) as fp:
for ln in fp:
ln = ln.strip()
if not ln or ln.startswith('#'):
continue
tokens = ln.split()
data.append(tokens[:len(cmscan_cols)-1] + \
[' '.join(tokens[len(cmscan_cols)-1:])])
if len(data) >= chunksize:
yield build_df(data)
data = []
if data:
yield build_df(data)