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

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

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

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

  • IRangesGenomicRanges:顾名思义,就是处理基因组区间信息 【GenomicRanges可以将染色体坐标范围、序列名称和正负链信息结合起来。许多Bioconductor包都高度依赖IRanges和GenomicRanges提供的底层数据结构】

  • GenomicFeatures:处理基因组上的gene model或其他序列特征(gene feature)信息(比如:genes、exons、UTRs、transcripts)

    A Gene Model is defined as any description of a gene product from a variety of sources including computational prediction, mRNA sequencing, or genetic characterization.
    The gene feature is meant to approximately cover the region of nucleic acid considered by workers in the field to be the gene.

  • Biostrings、BSgenome:处理序列,比如提取子集【Biostrings包含了序列比对、模式匹配等基本序列分析函数;BSgenome针对有注释的全基因组数据进行操作】

  • rtracklayer:读取像BED、GTF、WIG这样的文件 【rtracklayer包将数据导入USCS基因组浏览器(http://genome-asia.ucsc.edu/)进行浏览、操作、输出】

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结合基本运算、转换

  • 改变序列两侧范围:比如想在一个编码区域上、下游加一段序列

    # 现有的编码区假设是这样
    > x <- IRanges(start=c(40, 80), end=c(67, 114))
    > x
    IRanges object with 2 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]        40        67        28
      [2]        80       114        35
    # 上、下游同时增加4bp
    > x+4L
    IRanges object with 2 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]        36        71        36
      [2]        76       118        43
    # 看一下变化:start和end都同时向自己的方向增加了4bp
    
  • 改变中间:有时我们主要想保留序列中间的某一段区域,而不是增加/缩减两端序列

    # 原始序列范围
    > y <- IRanges(start=c(4, 6, 10, 12), width=13)
    > y
    IRanges object with 4 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]         4        16        13
      [2]         6        18        13
      [3]        10        22        13
      [4]        12        24        13
    # 现在只想保留y的所有基因的5-13bp区域(因为每个基因的起始、终止都不同,如果基因起始位点在5之前的,截取后就从5开始;如果起始位置在5之后的,截取后就从现在的位置开始;终止位置也是如此)
    > restrict(y,5,13)
    IRanges object with 4 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]         5        13         9
      [2]         6        13         8
      [3]        10        13         4
      [4]        12        13         2
    
  • 获得两端部分(flank),比如想得到左侧:转录起始位点( transition start site,TSS);右侧:转录终止位点(transcription termination site, TTS)

    > x
    IRanges object with 2 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]        40        67        28
      [2]        80       114        35
    # 现在要获得左侧7bp的起始位点(flank默认是计算上游)
    > flank(x,width = 7)
    IRanges object with 2 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]        33        39         7
      [2]        73        79         7
    # 如果要计算下游的终止位点(就要设置start=FALSE)
    
  • 如果ranges对象中包含多个基因,并且它们的区间存在重叠,比如基因1是10-15bp,gene2是15-18bp,那么我们想看总体覆盖的区间,也就是10-18bp。这时使用reduce函数,将重叠的区域进行缩减

    #模拟数据(随机产生20个起始位点,然后区间长度为5)
    > set.seed(12)
    > alns <- IRanges(start=sample(seq_len(50), 20), width=5)
    > head(alns, 4)
    IRanges object with 4 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]         4         8         5
      [2]        41        45         5
      [3]        46        50         5
      [4]        13        17         5
    # 将重叠区域缩减,最后的总覆盖是1-26和28-54
    > reduce(alns)
    IRanges object with 2 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]         1        26        26
      [2]        28        54        27
    
  • 上面已经将多个区域缩减到了几个大区域,怎么找这几个大区域的位置呢?就好像通过基因区域找到基因间区一样,利用gap函数得到(从前一个大区域的结尾到后一个大区域的开头,这算一个gap)

    > gaps(alns)
    IRanges object with 1 range and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]        27        27         1
    # 因为上面只得到两组大区域(可以想象成两个基因区域),然后找基因间区,就是27bp这个位置了
    
  • 可以寻找两个IRange对象的交集、补集、并集

    > a <- IRanges(start=4, end=13)
    > b <- IRanges(start=12, end=17)
    # 求a、b交集
    > intersect(a, b)
    IRanges object with 1 range and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]        12        13         2
    # 求a对b的补集(就是a中有b中没有的部分);颠倒参数就是求b对
    # a补集
    > setdiff(a,b)
    IRanges object with 1 range and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]         4        11         8
    # 并集是union
    

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

Welcome to our bioinfoplanet!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,869评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,716评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,223评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,047评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,089评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,839评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,516评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,410评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,920评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,052评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,179评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,868评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,522评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,070评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,186评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,487评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,162评论 2 356

推荐阅读更多精彩内容