-
Notifications
You must be signed in to change notification settings - Fork 8
/
blastparser.py
executable file
·341 lines (257 loc) · 9.09 KB
/
blastparser.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
#! /usr/bin/env python
"""
Yet Another BLAST parser for NCBI BLAST output.
Goals:
- nice introspection
- nice Pythonic accessibility
- maintainability in the face of changing NCBI BLAST output formats
Sample usage: ::
for record in parse_file('blast_output.txt'):
print '-', record.query_name, record.database.name
for hit in record.hits:
print '--', hit.subject_name, hit.subject_length
print ' ', hit.total_score, hit.total_expect
for submatch in hit:
print submatch.expect, submatch.bits
print submatch.query_sequence
print submatch.alignment
print submatch.subject_sequence
Here, 'submatch' is a BlastObjectSubmatch; 'hit' is a BlastSubjectHits;
'record' is a BlastQuery; and 'record.database' is a BlastDatabase. See
the docstrings below for attributes available on these objects.
Author: C. Titus Brown <[email protected]>
"""
__version__ = 0.2
__all__ = ['BlastParser', 'parse_fp', 'parse_file', 'parse_string',
'open_shelf']
__docformat__ = 'restructuredtext'
import math
from cStringIO import StringIO
import parse_blast
###
class BlastSubjectSubmatch(object):
"""
BlastSubjectSubmatch.
A specific submatch (score/alignment) of a query sequence to a
subject sequence.
Attributes:
- expect
- frame1
- frame2
- score
- query_start
- query_end
- subject_start
- subject_end
- query_sequence
- subject_sequence
Usage: ::
print submatch_obj.expect
(etc.)
"""
# __slots__ = ['expect', 'frame1', 'frame2',
# 'query_start', 'query_end', 'query_sequence',
# 'subject_start', 'subject_end', 'subject_sequence', 'identity']
def __init__(self, expect, frame1, frame2,
q_start, q_end, q_seq, s_start, s_end, s_seq, identity, score):
self.expect = math.pow(10, -expect)
self.frame1 = frame1
self.frame2 = frame2
self.query_start = q_start
self.query_end = q_end
self.query_sequence = q_seq
self.subject_start = s_start
self.subject_end = s_end
self.subject_sequence = s_seq
self.score = score
def __repr__(self):
return "<BlastSubjectSubmatch(expect=%g, query %d-%d, subject %d-%d))>"\
% (self.expect, self.query_start, self.query_end,
self.subject_start, self.subject_end)
class BlastSubjectHits(object):
"""
BlastSubjectHits.
A list of all of the matches between a query sequence and a subject
sequence.
Attributes:
* subject_name -- name of subject sequence.
* matches -- list of BlastSubjectSubmatch objects.
Usage: ::
print hits_object.subject_name
for match in hits_object:
print match
"""
# __slots__ = ['subject_name', 'matches' ]
def __init__(self, subject_name, matches):
self.subject_name = str(subject_name)
self.matches = matches
def __getitem__(self, i):
return self.matches[i]
def __len__(self):
return len(self.matches)
def __repr__(self):
seqname = build_short_sequence_name(self.subject_name)
return "<BlastSubjectHits(%s, %d matches)>" % (seqname, len(self))
class BlastQuery(object):
"""
A BLAST query (single sequence against database) containing all results.
Attributes:
* query_name -- name of query sequence (following 'Query=').
* hits -- a list of BlastSubjectHits, containing each match + alignment.
Usage: ::
print query_object.query_name
for hits_object in query_object:
print hits_object.subject_name
"""
# __slots__ = ['query_name', 'hits' ]
def __init__(self, query_name, hits):
self.query_name = query_name
self.hits = list(hits)
def __repr__(self):
query_short = build_short_sequence_name(self.query_name)
return "<BlastQuery(%s (%d hits))>" % (query_short, len(self.hits))
def __len__(self):
return len(self.hits)
def __getitem__(self, i):
return self.hits[i]
class _BlastShelf(object):
def __init__(self, filename, mode='r'):
from shelve import BsdDbShelf
from bsddb import btopen
_db = btopen(filename, 'r')
self.db = BsdDbShelf(_db)
def __iter__(self):
db = self.db
last_k, _ = db.last()
k, v = db.first()
while k != last_k:
yield k, v
k, v = db.next()
yield k, v
def open_shelf(filename, mode='r'):
from shelve import BsdDbShelf
from bsddb import btopen
return _BlastShelf(filename, mode)
def parse_file(filename):
"""
Parse records from a given file; 'filename' is the path to the file.
"""
b = BlastParser()
for record in b.parse_file(filename):
yield record
def parse_fp(fp, **kw):
"""
Parse records out of the given file handle.
"""
b = BlastParser()
for record in b.parse_fp(fp, **kw):
yield record
def parse_string(s):
"""
Parse records out of a string buffer.
"""
fp = StringIO(s)
b = BlastParser()
for record in b.parse_fp(fp):
yield record
class _PygrBlastHitParser(parse_blast.BlastHitParser):
def generate_intervals(self):
yield self.query_id, self.subject_id, \
BlastSubjectSubmatch(self.e_value,
None,
None,
self.query_start,
self.query_end,
self.query_seq,
self.subject_start,
self.subject_end,
self.subject_seq,
self.identity_percent,
self.blast_score)
class BlastParser(object):
"""
BlastParser objects coordinate the use of pyparsing parsers to
parse complete BLAST records.
Attributes:
* blast_record -- an individual BLAST record; returns BlastQuery object.
* blast_output -- list of records; returns list of BlastQuery objects.
Methods:
* reset() -- clear the blast parser of persistent information.
* parse_string(s)
* parse_file(filename)
* parse_fp(fp)
"""
def __init__(self):
self.p = _PygrBlastHitParser()
def parse_file(self, filename):
fp = open(filename)
for record in self.parse_fp(fp):
yield record
def parse_fp(self, fp):
subjects = []
matches = []
cur_query = None
cur_subject = None
for query_id, subject_id, submatch in self.p.parse_file(fp):
if cur_subject != subject_id or cur_query != query_id:
if matches:
assert cur_subject
subject_hits = BlastSubjectHits(cur_subject, matches)
subjects.append(subject_hits)
matches = []
cur_subject = subject_id
if cur_query != query_id:
if cur_query:
assert subjects, cur_query
yield BlastQuery(cur_query, subjects)
subjects = []
cur_query = query_id
matches.append(submatch)
if matches:
subjects.append(BlastSubjectHits(cur_subject, matches))
if subjects:
yield BlastQuery(cur_query, subjects)
def build_short_sequence_name(name, max_len=20):
if len(name) < max_len:
return name
name_l = name.split()
if len(name_l) > 1:
return build_short_sequence_name(name_l[0], max_len)
name = name_l[0]
if len(name) > max_len:
name = name[:max_len-3] + '...'
return name
#####
if __name__ == '__main__':
import sys
from shelve import BsdDbShelf
from bsddb import btopen
from optparse import OptionParser
### read command line parameters
parser = OptionParser()
parser.add_option('-z', '--zlib-compressed', action="store_true",
dest="zlib_compressed",
help="read gzipped BLAST output file")
parser.add_option('-n', '--ignore-empty-hits', action="store_true",
dest="ignore_empty_hits",
help="ignore BLAST hits with no results")
(options, args) = parser.parse_args()
(blast_file, output_file) = args
### open blast file, open/create database r/w
if options.zlib_compressed:
import gzip
blast_fp = gzip.open(blast_file)
else:
blast_fp = open(blast_file)
_db = btopen(output_file, 'c')
db = BsdDbShelf(_db)
### go!
for n, record in enumerate(parse_fp(blast_fp,
ignore_no_hits=options.ignore_empty_hits)):
if n % 100 == 0:
print '...', n
if options.ignore_empty_hits and not record:
continue
name = record.query_name
db[name] = record
print 'read %d records total' % (n + 1,)