15
$\begingroup$

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?

$\endgroup$
0

2 Answers 2

29
$\begingroup$

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 -
$\endgroup$
1
  • $\begingroup$ Ah, great solution if sorting is needed! I had tried this in the past but I was trying to pipe it though samtools view and then into sort. Didn't realise you could go straight into sort. $\endgroup$ Commented May 12, 2017 at 23:48
13
$\begingroup$

If you don't need sorting, you can avoid the computational overhead by piping the output from bwa mem into samtools view like so

bwa mem ref.fa in.fq | samtools view -b - > out.bam

The - in samtools view tells it to read from stdin and -b indicates BAM output.

You can also just allow samtools to infer the output format by using the -o option, which will guess the required format based on the file extension given

bwa mem ref.fa in.fq | samtools view -o out.bam - 
$\endgroup$
1
  • $\begingroup$ In my workflow, BWA output goes to MergeBamAlignment, so samtools view seemed lower overhead than samtools sort. (Directly piping from BWA to MergeBamAlignment, as suggested here, failed for me.) $\endgroup$ Commented Feb 1, 2023 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .