R生信相关

IRanges-R函数

2019-06-04  本文已影响0人  波波在敲代码

IRanges

v 1.0,2019.06.04,bobo,未完结

安装(install)

if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")
BiocManager::install("IRanges")

用法(usage)

## IRanges constructor:
IRanges(start=NULL, end=NULL, width=NULL, names=NULL)

## Supporting functions (not for the end user):
solveUserSEW0(start=NULL, end=NULL, width=NULL)
solveUserSEW(refwidths, start=NA, end=NA, width=NA,
             rep.refwidths=FALSE,
             translate.negative.coord=TRUE,
             allow.nonnarrowing=FALSE)

参数(arguments)

参数 说明
start 一个空向量或者整数向量,代表开始的位置
end 一个空向量或者整数向量,代表结束的位置
width 一个空向量或者整数向量,代表开始到结束的距离
names 空值或者是一组字符串向量,指定对应的开始到结束的内容
refwidths 包含参考宽度的正整数向量
rep.refwidths TRUE或者FALSE,只有refwidths为1时才支持TRUE
translate.negative.coord TRUE or FALSE
allow.nonnarrowing

构造(constructor)

返回包含了由开始、结束和宽度所构成IRanges对象。可以通过两种方式构造:

  1. start、end和width均由数字向量或空值构成,其中end = start + width -1,组成的三列矩阵包含数字(也有可能填充部分NAs)

  2. start属性值为逻辑向量或者是逻辑Rle对象,其生成的结果与as(start, "IRanges")的结果相同,需要注意的是,这样返回的IRanges实例是正常的。

需要注意的一点是names参数的值始终不会被回收。

示例(example)

注意:以下所有示例均需加载IRanges包!

library(IRanges)
  1. 基础示例:
IRanges(start=16, end=rep.int(20, 4))
>          start       end     width
>      <integer> <integer> <integer>
>  [1]        16        20         5
>  [2]        16        20         5
>  [3]        16        20         5
>  [4]        16        20         5
IRanges(start = c(5, 9, 6, 7),
        end = c(6, 8, 9, 12))
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         5         6         2
>  [2]         9         8         0
>  [3]         6         9         4
>  [4]         7        12         6
IRanges(start = c(5, 9, 6, 7),
        end = c(6, 10, 9, 12),
        name = c("A", "T", "G", "C"))

>        start       end     width
>    <integer> <integer> <integer>
>  A         5         6         2
>  T         9        10         1
>  G         6         9         4
>  C         7        12         6
IRanges(c(1, 10, 20), width=5)
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         1         5         5
>  [2]        10        14         5
>  [3]        20        24         5
  1. 基本操作

常用方法:

以start()为例说明start() ,end(),width()方法:

a <- IRanges(c(1, 10, 20), width=5)
a
>      <integer> <integer> <integer>
>  [1]         1         5         5
>  [2]        10        14         5
>  [3]        20        24         5
start(a) + 4 # 可以对start的值进行操作
a
>  5 14 24
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         1         5         5
>  [2]        10        14         5
>  [3]        20        24         5
start(a) <- start(a) + 4 #通过对start值进行更改从而更改整个数据
a
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         5         5         1
>  [2]        14        14         1
>  [3]        24        24         1

end(),width()的操作方法与start()类似。

range():

a <- IRanges(c(1, 10, 20), width=5)
a
range(a) # 统计数据的长度,从start的最小值到end的最大值
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         1        24        24

切片索引:通过一个向量按行进行索引:

a <- IRanges(c(1, 10, 20), width=5)
a
a[2:3] # 取2-3行为一个切片
>          start       end     width
>      <integer> <integer> <integer>
>  [1]        10        14         5
>  [2]        20        24         5

名字索引:通过name属性进行索引:

a <- IRanges(start = c(5, 9, 6, 7),
             end = c(6, 10, 9, 12),
             name = c("A", "T", "G", "T"))
a["T"] # 按名字切取第二行(尚不清楚为什么只切取了第一个符合条件的值)
>        start       end     width
>    <integer> <integer> <integer>
>  T         9        10         2

逻辑值索引:

a <- IRanges(c(1, 10, 20), width=5)
a
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         1         5         5
>  [2]        10        14         5
>  [3]        20        24         5
a[end(a) < 16] # 按逻辑值进行索引
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         1         5         5
>  [2]        10        14         5

算数加减:加减运算会同时在start和end段进行。

# 从两端同时进行运算
a <- IRanges(c(5, 10, 20), width=5)
a
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         5         9         5
>  [2]        10        14         5
>  [3]        20        24         5
a + 4
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         1        13        13
>  [2]         6        18        13
>  [3]        16        28        13
a - 2
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         7         7         1
>  [2]        12        12         1
>  [3]        22        22         1

整体加减shift:

library(IRanges)
a <- successiveIRanges(c(19, 5, 1, 2))
a
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         1        19        19
>  [2]        20        24         5
>  [3]        25        25         1
>  [4]        26        27         2
shift(a, shift=3) # shift会同时增减start和end,因此width保持不变
>          start       end     width
>      <integer> <integer> <integer>
>  [1]         4        22        19
>  [2]        23        27         5
>  [3]        28        28         1
>  [4]        29        30         2

截取范围restrict():

a <- IRanges(c(5, 10, 20), width=5)
restrict(a, 6, 22)
上一篇 下一篇

猜你喜欢

热点阅读