I would be highly appreciated to have a script able to perform the following operation:
Only extract scaffold_1 lines from file1.vcf in which POS number (column 2) falls within column 2 and column 3 numbers of scaffold_1 lines from listfile.txt (inclusive). Similarly, only extract scaffold_2 lines from file1.vcf in which POS number falls within column 2 and column 3 numbers of scaffold_2 lines from listfile.txt. In reality the vcf I have continues until scaffold_12 and has 17,000 lines.
file1.vcf
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT COUNTS
scaffold_1 160 . C A 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_1 210 . AC A 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_1 340 . A C 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_1 470 . T C 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_1 580 . A AC 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_1 630 . CA C 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 40 . C A 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 100 . AC A 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 370 . A C 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 600 . T C 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 800 . A AC 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 850 . C CTA 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 1120 . CA C 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
listfile.txt
scaffold_1 50 100
scaffold_1 200 350
scaffold_2 350 450
scaffold_2 750 850
The desired output file is the following:
desired_file.vcf
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT COUNTS
scaffold_1 210 . AC A 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_1 340 . A C 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 370 . A C 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 800 . A AC 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
scaffold_2 850 . C CTA 40 PASS . GT:DP:AD:RO:QR:AO:QA 0:1:1,2:1:40:2:40
To do this job I found the below awk code from another similar post but they don't differentiate scaffold_1 from scaffold_2, and I don't how to add this extra variable properly. Thank you.
awk 'NR==FNR{rng++;start[rng]=$2;end[rng]=$3;next} {for (i=1;i<=rng;i++) if (($2>=start[i])&&($2<=end[i])) {print; next}}' listfile.txt file1.vcf > output.vcf
<listfile.txt awk '{ print "$2 >= " $2 " && $2 <= " $3 }' | awk -f - file1.vcf > output.vcf
I tried to modify the last awk code like this, but it didn't work:
<listfile.txt awk '{ print "$1 == " $1 " && $2 >= " $2 " && $2 <= " $3 }' | awk -f - file1.vcf > output.vcf