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.

  • $\begingroup$ I searched for bedtools merge or bedtools collapse, but couldn't find a way. $\endgroup$
    – 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.

For more information: http://bedops.readthedocs.io/en/latest/content/reference/statistics/bedmap.html#score-operations


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.

  • $\begingroup$ 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 $\endgroup$
    – llrs
    Jan 16 '18 at 11:50
  • $\begingroup$ 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. $\endgroup$
    – Ido Tamir
    Jan 16 '18 at 12:04
  • $\begingroup$ Without the explanation your answer becomes pretty unhelpful if the location of the help files changes $\endgroup$ Jan 16 '18 at 12:35

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.