Question: Replace fields CHROM and POS in a vcf file
0
gravatar for hosseinv
4.4 years ago by
hosseinv20
Australia
hosseinv20 wrote:

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 perl vcf • 2.3k views
ADD COMMENTlink modified 4.4 years ago by dariober9.4k • written 4.4 years ago by hosseinv20
2
gravatar for dariober
4.4 years ago by
dariober9.4k
Glasgow - UK
dariober9.4k wrote:

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

ADD COMMENTlink written 4.4 years ago by dariober9.4k

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

ADD REPLYlink written 4.4 years ago by hosseinv20
Please log in to add an answer.

Help
Access

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