按预期使用R data.table foverlaps()或IRanges计算重叠

按预期使用R data.table foverlaps()或IRanges计算重叠,r,data.table,intervals,overlap,iranges,R,Data.table,Intervals,Overlap,Iranges,我很难像预期的那样计算时间间隔的重叠。这是一个R data.table,其间隔由开始到结束定义: > library(data.table) > dt1 = data.table(start=c(1, 5, 3), end=c(10, 15, 8)) > print(dt1) start end 1: 1 10 2: 5 15 3: 3 8 这是我将如何考虑这些区间的重叠,从0到20: [0, 1]: 0 (there are no in

我很难像预期的那样计算时间间隔的重叠。这是一个R data.table,其间隔由开始到结束定义:

> library(data.table)
> dt1 = data.table(start=c(1, 5, 3), end=c(10, 15, 8))
> print(dt1)
   start end
1:     1  10
2:     5  15
3:     3   8

这是我将如何考虑这些区间的重叠,从0到20:

[0, 1]: 0 (there are no intervals here)
[1, 3]: 1 (there is only one interval here, from [1, 10])
[3, 5]: 2 (two intervals here, both [1, 10] and [3, 8])
[5, 8]: 3
[8, 10]: 1
[10, 15]: 1
[15, 20]: 0
所以,我想用算法输出这个。比如:

   start end  overlaps
1:     0  1   0
2:     1  3   1
3:     3  5   2
4:     5  8   3      
5:     8  10  2      
6:    10  15  1      
7:    15  20  0   
但是,我无法找到如何使用R data.table中的
foverlaps()
IRanges的各种函数来实现这一点

> setkey(dt1, start, end)
> foverlaps(dt1, dt1, type="any")
   start end i.start i.end
1:     1  10       1    10
2:     3   8       1    10
3:     5  15       1    10
4:     1  10       3     8
5:     3   8       3     8
6:     5  15       3     8
7:     1  10       5    15
8:     3   8       5    15
9:     5  15       5    15
> foverlaps(dt1, dt1, type="within")
   start end i.start i.end
1:     1  10       1    10
2:     1  10       3     8
3:     3   8       3     8
4:     5  15       5    15
为了计算某个时间间隔内的重叠,这两项似乎都不相关

查看
IRanges
也不能给出预期的重叠间隔计数:

> library(IRanges)
> range1
IRanges object with 3 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         1        10        10
  [2]         3         8         6
  [3]         5        15        11
> countOverlaps(range1, range1)
[1] 3 3 3
> countOverlaps(range1, range1, type="within")
[1] 1 2 1
>库(IRanges)
>范围1
具有3个范围和0个元数据列的IRanges对象:
起始端宽度
[1]         1        10        10
[2]         3         8         6
[3]         5        15        11
>计数重叠(范围1,范围1)
[1] 3 3 3
>countOverlaps(range1,range1,type=“in”)
[1] 1 2 1
如何计算重叠间隔

#0和20来自哪里?
> # Where do the 0 and the 20 come from?
> points <- c(0, sort(c(dt1$start, dt1$end)), 20)
> x <- do.call(IRanges,
+              transpose(Map(c, start=head(points, -1), end=tail(points, -1))))
> x
IRanges object with 7 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         0         1         2
  [2]         1         3         3
  [3]         3         5         3
  [4]         5         8         4
  [5]         8        10         3
  [6]        10        15         6
  [7]        15        20         6
> y <- do.call(IRanges, dt1)
> y
IRanges object with 3 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         1        10        10
  [2]         3         8         6
  [3]         5        15        11
> countOverlaps(x, y, type="within")
[1] 0 1 2 3 2 1 0
>x点 具有7个范围和0个元数据列的IRanges对象: 起始端宽度 [1] 0 1 2 [2] 1 3 3 [3] 3 5 3 [4] 5 8 4 [5] 8 10 3 [6] 10 15 6 [7] 15 20 6 >y y 具有3个范围和0个元数据列的IRanges对象: 起始端宽度 [1] 1 10 10 [2] 3 8 6 [3] 5 15 11 >计数重叠(x,y,type=“in”) [1] 0 1 2 3 2 1 0

第5个结果略有不同,但确实有2个重叠,因为[8,10]与[1,10]和[5,15]重叠。

您能添加一个可重复的示例吗?给定的数据集与您在问题描述(表2)中提供的不匹配@PoGibas抱歉,我不明白。哪张桌子?<代码> DT1 < /代码>中有一个错误吗?这里是我如何考虑这些间隔的重叠,从0到20 <代码> DT1 < /代码>只有三。intervals@PoGibas是的,这三个间隔相互重叠。在
dt1
中有三个间隔,即
[1,10]、[5,15]、[3,8]
。这有意义吗?所以我从你的问题中了解到想要的结果是
foverlaps(dt1,dt1)[,.N-1,(.start,end)]
-1
删除与自身的重叠)感谢这一点---我也更正了输入错误。你能解释一下你能不能创建
x
?我基本上是从
数据表中构建
c(0,1,3,5,8,10,15,20)
向量,然后用它自己“压缩”这个向量,得到
c(0,1),c(1,3),…,c(15,20)