Adding the functionality to enable limma and edgeR objects#16
Adding the functionality to enable limma and edgeR objects#16federicomarini wants to merge 12 commits into
Conversation
…rted/usable outside that context
…would be the placeholders
…s types - if rownames applies to all of them, then we could be good as we are
| get_expr_values <- function(de_container, gene, intgroup, assay, normalized) { | ||
| if (is(de_container, "DESeqDataSet")) { | ||
| if (assay == "counts") { | ||
| values <- counts(de_container, normalized = normalized) | ||
| } else { | ||
| values <- assay(de_container, assay) | ||
| } | ||
| intgroup_values <- colData(de_container)[, intgroup, drop = FALSE] | ||
| color_by_values <- colData(de_container)[, color_by, drop = FALSE] | ||
| } else if (is(de_container, "EList")) { | ||
| values <- de_container$E[gene, , drop = FALSE] | ||
| intgroup_values <- de_container$samples[, intgroup, drop = FALSE] | ||
| color_by_values <- de_container$samples[, color_by, drop = FALSE] | ||
| } else if (is(de_container, "DGEList")) { | ||
| values <- de_container$counts[gene, , drop = FALSE] | ||
| if (normalized) { | ||
| values <- cpm(de_container, normalized.lib.sizes = TRUE)[gene, , drop = FALSE] | ||
| } | ||
| intgroup_values <- de_container$samples[, intgroup, drop = FALSE] | ||
| color_by_values <- de_container$samples[, color_by, drop = FALSE] | ||
| } | ||
| df <- data.frame(exp_value = as.numeric(values), intgroup_values, color_by_values) | ||
| rownames(df) <- colnames(values) | ||
| return(df) | ||
| } |
There was a problem hiding this comment.
this actually needs to stay "out of the other function" as this can be called also in a standalone manner
| p <- ggplot(df, aes(x = .data$plotby, y = .data$exp_value, col = .data[[color_by]])) + |
There was a problem hiding this comment.
we could stick to the same "encoding", choosing either $var or the [[var]] notation
| p <- p + stat_summary( | ||
| fun = mean, geom = "line", aes(group = 1), | ||
| color = "grey80", linewidth = 0.8 | ||
| ) + | ||
| theme_bw() + | ||
| theme( | ||
| axis.title.x = element_text(size = rel(1.6)), | ||
| axis.title.y = element_text(size = rel(1.6)), | ||
| axis.text.x = element_text(size = rel(1.6)), | ||
| axis.text.y = element_text(size = rel(1.6)), | ||
| strip.text.x = element_text(size = rel(1.6)), | ||
| strip.text.y = element_text(size = rel(1.6)), | ||
| legend.text = element_text(size = rel(1.4)), | ||
| legend.title = element_text(size = rel(1.4)), | ||
| plot.title = element_text(size = rel(1.6)) | ||
| ) |
There was a problem hiding this comment.
We should keep this optional - in situations where we do not have a quantitative covariate to "link" groups together, it might be even a bit misleading IMO
|
|
||
| #' Get expression values | ||
| #' | ||
| #' Extract expression values, with the possibility to select other assay slots | ||
| #' | ||
| #' @param de_container An object containing the data for a Differential | ||
| #' Expression workflow (e.g. `DESeq2`, `edgeR` or `limma`). | ||
| #' Currently, this can be a `DESeqDataSet` object, normally obtained after | ||
| #' running your data through the `DESeq2` framework. | ||
| #' @param gene Character, specifies the identifier of the feature (gene) to be | ||
| #' extracted | ||
| #' @param intgroup A character vector of names in `colData(de_container)` to use for | ||
| #' grouping. | ||
| #' @param assay Character, specifies with assay of the `de_container` object to use for | ||
| #' reading out the expression values. Defaults to "counts". | ||
| #' @param normalized Logical value, whether the expression values should be | ||
| #' normalized by their size factor. Defaults to TRUE, applies when `assay` is | ||
| #' "counts" | ||
| #' | ||
| #' @return A tidy data.frame with the expression values and covariates for | ||
| #' further processing | ||
| #' | ||
| #' @export | ||
| #' | ||
| #' @importFrom DESeq2 counts estimateSizeFactors sizeFactors normalizationFactors | ||
| #' @importFrom SummarizedExperiment colData assays | ||
| #' | ||
| #' @examples | ||
| #' library("macrophage") | ||
| #' library("DESeq2") | ||
| #' library("org.Hs.eg.db") | ||
| #' library("AnnotationDbi") | ||
| #' | ||
| #' # dds object | ||
| #' data(gse, package = "macrophage") | ||
| #' dds_macrophage <- DESeqDataSet(gse, design = ~ line + condition) | ||
| #' rownames(dds_macrophage) <- substr(rownames(dds_macrophage), 1, 15) | ||
| #' keep <- rowSums(counts(dds_macrophage) >= 10) >= 6 | ||
| #' dds_macrophage <- dds_macrophage[keep, ] | ||
| #' # dds_macrophage <- DESeq(dds_macrophage) | ||
| #' | ||
| #' df_exp <- get_expr_values( | ||
| #' de_container = dds_macrophage, | ||
| #' gene = "ENSG00000125347", | ||
| #' intgroup = "condition" | ||
| #' ) | ||
| #' head(df_exp) | ||
| get_expr_values <- function(de_container, | ||
| gene, | ||
| intgroup, | ||
| assay = "counts", | ||
| normalized = TRUE) { | ||
| if (!(assay %in% names(assays(de_container)))) { | ||
| stop( | ||
| "Please specify a name of one of the existing assays: \n", | ||
| paste(names(assays(de_container)), collapse = ", ") | ||
| ) | ||
| } | ||
|
|
||
| # checking the normalization factors are in | ||
| if (is.null(sizeFactors(de_container)) & is.null(normalizationFactors(de_container))) { | ||
| de_container <- estimateSizeFactors(de_container) | ||
| } | ||
|
|
||
| if (assay == "counts") { | ||
| exp_vec <- counts(de_container, normalized = normalized)[gene, ] | ||
| } else { | ||
| exp_vec <- assays(de_container)[[assay]][gene, ] | ||
| } | ||
|
|
||
| exp_df <- data.frame( | ||
| exp_value = exp_vec, | ||
| colData(de_container)[intgroup] | ||
| ) | ||
|
|
||
| return(exp_df) | ||
| } |
There was a problem hiding this comment.
we should probably simply restore this and use it as it is inside the call to the plotting routine - but also, we need to dispatch on the object type
|
|
||
|
|
||
| # Function to get expression values based on the container type | ||
| get_expr_values <- function(de_container, gene, intgroup, assay, normalized) { |
There was a problem hiding this comment.
probably this also can benefit from having a specification on the color_by - are we using that info additionally at all?
Extending to include other classes - from limma and edgeR.
This is kept open as a way to track the progress on this, and to discuss implementation as it is ongoing