Answers.Body: <p>Looking at the <a href="http://www.htslib.org/doc/samtools-view.html" rel="nofollow noreferrer">samtools view docs</a>, I believe that you need to also set the <code>-p</code> flag if you want to retain &quot;filtered&quot; reads in the file and simply have them be unmapped:</p>
  <blockquote>
  <p>The -p, option sets the UNMAP flag on filtered alignments then writes them to the output file.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-24T03:09:48.273Z
Answers.Text: Yes, this does the trick. This flag was not recognized by the older version of samtools I was using (1.11) but after upgrading to 1.15 it works. Thank you Maximilian!
Answers.UserDisplayName: Patrick Bastedo
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-23T20:47:04.253Z
Answers.LastActivityDate: 2022-07-23T20:47:04.253Z
Answers.UserDisplayName: Maximilian Press
Body: <p>I have been using the <code>-q</code> option of <code>samtools view</code> to filter out reads whose mapping quality (MAPQ) scores are below a given threshold when mapping reads to a reference assembly with either <code>bwa mem</code> or <code>minimap2</code>. However, using <code>samtools idxstats</code> to count total mapped reads and unmapped reads indicates that these reads with lower MAPQ scores are actually being discarded altogether, since the number of unmapped reads reported (last row) is always zero when the BAM file is created with a <code>-q</code> argument, even though fewer reads are being mapped to the assembly.</p>
  <p>For example:</p>
  <pre><code>minimap2 -ax map-ont assembly.fasta reads.fastq | samtools view -b | samtools sort --reference assembly.fasta &gt; nanopore_mapped_no_q_thresh.bam
  samtools idxstats nanopore_mapped_no_q_thresh.bam
  </code></pre>
  <p>gives</p>
  <pre><code>contig_1      4751250 9897    0
  contig_2      37697   528     0
  contig_3      9131    2366    0
  contig_4      98997   701     0
  contig_5      4539    365     0
  contig_6      3933    3164    0
  *       0       0       142
  </code></pre>
  <p>while</p>
  <pre><code>minimap2 -ax map-ont assembly.fasta reads.fastq | samtools view -b -q 2 | samtools sort --reference assembly.fasta &gt; nanopore_mapped_q2.bam
  samtools idxstats nanopore_mapped_q2.bam
  </code></pre>
  <p>gives</p>
  <pre><code>contig_1      4751250 9716    0
  contig_2      37697   528     0
  contig_3      9131    1415    0
  contig_4      98997   700     0
  contig_5      4539    365     0
  contig_6      3933    3161    0
  *       0       0       0
  </code></pre>
  <p>Is there an alternative way to filter mapped reads by their MAPQ scores so that the filtered reads are still retained in the BAM file?</p>
  
Tags: <sam><samtools><genomics><read-mapping>
Title: how to retain reads with low mapping quality (MAPQ) scores when using samtools view -q
ThreadText: # how to retain reads with low mapping quality (MAPQ) scores when using samtools view -q
  
  (Asked by: username_0 on 2022-07-22)
  
  I have been using the `-q` option of `samtools view` to filter out reads whose mapping quality (MAPQ) scores are below a given threshold when mapping reads to a reference assembly with either `bwa mem` or `minimap2`. However, using `samtools idxstats` to count total mapped reads and unmapped reads indicates that these reads with lower MAPQ scores are actually being discarded altogether, since the number of unmapped reads reported (last row) is always zero when the BAM file is created with a `-q` argument, even though fewer reads are being mapped to the assembly.
  
  For example:
  
  ```
  minimap2 -ax map-ont assembly.fasta reads.fastq | samtools view -b | samtools sort --reference assembly.fasta > nanopore_mapped_no_q_thresh.bam
  samtools idxstats nanopore_mapped_no_q_thresh.bam
  
  ```
  
  gives
  
  ```
  contig_1      4751250 9897    0
  contig_2      37697   528     0
  contig_3      9131    2366    0
  contig_4      98997   701     0
  contig_5      4539    365     0
  contig_6      3933    3164    0
  *       0       0       142
  
  ```
  
  while
  
  ```
  minimap2 -ax map-ont assembly.fasta reads.fastq | samtools view -b -q 2 | samtools sort --reference assembly.fasta > nanopore_mapped_q2.bam
  samtools idxstats nanopore_mapped_q2.bam
  
  ```
  
  gives
  
  ```
  contig_1      4751250 9716    0
  contig_2      37697   528     0
  contig_3      9131    1415    0
  contig_4      98997   700     0
  contig_5      4539    365     0
  contig_6      3933    3161    0
  *       0       0       0
  
  ```
  
  Is there an alternative way to filter mapped reads by their MAPQ scores so that the filtered reads are still retained in the BAM file?
  
  **Tags:** sam, samtools, genomics, read-mapping
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-07-23. Score: 2 | Accepted answer
  
  Looking at the [samtools view docs](http://www.htslib.org/doc/samtools-view.html), I believe that you need to also set the `-p` flag if you want to retain "filtered" reads in the file and simply have them be unmapped:
  
  > The -p, option sets the UNMAP flag on filtered alignments then writes them to the output file.
  
  > **Comments:**
  > **username_0**: Yes, this does the trick. This flag was not recognized by the older version of samtools I was using (1.11) but after upgrading to 1.15 it works. Thank you username_1!  
  
  ---
  
  