Bioconductor学习R语言做生信生信必备生物知识

Bioconductor没想象的那么简单(part3)

2019-04-02  本文已影响23人  刘小泽

刘小泽写于19.4.2
主要看看IRanges的基本用法

上次写了关于RangeData的介绍:https://www.jianshu.com/p/9150213743d7

总的来说,主要会利用5个R包,来处理关于基因组区间(Genomic Range)的信息

Before diving into working with genomic ranges, we’re going to get our feet wet with generic ranges (i.e., ranges that represent a contiguous subsequence of elements over any type of sequence)

从简单的基因区间入手,就像批量跑流程一样,由小及大,逐渐培养区间思维"range thinking"

利用IRanges存储基因区间信息

IRanges包是GenomicRanges的一个依赖包,它会创建一个同名的对象,有两个基本的组成:起始和终止位点

# 例如:创建一个从4-13的区间
> rng <- IRanges(start = 4,end = 13)
> rng
IRanges object with 1 range and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         4        13        10

IRanges和GenomicRanges的坐标都是1-based系统,也可以给定任意的起始或终止坐标,并给出区间长度来创建

# 给定一个起始位置
> IRanges(start=4, width=3)
IRanges object with 1 range and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         4         6         3
# 给定一个终止位置
> IRanges(end=5, width=5)
IRanges object with 1 range and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         1         5         5

除了指定一个起始、终止位点,还可以使用向量创建多个区间:

> x <- IRanges(start=c(4, 7, 2, 20), end=c(13, 7, 5, 23))
> x
IRanges object with 4 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         4        13        10
  [2]         7         7         1
  [3]         2         5         4
  [4]        20        23         4

既然是一个range对象,那么其中的每个range都可以指定一个名称,使用names 函数

> names(x) <- paste0("gene",1:4)
> x
IRanges object with 4 ranges and 0 metadata columns:
            start       end     width
        <integer> <integer> <integer>
  gene1         4        17        14
  gene2         7        11         5
  gene3         2         9         8
  gene4        20        27         8

对于一个对象,最直接的查看它的内容就是通过str()[就像使用levels()查看因子型变量内容一样]

> str(x)
Formal class 'IRanges' [package "IRanges"] with 6 slots
  ..@ start          : int [1:4] 4 7 2 20
  ..@ width          : int [1:4] 14 5 8 8
  ..@ NAMES          : chr [1:4] "gene1" "gene2" "gene3" "gene4"
  ..@ elementType    : chr "ANY"
  ..@ elementMetadata: NULL
  ..@ metadata       : list()

有了起始、终止、区间,就可以抽出其中的任意部分,或者对其进行操作[操作过程和数据框很像]:

# 找到起始位点
> start(x)
[1]  4  7  2 20
# 找到终止位点
> end(x)
[1] 13  7  5 23
# # 找到区间长度
> width(x)
[1] 10  1  4  4
# 将所有的终止位点增加4
> end(x) <- end(x) + 4
> x
IRanges object with 4 ranges and 0 metadata columns:
            start       end     width
        <integer> <integer> <integer>
  gene1         4        17        14
  gene2         7        11         5
  gene3         2         9         8
  gene4        20        27         8
# 使用range()可以得到总区间
> range(x)
IRanges object with 1 range and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         2        27        26
# 找到起始位点小于5的
> x[start(x) < 5]
IRanges object with 2 ranges and 0 metadata columns:
            start       end     width
        <integer> <integer> <integer>
  gene1         4        17        14
  gene3         2         9         8
# 找到长度大于8的基因
> x[width(x) > 8]
IRanges object with 1 range and 0 metadata columns:
            start       end     width
        <integer> <integer> <integer>
  gene1         4        17        14
# 查看gene3的长度信息
> x['gene3']
IRanges object with 1 range and 0 metadata columns:
            start       end     width
        <integer> <integer> <integer>
  gene3         2         9         8
# 还可以轻松组合(merge)
> a <- IRanges(start=7, width=4)
> b <- IRanges(start=2, end=5)
> c(a,b)
IRanges object with 2 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         7        10         4
  [2]         2         5         4

上面可以看到,IRanges和数据框也差不了多少,而且生成的方式好像比数据框还要复杂一点,"存在即合理",既然一开始构建稍微麻烦一点,那么肯定他能做的事情也更多

IRanges结合基本运算、转换

欢迎关注我们的公众号~_~  
我们是两个农转生信的小硕,打造生信星球,想让它成为一个不拽术语、通俗易懂的生信知识平台。需要帮助或提出意见请后台留言或发送邮件到jieandze1314@gmail.com

Welcome to our bioinfoplanet!
上一篇 下一篇

猜你喜欢

热点阅读