# awk find Sickle cell anemia from father, mother, son, daughter

I have a file where i what to check for a pattern below and print out all the lines that matches the pattern but the output is empty.

here is the pattern that i am looking for.

• Father = 1/0 or 0/1 or 1|0 or 0|1
• Mother = 1/0 or 0/1 or 1|0 or 0|1
• Daughter 1 = 1|0 or 0|1 or 1/0 or 0/1
• Daughter 2 = 1|1 or 1/1
• Daughter 3 = 0|0 or 0/0
• Son 1 = 1|0 or 0|1 or 1/0 or 0/1
• Son 2 = 1|1 or 1/1

mysample file

#CHROM POS ID REF ALT FATHER MOTHER DAUGHTER1 DAUGHTER2 DAUGHTER3 SON1 SON2
1 736689 . T A 0/0:37:1.224:2.367:0.054:0.0:108:0.00:4.75:0.53:0.00:T,37,2.367:37,0 1/0:33:1.099:3.320:0.091:358.5:359:0.59:4.34:0.59:0.08:A,15,1.149,T,18,2.171:18,15 0|0:30:0.999:1.314:0.033:0.0:87:0.00:1.16:0.00:0.00:T,30,1.314:30,0 0|1:31:1.032:2.299:0.065:211.5:212:2.61:0.48:0.07:0.43:A,12,0.124,C,1,0.010,T,18,2.165:18,12 0|1:37:1.232:2.374:0.054:263.2:263:1.47:8.69:0.06:0.05:A,16,1.160,T,21,1.214:21,16 0|0:43:1.423:2.447:0.047:0.0:98:0.20:0.83:0.45:0.00:A,1,0.010,T,42,2.437:42,1 0|1:40:1.323:3.393:0.075:280.5:281:1.95:2.36:3.47:0.27:A,17,1.175,T,23,2.219:23,17
1 758663 . CGCC TTAG 0/0:27:0.893:0.296:0.000:0.0:90:5.15:0.85:3.67:0.01:27,0 0/1:21:0.699:0.234:0.000:2333.1:353:0.93:2.17:2.36:0.00:9,12 0|0:35:1.166:0.370:0.000:0.0:108:3.97:3.43:4.75:0.00:34,0 0|1:33:1.099:0.375:0.000:2820.8:447:0.07:8.69:0.53:0.00:17,15 0|1:23:0.766:0.232:0.000:1860.0:382:0.85:0.17:0.09:0.00:13,10 0|0:23:0.761:0.255:0.000:0.0:75:1.51:2.59:0.78:0.00:22,0 0|1:18:0.596:0.215:0.000:1686.8:275:0.00:2.17:0.10:0.00:9,9
1 762005 . G C 1/0:36:1.191:5.321:0.139:360.9:357:0.00:1.93:0.97:0.00:C,18,2.166,G,18,3.156:18,18 0/0:24:0.799:4.207:0.167:0.0:60:0.00:0.36:3.26:0.00:G,24,4.207:24,0 1|0:40:1.332:3.384:0.075:332.2:332:0.22:13.83:0.22:0.00:C,19,1.186,G,21,2.197:21,19 1|0:31:1.032:3.290:0.097:236.2:236:1.75:8.18:0.07:0.00:C,13,0.134,G,18,3.156:18,13 1|0:32:1.066:4.290:0.125:187.0:187:5.67:1.78:9.77:0.00:C,11,0.114,G,20,3.177,T,1,1.000:20,11 1|0:43:1.423:4.405:0.093:181.6:182:10.13:1.24:0.45:0.00:C,14,2.124,G,28,2.270,T,1,0.010:28,14 0|0:34:1.125:2.332:0.059:0.0:96:0.00:4.09:0.26:0.00:G,34,2.332:34,0


I tried this, but the output is empty:

awk  '$$9=="1|1" ||$$10=="0|0" || $$12=="1|1" ||$$8=="1|0" || $$11=="1|0" ||$$6=="1/0"||$7 =="1/0" {print}' variants.vcf > test.txt  • Should it match all conditions or any conditions? If Daughter 2 = 1|1 or 1/1, should that line be printed irrespective of the other genotypes? – terdon Sep 29 '19 at 11:33 • Yes should match all – user11766958 Sep 29 '19 at 11:46 • So, print all lines where at least one of each of your bullet points is true? Then why are you using || in your awk? – terdon Sep 29 '19 at 11:48 ## 1 Answer The fields aren't only the genotype. They contain much more information. So $9 will never be exactly 1|1, what you are interested in is that the field starts with 1|1. Also, if you need all of the conditions to match (meaning that at least one of the criteria for each sample is met), you need && not ||. So try this instead:

