This is a test version of Biostars. For the public version, visit https://www.biostars.org.
modify a file by deleting what is not contained in a second file

Hello, I have an awk file that allows me to modify a file according to the content of another file.

file to be modified (vcf.vcf) :

1       879069  rs375430553     C       G       .       PASS    RefPanelAF=0.000123191;AN=80;AC=0;INFO=1        GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879142  rs148506094     C       G       .       PASS    RefPanelAF=9.2393e-05;AN=80;AC=0;INFO=1 GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879317  rs7523549       C       T       .       PASS    TYPED;RefPanelAF=0.0342932;AN=80;AC=7;INFO=1    GT      0|0     0|0     0|0     0|0     0|1     0|0     0|0     0|0     1|1     0|0     0|1     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     1|0     0|0     0|0     0|0     0|0     1|1     0|0
1       879326  rs111371881     C       T       .       PASS    RefPanelAF=9.2393e-05;AN=80;AC=0;INFO=1 GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879341  rs139574491     G       A       .       PASS    RefPanelAF=0.00109332;AN=80;AC=0;INFO=1 GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879371  rs149846233     G       A       .       PASS    RefPanelAF=7.69941e-05;AN=80;AC=0;INFO=1        GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879409  rs200048444     C       G       .       PASS    RefPanelAF=0.00103172;AN=80;AC=0;INFO=1 GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0

file that allows you to modify (rsID):

rs148506094;"ENSG00000187634";"ENST00000342066"|"ENST00000341065"|"ENST00000455979"|"ENST00000618181"|"ENST00000622503"|"ENST00000618779"|"ENST00000616125"|"ENST00000617307"
rs200048444;"ENSG00000187634";"ENST00000342066"|"ENST00000341065"|"ENST00000455979"|"ENST00000618181"|"ENST00000622503"|"ENST00000618779"|"ENST00000616125"|"ENST00000617307"

result :

1       879069  rs375430553     C       G       .       PASS    RefPanelAF=0.000123191;AN=80;AC=0;INFO=1        GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879142  rs148506094     C       G       .       PASS    "ENSG00000187634";"ENST00000342066"|"ENST00000341065"|"ENST00000455979"|"ENST00000618181"|"ENST00000622503"|"ENST00000618779"|"ENST00000616125"|"ENST00000617307"       GT  0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879317  rs7523549       C       T       .       PASS    TYPED;RefPanelAF=0.0342932;AN=80;AC=7;INFO=1    GT      0|0     0|0     0|0     0|0     0|1     0|0     0|0     0|0     1|1     0|0     0|1     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     1|0     0|0     0|0     0|0     0|0     1|1     0|0
1       879326  rs111371881     C       T       .       PASS    RefPanelAF=9.2393e-05;AN=80;AC=0;INFO=1 GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879341  rs139574491     G       A       .       PASS    RefPanelAF=0.00109332;AN=80;AC=0;INFO=1 GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879371  rs149846233     G       A       .       PASS    RefPanelAF=7.69941e-05;AN=80;AC=0;INFO=1        GT      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879409  rs200048444     C       G       .       PASS    "ENSG00000187634";"ENST00000342066"|"ENST00000341065"|"ENST00000455979"|"ENST00000618181"|"ENST00000622503"|"ENST00000618779"|"ENST00000616125"|"ENST00000617307"       GT  0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0

code used (which was given to me on a forum):

BEGIN   { FS=";" }
NR==FNR { val[$1] = $2 FS $3; next }
FNR==1  { FS=OFS="\t"; $0=$0 }
!/^#/   { $8 = ($3 in val ? val[$3] : $8) }
{ print }

However, at the same time I would like to delete the lines that are in vcf.vcf but not in rsID.txt.

what is desired:

1       879142  rs148506094     C       G       .       PASS    "ENSG00000187634";"ENST00000342066"|"ENST00000341065"|"ENST00000455979"|"ENST00000618181"|"ENST00000622503"|"ENST00000618779"|"ENST00000616125"|"ENST00000617307"       GT  0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0
1       879409  rs200048444     C       G       .       PASS    "ENSG00000187634";"ENST00000342066"|"ENST00000341065"|"ENST00000455979"|"ENST00000618181"|"ENST00000622503"|"ENST00000618779"|"ENST00000616125"|"ENST00000617307"       GT  0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0 0|0      0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0     0|0

How to do this?

Thank you. Thank you.

awk

1 answer

Hi amandinelecerfdefer,

You can change your awk code to:

BEGIN   { FS=";" }
NR==FNR { val[$1] = $2 FS $3; next }
FNR==1  { FS=OFS="\t"; $0=$0 }
!/^#/   { $8 = ($3 in val ? val[$3] : "") }
$8!=""  { print }

Thank you for your answer, it works. You and this site are a great help thanks! I really need to deepen my knowledge of this magical language.

Log in to answer this question.