# How to manipulate a reference FASTA or bam to include variants from a VCF?

I have some software which takes fastas as the input. I need to include SNVs and InDels from a VCF into the reference hg38 and then use this.

The problem is, I don't know of an algorithmically sound way to do this.

1. Are there any existing software packages which could do this efficiently? Is it easier to output a FASTA, or a bam (and then convert to a FASTA)?

2. What about if I wanted to do the same with a bedpe of germline structural variants?

• Could you provide an example of what you're looking for? I'm having a hard time picturing what you want.
– Greg
Aug 6, 2017 at 19:48
• @Greg The above might be unclear. I have a reference FASTA. I have variants. I would now like to manipulate the reference FASTA to include these variants. The VCF tells us there's a SNP at XX location; I would like to like a FASTA that includes these SNPs. Does this make sense? Aug 6, 2017 at 20:11
• Oh I understand now. Here's a link to a similar biostars question that has some answers that look good. biostars.org/p/6553
– Greg
Aug 6, 2017 at 20:36

You could convert VCF to BED via vcf2bed --snvs, vcf2bed --insertions, and vcf2bed --deletions, and then use samtools faidx by way of a wrapper script to convert BED to FASTA, e.g.:
$vcf2bed --snvs < variants.vcf | bed2faidxsta.pl > snvs.fa$ vcf2bed --insertions < variants.vcf | bed2faidxsta.pl > insertions.fa
$vcf2bed --deletions < variants.vcf | bed2faidxsta.pl > deletions.fa  You need FASTA files for your reference genome, which have been indexed with samtools faidx, e.g., for hg38: $ cd /foo/bar/baz
$wget ftp://hgdownload.cse.ucsc.edu/goldenPath/hg38/chromosomes/*.fa.gz$ for fn in ls *.fa.gz; do gunzip $fn; done$ for fn in ls *.fa; do samtools faidx $fn; done  Once you have indexed FASTA files somewhere on your file system, you can pipe BED to the bed2faidxsta.pl script, to get out FASTA sequences. GATK has a solution that might work for you: FastaAlternateReferenceMaker, which : "Given a variant callset, this tool replaces the reference bases at variation sites with the bases supplied in the corresponding callset records." Input The reference, requested intervals, and any number of variant ROD files. Output A FASTA file representing the requested intervals. Usage example java -jar GenomeAnalysisTK.jar \ -T FastaAlternateReferenceMaker \ -R reference.fasta \ -o output.fasta \ -L input.intervals \ -V input.vcf \  There's a vcf2fq sub-program that was written as part of vcfutils to convert a VCF file into a fastq file given a reference sequence. Unfortunately this doesn't work properly with INDELs (it will just mask them, rather than actually converting them), so I wrote a modification to implement INDEL correction as well: ./vcf2fq.pl -f <input.fasta> <all-site.vcf> > <output.fastq>  It adds and removes sequence from the reference, as specified by the INDEL information in the VCF file. The code keeps a record of where in the reference sequence the INDELs should go, then inserts (or deletes) the INDELs, updating the reference position as necessary. It should work with things like deletions right next to insertions, but hasn't been extensively tested. • Could you explain how your perl script with the INDEL implementation works? i.e. what changes did you make for vcfutils.pl to work properly Nov 8, 2017 at 17:46 • "It should work with things like deletions right next to insertions, but hasn't been extensively tested." If you do benchmark this, let me know---this would be interesting to see Nov 8, 2017 at 19:18 • Another question: have you seen vcfutils.pl written in Python/R? I suspect these exist somewhere, as I don't want to re-invent the wheel. Is this worth a separate SO question? It would be good if your perl script was more advertised IMHO Nov 9, 2017 at 16:50 • I think an answer to that fits in with this question. The question is about manipulating FASTA files, not about vcfutils. No, I haven't seen any python/R alternatives. I don't see much of a point in them (certainly not a python version), given that the command-line functionality already exists via vcf2fq. – gringer Nov 9, 2017 at 20:37 • You're welcome to have a go at advertising this further. I've brought it up a few times on SeqAnswers, github, and I think reddit; basically every time I see a question asking something like this. – gringer Nov 9, 2017 at 20:40 Here's an update how this work in 2019. (The answer is quite similar to my update in this thread): bgzip and index your vcf file. $$bgzip -c input.vcf > input.vcf.gz$$ tabix input.vcf.gz  Now use bcftools consensus: $ bcftools consensus -f genome.fa input.vcf.gz > consensus.fa