<p><a href="https://www.biostars.org/p/367626/" rel="nofollow noreferrer">This post on biostars</a> has several suggestions.</p>
<p>THe one that seems most straightforward, starting where you are, is to use <a href="http://vcftools.sourceforge.net/perl_module.html#vcf-consensus" rel="nofollow noreferrer">vcftools</a>:</p>
<pre><code>cat ref.fa | vcf-consensus file.vcf.gz &gt; out.fa
</code></pre>
<p>Based on that I think that you should also be able to solve this problem using <a href="https://github.com/tobiasrausch/alfred" rel="nofollow noreferrer">alfred</a> and <a href="https://software.broadinstitute.org/gatk/documentation/tooldocs/3.8-0/org_broadinstitute_gatk_tools_walkers_fasta_FastaAlternateReferenceMaker.php" rel="nofollow noreferrer">GATK</a>.</p>
<p>If all of these suffer the same problem of needing a single sequence (which I very much doubt that they do), you could split the bam / ref files into multiple files, run whatever consensus workflow, and then combine them again. But that doesn't seem like it should be necessary.</p>
<p>See also <a href="https://www.biostars.org/p/210219/" rel="nofollow noreferrer">this post</a> and <a href="https://www.biostars.org/p/106888/" rel="nofollow noreferrer">this post</a>.</p>

CC BY-SA 4.0
2021-01-07T23:46:01.440Z
2021-01-07T23:46:01.440Z
Maximilian Press
<p>I am trying to generate a consensus sequence from a BAM file that was generated by mapping reads to a reference FASTA containing multiple sequences.</p>
<p>Usually, I generate consensus sequences from BAM files using samtools and bcftools:</p>
<pre><code>samtools mpileup -vf reference.fasta filename.sorted.bam | bcftools call -m -O z - &gt; filename.vcf.gz

bcftools index filename.vcf.gz

bcftools consensus -f reference.fasta filename.vcf.gz &gt; filename.consensus.fasta
</code></pre>
<p>However, this doesn't work when the reference FASTA contains more than 1 sequence.</p>
<p>Is there a way to generate a consensus sequence from a multi-reference BAM file?</p>
<p>It would also be awesome if the method filled in gaps in coverage with Ns or Xs in the generated consensus sequence but this isn't essential.</p>
<p>Thank you for your help!</p>

<sam><samtools><ngs><mpileup><bcftools>
How to generate a consensus sequence from a multi-reference BAM file?
# How to generate a consensus sequence from a multi-reference BAM file?

(Asked by: username_0 on 2021-01-07)

I am trying to generate a consensus sequence from a BAM file that was generated by mapping reads to a reference FASTA containing multiple sequences.
Usually, I generate consensus sequences from BAM files using samtools and bcftools:
samtools mpileup -vf reference.fasta filename.sorted.bam | bcftools call -m -O z - > filename.vcf.gz

bcftools index filename.vcf.gz

bcftools consensus -f reference.fasta filename.vcf.gz > filename.consensus.fasta

However, this doesn't work when the reference FASTA contains more than 1 sequence.
Is there a way to generate a consensus sequence from a multi-reference BAM file?
It would also be awesome if the method filled in gaps in coverage with Ns or Xs in the generated consensus sequence but this isn't essential.
Thank you for your help!

**Tags:** sam, samtools, ngs, mpileup, bcftools

---

## 1 answers

### Answer by username_1 on 2021-01-07. Score: 0 | Accepted answer

This post on biostars has several suggestions.
THe one that seems most straightforward, starting where you are, is to use vcftools:
cat ref.fa | vcf-consensus file.vcf.gz > out.fa

Based on that I think that you should also be able to solve this problem using alfred and GATK.
If all of these suffer the same problem of needing a single sequence (which I very much doubt that they do), you could split the bam / ref files into multiple files, run whatever consensus workflow, and then combine them again. But that doesn't seem like it should be necessary.
See also this post and this post.


---

