you could use samtofastq from Picard to convert your bam to fastq prior BWA mem alignment.
Hi,
I'm trying to use bwa mem for the first time and I wonder, is it possible to run it on a bam file instead of a fastq file? I noticed that bwa aln can be runned on bam file using '-b' but this option is invalid for bwa mem. The manual does not mention whether it is possible or not. I tried to simply run bwa mem on bam file and the output seems to be a header only sam file.
Does anyone has experience on the question?
Thanks in advance.
1 answer
Ok, I should have wait I guess. Finally I found the answer which is : no. This was detailed in the mail archive announcing the release of bwa mem. I put it as an answer in case someone else ask himself the same question.
you could try piping samtools fastq (former samtools bam2fq) to bwa and save both disk and time:
samtools fastq reads.bam | bwa mem ref.fa -
note: this works only for non-paired-end reads.
Nice solution, but are you sure the -p parameter works well? Does bwa correctly pairs the paired end if they come from the stdin ? Have you tested it?
from bwa manual page:
If -p is used, the command assumes the 2i-th and the (2i+1)-th read in reads.fq constitute a read pair (such input file is said to be interleaved)
but unfortunately samtools fastq does not output interleaved reads.
you can either try sorting reads by name BEFORE fastq generation:
samtools sort -n reads.bam | samtools fastq - | bwa mem -p ref.fa -
or you can either try sorting reads by name AFTER fastq generation (which is slightly faster):
samtools fastq reads.bam | paste - - - - | sort -k1,1 -S 3G | tr '\t' '\n' | bwa mem -p ref.fa -
I found this suggested solution from samtools' documentation and modified it according to our context:
samtools collate -u -O reads.bam | samtools fastq -0 /dev/null -s /dev/null - | bwa mem -p ref.fa -
samtools collate is probably much faster than sorting by name. I haven't tested it myself though, so future comers should use it with caution. Also, this code discards the singleton / supplementary / secondary reads, which may or may not be desired.
Log in to answer this question.