Skip to content

Instantly share code, notes, and snippets.

@slavailn
Created January 17, 2019 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slavailn/49236f8e31ff7e8668f649f12f61fa42 to your computer and use it in GitHub Desktop.
Save slavailn/49236f8e31ff7e8668f649f12f61fa42 to your computer and use it in GitHub Desktop.
Get BWA mapping stats (mapped, unmapped reads, number of reads with MAPQ20)
#! /bin/bash
echo -e "Samplename\tTotal_reads\tMapped_reads\tUnmapped_reads\tMAPQ20_reads\tPercent_mapped\tPercent_MAPQ20_reads"
for file in ./*bam
do
filename=`basename $file`
samplename=${filename%.bam}
total_reads=`samtools view -c $file`
mapped_reads=`samtools view -c -F 4 $file`
unmapped_reads=`samtools view -c -f 4 $file`
mapq_20=`samtools view -c -q 20 $file`
percent_mapped=`bc <<< "scale = 3; ($mapped_reads/$total_reads)*100"`
percent_mapq20=`bc <<< "scale = 3; ($mapq_20/$total_reads)*100"`
echo -e "$samplename\t$total_reads\t$mapped_reads\t$unmapped_reads\t$mapq_20\t$percent_mapped\t$percent_mapq20"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment