This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Converting bam -> fasta

I was wondering if there is an good way of converting an aligned bam into a consensus fasta file. I was following the solution on Questions Regarding Consensus Sequence Calling With Samtools / Bcftools / Vcfutils.Pl but the discuss has me a little worried. In addition, the first step takes a long time and my runs keep failing without any errors. Any suggestions would be most appreciated.

Thanks in advance.

sequencing alignment

2 answers

To fetch content of bam file,

samtools view file.bam

pipe this command with awk to obatin fasta file.

samtools view file.bam | awk '{print ">"$1"\n"$10}' > output.fasta

$1 is the header and $10 is the sequence in bam file separated by tab delimeter.

Well here's one way:

samtools view aligned.bam | awk 'BEGIN {FS="\t"} {print ">" $1 "\n" $10}' > aligned.fa

If you wanted BAM to fastq:

samtools view aligned.bam | awk 'BEGIN {FS="\t"} {print "@" $1 "\n" $10 "\n+\n" $11}' > aligned.fq

You could also use this http://bedtools.readthedocs.org/en/latest/content/tools/bamtofastq.html

Thanks but bam -> fa generates an output for each read pair - I want to generate a combined consensus bam file. Any suggestions?

Oh right, I guess PyPerl and I missed the bit about "consensus" fasta file. Perhaps you have seen this post?

@diviya.smith Did you find the solution?

Log in to answer this question.