<p>The PileupRead object as a query_position attribute, which you can use for this:</p>

<pre class="lang-py prettyprint-override"><code>for pileupcolumn in samfile.pileup() :
    for pileupread in pileupcolumn.pileups:
         if (pileupread.alignment.mapping_quality &lt;= 15):
             continue      
         if not pileupread.is_del and not pileupread.is_refskip:
             if pileupread.alignment.query_qualities[pileupread.query_position] &lt; 10:
                 # Skip entries with base phred scores &lt; 10
                 continue
             if pileupread.alignment.is_reverse: #negative
                   ReverseList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]
             else:
                   ForwardList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]
</code></pre>

<p>Note the 6th line, which implements a filter with a threshold of 10.</p>

CC BY-SA 3.0
2017-07-25T11:42:57.597Z
2017-07-25T11:42:57.597Z
Devon Ryan
<p>Is there a way to filter bases in BAM files based on phred quallities through python's pysam ?</p>

<p>I have a code here that</p>

<ul>
<li>Takes the nucleobases per position from a BAM file using pysam's pileup function</li>
<li><p>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.</p>

<pre><code> samfile = pysam.Samfile( filename, "rb" )
 ReverseList = [''] * lenref
 ForwardList = [''] * lenref
 for pileupcolumn in samfile.pileup() :
     for pileupread in pileupcolumn.pileups:
         if (pileupread.alignment.mapping_quality &lt;= 15):
               continue      
         if not pileupread.is_del and not pileupread.is_refskip:
               if pileupread.alignment.is_reverse: #negative
                      ReverseList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]
               else:
                      ForwardList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]

 samfile.close()
</code></pre></li>
</ul>

<p>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.</p>

CC BY-SA 3.0
2017-07-25T11:46:16.133Z
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.
Kamil S Jaron
CC BY-SA 3.0
2017-07-25T12:06:56.467Z
I am making a tool for analyzing BAM file so I want to filter it again to improve my results.
Ammar Sabir Cheema
CC BY-SA 3.0
2017-07-25T12:07:39.673Z
It is also reported in previous studies.
Ammar Sabir Cheema
CC BY-SA 3.0
2017-07-25T13:04:06.387Z
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
<sam><python><pysam>
Filtering bases based on phred qualities with pysam
# Filtering bases based on phred qualities with pysam

(Asked by: username_0 on 2017-07-25)

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() :
     for pileupread in pileupcolumn.pileups:
         if (pileupread.alignment.mapping_quality <= 15):
               continue      
         if not pileupread.is_del and not pileupread.is_refskip:
               if pileupread.alignment.is_reverse: #negative
                      ReverseList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]
               else:
                      ForwardList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]

 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.

**Tags:** sam, python, pysam
> **Comments:**
> **username_1**: 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.  
> **username_0**: I am making a tool for analyzing BAM file so I want to filter it again to improve my results.  
> **username_0**: It is also reported in previous studies.  
> **username_2**: 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?  

---

## 1 answers

### Answer by username_3 on 2017-07-25. Score: 4 | Accepted answer

The PileupRead object as a query_position attribute, which you can use for this:
for pileupcolumn in samfile.pileup() :
    for pileupread in pileupcolumn.pileups:
         if (pileupread.alignment.mapping_quality <= 15):
             continue      
         if not pileupread.is_del and not pileupread.is_refskip:
             if pileupread.alignment.query_qualities[pileupread.query_position] < 10:
                 # Skip entries with base phred scores < 10
                 continue
             if pileupread.alignment.is_reverse: #negative
                   ReverseList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]
             else:
                   ForwardList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]

Note the 6th line, which implements a filter with a threshold of 10.


---

