IRangesパッケージはGenomicRangesパッケージのベースになっているパッケージです。このパッケージではunion()
とintersect()
という関数で領域の和と積を取ることができます。
x <- IRanges(c(1, 19, 34, 40), width=c(8, 6, 2, 7))
y <- IRanges(c(7, 15,42), width=c(6,15, 3))
> x
IRanges of length 4
start end width
[1] 1 8 8
[2] 19 24 6
[3] 34 35 2
[4] 40 46 7
> y
IRanges of length 3
start end width
[1] 7 12 6
[2] 15 29 15
[3] 42 44 3
上の図は以下のコードで描画しました。
plotRanges <- function(x, xlim = x, main = deparse(substitute(x)),
col = "black", sep = 0.5, ...)
{
height <- 1
if (is(xlim, "Ranges"))
xlim <- c(min(start(xlim)), max(end(xlim)))
bins <- disjointBins(IRanges(start(x), end(x) + 1)) # max(coverage(grngs))
plot.new()
plot.window(xlim, c(0, max(bins)*(height + sep)))
ybottom <- bins * (sep + height) - height
rect(start(x)-0.5, ybottom, end(x)+0.5, ybottom + height, col=col, ...)
title(main)
axis(1)
}
png("qiita-20130404-IRanges-1.png",width=640,height=320)
par(mfrow=c(2,1))
plotRanges(x,xlim=c(1,50), main="x", col="black")
plotRanges(y,xlim=c(1,50), main="y", col="black")
dev.off()
png("qiita-20130404-IRanges-2.png",width=640,height=320)
par(mfrow=c(2,1))
plotRanges(union(x,y),xlim=c(1,50), main="union")
plotRanges(intersect(x,y),xlim=c(1,50), main="intersect")
dev.off()