# Filtering bases based on phred qualities with pysam

Is there a way to filter bases in BAM files based on phred quallities through python's pysam ?

I have a code here that

• Takes the nucleobases per position from a BAM file using pysam's pileup function
• Saves it in ReverseList and ForwardList based on both strands (i.e forward and reverse), I want to reject those bases that have phred quality below 25 to be not stored in the ForwardList and ReverseList lists so that they are not used for further analysis.

 samfile = pysam.Samfile( filename, "rb" )
ReverseList = [''] * lenref
ForwardList = [''] * lenref
for pileupcolumn in samfile.pileup() :
continue
else:

samfile.close()


Where lenref = 16569(length of mitochondrial genome) and filename is the name of BAM file. I want to filter based of phred qualities of bases.

• Am I the only one who is missing the point of filtering alignments by phred scores? Phred scores are assigned during base calling, if you want to filter them out, why after mapping but not before? Also variant callers take into account phred scores, having low phred scores should not worsen quality of calling variants. Jul 25 '17 at 11:46
• I am making a tool for analyzing BAM file so I want to filter it again to improve my results. Jul 25 '17 at 12:06
• It is also reported in previous studies. Jul 25 '17 at 12:07
• Welcome to Bioinformatics! This question seem to need more information to get a good answer (Although the existing one answers how to filter as required). Why do you want to filter the bases? What are you trying to achieve?
– llrs
Jul 25 '17 at 13:04

The PileupRead object as a query_position attribute, which you can use for this:

for pileupcolumn in samfile.pileup() :