Question: awk to organize bed file
0
gravatar for flogin
25 days ago by
flogin110
FioCruz/Brazil
flogin110 wrote:

Hi folk, I have a bed file like this:

AAeBV_JXPU01118970.1_163_1090   1091    164
AAeliv_supercont1.1288_9956_11805   11806   9957
AAeliv_supercont1.152_1983674_1982874   9573    10955
AAeliv_supercont1.172_482498_480625 10001   11983
AAeliv_supercont1.184_260250_259840 10001   10411
AAeliv_supercont1.219_1484226_1486015   11790   10001
AAeliv_supercont1.240_1286280_1287235   10092   10808
AAeliv_supercont1.286_1347920_1346848   11073   10001
AAeliv_supercont1.286_1361694_1362031   10001   10338
AAeliv_supercont1.286_1441550_1443009   10001   11460

I want to organize columns 2 and 3 by length, generating an output like that:

**AAeBV_JXPU01118970.1_163_1090 164   1091
**AAeliv_supercont1.1288_9956_11805 9957   11806
AAeliv_supercont1.152_1983674_1982874   9573    10955
AAeliv_supercont1.172_482498_480625 10001   11983
AAeliv_supercont1.184_260250_259840 10001   10411
**AAeliv_supercont1.219_1484226_1486015 10001   11790
AAeliv_supercont1.240_1286280_1287235   10092   10808
**AAeliv_supercont1.286_1347920_1346848 10001   11073
AAeliv_supercont1.286_1361694_1362031   10001   10338
AAeliv_supercont1.286_1441550_1443009   10001   11460

** represents lines modified.

So I wrote this awk:

awk '{n1=$2;n2=$3;if($2 > $3)$2=n2 && $3=n1;print $0}'

briefly, I put the value of column $2 in a variable n1, the value of column $3 in a variable n2, if $2 is grater than $3, the values should be replaced, and the line printed.

But, the output is:

**AAeBV_JXPU01118970.1_163_1090 1 1091
**AAeliv_supercont1.1288_9956_11805 1 11806
AAeliv_supercont1.152_1983674_1982874   9573    10955
AAeliv_supercont1.172_482498_480625 10001   11983
AAeliv_supercont1.184_260250_259840 10001   10411
**AAeliv_supercont1.219_1484226_1486015 1 11790
AAeliv_supercont1.240_1286280_1287235   10092   10808
**AAeliv_supercont1.286_1347920_1346848 1 11073
AAeliv_supercont1.286_1361694_1362031   10001   10338
AAeliv_supercont1.286_1441550_1443009   10001   11460

The minor value is replaced by 1, can anyone explain to me that incongruence? thanks.

awk bed • 87 views
ADD COMMENTlink modified 25 days ago by finswimmer11k • written 25 days ago by flogin110
3
gravatar for finswimmer
25 days ago by
finswimmer11k
Germany
finswimmer11k wrote:

The problem seems to be this statement:

$2=n2 && $3=n1

This seems to evaluate to 1. Instead write this:

$ awk '{n1=$2;n2=$3;if($2 > $3) {$2=n2; $3=n1} print $0}'
ADD COMMENTlink written 25 days ago by finswimmer11k

thanks finswimmer, that resolved

ADD REPLYlink written 25 days ago by flogin110
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1139 users visited in the last hour