Merge two files with multiple columns by 4 common columns
1
0
Entering edit mode
2.7 years ago
User000 ▴ 710

Dear all,

I have two files. File1.tsv:

CHROM   POS REF ALT Allele  Consequence IMPACT  SYMBOL  Gene
chrM    3338    G   A   A   missense_variant    MODERATE    MT-ND1  ENSG00000198888     
chrM    3395    T   C   C   missense_variant    MODERATE    MT-ND1  ENSG00000198888 

Files2.tsv:

CHROM   POS REF ALT FILTER  nHet    nHomAlt nHomRef
chrM    3338    G   A   PASS    0   1   499
chrM    3395    T   C   PASS    1   2   497 

My desired output is

 chrM   3338    G   A   A   missense_variant    MODERATE    MT-ND1  ENSG00000198888    chrM 3338    G   A   PASS    0   1   499     
 chrM   3395    T   C   C   missense_variant    MODERATE    MT-ND1  ENSG00000198888    chrM 3395    T   C   PASS    1   2   497

I tired to do a simple paste, but when I do paste file1.txt files2.txt the columns of the second file start from the new line, when i do paste file2.txt file1.txt it works. I do not know the reason. Also I would like to take into considerazion the first 4 columns (CHROM, POS,REF,ALT), which should match.

vcf • 1.2k views
ADD COMMENT
0
Entering edit mode

I do not know the reason

check your files are NOT windows files with CR/LF returns with file your.vcf

ADD REPLY
0
Entering edit mode
ASCII text, with CRLF line terminators

How to fix this?

ADD REPLY
2
Entering edit mode

Try dos2unix tool. It's present in most of distro repos.

$ tsv-join -H -f file2.txt -k 1,2,3,4  file1.txt -a CHROM,POS,REF,ALT,FILTER,nHet,nHomAlt,nHomRef

CHROM   POS REF ALT Allele  Consequence IMPACT  SYMBOL  Gene    CHROM   POS REF ALT FILTER  nHet    nHomAlt nHomRef
chrM    3338    G   A   A   missense_variant    MODERATE    MT-ND1  ENSG00000198888 chrM    3338    G   A   PASS    01499
chrM    3395    T   C   C   missense_variant    MODERATE    MT-ND1  ENSG00000198888 chrM    3395    T   C   PASS    12497

$ csvtk join --outer-join -tHf 1,2,3,4 file1.txt file2.txt                           

CHROM   POS REF ALT Allele  Consequence IMPACT  SYMBOL  Gene    FILTER  nHet    nHomAlt nHomRef
chrM    3338    G   A   A   missense_variant    MODERATE    MT-ND1  ENSG00000198888 PASS    0   1   499
chrM    3395    T   C   C   missense_variant    MODERATE    MT-ND1  ENSG00000198888 PASS    1   2   497

Note: tsv-join output looks like joined numbers at the end, but they are not. The numbers are tab separated.

ADD REPLY
1
Entering edit mode

tr -d '\r' < bad.vcf > fixed.vcf

ADD REPLY
0
Entering edit mode

is it possible to integrate it directly in the command line without creating a new file? This does not work..

vep-annotation-reporter {input.vcf} Allele Consequence IMPACT SYMBOL Gene -o - | tr -d '\r' > {output.tsv}
ADD REPLY
0
Entering edit mode

it's ok. there is no problem here.

ADD REPLY
0
Entering edit mode
2.7 years ago

create a composite key with awk , sort and use join. Something like (not tested)

join -t $'\t' -1 1 -2 1 \
   <(awk '{printf("%s|%s|%s|%s\t%s\n",$1,$2,$3,$4,$0);}' in1.vcf | sort -t $'\t' -k1,1) \
   <(awk '{printf("%s|%s|%s|%s\t%s\n",$1,$2,$3,$4,$0);}' in2.vcf | sort -t $'\t' -k1,1) 
ADD COMMENT
0
Entering edit mode

but fix your CRLF files before....

ADD REPLY

Login before adding your answer.

Traffic: 1781 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