Granges-(左)加入

时间:2018-08-06 17:51:00

标签: r bioconductor genomicranges

我有两个Granges对象,我希望将它们合并以合并两个GRanges,即使两个对象中都不存在元数据。

> t
GRanges object with 2 ranges and 5 metadata columns:
   seqnames         ranges strand |       pvalue      qvalue meth.diff           gc.X  gc.score
      <Rle>      <IRanges>  <Rle> |    <numeric>   <numeric> <numeric>      <GRanges> <numeric>
[1]       MT   [ 708,  708]      + | 2.898639e-04 0.007018699 0.2231039     MT:706-710        80
[2]       MT   [1147, 1147]      - | 6.043240e-05 0.003882324 0.2243177   MT:1146-1150        80
-------
seqinfo: 1 sequence from an unspecified genome; no seqlengths


> s
GRanges object with 1 range and 6 metadata columns:
  seqnames     ranges strand |       pvalue      qvalue meth.diff      gc.X     gc.name  gc.score
       <Rle>  <IRanges>  <Rle> |    <numeric>   <numeric> <numeric> <GRanges> <character> <numeric>
[1]       MT [708, 708]      + | 0.0002898639 0.007018699 0.2231039  MT:708:+  rs28412942         0
-------
seqinfo: 1 sequence from an unspecified genome; no seqlengths

我想在结尾处:

> combined
GRanges object with 2 ranges and 5 metadata columns:
   seqnames         ranges strand |       pvalue      qvalue meth.diff           gc.X  gc.score  gc.name
      <Rle>      <IRanges>  <Rle> |    <numeric>   <numeric> <numeric>      <GRanges> <numeric>
[1]       MT   [ 708,  708]      + | 2.898639e-04 0.007018699 0.2231039     MT:706-710        80   rs28412942
[2]       MT   [1147, 1147]      - | 6.043240e-05 0.003882324 0.2243177   MT:1146-1150        80   NA
-------
seqinfo: 1 sequence from an unspecified genome; no seqlengths

我尝试过的所有方法都要求具有相同的列数。 我可以尝试创建所需的列并用NA填充,但在我看来有点矫kill过正,我确定一种方法存在,但我找不到它:/

谢谢!

1 个答案:

答案 0 :(得分:0)

使用findOverlaps

很容易做到
library(GenomicRanges)  
m <- findOverlaps(s, t)

# Add gc.name to subject GRanges (i.e. left join)
mcols(t)$gc.name <- NA
mcols(t)[subjectHits(m), "gc.name"] = mcols(s)[queryHits(m), "gc.name"]
t
#GRanges object with 2 ranges and 6 metadata columns:
#      seqnames       ranges strand |    pvalue     qvalue meth.diff        gc.X
#         <Rle>    <IRanges>  <Rle> | <numeric>  <numeric> <numeric> <character>
#  [1]       MT [ 708,  708]      + | 0.3361535 0.06058539 0.4743142           a
#  [2]       MT [1147, 1147]      - | 0.4637233 0.19743361 0.3010486           b
#       gc.score     gc.name
#      <numeric> <character>
#  [1]        80  rs28412942
#  [2]        80        <NA>
#  -------
#  seqinfo: 1 sequence from an unspecified genome; no seqlengths

样本数据

set.seed(2018)
t <- GRanges(
    seqnames = c("MT", "MT"),
    IRanges(start = c(708, 1147), end = c(708, 1147)),
    strand = c("+", "-"),
    pvalue = runif(2),
    qvalue = runif(2),
    meth.diff = runif(2),
    gc.X = letters[1:2],
    gc.score = rep(80, 2))

set.seed(2018)
s <- GRanges(
    seqnames = "MT",
    IRanges(start = 708, end = 708),
    strand = "+",
    pvalue = runif(1),
    qvalue = runif(1),
    meth.diff = runif(1),
    gc.X = letters[3],
    gc.name = "rs28412942",
    gc.score = 0)