<p>Use <a href="https://github.com/bedops/bedops" rel="nofollow noreferrer">BEDOPS</a> <code>bedmap --max-element</code> or <code>bedmap --max-element-rand</code> to grab the maximal-scoring element over mapped intervals, e.g.:</p>

<pre><code>$ sort-bed elements.unsorted.bed5 &gt; elements.bed5
$ bedops --merge elements.bed5 &gt; merged.bed3
$ bedmap --max-element merged.bed3 elements.bed5 &gt; answer.bed
</code></pre>

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

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

<p>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. </p>

<p>You can add <code>--sci</code> and <code>--prec &lt;int&gt;</code> 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.</p>

<p>There are also <code>--min-element</code> and <code>--min-element-rand</code> options for, conversely, reporting minimal-scoring elements.</p>

<p>For more information: <a href="http://bedops.readthedocs.io/en/latest/content/reference/statistics/bedmap.html#score-operations" rel="nofollow noreferrer">http://bedops.readthedocs.io/en/latest/content/reference/statistics/bedmap.html#score-operations</a> </p>

CC BY-SA 3.0
2018-01-15T21:50:01.707Z
2018-01-15T22:45:32.870Z
2018-01-15T22:45:32.870Z
Alex Reynolds
<p><a href="http://bedtools.readthedocs.io/en/latest/content/tools/groupby.html" rel="nofollow noreferrer">groupby</a> from bedtools might help you:</p>
<pre><code>bedtools groupby -i variantsToRepeats.bed -grp 1-3 -c 5 -o max
</code></pre>
<p><code>-grp</code> to select the columns to group by<br />
<code>-c</code> to select the columns to summarize<br />
<code>-o</code> to select how to summarize the column selected.</p>

CC BY-SA 3.0
2018-01-16T11:50:54.997Z
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
CC BY-SA 3.0
2018-01-16T12:04:28.610Z
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
CC BY-SA 3.0
2018-01-16T12:35:46.530Z
Without the explanation your answer becomes pretty unhelpful if the location of the help files changes
heathobrien
CC BY-SA 3.0
2018-01-16T11:11:35.007Z
2018-01-16T11:53:31.220Z
2020-06-18T08:30:41.743Z
Ido Tamir
<p>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.</p>

CC BY-SA 3.0
2018-01-15T21:23:53.180Z
I searched for bedtools merge or bedtools collapse, but couldn't find a way.
719016
<bed><bedtools>
bed entries collapse based on score column
# bed entries collapse based on score column

(Asked by: username_0 on 2018-01-15)

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.

**Tags:** bed, bedtools
> **Comments:**
> **username_0**: I searched for bedtools merge or bedtools collapse, but couldn't find a way.  

---

## 2 answers

### Answer by username_1 on 2018-01-15. Score: 2

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  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


---

### Answer by username_2 on 2018-01-16. Score: 1

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.

> **Comments:**
> **username_3**: 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  
> **username_2**: 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.  
> **username_4**: Without the explanation your answer becomes pretty unhelpful if the location of the help files changes  

---

