Question: New Fasta Sequence From Reference Fasta And Variant Calls File?
gravatar for Gaffa
10.0 years ago by
Gaffa490 wrote:

Is there some utility that will take as input a reference sequence in fasta format and a file of variant calls, with both SNPs and short indels (i.e. VCF or similar), and output a new sequence, identical to the input reference except with the new variants introduced?

It wouldn't be a super tricky thing to script, but since it seems like something that would be relatively common to want to do, I'm wondering if there isn't already some available tools for doing this. Yet I haven't found any.

For simplicity let's assume a single haploid chromosome (trickier for diploid individuals with heterozygous calls).


reference sequence:

variant calls:
2    A    C
5    TT    T
8    G    GCC
10    A    T

new sequence:
vcf fasta consensus variant • 30k views
ADD COMMENTlink modified 4.3 years ago by FatihSarigol210 • written 10.0 years ago by Gaffa490

Hi gaffa. Would you mind putting a few example sequences and variants and resulting sequences? For a given input sequence, do you want only one output sequence or many containing all possible combinations of the variants for that sequence? Cheers

ADD REPLYlink written 10.0 years ago by Eric Normandeau10k

If I want all the possible combinations, is there a tool for doing that?

ADD REPLYlink written 6.6 years ago by eva10

@Eric: I have added an example. For a given input sequence I want only a single output sequence. I.e. I have sequenced some individual, called variants in relation to a reference genome, and now I want to construct the full sequence of the individual (for simplicity assume a single haploid chromosome).

ADD REPLYlink written 10.0 years ago by Gaffa490
gravatar for Vasisht
9.8 years ago by
Vasisht170 wrote:

GATK has an utility to create an alternate reference fasta file

GATK Fasta Alternate Reference

ADD COMMENTlink modified 3.8 years ago by WouterDeCoster45k • written 9.8 years ago by Vasisht170

Link provided above is broken now; instead use

ADD REPLYlink written 9.5 years ago by David Quigley11k

New link:

ADD REPLYlink written 8.5 years ago by Moss20

And again a link fix:

ADD REPLYlink written 8.0 years ago by Konrad690

And again a new one

ADD REPLYlink written 6.4 years ago by Amy20

And again:

ADD REPLYlink written 5.1 years ago by lexnederbragt1.2k

And now I fixed the link in the original answer :)

ADD REPLYlink written 3.8 years ago by WouterDeCoster45k

Thanks, that's exactly what I was looking for!

ADD REPLYlink written 9.8 years ago by Gaffa490

just saying one should be careful about this tool as it may shift frames in positions where there should be N's

ADD REPLYlink written 3.7 years ago by FatihSarigol210
gravatar for Jorge Amigo
7.8 years ago by
Jorge Amigo12k
Santiago de Compostela, Spain
Jorge Amigo12k wrote:

although this is an old post, I can think of many users landing here seeking for advice, so allow me to add that vcf-tools has this precisely capability embeded in its vcf-consensus

ADD COMMENTlink written 7.8 years ago by Jorge Amigo12k

I am trying to the same but getting an error. Can you help me out stepwise ? I am not good at this stuff. I have a reference fasta and the vcf file. Now need to generate the sequence using it

ADD REPLYlink written 4.9 years ago by mohammedabeddin110

there are now many other tools to generate consensus from vcf file, but if you want to do it with vcf-consensus, this is the way to do it (from the vcftools manual):

cat ref.fa | vcf-consensus file.vcf.gz > out.fa
ADD REPLYlink written 4.9 years ago by Jorge Amigo12k

This error:

cat: 123.fa: Is a directory
Broken VCF header, no column names?
 at /usr/share/perl5/ line 177, <__ANONIO__> line 1.
    Vcf::throw('Vcf4_1=HASH(0x12b2a48)', 'Broken VCF header, no column names?') called at /usr/share/perl5/ line 869
    VcfReader::_read_column_names('Vcf4_1=HASH(0x12b2a48)') called at /usr/share/perl5/ line 604
    VcfReader::parse_header('Vcf4_1=HASH(0x12b2a48)') called at /usr/bin/vcf-consensus line 54
    main::do_consensus('HASH(0xdd5cb8)') called at /usr/bin/vcf-consensus line 9
