R/correlation_heatmap.R

Defines functions correlation_heatmap

Documented in correlation_heatmap

#' Plot heatmap
#'
#' Plot a phenotype x phenotype correlation matrix based on genetic overlap.
#' @param top_targets \link[data.table]{data.table} of prioritised targets
#' generated by \link[MSTExplorer]{prioritise_targets}.
#'
#' @inheritParams HPOExplorer::make_phenos_dataframe
#' @inheritParams KGExplorer::plot_ontology_heatmap
#' @inheritParams KGExplorer::plot_
#' @returns Plot
#'
#' @export
#' @examples
#' top_targets <- MSTExplorer::example_targets$top_targets[seq(100),]
#' hm <- correlation_heatmap(top_targets = top_targets)
correlation_heatmap <- function(top_targets,
                                row_side_vars = c("ancestor_name",
                                                  "ontLvl"),
                                col_side_vars = c("n_celltypes",
                                                  "n_genes"),
                                row_labels = "hpo_name",
                                phenotype_to_genes=
                                  HPOExplorer::load_phenotype_to_genes(),
                                col = pals::gnuplot(),
                                show_plot = TRUE,
                                save_path = tempfile(
                                  fileext = "correlation_heatmap.pdf"),
                                height = 10,
                                width = height*1.3,
                                fontsize = 7,
                                seed = 2023,
                                verbose = TRUE
){
  requireNamespace("pals")
  if(!is.null(seed)) set.seed(seed)
  #### Create matrix ####
  X_cor <- HPOExplorer::hpo_to_matrix(terms = unique(top_targets$hpo_id),
                                      phenotype_to_genes = phenotype_to_genes,
                                      formula = "gene_symbol ~ hpo_id",
                                      run_cor = TRUE,
                                      as_sparse = FALSE,
                                      verbose = verbose)
  #### Create row/col annotations ####
  annot <- agg_results(phenos = top_targets,
                       group_var = unique(c("hpo_name","hpo_id",row_side_vars)),
                       count_var = "CellType",
                       verbose = verbose)[,utils::head(.SD, 1),
                                          keyby = "hpo_id"]
  annot <- annot[rownames(X_cor),]
  #### Create heatmap ####
  hm <- KGExplorer::plot_ontology_heatmap(
    ont=NULL,
    X = X_cor,
    annot = annot,
    row_labels = annot[[row_labels]],
    fontsize = fontsize,

    row_side_vars = row_side_vars,
    col_side_vars = col_side_vars,
    col = col,
    show_plot = show_plot,
    save_path = save_path,
    height = height,
    width = width)
  #### Return ####
  return(hm)
}
neurogenomics/MultiEWCE documentation built on May 7, 2024, 1:52 p.m.