gpt4 book ai didi

r - 任何方式强制 "list"到 S4 "List"?

转载 作者:行者123 更新时间:2023-12-04 10:32:39 25 4
gpt4 key购买 nike

有没有办法将简单的类似列表的对象强制转换为 S4“列表”对象?我需要对我的数据进行一些矢量化。显然,我在函数中使用了 nested-lapply,并将其返回类型检查为“列表”。我想要“列表”之类的对象。我怎样才能做到这一点?谢谢。

这是澄清问题的可重现示例:

数据

    foo <- GRanges(
seqnames=Rle(c("chr1", "chr2", "chr3", "chr4"), c(3, 2, 1, 2)),
ranges=IRanges(seq(1, by=9, len=8), seq(7, by=9, len=8)),
rangeName=letters[seq(1:8)], score=sample(1:20, 8, replace = FALSE))

bar <- GRanges(
seqnames=Rle(c("chr1", "chr2", "chr3","chr4"), c(4, 3, 1, 1)),
ranges=IRanges(seq(2, by=5, len=9), seq(4, by=5, len=9)),
rangeName=letters[seq(1:9)], score=sample(1:20, 9, replace = FALSE))

moo <- GRanges(
seqnames=Rle(c("chr1", "chr2", "chr3","chr4"), c(3, 4, 2,1)),
ranges=IRanges(seq(5, by=7, len=10), seq(8, by=7, len=10)),
rangeName=letters[seq(1:10)], score=sample(1:20, 10, replace = FALSE))

重叠命中指数

    grl <- GRangesList(bar, moo)
res <- lapply(grl, function(ele_) {
tmp <- as(findOverlaps(foo, ele_), "List")
})

重复区域的解释(第一个列表元素对应于条):

[[1]]
IntegerList of length 8
[[1]] 1 2 # 1st regions from foo overlapped with 1st,2nd regions from bar
[[2]] 3
[[3]] 4
[[4]] 6 7 # 1st regions from foo overlapped with 6st,7th regions from bar

目标只保留一个(又名,删除多个相交的区域),例如:

[[1]]
IntegerList of length 8
[[1]] 2 # only keep 2nd region from bar
[[2]] 3
[[3]] 4
[[4]] 6 7 # only keep 6th region from bar

删除重复区域

obj.ov <- lapply(res, function(ele_) {
re <- lapply(grl, function(obj) {
id0 <- as(which.max(extractList(obj$score, ele_)), "List")
id0 <- id0[!is.na(id0)]
})
re <- re[!duplicated(re)]
})

进一步的步骤

as.obj.ov <- as(obj.ov, "List") # 如果这个强制转换不正确,就不能像 obj.ov

那样扩展

然后,as.obj.ov 必须像 obj.ov 一样可扩展作为命中索引向量,类型也必须是 S4“列表”对象。

我需要让 obj.ov 作为 S4“列表” 对象。是否有可能在 R 中进行这种强制转换?

任何可能的方法、解决方案或想法都值得赞赏。

最佳答案

我们可以使用select = "first" 来获得第一个匹配项。

lapply(grl, function(ele_) {
ix <- findOverlaps(foo, ele_, select = "first")
ele_[ix[!is.na(ix)]]
})

[[1]]
GRanges object with 4 ranges and 2 metadata columns:
seqnames ranges strand | rangeName score
<Rle> <IRanges> <Rle> | <character> <integer>
[1] chr1 [ 2, 4] * | a 18
[2] chr1 [12, 14] * | c 2
[3] chr1 [17, 19] * | d 19
[4] chr2 [27, 29] * | f 15
-------
seqinfo: 4 sequences from an unspecified genome; no seqlengths

[[2]]
GRanges object with 6 ranges and 2 metadata columns:
seqnames ranges strand | rangeName score
<Rle> <IRanges> <Rle> | <character> <integer>
[1] chr1 [ 5, 8] * | a 11
[2] chr1 [12, 15] * | b 13
[3] chr1 [19, 22] * | c 14
[4] chr2 [26, 29] * | d 20
[5] chr2 [40, 43] * | f 8
[6] chr4 [68, 71] * | j 1
-------
seqinfo: 4 sequences from an unspecified genome; no seqlength

关于r - 任何方式强制 "list"到 S4 "List"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38217165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com