窗口GRanges的平均值

时间:2016-12-01 14:48:20

标签: r genomicranges

我试图找到GRanges对象中元列的某个窗口的平均值。

所以我有一个数据对象:

gr.windows
GRanges object with 6077 ranges and 0 metadata columns:
     seqnames                 ranges strand
        <Rle>              <IRanges>  <Rle>
 [1]     chr1       [     1, 100000]      *
 [2]     chr1       [100001, 200000]      *
 [3]     chr1       [200001, 300000]      *
 [4]     chr1       [300001, 400000]      *
 [5]     chr1       [400001, 500000]      *

和一个窗口对象:

gr.RleList <-  mcolAsRleList(gr.data, varname = "value")

gr.RleList
RleList of length 3
$chr1
numeric-Rle of length 249239887 with 5816335 runs
 Lengths:             10467                 1                 1                 1 ...                 1                 1                13                 2
 Values :                NA                 0                 1               0.5 ...                 0               NaN                NA               NaN

然后我将数据对象转换为RleList:

gr.binnedAvg <- binnedAverage(gr.windows, gr.RleList, "value")
gr.binnedAvg
GRanges object with 6077 ranges and 1 metadata column:
     seqnames                 ranges strand |      value
        <Rle>              <IRanges>  <Rle> | <numeric>
 [1]     chr1       [     1, 100000]      * |      <NA>
 [2]     chr1       [100001, 200000]      * |      <NA>
 [3]     chr1       [200001, 300000]      * |      <NA>
 [4]     chr1       [300001, 400000]      * |      <NA>
 [5]     chr1       [400001, 500000]      * |      <NA>

unique(mcols(gr.binnedAvg)$value)
[1] NA

如果我然后使用binnedAverage,我只获得NA值。

library(BSgenome.Hsapiens.UCSC.hg19)
library(data.table)

gr.windows <- tileGenome(seqinfo(Hsapiens), tilewidth=100000,cut.last.tile.in.chrom=TRUE)

gr.data <- GRanges(c("chr1", "chr2"), IRanges(c(10, 50), c(15, 55)), value = c(20, 10))
gr.data.RleList <-  mcolAsRleList(gr.data, varname = "value")
seqlevels(gr.windows, force=TRUE) <- names(gr.data.RleList)
gr.data.binnedAvg <- binnedAverage(gr.windows, gr.data.RleList, "value")

gr.data.binnedAvg
GRanges object with 4925 ranges and 1 metadata column:
     seqnames                 ranges strand |     value
        <Rle>              <IRanges>  <Rle> | <numeric>
 [1]     chr1       [     1, 100000]      * |      <NA>
 [2]     chr1       [100001, 200000]      * |         0
 [3]     chr1       [200001, 300000]      * |         0
 [4]     chr1       [300001, 400000]      * |         0

我也尝试用相同的结果来总结这些值。 我列表中的NA值是否会搞砸了,或者我的方法中有什么问题?

最小例子:

{{1}}

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

使用&#34;最小示例&#34;,以下作品:

library(BSgenome.Hsapiens.UCSC.hg19)
library(GenomicRanges)

gr.windows <- tileGenome(seqinfo(Hsapiens), tilewidth=100000, cut.last.tile.in.chrom=TRUE)
gr.data <- GRanges(c("chr1", "chr2"), IRanges(c(10, 50), c(15, 55)), value=c(20, 10))

gr.data.cov <- GenomicRanges::coverage(gr.data, weight="value")

seqlevels(gr.windows, pruning.mode="coarse") <- names(gr.data.cov)

gr.data.binnedAvg <- binnedAverage(gr.windows, gr.data.cov, "value")

延迟回复,但可能对将来参考有用。