Hello, 

I'm trying to merge peak files in a way that I can't find so far. 

I have 16 peak files concatenated in my_peaks.bed

I've tried to use bedtools merge following this example: 

    $ cat my_peaks.bed
    chr1  100 300
    chr1  200 500
    chr1  0 700
    
    $ bedtools merge -i my_peaks.bed -c 1 -o count
    chr1  0 700 3

But actually the kind of output that I want would be the following:

    $ bedtools merge -i my_peaks.bed -c 1 -o count
    chr1  0  100  1
    chr1  100  200  2
    chr1  200  300  3
    chr1  300  500  2
    chr1  500  700  1

I've looked into bedops option --partition which would give : 

    $ bedops --partition my_peaks.bed
    chr1  0  100 
    chr1  100  200 
    chr1  200  300 
    chr1  300  500 
    chr1  500  700 

But then I don't know how to keep the peak count. 



Thanks for help


You're very close; just pipe the partioned elements to BEDOPS *[bedmap][1]* to count peaks over partitions:

    $ sort-bed my_peaks.unsorted.bed > my_peaks.bed
    $ bedops --partition my_peaks.bed | bedmap --echo --count --delim '\t' - my_peaks.bed
    chr1	0	100	1
    chr1	100	200	2
    chr1	200	300	3
    chr1	300	500	2
    chr1	500	700	1

Note that you first need to sort your peaks. Using (BEDOPS) `sort-bed` will be faster than Unix `sort` and will sort on the stop column, where there are matches on the start column, which `sortBed` does not do. You only need to sort once, at the start.


  [1]: http://bedops.readthedocs.io/en/latest/content/reference/statistics/bedmap.html