Hi,

I have a couple of bed files with regions (peaks of ChIPseq), and I would like to create a unique file with merge regions, where overlapping regions are fused IF the overlap cover more than xx % (in my case 60%) of one of the 2 regions (not necessary both).

I know how to do that with 2 files, however I have more (at least 4). I could do recursive and pipes to do it pairwise multiple times (with bedops or bedtools), but I was wondering if there would not be a cleaner way. I can use bedops to merge everything that overlap by 1bp for my bed files at once, but I do not find a way to control on the overlap fraction...

Any idea?
Let's look at the following [BEDOPS][1]-based solution, which is a modification of the approach authored [here][2] by Shane Neph:

```
$ bedops --everything file1.bed file2.bed ... fileN.bed \
    | bedmap --echo-map --fraction-both 0.6 - \
    | awk '(split($0, a, ";") > 1)' - \
    | sed 's/\;/\n/g' - \
    | sort-bed - \
    | uniq - \
    > answer.bed
```

It might look a bit overwhelming, but it is really quite straightforward if we walk through this line by line.

1. Take the multiset union of all elements from all *N* input files with `bedops --everything`. Note that this is *not* the same as a `--merge` operation, which instead makes contiguous regions from all overlapping input regions. Pipe this unioned set to `bedmap`.
2. For each element from all files, use `bedmap --echo-map` to print any other elements from the multiset union that overlap that first element by 60% or more, using `--fraction-both 0.6`. The result will be a semi-colon delimited string that contains one or more overlapping elements. Note that this result will always include the original element, because it overlaps itself by 100%. Pipe this result to the `awk` statement.
3. The `awk` command splits the string on the semi-colon delimiter and prints out the string if there are two or more overlapping elements. We do this filter step, because we are not interested in any element overlapping itself and only itself, but only multiple (2+) elements that meet the 60% or greater overlap criterion. We pipe this string to `sed`.
4. The `sed` statement replaces all semi-colons with newline characters. This creates BED-formatted output that we pipe to `sort-bed`.
5. The `sort-bed` tool takes the overlapping elements and puts them in lexicographically-sorted order. We pipe this to `uniq` to remove duplicate elements.

The file `answer.bed` is a sorted BED file that contains all unique elements from `file1.bed` through `fileN.bed`, where there is mutual 60% or greater overlap.

Note that this is a highly efficient solution, compared with O(2^N) pairwise comparisons required by other approaches. Further, we make use of Unix standard input and output streaming (note the hyphen at the end of each command, denoting standard input) to avoid the cost of making unnecessary intermediate files; writing to disk is expensive. The only thing written to disk is the final result.

 [1]: http://bedops.readthedocs.org/en/latest/
 [2]: http://bedops.readthedocs.org/en/latest/content/usage-examples/multiple-inputs.html