Created
July 11, 2018 11:37
-
-
Save lwaldron/d0502cb9f02ce0df4a6dfa0ee6b695ce to your computer and use it in GitHub Desktop.
Example of RaggedExperiment::qreduceAssay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## ------------------------------------------------------------------------ | |
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