【问题标题】:Combining IRanges objects and maintaining mcols组合 IRange 对象并维护 mcol
【发布时间】:2019-08-30 03:47:36
【问题描述】:

我将从一个示例开始,然后描述我尝试使用的逻辑。

我有两个普通的IRanges 对象,它们跨越相同的总范围,但可能在不同数量的范围内这样做。每个IRanges 都有一个mcol,但mcol 在IRanges 中是不同的。

a
#IRanges object with 1 range and 1 metadata column:
#          start       end     width | on_betalac
#      <integer> <integer> <integer> |  <logical>
#  [1]         1       167       167 |      FALSE
b
#IRanges object with 3 ranges and 1 metadata column:
#          start       end     width |  on_other
#      <integer> <integer> <integer> | <logical>
#  [1]         1       107       107 |     FALSE
#  [2]       108       112         5 |      TRUE
#  [3]       113       167        55 |     FALSE

您可以看到这两个IRanges 的范围都是 1 到 167,但 a 有一个范围,b 有三个范围。我想将它们结合起来得到这样的输出:

my_great_function(a, b)
#IRanges object with 3 ranges and 2 metadata columns:
#          start       end     width | on_betalac  on_other
#      <integer> <integer> <integer> |  <logical> <logical>
#  [1]         1       107       107 |     FALSE     FALSE
#  [2]       108       112         5 |     FALSE      TRUE
#  [3]       113       167        55 |     FALSE     FALSE

输出类似于输入的disjoin,但它保留mcols,甚至扩展它们,以便输出范围与导致它的输入范围具有相同的mcol 值.

【问题讨论】:

    标签: r iranges


    【解决方案1】:

    选项 1:使用IRanges::findOverlaps

    m <- findOverlaps(b, a)
    c <- b[queryHits(m)]
    mcols(c) <- cbind(mcols(c), mcols(a[subjectHits(m)]))
    #IRanges object with 3 ranges and 2 metadata columns:
    #          start       end     width |  on_other on_betacalc
    #      <integer> <integer> <integer> | <logical>   <logical>
    #  [1]         1       107       107 |     FALSE       FALSE
    #  [2]       108       112         5 |      TRUE       FALSE
    #  [3]       113       167        55 |     FALSE       FALSE
    

    生成的对象c 是一个具有两个元数据列的IRanges 对象。

    选项 2:使用IRanges::mergeByOverlaps

    c <- mergeByOverlaps(b, a)
    c
    #DataFrame with 3 rows and 4 columns
    #          b  on_other         a on_betacalc
    #  <IRanges> <logical> <IRanges>   <logical>
    #1     1-107     FALSE     1-167       FALSE
    #2   108-112      TRUE     1-167       FALSE
    #3   113-167     FALSE     1-167       FALSE
    

    生成的输出对象是DataFrame,其中IRanges 列和原始元数据列作为附加列。

    选项 3:使用data.table::foverlaps

    library(data.table)
    a.dt <- as.data.table(cbind.data.frame(a, mcols(a)))[, width := NULL]
    b.dt <- as.data.table(cbind.data.frame(b, mcols(b)))[, width := NULL]
    
    setkey(b.dt, start, end)
    foverlaps(a.dt, b.dt, type = "any")[, `:=`(i.start = NULL, i.end = NULL)][]
       start end on_other on_betacalc
    1:     1 107    FALSE       FALSE
    2:   108 112     TRUE       FALSE
    3:   113 167    FALSE       FALSE
    

    生成的对象是data.table。

    选项 4:使用fuzzyjoin::interval_left_join

    library(fuzzyjoin)
    a.df <- cbind.data.frame(a, mcols(a))
    b.df <- cbind.data.frame(b, mcols(b))
    interval_left_join(b.df, a.df, by = c("start", "end"))
    #  start.x end.x width.x on_other start.y end.y width.y on_betacalc
    #1       1   107     107    FALSE       1   167     167       FALSE
    #2     108   112       5     TRUE       1   167     167       FALSE
    #3     113   167      55    FALSE       1   167     167       FALSE
    

    生成的对象是data.frame。


    样本数据

    library(IRanges)
    a <- IRanges(1, 167)
    mcols(a)$on_betacalc = F
    
    b <- IRanges(c(1, 108, 113), c(107, 112, 167))
    mcols(b)$on_other <- c(F, T, F)
    

    【讨论】:

    • 谢谢,这看起来很有希望,尤其是选项 1。我现在正在考虑输入两个以上 IRange 的情况。这会是选项 #1 的简单扩展,还是我应该提出一个新的 SO 问题?
    • 不客气@rcorty;是的,合并超过 2 个IRanges 更具挑战性;我记得不久前在这里回答了一个类似的问题,也许需要一些时间在这里搜索相关问题(以及在 Biostars/SeqAnswers 上)。在 3 IRanges 的情况下,合并的顺序很重要;显然,欢迎您提出新问题,最好将可重现的样本数据包含在 dput 中(这意味着我们不必像我在这里那样手动构建样本数据)。
    • [继续] 至于我上面给出的不同选项:它们都给你相同的结果,所以一个解决方案确实没有比另一个解决方案有重大优势(请注意,我没有对不同的性能方面的选择)。
    • 谢谢,相关问题我去看看。我对 IRange 非常陌生。 #1 吸引我的唯一原因是输出正是我想要的格式,所以不必做任何争论。您能否评论我提出的解决方案(如下)?
    • @rcorty 我猜你的意思是你自己发布的解决方案?似乎有比我发布的选项更多的数据争论。只需cbinding 元数据列(如选项 1 所示)应该适用于 任何 数量的元数据列。我认为for 循环加上sapply 是不必要的,并且对于较大的IRanges 对象可能会使事情变得非常慢。我想IRanges 选项会非常快; data.table 解决方案同上; fuzzyjoin::interval_join 实际上是基于 IRanges::findOverlaps 所以应该具有类似的性能。
    【解决方案2】:

    这是我能想到的。不像 MauritsEvers 那样优雅,但在某些方面可能对其他人有用。

    combine_exposures <- function(...) {
    
      cd <- c(...)
      mc <- mcols(cd)
      dj <- disjoin(x = cd, with.revmap = TRUE)
      r <- mcols(dj)$revmap
    
      d <- as.data.frame(matrix(nrow = length(dj), ncol = ncol(mc)))
      names(d) <- names(mc)
    
      for (i in 1:length(dj)) {
        d[i,] <- sapply(X = 1:ncol(mc), FUN = function(j) { mc[r[[i]][j], j] })
      }
    
      mcols(dj) <- d
      return(dj)
    }
    

    这里是dput(c(e1, e2, e3, e4))(e1、e2、e3 和 e4 是一些示例 IRanges,它们都跨越 1,167):

    new("IRanges", start = c(1L, 1L, 108L, 113L, 1L, 1L), width = c(167L, 
    107L, 5L, 55L, 167L, 167L), NAMES = NULL, elementType = "ANY", 
        elementMetadata = new("DataFrame", rownames = NULL, nrows = 6L, 
            listData = list(on_betalac = c(FALSE, NA, NA, NA, NA, 
            NA), on_other = c(NA, FALSE, TRUE, FALSE, NA, NA), on_pen = c(NA, 
            NA, NA, NA, FALSE, NA), on_quin = c(NA, NA, NA, NA, NA, 
            FALSE)), elementType = "ANY", elementMetadata = NULL, 
            metadata = list()), metadata = list())
    

    【讨论】:

      猜你喜欢
      • 2018-03-14
      • 1970-01-01
      • 2011-04-18
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多