Here is a general approach with BEDOPS bedmap --count
, which generalizes to n internally-disjoint input files:
$ bedops --everything 1.bed 2.bed 3.bed ... 20.bed \
| bedmap --count --echo --delim '\t' - \
| uniq \
| awk -voverlaps=5 '$1 >= overlaps' \
| cut -f2- \
> common.bed
The bedmap
step uses, by default, overlap of one or more bases for inclusion. You can modify this threshold to be more stringent.
By changing the test in the awk
statement, this approach can be modified to return other subsets of the input's power set, e.g., all elements common to exactly, less than, or greater than n inputs.
Once you have the elements which meet your overlap threshold, you can then process those elements to get their overlapping regions via a final operation:
$ bedops --partition common.bed | bedmap --count --echo --delim '\t' - common.bed | awk '$1 >= 2' | cut -f2- > overlaps.bed
If your inputs are not internally disjoint — if some elements may overlap within any one of the 20 input files — you might instead apply some ID-based tricks I describe in my answer over here: A: Intersect multiple BED files