ADD REPLYlink modified 17 months ago by Ram32k • written 4.9 years ago by mohammedabeddin110

it seems like you may have extracted the reference into a folder, and that the vcf file you're using is wrongly formatted too. but since you've already been through this answer, I would stick to it since the samtools + bcftools option is much faster and efficient than cat + vcf-tools, plus it allows you to deal with compressed files directly.

ADD REPLYlink modified 4.9 years ago • written 4.9 years ago by Jorge Amigo12k
gravatar for Francisco Roque
10.0 years ago by
Bergen, Norway
Francisco Roque110 wrote:

This will not apply directly to the vcf file, but using extra annotation from bcftools (assuming you are using it for snp and indel calling):

samtools mpileup -uf reference.fasta alignment.bam | \
bcftools view -cg - | vcf2fq &gt; consensus.fastq

After that just convert the fastq into a consensus fasta using seqtk, converting all bases with quality lower than 20 to lowercase

seqtk fq2fa consensus.fastq 20 > consensus.fasta

Note: This answer was adapted from How To Generate A Consensus Fasta Sequence From Sam Tools Pileup?

ADD COMMENTlink modified 17 months ago by Ram32k • written 10.0 years ago by Francisco Roque110
gravatar for Pierre Lindenbaum
10.0 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum134k wrote:

Just for fun, using XSLT. The following stylesheet inserts/deletes a sequence from a TSeq_sequence record.

<xsl:stylesheet xmlns:xsl="&lt;a href="" "="" rel="nofollow">'

<xsl:output method="xml" indent="yes"/>
<xsl:param name="index" select="0"/>
<xsl:param name="length" select="0"/>
<xsl:param name="insert"></xsl:param>
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

<xsl:template match="/">
<xsl:apply-templates select="*"/>

<xsl:template match="*"> 
<xsl:apply-templates select="*|@*|text()"/>

<xsl:template match="TSeq_sequence">
        <xsl:when test="$index &gt; 0">
            <xsl:variable name="seq" select="translate(normalize-space(text()), $uppercase, $smallcase)"/>
            <xsl:value-of select="substring($seq,1,$index - 1)"/>
            <xsl:value-of select="translate($insert, $smallcase, $uppercase)"/>
            <xsl:value-of select="substring($seq,$index + $length)"/>
            <xsl:value-of select="."/>



xsltproc --param index '2'  \
    --param length 1 \
    --stringparam insert "HelloWorld" \
    --novalid jeter.xsl  \


ADD COMMENTlink modified 17 months ago by Ram32k • written 10.0 years ago by Pierre Lindenbaum134k
gravatar for castelli
4.8 years ago by
castelli0 wrote:

You may use vcfx. It creates a fasta file (two sequences per sample) using a reference sequence and replacing each variable site on the right location. It supports indels. Take a look here:

ADD COMMENTlink written 4.8 years ago by castelli0

Again: C: VCF to FASTA

ADD REPLYlink written 4.8 years ago by Matt Shirley9.5k
gravatar for FatihSarigol
4.3 years ago by
FatihSarigol210 wrote:

I found a way eventually and wanted to come back to this post to share, and noticed that Francisco Roque's answer was almost the same, but in case that doesn't work, I have just a little bit different version:

samtools mpileup -d8000 -q 20 -Q 10 -uf  REFERENCE.fasta Your_File.bam | bcftools call -c | vcf2fq  > OUTPUT.fastq

-d, --max-depth == -q, -min-MQ Minimum mapping quality for an alignment to be used == -Q, --min-BQ Minimum base quality for a base to be considered == (You can use different values of course, see

Which generated for me a weird format that looks like fastq but isn't, so you can't convert it using a converter, but you can use the following sed command, which I wrote specific for this output:

sed -i -e '/^+/,/^\@/{/^+/!{/^\@/!d}}; /^+/ d; s/@/>/g' OUTPUT.fastq

In the end, make sure to compare your new fasta files to your reference to be sure that everything is fine.


ADD COMMENTlink modified 3.0 years ago • written 4.3 years ago by FatihSarigol210
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1939 users visited in the last hour