From f27c580ce9e9023d27fb0149a2a91c806dec35a4 Mon Sep 17 00:00:00 2001 From: "Brian M. Schilder" <34280215+bschilder@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:09:34 +0000 Subject: [PATCH] allow add_ancestors to set name --- R/add_ancestors.R | 23 ++++++++++++++--------- R/add_hoverboxes.R | 2 +- R/plot_clinvar.R | 5 +++-- R/plot_ontology.R | 3 +-- R/plot_ontology_circular.R | 3 ++- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/R/add_ancestors.R b/R/add_ancestors.R index adb5ef8..a68b7e8 100644 --- a/R/add_ancestors.R +++ b/R/add_ancestors.R @@ -14,17 +14,20 @@ #' @inheritParams simona::dag_offspring #' @examples #' ont <- get_ontology("hp") -#' ont2 <- add_ancestors(ont) +#' ont2 <- add_ancestors(ont, lvl=4) add_ancestors <- function(ont, lvl=2, include_self=TRUE, + prefix="ancestor", + fill_na=TRUE, force_new=FALSE){ - term <- ancestor <- NULL; + term <- NULL; if(is.null(lvl)) return (ont) messager("Adding ancestor metadata.") #### Check if ancestor metadata already present - if(all(c("ancestor","ancestor_name") %in% colnames(ont@elementMetadata)) && + prefix_name <- paste0(prefix,"_name") + if(all(c(prefix,prefix_name) %in% colnames(ont@elementMetadata)) && isFALSE(force_new)){ messager("Ancestor metadata already present.", "Use force_new=TRUE to overwrite.") @@ -39,16 +42,18 @@ add_ancestors <- function(ont, term=simona::dag_offspring(ont, term = x, include_self = include_self) ) - }) |> data.table::rbindlist(idcol = "ancestor", fill = TRUE) + }) |> data.table::rbindlist(idcol = prefix, fill = TRUE) #### Ensure one row per term #### ancestors_groups <- ancestors_groups[, .SD[1], keyby = "term"] - ancestors_groups <- ancestors_groups[ont@terms][is.na(ancestor), - ancestor:=term] - ont@elementMetadata$ancestor <- ancestors_groups$ancestor + if(isTRUE(fill_na)){ + ancestors_groups <- ancestors_groups[ont@terms][is.na(get(prefix)), + (prefix):=term] + } + ont@elementMetadata[[prefix]] <- ancestors_groups[[prefix]] #### Add ancestor_name col - ont@elementMetadata$ancestor_name <- map_ontology_terms( + ont@elementMetadata[[prefix_name]] <- map_ontology_terms( ont = ont, - terms = ont@elementMetadata$ancestor, + terms = ont@elementMetadata[[prefix]], to = "name") #### Return #### return(ont) diff --git a/R/add_hoverboxes.R b/R/add_hoverboxes.R index 0741d8d..d5db22e 100644 --- a/R/add_hoverboxes.R +++ b/R/add_hoverboxes.R @@ -17,7 +17,7 @@ #' @export #' @importFrom stringr str_wrap #' @examples -#' ont <- get_ontology("hp", terms=10) +#' ont <- get_ontology("hp") #' g <- ontology_to(ont, to="tbl_graph") #' g2 <- add_hoverboxes(g) add_hoverboxes <- function(g, diff --git a/R/plot_clinvar.R b/R/plot_clinvar.R index 201a848..9fa0e5a 100644 --- a/R/plot_clinvar.R +++ b/R/plot_clinvar.R @@ -5,8 +5,9 @@ plot_clinvar <- function(hits, x="region", y="N", fill="Type", - by=c("build","region","Type","ClinSigSimple"), - rows="ClinSigSimple"){ + rows="ClinSigSimple", + by=c("build",x,fill,rows) + ){ requireNamespace("ggplot2") requireNamespace("forcats") diff --git a/R/plot_ontology.R b/R/plot_ontology.R index c71f333..be3cf38 100644 --- a/R/plot_ontology.R +++ b/R/plot_ontology.R @@ -14,8 +14,7 @@ plot_ontology <- function(ont, "graphviz", "tidygraph", "visnetwork"), - ...){ - requireNamespace("DiagrammeR") + ...){ #### Filter ont #### ont <- filter_ontology(ont, terms=terms) diff --git a/R/plot_ontology_circular.R b/R/plot_ontology_circular.R index 98f031c..374fc47 100644 --- a/R/plot_ontology_circular.R +++ b/R/plot_ontology_circular.R @@ -4,6 +4,7 @@ #' @keywords internal plot_ontology_circular <- function(ont, ...){ + requireNamespace("DiagrammeR") simona::dag_circular_viz(ont, ...) -} \ No newline at end of file +}