Mutiple gziped files inputs and multiple if else to awk

Viewed 48

I have file1.gz That looks like

NC_044998.1     3756    0       AA
NC_044998.1     3757    0       AA
NC_044998.1     3758    0       AT
NC_044998.1     3759    0       GG
NC_044998.1     3760    0       GG
NC_044998.1     3761    0       GC
NC_044998.1     3763    0       TT
NC_044998.1     3764    0       GG

I need to annotate file1.gz according with it's col4. If col4 is 2x the same letter (as in GG) print col5 as "homozygous" else print "heterozygous". Like this:

NC_044998.1     3756    0       A    A       homozygous
NC_044998.1     3757    0       A    A       homozygous
NC_044998.1     3758    0       A    T       heterozygous
NC_044998.1     3759    0       G    G       homozygous
NC_044998.1     3760    0       G    G       homozygous
NC_044998.1     3761    0       G    C       heterozygous
NC_044998.1     3763    0       T    T       homozygous
NC_044998.1     3764    0       G    G       homozygous

Solved this by doing (except that splits col4 into 2).

gunzip -c file1.gz | awk -v OFS="\t" 'sub(/./,"&\t",$4)1;' | awk -F $'\t' 'BEGIN {OFS = FS} {if ($4==$5) print $0,"homozygous"; else print $0,"heterozygous";}' > file1.tmp

Then, I need to compare this output to file2.gz bellow

chromo  position        major   minor   ref     anc     knownEM nInd
NC_044998.1     3756    A       C       A       A       0.000000        1
NC_044998.1     3757    A       C       T       T       0.000000        1
NC_044998.1     3758    A       C       A       A       0.000000        1
NC_044998.1     3759    G       A       G       G       0.000000        1
NC_044998.1     3760    G       A       G       G       0.000000        1
NC_044998.1     3761    G       A       G       G       0.000000        1
NC_044998.1     3762    A       C       A       A       0.000000        1
NC_044998.1     3764    G       A       C       C       0.000000        1

Adding an annotation so that if col1, col2, col4 file1.gz == col1, col2, col5 file2.gz I add "ref" to my annotation and col1, col2, col5 file1.gz == col1, col2, col5 file2.gz add "ref". Otherwise, add "dev" in both cases.

so the final output would be:

NC_044998.1     3756    0       A    A       homozygous    ref    ref
NC_044998.1     3757    0       A    A       homozygous    dev    dev
NC_044998.1     3758    0       A    T       heterozygous  ref    dev
NC_044998.1     3759    0       G    G       homozygous    ref    ref
NC_044998.1     3760    0       G    G       homozygous    ref    ref
NC_044998.1     3761    0       G    C       heterozygous  ref    dev
NC_044998.1     3763    0       T    T       homozygous 
NC_044998.1     3764    0       G    G       homozygous    dev    dev

I know how to do that separately

gunzip -c  file2.gz | awk -F $'\t' 'BEGIN {OFS = FS} NR==FNR{c[$1,$2,$5];next} {if (($1,$2,$4) in c ) print $0,"ref"; else print $0,"dev";}' - file1.tmp > file2.tmp

gunzip -c  file2.gz |awk -F $'\t' 'BEGIN {OFS = FS} NR==FNR{c[$1,$2,$5];next} {if (($1,$2,$5) in c ) print $0,"ref"; else print $0,"dev";}' - file2.tmp > output

I wonder how can I do these three steps in one script, possibly w/o the need to generated temp files.

1 Answers

Here is an awk to combine last 2 steps into one:

awk 'BEGIN {
   FS=OFS="\t"
}
FNR==NR {
   a[$1,$2,$5]
   next
}
FNR==1 {
   next
}
{
   print $0, (($1,$2,$4) in a ? "ref":"dev"), (($1,$2,$5) in a ? "ref":"dev")
} file2 file1'

btw your first awk script can also be done in one step as this:

awk 'BEGIN {
   FS=OFS="\t"
}
length($NF)==2 {
   p=substr($NF,1,1)
   s=substr($NF,2,1)
   $NF = p OFS s OFS (p==s ? "homozygous" : "heterozygous")
} 1' file
Related