awk select values with scientific notations

Viewed 42

Dear bioinformaticians,

I have a problem with awk and scientific notation of values.

I want to use awk to select rows based on the min value of $5 to keep only one ID of $1 ::

Chrpa1_10100    2434    PF00063 des 3.9E-21 IPR001609   
Chrpa1_10100    2434    PF03547 des 4.5E-7  IPR004776   
Chrpa1_10100    2434    PF07857 des 3.3E-7  IPR012435   
Chrpa1_10100    2434    PF13516 des 0.085   IPR001611   

in this particular case, I want :

Chrpa1_10100 2434 PF00063 des 3.9E-21 IPR001609

If I do :

cat file |sort -k1,1|uniq| awk -F'\t' '$5 > max[$1] { max[$1]=$5; row[$1]=$0 } END { for (i in row) print row[i] }'

but I want this one :

Chrpa1_10100 2434    PF13516 des 0.085   IPR001611

Thank you in advance,

Cheers

1 Answers

my command line is giving to me this : Chrpa1_10100 2434 PF13516 des 0.085 IPR001611 but I want this Chrpa1_10100 2434 PF00063 des 3.9E-21 IPR001609

Out of following numbers

3.9E-21 4.5E-7 3.3E-7 0.085

3.9E-21 is smallest, 0.085 is biggest, this code

awk -F'\t' '$5 > max[$1] { max[$1]=$5; row[$1]=$0 } END { for (i in row) print row[i] }'

selects biggest number, hence it did find line with 0.085. If you want 3.9E-21 you need to implement finding minimal value but remembering that while unknown mean zero is fine for finding max of positive numbers, it is not for minumum, therefore I suggest following ameloration

awk -F'\t' '!($1 in min)||($5 < min[$1]){ min[$1]=$5; row[$1]=$0 } END { for (i in row) print row[i] }'

Explanation: changed max to min and > to < and altered condition so is true if there is not key $1 in min array or value of $5 is smaller than value in min under key $1.

When file.txt content is

Chrpa1_10100 2434 PF00063 des 3.9E-21 IPR001609
Chrpa1_10100 2434 PF03547 des 4.5E-7 IPR004776
Chrpa1_10100 2434 PF07857 des 3.3E-7 IPR012435
Chrpa1_10100 2434 PF13516 des 0.085 IPR001611

then

awk '!($1 in min)||($5 < min[$1]){ min[$1]=$5; row[$1]=$0 } END { for (i in row) print row[i] }' file.txt

gives output

Chrpa1_10100 2434 PF00063 des 3.9E-21 IPR001609

(tested in gawk 4.2.1)

Related