I have in excess of 400 BED files of transcription factor binding site coordinates that I want to compare with one master BED file of binding regions. The aim is to identify which of the ~400 TFBS overlap with the master file.

Normally I would go straight to bedtools, but given the number of TFBS files I wonder if there is a "better" method? [bedtools intersect][1] would appear to do the trick, but for 400 files....?

The type of output I am looking to get is:

```
master_pos1    TFBS8, TFBS16, TFBS200
master_pos2    TFBS1, TFBS333
..
```

Thank you

 [1]: http://bedtools.readthedocs.org/en/latest/content/tools/intersect.html
If your BED files are sorted, you could use [*bedops*][1] to union all the TFBS to standard output, and pipe that result to [*bedmap*][2] to do the mapping of TFs to master regions.

(This technique assumes that the TFBS files are minimally BED4. That is, the fourth column in each TFBS file contains the ID of the TF. If that is not the case, describe your format in more detail and I'll suggest a quick one-liner with *awk* to fix up files into the correct form.)

Here's the one-liner that unions and maps:

    $ bedops --everything tfbs*.bed | bedmap --echo --echo-map-id-uniq --delim '\t' master.bed - > answer.bed

Piping to standard output avoids the unnecessary step of making an intermediate file somewhere on the hard drive, which is otherwise very expensive in time. So this should be very fast.

Assuming that the ID fields in each of `tfbs001.bed` through `tfbs400.bed` contain the desired TF names or other identifiers of choice, the file `answer.bed` contains the results as you expect, except that it uses a semi-colon as an ID delimiter, instead of a comma. You could add `--multidelim ','` to the *bedmap* statement, if that is a requirement.

If your BED files are not sorted, you could first prepare them with BEDOPS [*sort-bed*][3], which is faster at sorting BED files than GNU *sort*.

```
$ for tfbs_fn in `ls tfbs*.bed`; do sort-bed $tfbs_fn > sorted.$tfbs_fn; done
$ sort-bed master.bed > sorted.master.bed
```

Then use the sorted files in downstream BED ops. You only need to sort once.

 [1]: http://bedops.readthedocs.org/en/latest/content/reference/set-operations/bedops.html
 [2]: http://bedops.readthedocs.org/en/latest/content/reference/statistics/bedmap.html
 [3]: http://bedops.readthedocs.org/en/latest/content/reference/file-management/sorting/sort-bed.html