How to extract unmatched reads using bwa and samtools?
Yes, correct but overly many steps. There is no need to convert between bam and sam. samtools can read from stdin and handles both sam and bam and samtools fastq can interpret flags, therefore one can shorten this to:
bwa mem (...options) | samtools view -o out.bam
samtools fastq -f 4 out.bam &gt; unmatched.fastq