Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-07T23:46:01.440Z
Answers.LastActivityDate: 2021-01-07T23:46:01.440Z
Answers.UserDisplayName: Maximilian Press
Body: <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>
  
Tags: <sam><samtools><ngs><mpileup><bcftools>
Title: How to generate a consensus sequence from a multi-reference BAM file?
ThreadText: # 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](https://www.biostars.org/p/367626/) has several suggestions.
  
  THe one that seems most straightforward, starting where you are, is to use [vcftools](http://vcftools.sourceforge.net/perl_module.html#vcf-consensus):
  
  ```
  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](https://github.com/tobiasrausch/alfred) and [GATK](https://software.broadinstitute.org/gatk/documentation/tooldocs/3.8-0/org_broadinstitute_gatk_tools_walkers_fasta_FastaAlternateReferenceMaker.php).
  
  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](https://www.biostars.org/p/210219/) and [this post](https://www.biostars.org/p/106888/).
  
  
  ---
  
  