Skip to content

Commit

Permalink
use filters arg to be more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
bschilder committed Jan 26, 2024
1 parent a104e24 commit 431c2d6
Show file tree
Hide file tree
Showing 41 changed files with 260 additions and 238 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ RoxygenNote: 7.3.1
Depends:
R (>= 4.1)
Imports:
HPOExplorer (>= 0.99.10),
HPOExplorer (>= 1.0.0),
KGExplorer,
orthogene,
data.table,
Expand Down Expand Up @@ -61,7 +61,8 @@ Suggests:
patchwork,
ggpubr,
igraph,
orthogene
ggdendro,
dendextend
Remotes:
github::neurogenomics/HPOExplorer,
github::neurogenomics/KGExplorer,
Expand Down
12 changes: 5 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export(agg_results)
export(correlation_heatmap)
export(create_dt)
export(ewce_para)
export(frequency_barplot)
export(frequency_histogram)
export(gen_overlap)
export(gen_results)
export(get_bg)
Expand All @@ -18,14 +16,16 @@ export(map_celltype)
export(map_tissue)
export(map_tissues)
export(merge_results)
export(multiewce_plot)
export(plot_bar_dendro)
export(plot_bar_summary)
export(plot_frequency_bar)
export(plot_frequency_histogram)
export(plot_ont_lvl)
export(plot_report)
export(prioritise_targets)
export(prioritise_targets_network)
export(report_plot)
export(subset_phenos)
export(subset_results)
export(summary_plot)
export(terminal_celltypes)
export(ttd_check)
import(GeneOverlap)
Expand All @@ -34,10 +34,8 @@ import(KGExplorer)
import(data.table)
import(ggplot2)
importFrom(EWCE,bootstrap_enrichment_test)
importFrom(EWCE,ewce_plot)
importFrom(HPOExplorer,add_ancestor)
importFrom(HPOExplorer,add_gene_frequency)
importFrom(HPOExplorer,filter_descendants)
importFrom(HPOExplorer,get_gene_lists)
importFrom(HPOExplorer,load_phenotype_to_genes)
importFrom(data.table,":=")
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@
- `prioritise_targets`
- `prioritise_targets_heatmap`
- `prioritise_targets_network`
- `report_plot`
- `plot_report`
- `correlation_heatmap`
- `frequency_bar`
- `frequency_histogram`
- `plot_frequency_histogram`

