Calculate e raise to the power in awk | UNIX

Input data:

I'd like to calculate using value in second column. The value needs to be created using a formula, that employs exp (that is e raise to the power).

awk '{

if(FNR==1){ 
##if first line than print as is and add third column
print $0,"weight"
}

else{
if($2<=0.01){
##if MAF less than 0.01 

print $0, exp((1-$2)*25)/(1+exp((1-$2)*25))
}
else{
print $0, exp((0.5-$2)*0.5)/(1+exp((0.5-$2)*0.5))
} } }'   merged_allCHRs_headers  > merged_allCHRs_weights

I get output

I'd like ascertain what I'm doing is right.

Thanks

I get the same results as you do applying your script to your sample data. But, not knowing WHAT formula to apply under which conditions, I can't tell "what (you're) ... doing is right." And, I'm afraid, nobody without a profound background in genetics can.

Aside, your code might benefit (improved readability and understandability, ease of maintenance) from some structuring and, mayhap, simplification. Like e.g.

awk '
FNR == 1        {print $0,"weight"
                 next
                }

                {TMP =  exp (($2<=0.01) ? (1-$2)*25 : (0.5-$2)*0.5 )
                 print $0, TMP / (1 + TMP)
                }
'  file
SNP MAF weight
10:60523:T:G 0.013 0.560576
10:62010:C:T 0.01125 0.560792
10:69083:C:T 0.3581 0.51773
10:73537:G:A 0.005268 1
1 Like

Assuming your formula is correct, checked longhand in Python 3.8.0 using the math library:

Last login: Fri Nov 15 17:16:16 on ttys000
AMIGA:amiga~> python3.8
Python 3.8.0rc1 (v3.8.0rc1:34214de6ab, Oct  1 2019, 12:56:49) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> 
>>> math.exp((0.5-0.013)*0.5)/(1.0+math.exp((0.5-0.013)*0.5))
0.5605759881403045
>>> math.exp((0.5-0.01125)*0.5)/(1.0+math.exp((0.5-0.01125)*0.5))
0.5607915159336736
>>> math.exp((0.5-0.3581)*0.5)/(1.0+math.exp((0.5-0.3581)*0.5))
0.5177300630065548
>>> math.exp((1.0-0.005268)*25)/(1.0+math.exp((1.0-0.005268)*25))
0.9999999999841571
>>> _

So your rounded values seem OK.
Again this assumes that your formula is correct.

1 Like