I am trying to find a way to collapse rows with intersecting ranges, denoted by "start" and "stop" columns, and record the collapsed values into new columns. For example I have this data frame:
my.df<- data.frame(chrom=c(1,1,1,1,14,16,16), name=c("a","b","c","d","e","f","g"), start=as.numeric(c(0,70001,70203,70060, 40004, 50000872, 50000872)), stop=as.numeric(c(71200,71200,80001,71051, 42004, 50000890, 51000952)))
chrom name start stop
1 a 0 71200
1 b 70001 71200
1 c 70203 80001
1 d 70060 71051
14 e 40004 42004
16 f 50000872 50000890
16 g 50000872 51000952
And I am trying to find the overlapping ranges and record the biggest range covered by the collapsed overlapping rows in "start" and "stop" and the names of the collapsed rows, so I would get this:
chrom start stop name
1 70001 80001 a,b,c,d
14 40004 42004 e
16 50000872 51000952 f,g
I think I could use the packages IRanges like this:
library(IRanges)
ranges <- split(IRanges(my.df$start, my.df$stop), my.df$chrom)
But then I have trouble getting the collapsed columns: I have tried with findOvarlaps but this
ov <- findOverlaps(ranges, ranges, type="any")
but I don't think this is right.
Any help would be extremely appreciated.
See Question&Answers more detail:os