I have a code below:
def FilterReads(in_file, out_file):
def read_ok(read):
"""
read_ok - reject reads with a low quality (<5) base call
read - a PySam AlignedRead object
returns: True if the read is ok
"""
if any([ord(c)-33 < _BASE_QUAL_CUTOFF for c in list(read.qual)]):
return False
else:
return True
_BASE_QUAL_CUTOFF = 30
bam_in = pysam.Samfile(in_file, 'rb')
bam_out = pysam.Samfile(out_file, 'wb', template=bam_in)
for read in bam_in.fetch():
if read_ok(read):
bam_out.write(read)
This code works fine by
- Rejecting reads having a base with phred quality score below five
- But it first takes a BAM file
- And then creates a filtered BAM file for further analysis but this takes a lot of time.
So is there a way to reject these reads using pileup in pysam so that I may not have to create a file then sort it and again read it? Or can I modify this code to perform the same function
if …: return False else: return True
is an anti-pattern. Just writereturn not …
. Or, better yet, logically invert the condition; in your case, you could writereturn all([ord(c) - 33 >= _BASE_QUAL_CUTOFF for c in list(read.qual)])
(this follows from straightforward application of de Morgan’s laws). $\endgroup$any(ord(c)-33 < _BASE_QUAL_CUTOFF for c in read.qual):
$\endgroup$fetch
method. $\endgroup$