10
$\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$
  • $\begingroup$ bwa mem genome read1 read2 > output.bam $\endgroup$ – Vijay Dec 7 '17 at 21:54
22
$\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 -
| improve this answer | |
$\endgroup$
  • $\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$ – Michael Hall May 12 '17 at 23:48
10
$\begingroup$

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.

| improve this answer | |
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.