質問

I have a csv file that looks like this:

ID1              ID2         AVG1     AVG2         
ENSG00000207447 RNU6-2    0.101431  0.163308     
ENSG00000207427 SNORA51   0.096551  0            
ENSG00000201784 SNORD14A  0.068653  0.0320985    
ENSG00000207315 SNORA28   0         0.01936      
ENSG00000207787 MIR98     0         0        

I want to calculate the log2 ratio of AVG1 and AVG2 and print it next to the AVG columns for each line so that it looks like this:

ID1              ID2       AVG1         AVG2       log2Ratio
ENSG00000207447 RNU6-2    0.101431  0.163308     2.0632577007
ENSG00000207427 SNORA51   0.096551  0            inf
ENSG00000201784 SNORD14A  0.068653  0.0320985    7.105015172
ENSG00000207315 SNORA28   0         0.01936      0
ENSG00000207787 MIR98     0         0            inf

I've tried

$ awk 'NF> 2 { ratio =($3/$4)/log(2) ; print $0, ratio }' testlog2.csv > testlog2_2.csv
and
$ awk 'BEGIN{OFS="\t"} {if ($4==0) {print $0, "inf"} else {print $0, ($3/$4)/log(2)}}' testlog2.csv > testlog2_2.csv

but awk keeps crashing whenever the denominator is 0. If you have any suggestions how to get round it, that would be excellent.

役に立ちましたか?

解決

Your problem is, you should print the head line separately.

to verify it:

kent$  awk 'BEGIN{print "A"/"B"}'                                                                                                                                           
awk: cmd. line:1: fatal: division by zero attempted

You could change your awk line into:

 awk 'BEGIN{OFS="\t"} NR==1{print;next}{if ($4==0) {print $0, "inf"} else {print $0, ($3/$4)/log(2)}}' file

I didn't change much in your codes, basically copy and paste, just add the NR==1 part.

NOTE

if you run the code, you will see that the output is not same as your expected (example in your question). I hope your logic ($3/$4)/log(2) is correct.

the output with NR==1{...} fix:

ID1              ID2       AVG1      AVG2
ENSG00000207447  RNU6-2    0.101431  0.163308   0.896061
ENSG00000207427  SNORA51   0.096551  0          inf
ENSG00000201784  SNORD14A  0.068653  0.0320985  3.08567
ENSG00000207315  SNORA28   0         0.01936    0
ENSG00000207787  MIR98     0         0          inf
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top