美文网首页生物信息数据科学
43.《Bioinformatics Data Skills》之

43.《Bioinformatics Data Skills》之

作者: DataScience | 来源:发表于2021-07-12 20:33 被阅读0次

IRange可以寻找最近的两个范围和计算范围间的距离。


寻找最近范围

通过例子说明,首先建立query与subject范围对象:

> qry <- IRanges(start=6, end=13, name='query')
> sbj <- IRanges(start=c(2, 4, 18, 19), end=c(4, 5, 21, 24), names=1:4)
> qry
IRanges object with 1 range and 0 metadata columns:
            start       end     width
        <integer> <integer> <integer>
  query         6        13         8
> sbj
IRanges object with 4 ranges and 0 metadata columns:
        start       end     width
    <integer> <integer> <integer>
  1         2         4         3
  2         4         5         2
  3        18        21         4
  4        19        24         6

确定最近范围的函数有nearest, precede, follow,分别使用:

> nearest(qry, sbj)
[1] 2
> precede(qry, sbj)
[1] 3
> follow(qry, sbj)
[1] 2

三个函数的参数都是query对象与subject对象,它们都寻找与query的范围最近的subject的范围,返回结果代表最近subject范围坐标。区别在于(见图1):

  • nearest在所有subject范围中寻找与query最近的范围,
  • precede只寻找在query前方最近的subject范围(坐标轴右方为前),
  • follow只寻找query后方最近的subject范围。
  • 值得注意的是,nearest会返回与query重叠的subject范围,而precedefollow不会。
图1

由于nearest函数是向量化的,query对象可以包含多个范围,每个范围都会返回结果:

> qry <- IRanges(start=c(3,6), width=10, name='query')
> nearest(qry, sbj)
[1] 2 2

范围间距离

  1. distanceToNearest:返回与query最近的subject范围间的距离,例如

    > qry <- IRanges(sample(seq_len(1000), 5), width = 10)
    > sbj <- IRanges(sample(seq_len(1000), 5), width = 10)
    > qry
    IRanges object with 5 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]       776       785        10
      [2]       140       149        10
      [3]        73        82        10
      [4]       289       298        10
      [5]       764       773        10
    > sbj
    IRanges object with 5 ranges and 0 metadata columns:
              start       end     width
          <integer> <integer> <integer>
      [1]       920       929        10
      [2]       713       722        10
      [3]       292       301        10
      [4]       943       952        10
      [5]       204       213        10
    > distanceToNearest(qry, sbj)
    Hits object with 5 hits and 1 metadata column:
          queryHits subjectHits |  distance
          <integer>   <integer> | <integer>
      [1]         1           2 |        53
      [2]         2           5 |        54
      [3]         3           5 |       121
      [4]         4           3 |         0
      [5]         5           2 |        41
      -------
      queryLength: 5 / subjectLength: 5
    

    返回结果的形式与findOverlaps相似,前两列对应query坐标与subject坐标,第三列给出两个范围的距离。

  2. distance,此函数返回两个IRange对象配对范围间的距离:

    > distance(qry, sbj)
    [1] 134 563 209 644 550
    

    可以发现不重叠的两个范围间的距离结果为“(前一个范围的start - 后一个范围的end) -1”。那么两个范围如果是连续的,其距离应该是0,例如[2,3][4,5]间的距离是0:

    > distance(IRanges(2,3), IRanges(4,5))
    [1] 0
    

    查询说明文档,IRange是这样定义范围间的距离关系的:

    • x, y 重叠 => distance(x, y) == 0

    • 也就是说:x, y 相邻或者重叠 <=> distance(x, y) == 0

相关文章

网友评论

    本文标题:43.《Bioinformatics Data Skills》之

    本文链接:https://www.haomeiwen.com/subject/gvedpltx.html