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.
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
answer.bed will contain the maximal-scored element from
elements.bed5 that overlaps the merged intervals in
--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
--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-rand options for, conversely, reporting minimal-scoring elements.
groupby from bedtools might help you:
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.