Skip to content

Instantly share code, notes, and snippets.

@mtmorgan
Created June 24, 2015 18:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtmorgan/bd147505b89e42a151f9 to your computer and use it in GitHub Desktop.
Save mtmorgan/bd147505b89e42a151f9 to your computer and use it in GitHub Desktop.
dplyr / SummarizedExperiment compatibility layer -- half baked
## dplyr compatibility
as.data.frame.RangedSummarizedExperiment <-
function(x, row.names=NULL, optional=FALSE, ...)
{
colData <- colData(x)
rownames(colData) <- NULL
cbind(as.data.frame(rowRanges(x)[as.vector(row(x))]),
as.data.frame(colData[as.vector(col(x)),, drop=FALSE]),
sapply(assays(x), as.vector))
}
setMethod("as.data.frame", "RangedSummarizedExperiment",
as.data.frame.RangedSummarizedExperiment)
select_.RangedSummarizedExperiment <-
function(.data, ..., .dots)
{
dots <- lazyeval::all_dots(.dots, ...)
include <- c("seqnames", "start", "end", "strand")
rows <- rowRanges(x)
cols <- colData(x)
vars <- select_vars_(c(include, names(mcols(rows)), names(cols)),
dots, include)
mcols(rows) <- mcols(rows)[, names(mcols(rows)) %in% vars, drop=FALSE]
cols <- cols[, names(cols) %in% vars, drop=FALSE]
initialize(.data, rowRanges=rows, colData=cols)
}
filter_.RangedSummarizedExperiment <-
function(.data, ..., .dots)
{
dots <- lazyeval::all_dots(.dots, ..., all_named=TRUE)
## FIXME: report invalid '...'
## rows
i <- seq_len(nrow(.data))
df <- tbl_df(cbind(as.data.frame(rowRanges(.data)),
.__i__=seq_len(nrow(.data))))
for (d in dots)
tryCatch({
i <- intersect(i, filter_(df, d)$.__i__)
}, error=function(...) {})
## columns
j <- seq_len(ncol(.data))
df <- tbl_df(cbind(as.data.frame(colData(.data)),
.__j__=seq_len(ncol(.data))))
for (d in dots)
tryCatch({
j <- intersect(j, filter_(df, d)$.__j__)
}, error=function(...) {})
.data[i, j]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment