<p>The <code>-f</code> option of <code>samtools view</code> is for <a href="http://www.htslib.org/doc/1.11/samtools-flags.html" rel="nofollow noreferrer">flags</a> and can be used to filter reads in bam/sam file matching certain criteria such as properly paired reads (<code>0x2</code>) :</p>
<pre><code>samtools view -f 0x2 -b in.bam &gt; out.bam
</code></pre>

CC BY-SA 4.0
2022-06-22T06:06:08.087Z
Thanks. If I remove the low mapping quality reads and reads with high mismatches after the alignment, will the flags be updated? If they are not updated, there will possibly be unpaired reads even if I use flags for filtering in my point of view.
Wang Ming
CC BY-SA 4.0
2022-06-22T10:06:01.957Z
That is a good question and there is a good change those flags won't be updated, it would need some testing. Here is a post on [seqanswers](https://www.seqanswers.com/forum/bioinformatics/bioinformatics-aa/20631-remove-reads-from-bam-whose-mate-has-already-been-filtered?t=24472) that might help you.
Paul Endymion
CC BY-SA 4.0
2022-06-20T13:43:49.460Z
2022-06-20T13:43:49.460Z
Paul Endymion
<p>I have sam and bam files for the chimeric reads, which come from two different parts of the genome (For example, the first half of the read from part of Chromosome 1 and the second half of the read from part of Chromosome 3). I have removed the low mapping quality reads (MAPQ &lt;= 30) and reads with high mismatches. This might result in some unpaired reads which need to be removed before further analysis. May I ask whether there is any way to remove the unpaired reads in sam/bam files? Thanks!</p>

<phylogenetics><samtools>
How to remove the unpaired reads in sam/bam files?
# How to remove the unpaired reads in sam/bam files?

(Asked by: username_0 on 2022-06-19)

I have sam and bam files for the chimeric reads, which come from two different parts of the genome (For example, the first half of the read from part of Chromosome 1 and the second half of the read from part of Chromosome 3). I have removed the low mapping quality reads (MAPQ <= 30) and reads with high mismatches. This might result in some unpaired reads which need to be removed before further analysis. May I ask whether there is any way to remove the unpaired reads in sam/bam files? Thanks!

**Tags:** phylogenetics, samtools

---

## 1 answers

### Answer by username_1 on 2022-06-20. Score: 1

The -f option of samtools view is for flags and can be used to filter reads in bam/sam file matching certain criteria such as properly paired reads (0x2) :
samtools view -f 0x2 -b in.bam > out.bam

> **Comments:**
> **username_0**: Thanks. If I remove the low mapping quality reads and reads with high mismatches after the alignment, will the flags be updated? If they are not updated, there will possibly be unpaired reads even if I use flags for filtering in my point of view.  
> **username_1**: That is a good question and there is a good change those flags won't be updated, it would need some testing. Here is a post on [seqanswers](https://www.seqanswers.com/forum/bioinformatics/bioinformatics-aa/20631-remove-reads-from-bam-whose-mate-has-already-been-filtered?t=24472) that might help you.  

---

