IRanges包
IRanges 包旨在表示序列、表示沿这些序列的索引的范围以及与这些范围相关的数据。 IRanges 使用游程编码来提供更高的性能。例如,序列 {1, 1, 1, 2, 3, 3} 可以表示为值 = {1, 2, 3},运行长度 = {3, 1, 2}。
IRange在处理基因或序列的空间关系上很方便。避免了在R中使用for循环。
An Overview of the IRanges package (bioconductor.org)
Bioconductor的地基--IRanges-阿里云开发者社区 (aliyun.com)
IRanges构建语法:IRanges(start=NULL, end=NULL, width=NULL, names=NULL)
library(IRanges)
rng <- IRanges(start=4, end=13)
x <- IRanges(start=c(4,7,2,20), end=c(13,7,5,23))
names(x) <- letters[1:4] #letter内置常量,小写字母,LETTER,大写字母,
class(x) # 查看类
#一些常用方法: start(), end(), width(),range()
# 算术、切片、逻辑操作
start(x) + 4
x[2:4]
x['a']
x[start(x) <4]
# merge
a <- IRanges(start=7, width=4)
b <- IRanges(start=2, end=5)
c(a,b)
x <- IRanges(start=c(40,80), end=c(67,114))
#IRanges object with 2 ranges and 0 metadata columns:
# start end width
# <integer> <integer> <integer>
# [1] 50 57 8
# [2] 90 104 15
x + 4L
x - 10L
# 这些运算从两端同时进行
# 截取部分区域,restrict()
y <- IRanges(start=c(4,6,10,12), width=13)
restrict(y,5,10)
# flank()可以提取每个range的两端部分,
flank(x,width=7)
# reduce() 相当于read组装
set.seed(0)
alns <- IRanges(start=sample(seq_len(50),20),width=5) #sample随机取样,seq_len产生范围数据
head(alns,4)
reduce(alns)
# gaps找出不同序列间隔,默认不关注the beginning of the sequences to the start position of the first ranges, same as end
gaps(alns)
# 集合操作:交集intersect, 差级setdiff, 合集union, 逐对操作,pintersect,psetdiff,punion,pgap
将区间可视化
ir <- IRanges(c(1, 8, 14, 15, 19, 34, 40),
width=c(12, 6, 6, 15, 6, 2, 7))
plotRanges <- function(x, xlim=x, main=deparse(substitute(x)),col="black", sep=0.5, ...)
{
height <- 1
if (is(xlim, "IntegerRanges"))
xlim <- c(min(start(xlim)), max(end(xlim)))
bins <- disjointBins(IRanges(start(x), end(x) + 1))
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)
}
plotRanges(ir)
网友评论