Samtools can be used to select reads above certain mapping quality. 

    samtools view -h -b -q 30 aligned.bam -o above.mapQ30.bam

**But, how to select a read below certain mapping quality - all aligned reads below mapQ 30?**

I know it can be done using awk. But, the pipeline gets lengthy and time consuming when first need to convert bam to sam - separate header - use awk for mapQ below 30 - add header - sam file - convert to bam.
Really, its taking a lots of time.

Thanks,


I haven't found any straight forward method, but if someone has similar problem in the future; **below is my proposed solution:**

**Note: Check and make sure the mapped alignment is sorted.**

`samtools view -h mapped.bam -o mapped.sam`

` grep '^@' mapped.sam > RG.header`  # this is important when there is header info in the sam/bam file

` grep -v '^@' mapped.sam > SAM_file.sam ` # retains the non header part of the mapped file

`awk '($5 < 40)' SAM_file.sam > Below.mapQ40.sam`

`cat RG.header Below.mapQ40.sam > Below.mapQ40_mapped.sam`

`samtools view -h -S -b Below.mapQ40_mapped.sam -o Below.mapQ40_mapped.bam`

`samtools index Below.mapQ40_mapped.bam`


**If you don't care about header info (or if its absent):**

`samtools view -h mapped.bam -o mapped.sam`

`awk '($5 < 40)' mapped.sam > Below.mapQ40_mapped.sam`

`samtools view -h -S -b Below.mapQ40_mapped.sam -o Below.mapQ40_mapped.bam`

`samtools index Below.mapQ40_mapped.bam`





