我有两个 GRange 列表,我正在尝试将 countOverlaps 函数应用于列表的每个组合并返回如下结果列表:
library(GenomicRanges)
gr1 <- GRanges(seqnames = c("chr1", "chr2"), ranges = IRanges(c(7,13), width = 3), strand = c("+", "-"))
gr2 <- GRanges(seqnames = c("chr1", "chr3"), ranges = IRanges(c(5,13), width = 3), strand = c("+", "-"))
grlA <- GRangesList("a" = gr1, "b" = gr2)
gr1 <- GRanges(seqnames = c("chr1", "chr2"), ranges = IRanges(c(1,13), width = 3), strand = c("+", "-"))
gr2 <- GRanges(seqnames = c("chr1", "chr3"), ranges = IRanges(c(3,13), width = 3), strand = c("+", "-"))
grlB <- GRangesList("c" = gr1, "d" = gr2)
我想在 grlA 中获取对象“a”和对象“b”的列表,其中包含 grlB 的每个值的函数结果:
(列出 $a、$b 和 c、d 的数据框)
$c
抗体
$d
抗体
这可以获取列表的所有组合:
comb_apply <- function(f,..., MoreArgs=list()){
exp <- unname(as.list(expand.grid(...,stringsAsFactors = FALSE)))
do.call(mapply, c(list(FUN=f, SIMPLIFY=FALSE, MoreArgs=MoreArgs), exp))
}
# This function is thanks to Michael Lawrence's help posted in the bioconductor package
t= comb_apply(function(i, j) countOverlaps(grlA[[i]], grlB[[j]]), seq_along(grlA), seq_along(grlB))
names(t)=apply(expand.grid(names(grlA), names(grlB)), 1, paste, collapse="_")
但是为了得到我想要的(数据帧列表),我需要使用 grep 命令来选择属于 grlB 的数据帧并将它们保存在单独的列表中,但这非常慢......
new=list()
for (i in names(grlB)) {
df = as.data.frame(t[grep(i,names(t))])
new[[length(new)+1]] <- df
}
有没有另一种方法可以在没有 grep 的情况下做到这一点?谢谢!