Converting bam -> fasta
2
0
Entering edit mode
10.2 years ago
diviya.smith ▴ 60

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 • 7.0k views
ADD COMMENT
1
Entering edit mode
10.2 years ago
Renesh ★ 2.2k

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.

ADD COMMENT
0
Entering edit mode
10.2 years ago
Dave Tang ▴ 210

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

ADD COMMENT
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

@diviya.smith Did you find the solution?

ADD REPLY

Login before adding your answer.

Traffic: 1710 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6