I have two GRange Lists and I am trying to apply countOverlaps function to each combination of the lists and return a list of results like this:
gr1 <- GRanges(seqnames = "chr2", ranges = IRanges(3, 6),strand = "+", score = 5L, GC = 0.45)
gr2 <- GRanges(seqnames = c("chr1", "chr1"), ranges = IRanges(c(7,13), width = 3), strand = c("+", "-"), score = 3:4, GC = c(0.3, 0.5))
grlA <- GRangesList("a" = gr1, "b" = gr2)
gr1 <- GRanges(seqnames = "chr2", ranges = IRanges(1, 10),strand = "+", score = 5L, GC = 0.45)
gr2 <- GRanges(seqnames = c("chr1", "chr1"), ranges = IRanges(c(3,13), width = 3), strand = c("+", "-"), score = 3:4, GC = c(0.2, 0.3))
grlB <- GRangesList("c" = gr1, "d" = gr2)
I would like to get a list of object "a" and object "b" in grlA containing the results of the function for each value of grlB:
(list $a, $b and dataframes for c,d)
$a
c d
$b
c d
I have tried this to get all the combinations of the list but I get an error...
comb_apply <- function(f,..., MoreArgs=list()){
exp <- unname(expand.grid(...,stringsAsFactors = FALSE))
do.call(mapply, c(list(FUN=f, SIMPLIFY=FALSE, MoreArgs=MoreArgs), exp))
}
comb_apply(countOverlaps,grlA,grlB)
Error in as.vector(x, mode = "character") :
no method for coercing this S4 class to a vector
What am I doing wrong?
Thank you for help/suggestions!
Thank you. It is very slow but it may have done something wrong in my original function since the output is not quite what I was looking for with lists of a containing data frames of b, anyway I will try to fix that. Thanks!
One way to make it faster would be to use
GenomicRangesList()
instead ofGRangesList()
. The latter has a representation that is not amenable to iteration.Great, thank you. Now just trying to figure out how I can get a rbind data frame for the object B, I think I will just resort to looping.
If you clarify your problem, we might be able to help.