[英]Counting overlaps as expected with R data.table foverlaps() or 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 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 的各種功能來執行此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
如何計算重疊間隔?
> # 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
第 5 個結果略有不同,但確實有 2 個重疊,因為 [8, 10] 與 [1, 10] 和 [5, 15] 重疊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.