This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 269
/
exclude_seqs_by_blast.py
370 lines (279 loc) · 11.7 KB
/
exclude_seqs_by_blast.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
#!/usr/bin/env python
# file exclude_seqs_by_blast.py
from __future__ import division
"""
A lightweight script for BLASTing one or more sequences against a number of BLAST databases, and returning FASTA files a) of the results that did match b) of the results that didn't match c) raw blast results and also d) returning a report containing the parameters used, which sequences were excluded and why.
"""
from os.path import join
from time import strftime, time
from skbio.parse.sequences import parse_fasta
from bfillings.blast import blast_seqs, Blastall, BlastResult
__author__ = "Jesse Zaneveld"
__copyright__ = "Copyright 2011, The QIIME Project"
__credits__ = ["Jesse Zaneveld", "Rob Knight", "Adam Robbins-Pianka"]
__license__ = "GPL"
__version__ = "1.9.0"
__maintainer__ = "Jesse Zaneveld"
__email__ = "zaneveld@gmail.com"
FORMAT_BAR = """------------------------------""" * 2
def blast_genome(seqs, blast_db, e_value, max_hits, word_size, working_dir,
blast_mat_root, extra_params=[], DEBUG=True):
"""Blast sequences against all genes in a genome
seqs -- input sequences as strings
blast_db -- path to blast database
e_value -- e_value (float)
max_hits -- maximum sequences detected by BLAST to show
word_size -- word size for initial BLAST screen.
blast_mat_root -- location of BLAST matrix files
extra_params -- additional paramters to pass to BLAST
DEBUG -- display verbose debugging outout
"""
# set up params to use with blastp or
params = {
# matrix
"-M": "BLOSUM62",
# max procs
"-a": "1",
# expectation
"-e": e_value,
# max seqs to show
"-b": max_hits,
# Word size
"-W": word_size,
# max one line descriptions
"-v": max_hits,
# tabular output
"-m": "9",
# program
"-p": "blastn"
}
params.update(extra_params)
output = blast_seqs(seqs,
Blastall,
blast_db=blast_db,
params=params,
WorkingDir=working_dir,
add_seq_names=False,
blast_mat_root=blast_mat_root)
raw_output = [x for x in output['StdOut']]
return raw_output
def find_homologs(query_file, subject_genome, e_value, max_hits,
working_dir, blast_mat_root, wordsize,
percent_aligned, extra_params={},
require_hit=False, DEBUG=True):
"""BLAST query_file against subject_genome
query_file -- .nuc file or other FASTA file to BLAST against all files in file_list
subject_genome -- path to a KEGG .nuc file or other FASTA formated file.
e-value -- e-value threshold for blasts
percent_aligned -- minumum percent alignment, between 0.0 and 1.0
max_hits,blast_mat_root,extra_params -- these are passed along to blastn
DEBUG -- if True, display debugging output
"""
start_time = time()
raw_blast_output = []
seqs = open(query_file, "U").readlines()
if DEBUG:
print "BLASTING %s vs. %s" % (query_file, subject_genome)
blast_db = subject_genome
raw_output_data = blast_genome(seqs,
blast_db, e_value,
max_hits, wordsize, working_dir,
blast_mat_root, extra_params,
DEBUG=DEBUG)
if DEBUG:
print "Length of raw BLAST results:", len(raw_output_data)
curr_blast_result = BlastResult(raw_output_data)
align_filter = make_percent_align_filter(percent_aligned)
# should a mismatch filter be added?
filtered_ids, removed_ids = query_ids_from_blast_result(curr_blast_result,
align_filter, DEBUG=DEBUG)
return raw_output_data, filtered_ids, removed_ids
def sequences_to_file(results, outfile_name):
"""Translate a generator of label,seq tuples to an output file """
f = open(outfile_name, 'w+')
for label, seq in results:
output_lines = []
output_lines.append(">%s\n" % label)
output_lines.append("%s\n" % seq)
f.writelines(output_lines)
f.close()
def no_filter(blast_subject_entry):
"""A placeholder filter function which always returns True"""
return True
def make_percent_align_filter(min_percent):
"""Return a filter function that filters BLAST results on % alignment
min_percent -- minimum percent match as a float between 0 and 1"""
min_percent = float(min_percent) * 100
def align_filter(blast_result):
if float(blast_result['% IDENTITY']) < min_percent:
return False
else:
return True
return align_filter
def check_align_percent_field(d):
"""Check for empty percent identity fields in a dict"""
if d['% IDENTITY']:
return True
else:
return False
def query_ids_from_blast_result(
blast_result, filter_fn=no_filter, DEBUG=False):
"""Returns a list of blast query ids, filtered by a given function.
--blast_result: BLAST result from BLAST app controller
--filter_fn: a function that, given a dict representing a BLAST result
returns True or False based on whether the result passes
some filter.
"""
ok_ids = []
removed_ids = []
for id in blast_result:
for entry in blast_result[id]:
for subentry in entry:
if not check_align_percent_field(subentry):
continue
if not filter_fn(subentry):
removed_ids.append(id)
continue
ok_ids.append(subentry['QUERY ID'])
ok_ids = set(ok_ids)
# Ensure query seqs with multiple BLAST hits, only some of which
# are filtered out, don't end up in removed_ids
removed_ids = set(removed_ids) - ok_ids
return ok_ids, removed_ids
def ids_from_fasta_lines(lines):
"""Extract ids from label lines"""
ids = []
for line in lines:
if not line.startswith(">"):
continue
id = id_from_fasta_label_line(line)
ids.append(id)
return ids
def id_from_fasta_label_line(line):
"Extract id from fasta label line"
id_field = line.split()[0]
id = id_field.strip(">")
return id
def seqs_from_file(ids, file_lines):
"""Extract labels and seqs from file"""
for label, seq in parse_fasta(file_lines):
if id_from_fasta_label_line(label) in ids:
yield label, seq
def compose_logfile_lines(start_time, db_format_time, blast_time, option_lines,
formatdb_cmd, blast_results, options, all_ids,
hit_ids, removed_hit_ids,
included_ids, DEBUG):
"""Compose lines for a logfile from data on analysis"""
log_lines = []
log_lines.append("Sequence exclusion analysis run on %s" % strftime("%c"))
log_lines.append(
"Formatting subject database took %2.f seconds" %
(db_format_time))
log_lines.append(
"BLAST search took %2.f minute(s)" %
((blast_time) / 60.0))
log_lines.append(
"Total analysis completed in %2.f minute(s)" %
((time() - start_time) / 60.0))
log_lines.append(FORMAT_BAR)
log_lines.append(
"| Options |")
log_lines.append(FORMAT_BAR)
log_lines.extend(option_lines)
log_lines.append("Subject database formatted with command: %s"
% formatdb_cmd)
log_lines.append(FORMAT_BAR)
log_lines.append(
"| Results |")
log_lines.append(FORMAT_BAR)
log_lines.append("BLAST results above e-value threshold:")
log_lines.append(
"\t".join(["Query id", "Subject id", "percent identity", "alignment length",
"mismatches", "gap openings", "q. start", "q. end", "s. start", "s. end", "e-value", "bit score"]))
for line in blast_results:
if line.startswith("#"):
continue
else:
log_lines.append(line)
log_lines.append(
"Hits matching e-value and percent alignment filter: %s" %
','.join(sorted(hit_ids)))
log_lines.append(FORMAT_BAR)
log_lines.append(
"| Summary |")
log_lines.append(FORMAT_BAR)
log_lines.append("Input query sequences: %i" % len(all_ids))
log_lines.append(
"Query hits from BLAST: %i" %
(len(hit_ids) + len(removed_hit_ids)))
log_lines.append(
"Query hits from BLAST lacking minimal percent alignment: %i" %
len(removed_hit_ids))
log_lines.append("Final hits: %i" % len(hit_ids))
log_lines.append("Output screened sequences: %i" % len(included_ids))
log_lines.append(FORMAT_BAR)
log_lines.append(
"| Output |")
log_lines.append(FORMAT_BAR)
log_lines.append(
"Writing excluded sequences (hits matching filters) to: %s" %
join(options.outputdir, "matching.fna"))
log_lines.append(
"Writing screened sequences (excluding hits matching filters) to: %s" %
join(options.outputdir, "non-matching.fna"))
log_lines.append(
"Writing raw BLAST results to: %s" %
join(options.outputdir, 'raw_blast_results.txt'))
# format for printing
revised_log_lines = []
for line in log_lines:
line = line + "\n"
revised_log_lines.append(line)
if DEBUG:
for line in log_lines:
print line
return revised_log_lines
def check_options(parser, options):
"""Check to insure required options have been supplied"""
if options.percent_aligned > 1.0:
parser.error(
"Please check -p option: should be between 0.0(0%) and 1.0(100%)")
if options.querydb is None:
parser.error(
"Please check -i option: must specify path to a FASTA file")
try:
f = open(options.querydb, 'r')
f.close()
except IOError:
parser.error(
"Please check -i option: cannot read from query FASTA filepath")
if options.subjectdb is None:
parser.error(
"Please check -d option: must specify path to a FASTA file")
try:
f = open(options.subjectdb, 'r')
f.close()
except IOError:
parser.error(
"Please check -d option: cannot read from subject FASTA filepath")
if options.outputdir is None:
parser.error(
"Please check -o option: must specify the output directory path")
def format_options_as_lines(options):
"""Format options as a string for log file"""
option_lines = []
option_fields = str(options).split(",")
for field in option_fields:
option_lines.append(str(field).strip("{").strip("}"))
return option_lines
def ids_to_seq_file(ids, infile, outfile, suffix=''):
"""Lookup FASTA recs for ids and record to file
ids -- list of ids to lookup seqs for in infile
infile -- path to FASTA file
outfile -- base path to which to write FASTA entries
with ids in supplied ids
suffix -- will be appended to outfile base path
"""
seqs = seqs_from_file(ids, open(infile).readlines())
out_path = outfile + suffix
sequences_to_file(seqs, out_path)