Just a minor tweak: you'll need the -S flag for samtools to indicate that it is accepting SAM (not BAM) as input.
Short Read Aligner - Output To Bam/Sam Only Aligned Reads
I want to align PE Illumina reads (FastQ) onto very short reference (mitochondrial). I'm not interested in reads that didn't align...
Do you know any short read aligner that can output BAM file? Or how to limit SAM output to aligned sequences only?
• 4,955 views
•
link
1 answer
Please refer to this question
Any aligner will probably send alignments to stdout, so you can do something like
aligner input.fastq ... | samtools view -SF 0x04 -b - > only.aligned.bam
(And use -f 0x02 to require properly paired reads).
Where the -F flag removes unaligned reads. See the manual.
• 2 views
•
link
Log in to answer this question.
Often it is counterproductive to filter the output right away. You may need the information on unaligned/unpaired reads at some point. Usually it is better to first generate the full SAM file, then filter it as the answers below show it.