Skip to content

Commit

Permalink
new feature for exploration of molecular features
Browse files Browse the repository at this point in the history
  • Loading branch information
pcastellanoescuder committed Jan 12, 2024
1 parent 74fe729 commit 315584f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ddh
Type: Package
Title: Utility Functions and Data Sets for Data-driven Hypothesis
Version: 0.3.21
Version: 0.3.22
Authors@R: c(
person(given = "Matthew",
family = "Hirschey",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export(make_male_anatogram)
export(make_metabolite_table)
export(make_metadata_cell)
export(make_molecular_features)
export(make_molecular_features_boxplots)
export(make_molecular_features_pathways)
export(make_molecular_features_pathways_table)
export(make_molecular_features_segments)
Expand Down
88 changes: 84 additions & 4 deletions R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,88 @@ make_molecular_features_pathways <- function(input = list(),
make_bomb_plot()})
}

## MOLECULAR FEATURES BOXPLOT ---------------------------------------------------
#' Molecular Features Boxplot
#'
#' Gene expression boxplot for the genes associated with the query ablation.
#'
#' @param input Expecting a list containing type and content variable.
#'
#' @return If no error, then returns a boxplot. If an error is thrown, then will return a bomb plot.
#'
#' @importFrom magrittr %>%
#'
#' @export
#' @examples
#' make_molecular_features_boxplots(input = list(type = 'gene', query = 'ROCK1', content = 'ROCK1'))
make_molecular_features_boxplots <- function(input = list(),
target_genes = NULL,
sex_select = NULL,
lineage_select = NULL,
lineage_subtype_select = NULL) {

gene_molecular_features_hits <- ddh::make_molecular_features_table(input = input)
gene_molecular_features_segments <- ddh::make_molecular_features_segments_table(input = input) %>%
dplyr::filter(group %in% c("sensitive", "resistant"))

make_molecular_features_boxplots_raw <- function() {

if (is.null(target_genes)) {
known_genes <- get_content("universal_proteins", dataset = TRUE)
target_genes <- gene_molecular_features_hits$Feature[gene_molecular_features_hits$Feature %in% known_genes$gene_name][1]

if (is.na(target_genes)) {stop("Provide a valid gene target")}
}

data_universal_expression_long <-
get_data_object(object_names = target_genes,
dataset_name = "universal_expression_long",
pivotwider = TRUE) %>%
dplyr::mutate(across(contains(c("gene_expression", "protein_expression")), as.numeric)) %>%
dplyr::filter(depmap_id %in% gene_molecular_features_segments$depmap_id)

plot_data <- data_universal_expression_long %>%
dplyr::left_join(gene_molecular_features_segments %>%
dplyr::select(depmap_id, group, cell_name, sex, lineage, lineage_subtype),
by = "depmap_id") %>%
dplyr::mutate(group = stringr::str_to_title(group)) %>%
dplyr::mutate(group = factor(group, levels = c("Resistant", "Sensitive")))

if (!is.null(sex_select)) {
plot_data <- plot_data %>%
dplyr::filter(sex %in% sex_select)
}
if (!is.null(lineage_select)) {
plot_data <- plot_data %>%
dplyr::filter(lineage %in% lineage_select)
}
if (!is.null(lineage_subtype_select)) {
plot_data <- plot_data %>%
dplyr::filter(lineage_subtype %in% lineage_subtype_select)
}

if (nrow(plot_data) < 1) {stop("No data for this query")}

plot_complete <- ggplot2::ggplot(plot_data, ggplot2::aes(id, gene_expression)) +
ggplot2::geom_boxplot(ggplot2::aes(fill = group), alpha = 0.8) +
ggplot2::labs(
x = NULL,
y = "Gene Expression",
fill = "Segment") +
theme_ddh() +
scale_fill_ddh_d(palette = input$type) +
ggplot2::theme(axis.text.x = ggplot2::element_text(size = 15))

return(plot_complete)
}

# error handling
tryCatch(make_molecular_features_boxplots_raw(),
error = function(e){
message(e)
make_bomb_plot()})
}

## CCA GENES PLOT ---------------------------------------------------
#' Co-essentiality Pathway Plot
#'
Expand All @@ -2064,8 +2146,7 @@ make_molecular_features_pathways <- function(input = list(),
make_cca_genes <- function(input = list(),
n_features = 5,
gset = NULL,
facet_by_geneset = FALSE,
...) {
facet_by_geneset = FALSE) {

gene_pathways_hits <- ddh::make_cca_genes_table(input = input, gene_set = gset)

Expand Down Expand Up @@ -2129,8 +2210,7 @@ make_cca_genes <- function(input = list(),
make_cca_pathways <- function(input = list(),
n_features = 10,
gset = NULL,
facet_by_geneset = FALSE,
...) {
facet_by_geneset = FALSE) {

pathway_pathways_hits <- ddh::make_cca_pathways_table(input = input, gene_set = gset)

Expand Down
6 changes: 4 additions & 2 deletions R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,8 @@ make_cca_genes_table <- function(input = list(),
dplyr::mutate(Pathway = stringr::str_replace_all(Pathway, "_", " ")) %>%
dplyr::mutate(Pathway = stringr::str_to_sentence(Pathway)) %>%
dplyr::select(Query = id, Pathway, "# Genes" = PathwaySize,
"Explained Variance" = PathwayVarExp, CC = CC1, "Gene Set")
"Explained Variance" = PathwayVarExp, CC = CC1, "Gene Set") %>%
dplyr::slice(1:20)

if(!is.null(gene_set)) {
if (!any(gene_set %in% gene_pathway_hits$`Gene Set`)) stop ("Gene set not found.")
Expand Down Expand Up @@ -983,7 +984,8 @@ make_cca_pathways_table <- function(input = list(),
"Explained Variance Query" = QueryVarExp,
"Explained Variance Pathway" = PathwayVarExp,
CC = CC1, "Query Gene Set" = Query_geneset) %>%
dplyr::select(-CCS)
dplyr::select(-CCS) %>%
dplyr::slice(1:20)

if(!is.null(gene_set)) {
if (!any(gene_set %in% pathway_pathway_hits$`Gene Set`)) stop ("Gene set not found.")
Expand Down
3 changes: 1 addition & 2 deletions man/make_cca_genes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/make_cca_pathways.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/make_molecular_features_boxplots.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 315584f

Please sign in to comment.