Skip to content

Instantly share code, notes, and snippets.

@lwaldron lwaldron/REexample.R
Created Jul 11, 2018

Embed
What would you like to do?
Example of RaggedExperiment::qreduceAssay
## ------------------------------------------------------------------------
library(GenomicRanges)
library(RaggedExperiment)
sample1 <- GRanges(
c(A = "chr1:1-10:-", B = "chr1:8-14:+", C = "chr2:15-18:+"),
score = 3:5)
sample2 <- GRanges(
c(D = "chr1:1-10:-", E = "chr2:11-18:+"),
score = 1:2)
colDat <- DataFrame(id = 1:2)
ragexp <- RaggedExperiment(sample1 = sample1,
sample2 = sample2,
colData = colDat)
query <- GRanges(c("chr1:1-14:-", "chr2:11-18:+"))
## ------------------------------------------------------------------------
weightedmean <- function(scores, ranges, qranges)
{
isects <- pintersect(ranges, qranges)
sum(scores * width(isects)) / sum(width(isects))
}
## ------------------------------------------------------------------------
(res <- qreduceAssay(ragexp, query, simplifyReduce = weightedmean))
## ------------------------------------------------------------------------
ragexp2 <- RaggedExperiment(sample1=GRanges(
c(A = "chr1:1-10:-", B = "chr1:11-14:-"),
score = 3:4))
## ------------------------------------------------------------------------
try(qreduceAssay(ragexp2, query, simplifyReduce = weightedmean))
## ------------------------------------------------------------------------
matrix(c((3 * 10 + 4 * 4) / 14, 0), ncol=1, dimnames=dimnames(res[, 1, drop=FALSE]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.