# bed entries collapse based on score column

I have a bed file where some entries have the exact same chr:start-end but change in the name and score column. I would like to apply bedtools or a similar tool to obtain a filtered file where for each set of chr:start-end entries, only the one with the highest score (column 5) is used, the others filtered out. I would like to do this with a bed format toolkit (bedtools, etc) rather than scripting it.

• I searched for bedtools merge or bedtools collapse, but couldn't find a way. – 719016 Jan 15 '18 at 21:23

Use BEDOPS bedmap --max-element or bedmap --max-element-rand to grab the maximal-scoring element over mapped intervals, e.g.:

$sort-bed elements.unsorted.bed5 > elements.bed5$ bedops --merge elements.bed5 > merged.bed3
\$ bedmap --max-element merged.bed3 elements.bed5 > answer.bed


The file answer.bed will contain the maximal-scored element from elements.bed5 that overlaps the merged intervals in merged.bed3.

The --max-element and --max-element-rand options differ on handling ties. The first option grabs the first match from ties by sort-bed (lexiographical) sort order; the second option grabs a random match on ties.

The option you pick would depend on what you're doing with the answer. Picking a random hit might be better if you're doing downstream statistical tests, for instance.

You can add --sci and --prec <int> options to format the score output to your desired level of precision. I think the default assumption is six digits of precision; adjust as needed.

There are also --min-element and --min-element-rand options for, conversely, reporting minimal-scoring elements.

bedtools groupby -i variantsToRepeats.bed -grp 1-3 -c 5 -o max


-grp to select the columns to group by
-c to select the columns to summarize
-o to select how to summarize the column selected.

• Could you please expand your answer? An explanation might help future readers. Thanks! I edited your question from the help page you linked. I hope it is now better, and I understood it correctly – llrs Jan 16 '18 at 11:50
• How lazy can people be? I even linked the help page and to anybody with an IQ over 80 the options should be immediately obvious. – Ido Tamir Jan 16 '18 at 12:04
• Without the explanation your answer becomes pretty unhelpful if the location of the help files changes – heathobrien Jan 16 '18 at 12:35