Hi,

I've a GRanges representing a intervals for all genes in the genome. A lot of these intervals are overlapping. I would like to use the `reduce()` from `GenomicRanges` package in order to make a non-overlapping set of interval. However I would like to do it for each gene separately. Thus for one specific gene, intervals for this gene should not overlap ; but intervals for different genes may overlap. One solution would like to split the GRanges by gene and apply reduce() on each subset but I'm wondering if there is a more efficient way ?

Thanks

Actual data

    chrom	start	end	hgnc
    1	100	200	MYC
    1	150	300	MYC
    1	400	500	MYC
    1	150	230	TP53
    1	200	350	TP53
    1	420	550	TP53

expected result

    chrom	start	end	hgnc
    1	100	300	MYC
    1	400	500	MYC
    1	150	350	TP53
    1	420	550	TP53


My actual solution :

    # gene is the dataframe used to create the initial GRanges
     do.call(rbind,lapply(
      split(gene,gene$hgnc),
      function(x){
        as.data.frame(
          reduce(
            GRanges(x$chrom,IRanges(x$start,x$end))))}))


Looks like we can't avoid *reduce()*, but we can avoid `do.call(rbind, lapply(...` using *data.table*:

    library(data.table) 
    library(GenomicRanges) # reduce function

    # example data
    x <- fread("
    chrom   start   end hgnc
    1   100 200 MYC
    1   150 300 MYC
    1   400 500 MYC
    1   150 230 TP53
    1   200 350 TP53
    1   420 550 TP53")

    x[, as.data.table(reduce(IRanges(start, end))), by = .(chrom, hgnc)]
    #    chrom hgnc start end width
    # 1:     1  MYC   100 300   201
    # 2:     1  MYC   400 500   101
    # 3:     1 TP53   150 350   201
    # 4:     1 TP53   420 550   131

Code stolen and adapted from StackOverflow post: 

- [Finding all overlaps in one iteration of foverlap in R's data.table](https://stackoverflow.com/a/45574386/680068)
