Skip to content

Instantly share code, notes, and snippets.

@malcook
Created February 6, 2014 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcook/8836871 to your computer and use it in GitHub Desktop.
Save malcook/8836871 to your computer and use it in GitHub Desktop.
eXtensions to BioConductor's SummarizedExperiment framework, as proposed http://thread.gmane.org/gmane.science.biology.informatics.conductor/52971
subset.SummarizedExperiment<-function(x
,rowSubset=TRUE
,colSubset=TRUE
,assaySubset=TRUE
,drop=FALSE
,provenanceTrack=FALSE
,...) {
## PURPOSE: implement subsetting of SummarizedExperiments by
## rowSubset : expression in terms of the GRanges attributes (and
## its mcols meta-data) held in rowData
## colSubset : expression over the experimental meta-data held in
## the colData DataFrame
## assaySubset : character vector of the names of assays to be selected
## provenanceTrack : control whether the text of the subsetting
## should be noted in the exptData
## AUTHOR: malcolm_cook@stowers.org
x<-x[
eval(as.expression(substitute(rowSubset)),as.data.frame(rowData(x)),.GlobalEnv)
,eval(as.expression(substitute(colSubset)),colData(x),.GlobalEnv)
,drop=drop,...]
if (! identical(TRUE,assaySubset)) assays(x)<-assays(x)[assaySubset]
if(provenanceTrack) {
exptData(x)$rowSubset<-c(exptData(x)$rowSubset,as.character(substitute(rowSubset)))
exptData(x)$colSubset<-c(exptData(x)$colSubset,as.character(substitute(colSubset)))
exptData(x)$assaySubset<-c(exptData(x)$assaySubset,as.character(substitute(assaySubset)))
}
x
}
attr(subset,'ex')<-function() {
example(SummarizedExperiment)
assays(se1)$a2<-assays(se1)$counts*2
assays(se1)$a3<-assays(se1)$counts*3
benchmark(replications=100
,se1.ss1<-se1[start(rowData(se1))==344757,se1$Treatment=='ChIP']
,se1.ss2<-subset(se1,start==344757,Treatment=='ChIP')
)
stopifnot(identical(assays(se1.ss1),assays(se1.ss2)))
se1.ss3<-subset(se1,strand=='+',Treatment=='ChIP',c('a2','a3'))
stopifnot(identical(c('a2','a3'),names(assays(se1.ss3))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment