如果你读到这篇文章,并花宝贵的时间帮助我解决我遇到的问题,我真的很感激。
在R中,我想对来自一个数据帧的小的、连续的bin的数据进行排序,将其排序为不规则大小的(非重叠的(bin,并在另一个数据框中针对所有重叠的间隔进行分布。
我的第一个数据帧是这样的(实际的数据帧是几十万行(:
chr bin from to BS_seq_Count
SL4.0ch01 1 1 500 3
SL4.0ch01 2 501 1000 10
SL4.0ch01 3 1001 1500 3
SL4.0ch02 1 1 500 3
SL4.0ch02 2 501 1000 10
SL4.0ch02 3 1001 1500 3
SL4.0ch03 1 1 500 3
SL4.0ch03 2 501 1000 10
SL4.0ch03 3 1001 1500 3
...
这是我想与之重叠并排序到相应仓中的数据帧:
chr bin from to
SL4.0ch01 1 200 700
SL4.0ch01 2 800 1300
SL4.0ch02 1 300 400
SL4.0ch03 1 50 600
SL4.0ch03 2 700 800
SL4.0ch03 3 1000 1200
...
最后应该是这样的(小数/四舍五入没有那么重要,但部分重叠的计数也应该被分类到仓中(:
chr bin from to count
SL4.0ch01 1 200 700 5.8
SL4.0ch01 2 800 1350 6.1
SL4.0ch02 1 300 400 0.6
SL4.0ch03 1 50 600 4.7
SL4.0ch03 2 700 800 2
SL4.0ch03 3 1000 1200 1.2
...
我曾想过将GenomicRanges与findOverlaps函数一起使用,但在这种情况下,我不知道如何使其正确工作。
如果有人对如何解决这个问题有想法,我们将不胜感激!
提前谢谢你,祝你周末愉快,身体健康!
我很确定有一种更有效的非等联接方法,但这里有一种data.table
笛卡尔联接方法:
library(data.table)
dt1 <- setorder(data.table(chr = paste0("SL4.0ch01", rep(1:3, each = 3)), bin = rep(1:3, 3), from = rep(c(1, 501, 1001), 3), to = rep(c(500, 1000, 1500), 3), ct = rep(c(3, 10, 3), 3)), chr)
dt2 <- data.table(chr = paste0("SL4.0ch01", c(1,1,2,3,3,3)), bin = c(1,2,1,1,2,3), from = c(200, 800, 300, 50, 700, 1000), to = c(700, 1350, 400, 600, 800, 1200))
dt3 <- merge.data.table(dt1, dt2, by = "chr", allow.cartesian = TRUE)[, overlap := 0]
dt3[from.x < to.y & from.y < to.x, overlap := ct*(pmin(to.x, to.y) - pmax(from.x, from.y))/(to.x - from.x)]
dt2[, count := dt3[, .(count = sum(overlap)), by = .(chr, bin.y)]$count]
dt2
#> chr bin from to count
#> 1: SL4.0ch011 1 200 700 5.7915832
#> 2: SL4.0ch011 2 800 1350 6.1062124
#> 3: SL4.0ch012 1 300 400 0.6012024
#> 4: SL4.0ch013 1 50 600 4.6893788
#> 5: SL4.0ch013 2 700 800 2.0040080
#> 6: SL4.0ch013 3 1000 1200 1.1963928
这里有一个使用foverlaps()
的解决方案,IRanges::findOverlaps()
函数的data.table版本:
library(data.table)
foverlaps(dt1, setkey(dt2, chr, from, to), nomatch = NULL)[
, .(count = sum(BS_seq_Count / (i.to - i.from + 1L) *
(pmin(to, i.to) - pmax(from, i.from) + 1L))),
by = .(chr, bin, from, to)]
chr bin from to count <char> <int> <int> <int> <num> 1: SL4.0ch01 1 200 700 5.806 2: SL4.0ch01 2 800 1350 6.120 3: SL4.0ch02 1 300 400 0.606 4: SL4.0ch03 1 50 600 4.706 5: SL4.0ch03 2 700 800 2.020 6: SL4.0ch03 3 1000 1200 1.220
数据
library(data.table)
dt1 <- fread("
chr bin from to BS_seq_Count
SL4.0ch01 1 1 500 3
SL4.0ch01 2 501 1000 10
SL4.0ch01 3 1001 1500 3
SL4.0ch02 1 1 500 3
SL4.0ch02 2 501 1000 10
SL4.0ch02 3 1001 1500 3
SL4.0ch03 1 1 500 3
SL4.0ch03 2 501 1000 10
SL4.0ch03 3 1001 1500 3")
dt2 <- fread("
chr bin from to
SL4.0ch01 1 200 700
SL4.0ch01 2 800 1350
SL4.0ch02 1 300 400
SL4.0ch03 1 50 600
SL4.0ch03 2 700 800
SL4.0ch03 3 1000 1200")