This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Replace fields CHROM and POS in a vcf file

Hi,

I want to replace the values of the two fields (CHROM and POS) of my vcf file with their corresponding values in a txt file containing only these two columns.

An example of my vcf file (simplified):

##fileformat=VCFv4.1
##filedate=20140505
##INFO=<ID=DP,...
##...
#CHROM    POS    ID    REF    ALT    QUAL    FILTER    INFO    FORMAT    NH0001
CHROM-A-g11-0015-147754-150523    207    -    -    -    -    -    -    -    0/0
CHROM-A-g11-0015-147754-150523    241    -    -    -    -    -    -    -    0/0
CHROM-B-g11-0017-150523-151267    53    -    -    -    -    -    -    -    0/1

And my txt file:

#CHROM    POS
A    147961
A    147995
B    150576

Can anyone tell me how to do the replacing?

Thank you!

Hossein

unix vcf perl

1 answer

What about this?

## Remove header from txt file
tail -n+2 sub.txt > newpos.txt

## Get header from vcf
grep -P '^#' test.vcf > new.vcf

grep -v -P '^#' test.vcf \
| cut -f3- \
| paste newpos.txt - >> new.vcf

cat new.vcf
##fileformat=VCFv4.1
##filedate=20140505
##INFO=<ID=DP,...
##...
#CHROM    POS    ID    REF    ALT    QUAL    FILTER    INFO    FORMAT    NH0001
A    147961    -    -    -    -    -    -    -    0/0
A    147995    -    -    -    -    -    -    -    0/0
B    150576    -    -    -    -    -    -    -    0/1

Thank you Dariober.

This is working good enough. I am wondering what if I want to change only one field, say ID field where I have its correct values in a text file of one column?

Thanks again

Hossein

Log in to answer this question.