2
$\begingroup$

I have a bed file where some entries have the same name in column 4 but are book-ended in coordinates, e.g. see below:

chr1    0       10000   LOC101929784::chr1:13201-13800::E1
chr1    10000   10600   LOC101929784::chr1:13201-13800::XYZ
chr1    10600   10800   LOC101929784::chr1:13201-13800::E1
chr1    10800   12000   DDX11L1::chr1:11868-14409.E1
chr1    12000   12200   DDX11L1::chr1:11868-14409.E1
chr1    12200   13000   DDX11L1::chr1:11868-14409.E1
chr1    13000   13200   DDX11L1::chr1:11868-14409.E1
chr1    13200   15800   WASH7P::chr1:14403-29570.E1
chr1    15800   16000   WASH7P::chr1:14403-29570.E1
chr1    16000   18200   WASH7P::chr1:14403-29570.E1
chr1    18200   18400   WASH7P::chr1:14403-29570.E1
chr1    18400   23000   WASH7P::chr1:14403-29570.E1
chr1    23000   23400   WASH7P::chr1:14403-29570.E1
chr1    23400   24000   WASH7P::chr1:14403-29570.E1
chr1    24000   24200   WASH7P::chr1:14403-29570.E1
chr1    24200   25200   WASH7P::chr1:14403-29570.E1
chr1    25200   25800   WASH7P::chr1:14403-29570.E1
chr1    25800   26200   WASH7P::chr1:14403-29570.E1
chr1    26200   26400   WASH7P::chr1:14403-29570.E1
chr1    26400   27000   WASH7P::chr1:14403-29570.E1
chr1    27000   27600   WASH7P::chr1:14403-29570.E1
chr1    27600   27800   WASH7P::chr1:14403-29570.E1
chr1    27800   29600   WASH7P::chr1:14403-29570.E1
chr1    29600   30800   MIR1302-2HG::chr1:29553-31109.E1
chr1    30800   31400   MIR1302-2HG::chr1:29553-31109.E1

I would like to apply an operation that seems to me is a mix of bedtools merge -d 0 and bedtools groupby, but I can't figure out what's the combination.

For example, the 4 entries with DDX11L1::chr1:11868-14409.E1 I would like merged as they are book-ended, same with the WASH7P::chr1:14403-29570.E1 entries and the MIR1302-2HG::chr1:29553-31109.E1 entries. There are two LOC101929784::chr1:13201-13800::E1 entries, but I don't want to merge them because they are not book-ended, since they are separated by LOC101929784::chr1:13201-13800::XYZ.

Any ideas?

$\endgroup$
5
$\begingroup$
awk 'BEGIN{OFS="\t"}{if($1==lchrom && $4==lname && $2 == lend) {lend = $3}else{if(lchrom) {print lchrom, lstart, lend, lname;}; lchrom=$1; lstart=$2; lend=$3; lname=$4}}END{print lchrom, lstart, lend, lname}' foo.bed > new.bed

That produces:

chr1    0   10000   LOC101929784::chr1:13201-13800::E1
chr1    10000   10600   LOC101929784::chr1:13201-13800::XYZ
chr1    10600   10800   LOC101929784::chr1:13201-13800::E1
chr1    10800   13200   DDX11L1::chr1:11868-14409.E1
chr1    13200   29600   WASH7P::chr1:14403-29570.E1
chr1    29600   31400   MIR1302-2HG::chr1:29553-31109.E1

This explicitly checks for bookends and nothing else and assumes that everything is sorted.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.