FORUMCategory: BioinformaticsFiltering bam files using samtools
ramaniak Staff asked 2 years ago



How do I filter by bam file using samtools (only mapped reads, only unmapped reads, subset a bam file)?

1 Answers
Best Answer
jflucier Staff answered 2 years ago



 
To filter bam files using samtool you need to call samtools view with the appropriate hex flag. To get output in bam format, you need to use the -b flag. To retreive all mapped read use this command
samtools view -b -F 4 file.bam > mapped.bam
to get unmapped: samtools view -b -f 4 file.bam > unmapped.bam
To know what sam flags to pass to the -f option, i found this utility quite usefull: https://broadinstitute.github.io/picard/explain-flags.html