How do I filter by bam file using samtools (only mapped reads, only unmapped reads, subset a bam file)?
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