Answers.Body: <p>The reason is that <code>sambamba</code> does not use the absolute value of the template length, so, if the read maps so the minus strand, the template length is actually negative. Setting a template length &gt; 0 means that all reads mapping to the minus strand are removed.</p>
  <p>In fact, you can't still filter for negative template lengths. See <a href="https://github.com/biod/sambamba/pull/479" rel="nofollow noreferrer">this GitHub PR</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-23T11:52:05.837Z
Answers.LastActivityDate: 2022-06-23T11:52:05.837Z
Answers.UserDisplayName: Einar
Body: <p>I have a BAM file (DNA, shallow whole genome sequencing at ~1X) where I want to filter reads (using sambamba) to keep only those which have a template length &gt; 20 and mapping quality &gt; 20, discarding unmapped reads.</p>
  <p>The filter I have used is this:</p>
  <pre><code>sambamba view -h \
     -t 5 \
     -F &quot;paired and template_length &gt; 0 and mapping_quality &gt; 20 and not (unmapped or mate_is_unmapped)&quot; \
     -f bam 142-ready.bam \
     -o 142-filter.bam
  </code></pre>
  <p>However, inspection of the reads in IGV led me to think that perhaps reads on the minus strand are being somehow filtered out.</p>
  <p>Pre filter:</p>
  <p><a href="https://i.sstatic.net/N2Ynp.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/N2Ynp.png" alt="Unfiltered view in IGV with mate pairs highlighted" /></a></p>
  <p>Post filter:</p>
  <p><a href="https://i.sstatic.net/peGxL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/peGxL.png" alt="Filtered view in IGV, showing only one mate pair" /></a></p>
  <p>The only way I don't get such an aggressive filtering is by setting only <code>not (unmapped or mate_is_unmapped)</code>. But that, obviously, defeats the purpose as it doesn't include what I need.</p>
  <p>I can't, however, see what exactly is wrong with the filter. What is the correct way to filter for template length while not accidentally discarding reads on the minus strand?</p>
  
Tags: <sam><filtering><reads><sambamba>
Title: Filtering paired-end reads with sambamba: avoid discarding reads on the minus strand
ThreadText: # Filtering paired-end reads with sambamba: avoid discarding reads on the minus strand
  
  (Asked by: username_0 on 2022-06-23)
  
  I have a BAM file (DNA, shallow whole genome sequencing at ~1X) where I want to filter reads (using sambamba) to keep only those which have a template length > 20 and mapping quality > 20, discarding unmapped reads.
  
  The filter I have used is this:
  
  ```
  sambamba view -h \
     -t 5 \
     -F "paired and template_length > 0 and mapping_quality > 20 and not (unmapped or mate_is_unmapped)" \
     -f bam 142-ready.bam \
     -o 142-filter.bam
  
  ```
  
  However, inspection of the reads in IGV led me to think that perhaps reads on the minus strand are being somehow filtered out.
  
  Pre filter:
  
  [![Unfiltered view in IGV with mate pairs highlighted](https://i.sstatic.net/N2Ynp.png)](https://i.sstatic.net/N2Ynp.png)
  
  Post filter:
  
  [![Filtered view in IGV, showing only one mate pair](https://i.sstatic.net/peGxL.png)](https://i.sstatic.net/peGxL.png)
  
  The only way I don't get such an aggressive filtering is by setting only `not (unmapped or mate_is_unmapped)`. But that, obviously, defeats the purpose as it doesn't include what I need.
  
  I can't, however, see what exactly is wrong with the filter. What is the correct way to filter for template length while not accidentally discarding reads on the minus strand?
  
  **Tags:** sam, filtering, reads, sambamba
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-06-23. Score: 1 | Accepted answer
  
  The reason is that `sambamba` does not use the absolute value of the template length, so, if the read maps so the minus strand, the template length is actually negative. Setting a template length > 0 means that all reads mapping to the minus strand are removed.
  
  In fact, you can't still filter for negative template lengths. See [this GitHub PR](https://github.com/biod/sambamba/pull/479).
  
  
  ---
  
  