Replace fields CHROM and POS in a vcf file
1
0
Entering edit mode
8.2 years ago
hosseinv ▴ 20

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 • 4.3k views
ADD COMMENT
4
Entering edit mode
8.2 years ago

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 COMMENT
0
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 1101 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6