I want to go straight from bwa mem alignment to BAM format as I don't need the SAM file and it takes up too much space. How do I achieve this?
Asked
Active
Viewed 2.6k times
2 Answers
27
For directly outputting a sorted bam file you can use the following:
bwa mem genome.fa reads.fastq | samtools sort -o output.bam -
Optionally using multiple threads:
bwa mem -t 8 genome.fa reads.fastq | samtools sort -@8 -o output.bam -
Wouter De Coster
- 386
- 2
- 7
12
Found the solution. You just need to pipe the output from bwa mem into samtools view like so
bwa mem ref.fa in.fq | samtools view -bS - > out.bam
The - in samtools view tells it to read from stdin.
Michael Hall
- 353
- 1
- 2
- 11
-
In my workflow, BWA output goes to MergeBamAlignment, so
samtools viewseemed lower overhead thansamtools sort. (Directly piping from BWA to MergeBamAlignment, as suggested here, failed for me.) – Ulrich Stern Feb 01 '23 at 18:53
samtools viewand then intosort. Didn't realise you could go straight intosort. – Michael Hall May 12 '17 at 23:48