* Added `example_targets` data:
- Speeds up run time of examples/tests.
Expand Down
2 changes: 1 addition & 1 deletion R/add_ctd.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' \item{"mean_exp_quantiles"} Mean expression quantile per cell-type.
#' }
#' @inheritParams prioritise_targets
#' @inheritParams report_plot
#' @inheritParams plot_report
#' @inheritParams data.table::merge.data.table
#'
#' @export
Expand Down
4 changes: 2 additions & 2 deletions R/agg_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Aggregate enrichment results from generated by \link[MultiEWCE]{gen_results}.
#' Counts unique entries in \code{count_var} while grouping by \code{group_var}.
#' @param sep Separator for collapsed character columns.
#' @inheritParams summary_plot
#' @inheritParams plot_bar_summary
#' @inheritParams HPOExplorer::make_phenos_dataframe
#' @inheritParams HPOExplorer::make_network_object
#' @returns Aggregated \link[data.table]{data.table}
Expand All @@ -13,7 +13,7 @@
#' @importFrom stringr str_wrap
#' @import HPOExplorer
#' @examples
#' phenos <- subset_results(cell_type = "Microglia")
#' phenos <- subset_results(filters = list(CellType="Microglia"))
#' agg_res <- agg_results(phenos = phenos)
agg_results <- function(phenos,
count_var = "hpo_name",
Expand Down
23 changes: 8 additions & 15 deletions R/ggnetwork_plot_full.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@
#'
#' @param results The cell type-phenotype enrichment results generated by
#' \link[MultiEWCE]{gen_results}
#' and merged together with \link[MultiEWCE]{merge_results}.
#' @param cell_type The cell type of interest to be plotted.
#' Can be a single string (e.g. \code{"Astrocytes"}) or a character vector
#' of multiple cell types (e..g. \code{c("Astrocytes","Microglia")}).
#' Set to \code{NULL} if you wish to include all cell-types that are available
#' (after \code{q_threshold} and \code{fold_threshold} filtering).
#' If >1 cell-type remains, results will be aggregated automatically
#' such that there is only 1 node per phenotype.
#' and merged together with \link[MultiEWCE]{merge_results}
#' @param q_threshold The q value threshold to subset the \code{results} by.
#' @param fold_threshold The minimum fold change in specific expression
#' to subset the \code{results} by.
#' @inheritParams HPOExplorer::make_phenos_dataframe
#' @inheritParams HPOExplorer::make_network_plot
#' @inheritParams HPOExplorer::filter_descendants
#' @inheritParams HPOExplorer::make_network_object
#' @inheritParams KGExplorer::filter_dt
#' @inheritDotParams HPOExplorer::make_network_plot
#' @returns A named list of outputs,
#' including a interactive network plot of the selected subset
Expand All @@ -29,8 +23,8 @@
#' @import HPOExplorer
#' @import KGExplorer
#' @examples
#' res <- ggnetwork_plot_full(cell_type = "Microglia")
ggnetwork_plot_full <- function(cell_type,
#' res <- ggnetwork_plot_full(filters = list(CellType = "Microglia"))
ggnetwork_plot_full <- function(filters,
keep_descendants = NULL,
results = load_example_results(),
hpo = HPOExplorer::get_hpo(),
Expand All @@ -46,16 +40,15 @@ ggnetwork_plot_full <- function(cell_type,
verbose = TRUE,
...){
messager("ggnetwork_plot_full",v=verbose)
phenos <- subset_phenos(keep_descendants = keep_descendants,
phenos <- subset_phenos(filters = filters,
keep_descendants = keep_descendants,
results = results,
hpo = hpo,
cell_type = cell_type,
q_threshold = q_threshold,
fold_threshold = fold_threshold,
verbose = verbose)
phenos$hpo_name <- HPOExplorer::map_phenotypes(terms = phenos$hpo_id,
hpo = hpo,
to="name")
phenos <- HPOExplorer::add_hpo_name(phenos = phenos,
hpo = hpo)
#### Aggregate across multiple celltypes ####
phenos <- agg_results(phenos = phenos,
count_var = "CellType",
Expand Down
2 changes: 2 additions & 0 deletions R/map_celltype.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param rm_prefixes Prefixes to remove from cell type names in
#' \code{input_col} before performing mapping.
#' @param by Columns to merge on.
#' @param input_col Column to use for linking with the \code{map} data.
#' @inheritParams ggnetwork_plot_full
#' @export
#' @import data.table
Expand All @@ -25,6 +26,7 @@ map_celltype <- function(results,
if(all(new_cols %in% names(results))) {
return(results)
}
messager("Mapping cell types to cell ontology terms.")
results[,author_celltype:=gsub(paste(paste0("^",rm_prefixes,"_"),
collapse = "|"),"",
get(input_col),ignore.case = TRUE)]
Expand Down
7 changes: 5 additions & 2 deletions R/map_tissue.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ map_tissue <- function(results,
package = "MultiEWCE",
name="anatomy_maps")
){
ancestor <- ancestor_name <- tissue_ontology_term_id <- tissue <- n_tissues <-
n_ancestors <- cl_name <- NULL;
results <- map_celltype(results)
new_cols <- c("tissue_ontology_term_id","tissue")
if(all(new_cols %in% names(results))) {
return(results)
}
messager("Mapping cell types to tissue ontology terms.")
cl <- KGExplorer::get_ontology("cl")
#### Assign each tissue an ancestral group ####
uberon <- KGExplorer::get_ontology("uberon",
Expand Down Expand Up @@ -50,15 +53,15 @@ map_tissue <- function(results,
#### Create tissue-celltype heatmap #####

ggplot2::ggplot(map2,
ggplot2::aes(x=cell_type, y=ancestor_name,
ggplot2::aes(x=cl_name, y=ancestor_name,
fill=ancestor_name)
) +
ggplot2::geom_tile(show.legend = FALSE) +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90,
hjust = 1, vjust=0.5))


return(results_cl)
return(map2)
}


103 changes: 59 additions & 44 deletions R/multiewce_plot.R → R/plot_bar_dendro.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
#' EWCE plot
#' Plot bar dendrogram
#'
#' A shallow wrapper for the \link[EWCE]{ewce_plot} function.
#' @inheritParams EWCE::ewce_plot
#' @inheritParams HPOExplorer::add_ancestor
#' Create a plot summarising MultiEWCE results as a bar chart with multiple
#' facets and a cell ontology-based dendrogram.
#' @param ancestor_names Keep terms with specific ancestors.
#' @param celltype_col Name of the cell type column in the \code{results}.
#' @inheritParams ggnetwork_plot_full
#' @inheritParams HPOExplorer::add_ancestor
#' @inheritParams ggplot2::facet_wrap
#' @inheritDotParams EWCE::ewce_plot
#' @returns A bar chart with dendrogram of EWCE results in each cell type.
#'
#' @export
#' @importFrom EWCE ewce_plot
#' @examples
#' results <- load_example_results(multi_dataset=TRUE)
#' out <- multiewce_plot(results = results)
multiewce_plot <- function(results = load_example_results(multi_dataset = TRUE),
ancestor_names=c(
"Abnormality of the nervous system",
"Abnormality of the cardiovascular system",
"Abnormality of the immune system",
"Abnormality of the respiratory system",
"Abnormality of the eye",
"Abnormality of the endocrine system",
"Neoplasm"),
...) {
#' out <- plot_bar_dendro(results = results)
plot_bar_dendro <- function(results = load_example_results(multi_dataset = TRUE),
celltype_col = "cl_name",
ancestor_names=c(
"Abnormality of the nervous system",
"Abnormality of the cardiovascular system",
"Abnormality of the immune system",
"Abnormality of the respiratory system",
"Abnormality of the eye",
"Abnormality of the endocrine system",
"Neoplasm"),
facets = "ancestor_name",
...) {
requireNamespace("ggplot2")
requireNamespace("patchwork")
requireNamespace("dendextend")
requireNamespace("ggdendro")
hpo_id <- p <- fold_change <- cl_name <- ancestor_name <-
enriched_phenotypes_norm <- enriched_phenotypes <- top_ancestor_name <-
color <- NULL;

results <- map_celltype(results)
#### Get celltype dendrogram ####
Expand All @@ -35,27 +45,29 @@ multiewce_plot <- function(results = load_example_results(multi_dataset = TRUE),
#### Method 1 ####
# X <- KGExplorer::ontology_to(ont = cl,
# to="similarity")
# X <- X[unique(results$cell_type_ontology_term_id),
# unique(results$cell_type_ontology_term_id)]
# X <- X[unique(results$cl_id),
# unique(results$cl_id)]
# hc <- stats::hclust(stats::dist(X) )
# ddata <- ggdendro::dendro_data(hc)
#### Method 2 ####
# g <- KGExplorer::ontology_to(ont = cl,
# to="tbl_graph")
# KGExplorer::filter_graph(g, node_filters = list(name=results$cell_type_ontology_term_id))
# KGExplorer::filter_graph(g,
# node_filters = list(name=results$cl_id))

#### Method 3 ####
hc <- KGExplorer::ontology_to(ont = cl,
to="igraph_dist_hclust")
dend <- as.dendrogram(hc)
dend2 <- dendextend::prune(dend,
leaves=labels(dend)[!labels(dend) %in% results$cell_type_ontology_term_id]
)
dend <- stats::as.dendrogram(hc)
messager("Pruning dendrogram.")
dend2 <- dendextend::prune(
dend,
leaves=labels(dend)[!labels(dend) %in% results$cl_id])
ddata <- ggdendro::dendro_data(dend2)


ddata$labels$label <- KGExplorer::map_ontology_terms(ont = cl,
terms=ddata$labels$label)
terms=ddata$labels$label)
ggdend <-
ggdendro::ggdendrogram(ddata) +
ggplot2::scale_x_discrete(expand =c(.01, .01)) +
Expand All @@ -82,20 +94,21 @@ multiewce_plot <- function(results = load_example_results(multi_dataset = TRUE),
p=mean(p),
q=mean(q),
fold_change=mean(fold_change)),
by=c("CellType","cell_type","cell_type_ontology_term_id",
"ancestor","ancestor_name")]
dat <- dat[cell_type %in% unique(ddata$labels$label),]
dat <- dat[ancestor_name %in% ancestor_names,]
by=c(celltype_col,"ancestor","ancestor_name")]

dat <- dat[get(celltype_col) %in% unique(ddata$labels$label),]
dat <- KGExplorer::filter_dt(dat,
filters = list(ancestor_name=ancestor_names))

dat$cell_type <- factor(dat$cell_type,
levels=unique(ddata$labels$label),
ordered = TRUE)
dat[[celltype_col]] <- factor(dat[[celltype_col]],
levels=unique(ddata$labels$label),
ordered = TRUE)
#### Get the top HPO category for each cell type ####
### NOrmalise count within each ancestor
dat[,enriched_phenotypes_norm:=enriched_phenotypes/max(enriched_phenotypes),
dat[,enriched_phenotypes_norm:=(enriched_phenotypes/max(enriched_phenotypes)),
by=c("ancestor")]
dat[,top_ancestor_name:=head(ancestor_name[which.max(enriched_phenotypes_norm)],1),
by=c("cell_type","cell_type_ontology_term_id")]
by=c(celltype_col)]
# cellmeta <- data.table::data.table(
# cl@elementMetadata[,c("name","ancestor","ancestor_name")],
# key = "name"
Expand All @@ -105,36 +118,38 @@ multiewce_plot <- function(results = load_example_results(multi_dataset = TRUE),
columns = "top_ancestor_name",
preferred_palettes = "brewer.set2",
as="dict")[[1]]
dat2 <- unique(dat[,c("cell_type","top_ancestor_name")]
dat2 <- unique(dat[,c(celltype_col,"top_ancestor_name"), with=FALSE]
)[,color:=color_dict[top_ancestor_name]] |>
data.table::setkeyv("cell_type")
data.table::setkeyv(celltype_col)
color_vector <- dat2[unique(ddata$labels$label),]$color
color_vector[is.na(color_vector)] <- "grey20"
# color_vector <- KGExplorer::map_colors(unique(dat[,c("cell_type","top_ancestor_name")]),
# color_vector <- KGExplorer::map_colors(unique(dat[,c(celltype_col,"top_ancestor_name")]),
# columns = "top_ancestor_name",
# preferred_palettes = "brewer.set2",
# as="vector")[[1]]

ggbars <- ggplot2::ggplot(dat, ggplot2::aes(x=cell_type,
ggbars <- ggplot2::ggplot(dat, ggplot2::aes(x=!!ggplot2::sym(celltype_col),
y=enriched_phenotypes,
fill=ancestor_name)) +
ggplot2::geom_bar(stat="identity", show.legend = FALSE) +
ggplot2::scale_fill_manual(values=color_dict) +
ggplot2::facet_wrap(facets = "ancestor_name",
ggplot2::facet_wrap(facets = facets,
scales="free_y", ncol = 1) +
ggplot2::labs(x=NULL,y="Enriched phenotypes") +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(
angle = 90, hjust = 1, vjust=0.5,
color = unname(color_vector)
),
strip.background = ggplot2::element_rect(fill = "transparent")
strip.background = ggplot2::element_rect(fill = "transparent")
)

)

patchwork::wrap_plots(ggbars ,
ggp <- patchwork::wrap_plots(ggbars ,
ggdend + ggplot2::scale_y_reverse(),

ncol = 1, heights = c(1,.3))

#### Return ####
return(
list(data=dat,
plot=ggp)
)
}
Loading

0 comments on commit 431c2d6

Please sign in to comment.