Skip to content

Adding the functionality to enable limma and edgeR objects#16

Draft
federicomarini wants to merge 12 commits into
develfrom
adding_limmaedgeR
Draft

Adding the functionality to enable limma and edgeR objects#16
federicomarini wants to merge 12 commits into
develfrom
adding_limmaedgeR

Conversation

@federicomarini

Copy link
Copy Markdown
Contributor

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

Comment thread R/gene_plot.R Outdated
Comment on lines +159 to +183
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)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this actually needs to stay "out of the other function" as this can be called also in a standalone manner

Comment thread R/gene_plot.R Outdated
Comment on lines +208 to +209
p <- ggplot(df, aes(x = .data$plotby, y = .data$exp_value, col = .data[[color_by]])) +

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could stick to the same "encoding", choosing either $var or the [[var]] notation

Comment thread R/gene_plot.R
Comment on lines +278 to +293
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))
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread R/gene_plot.R
Comment on lines -224 to -300

#' 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)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread R/gene_plot.R Outdated


# Function to get expression values based on the container type
get_expr_values <- function(de_container, gene, intgroup, assay, normalized) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably this also can benefit from having a specification on the color_by - are we using that info additionally at all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants