awk to organize bed file
1
0
Entering edit mode
5.5 years ago
flogin ▴ 280

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 • 749 views
ADD COMMENT
3
Entering edit mode
5.5 years ago

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 COMMENT
0
Entering edit mode

thanks finswimmer, that resolved

ADD REPLY

Login before adding your answer.

Traffic: 1663 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6