awk '($$6~/^1\/0/ ||$$6~/^0\/1/ || $$6~/^1\\|0/ ||$$6~/^0\\|1/) &&
($$7~/^1\/0/ ||$$7~/^0\/1/ || $$7~/^1\\|0/ ||$$7~/^0\\|1/  ) &&
($$8~/^1\/0/ ||$$8~/^0\/1/ || $$8~/^1\\|0/ ||$$8~/^0\\|1/) &&
($$9~/^1\\|1/ ||$$9~/^1\/1/ ) &&
($$10~/^0\\|0/ ||$$10~/^0\/0/ ) &&
($$11~/^1\/0/ ||$$11~/^0\/1/ || $$11~/^1\\|0/ ||$$11~/^0\\|1/) &&
($$12~/^1\\|1/ ||$$12 ~/^1\/1/)' foo.vcf


Running that on your file returns:

1 736689 . T A 0/0:37:1.224:2.367:0.054:0.0:108:0.00:4.75:0.53:0.00:T,37,2.367:37,0 1/0:33:1.099:3.320:0.091:358.5:359:0.59:4.34:0.59:0.08:A,15,1.149,T,18,2.171:18,15 0|0:30:0.999:1.314:0.033:0.0:87:0.00:1.16:0.00:0.00:T,30,1.314:30,0 0|1:31:1.032:2.299:0.065:211.5:212:2.61:0.48:0.07:0.43:A,12,0.124,C,1,0.010,T,18,2.165:18,12 0|1:37:1.232:2.374:0.054:263.2:263:1.47:8.69:0.06:0.05:A,16,1.160,T,21,1.214:21,16 0|0:43:1.423:2.447:0.047:0.0:98:0.20:0.83:0.45:0.00:A,1,0.010,T,42,2.437:42,1 0|1:40:1.323:3.393:0.075:280.5:281:1.95:2.36:3.47:0.27:A,17,1.175,T,23,2.219:23,17
1 758663 . CGCC TTAG 0/0:27:0.893:0.296:0.000:0.0:90:5.15:0.85:3.67:0.01:27,0 0/1:21:0.699:0.234:0.000:2333.1:353:0.93:2.17:2.36:0.00:9,12 0|0:35:1.166:0.370:0.000:0.0:108:3.97:3.43:4.75:0.00:34,0 0|1:33:1.099:0.375:0.000:2820.8:447:0.07:8.69:0.53:0.00:17,15 0|1:23:0.766:0.232:0.000:1860.0:382:0.85:0.17:0.09:0.00:13,10 0|0:23:0.761:0.255:0.000:0.0:75:1.51:2.59:0.78:0.00:22,0 0|1:18:0.596:0.215:0.000:1686.8:275:0.00:2.17:0.10:0.00:9,9
1 762005 . G C 1/0:36:1.191:5.321:0.139:360.9:357:0.00:1.93:0.97:0.00:C,18,2.166,G,18,3.156:18,18 0/0:24:0.799:4.207:0.167:0.0:60:0.00:0.36:3.26:0.00:G,24,4.207:24,0 1|0:40:1.332:3.384:0.075:332.2:332:0.22:13.83:0.22:0.00:C,19,1.186,G,21,2.197:21,19 1|0:31:1.032:3.290:0.097:236.2:236:1.75:8.18:0.07:0.00:C,13,0.134,G,18,3.156:18,13 1|0:32:1.066:4.290:0.125:187.0:187:5.67:1.78:9.77:0.00:C,11,0.114,G,20,3.177,T,1,1.000:20,11 1|0:43:1.423:4.405:0.093:181.6:182:10.13:1.24:0.45:0.00:C,14,2.124,G,28,2.270,T,1,0.010:28,14 0|0:34:1.125:2.332:0.059:0.0:96:0.00:4.09:0.26:0.00:G,34,2.332:34,0

• Hi @terdon, is there a particular reason for escaping the | with two backslahes? – haci Oct 9 '19 at 11:17
• @haci yes, the double escape is needed inside // in awk. Something about how awk interprets regexes in its match (//) operator. An unescaped | means "OR" in regular expressions, so it needs to be escaped. The double escape is an awk thing. – terdon Oct 9 '19 at 11:29
• Could you please take a look at this question? bioinformatics.stackexchange.com/questions/10548/… The answer I proposed for that question (which basically is more or less your answer above except for single backslashes to escape |) gives the expected output, whereas the one with double backslashes matches lines/columns it should not and I cannot figure out why. – haci Oct 9 '19 at 11:53
• @haci um. I don't know. I can't figure it out either. I know awk sometimes requires the double escaping, but I never remember exactly when or why. I also know that in this case, it doesn't work without the double escaping, but you're absolutely right, in your answer it fails with the double. I think I'll probably have to post a question on Unix & Linux, I just need to make a minimal reproducible example of this. I'll let you know, if I do. – terdon Oct 9 '19 at 13:05