diff --git a/NAMESPACE b/NAMESPACE index 76f9c43..024c442 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -33,6 +33,7 @@ export(gpt_annot_class) export(gpt_annot_codify) export(gpt_annot_melt) export(gpt_annot_plot) +export(gpt_annot_plot_branches) export(gpt_annot_read) export(hpo_api) export(hpo_to_matrix) diff --git a/R/gpt_annot_plot_branches.R b/R/gpt_annot_plot_branches.R new file mode 100644 index 0000000..c887a85 --- /dev/null +++ b/R/gpt_annot_plot_branches.R @@ -0,0 +1,74 @@ +#' Plot annotations from GPT: by branch +#' +#' Plot annotations from GPT by ancestral HPO branch. +#' @param gpt_annot Output from \link{gpt_annot_read}. +#' @param metric Annotation metric to use (name of column in \code{gpt_annot}). +#' @param fill_lab Fill label in legend. +#' @param show_plot Show the plot. +#' @inheritParams gpt_annot_check +#' @inheritParams add_ont_lvl +#' @inheritParams add_ancestor +#' @param metric Annotation metric to plot. +#' @returns Named list of plot and data. +#' +#' @export +#' @examples +#' out <- gpt_annot_plot_branches() +gpt_annot_plot_branches <- function(hpo=get_hpo(), + gpt_annot = gpt_annot_read(hpo = hpo), + keep_descendants=NULL, + keep_ont_levels=NULL, + metric="congenital_onset", + fill_lab=gsub("_"," ",metric), + show_plot=TRUE){ + hpo_name <- n <- ancestor_name <- NULL; + + metric <- metric[1] + gpt_annot <- add_ancestor(gpt_annot, + keep_descendants = keep_descendants, + hpo = hpo) + gpt_annot <- add_ont_lvl(gpt_annot, + keep_ont_levels=keep_ont_levels, + hpo = hpo) + branches_dt <- gpt_annot[,list(n=data.table::uniqueN(hpo_name)), + by=c("ancestor_name",metric)] + branches_dt[,c("proportion_always", + "proportion_often"):=list( + sum(n[get(metric) %in% c("always")], na.rm = TRUE)/ + sum(n, na.rm = TRUE), + sum(n[get(metric) %in% c("often")], na.rm = TRUE)/ + sum(n, na.rm = TRUE) + ), + by=c("ancestor_name")] + data.table::setorderv(branches_dt,c("proportion_always","proportion_often"), + c(-1), na.last = TRUE) + branches_dt[,c(metric):=factor( + get(metric), + levels=c("always","often","rarely","never",NA), + ordered = TRUE)] + branches_dt[,ancestor_name:=factor( + ancestor_name, + levels=unique(branches_dt$ancestor_name), + ordered = TRUE)] +# +# ggstatsplot::ggbarstats(branches_dt[!is.na(get(metric))], +# x="ancestor_name", +# y="n",fill=metric) + p <- ggplot2::ggplot(branches_dt[!is.na(get(metric))], + ggplot2::aes(x=ancestor_name, + y=n, + fill=!!ggplot2::sym(metric))) + + ggplot2::geom_bar(stat = "identity",position = "fill") + + ggplot2::scale_fill_brewer(palette = "GnBu", direction = -1) + + ggplot2::scale_y_continuous(labels = scales::percent) + + ggplot2::labs(x="HPO ancestor", + y="Phenotypes", + fill=fill_lab) + + ggplot2::theme_bw() + + ggplot2::theme( + axis.text.x = ggplot2::element_text(angle = 90, hjust = 1, vjust = .5)) + + if(show_plot) methods::show(p) + return(list(data=branches_dt, + plot=p)) +} diff --git a/man/gpt_annot_plot_branches.Rd b/man/gpt_annot_plot_branches.Rd new file mode 100644 index 0000000..0403510 --- /dev/null +++ b/man/gpt_annot_plot_branches.Rd @@ -0,0 +1,45 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/gpt_annot_plot_branches.R +\name{gpt_annot_plot_branches} +\alias{gpt_annot_plot_branches} +\title{Plot annotations from GPT: by branch} +\usage{ +gpt_annot_plot_branches( + hpo = get_hpo(), + gpt_annot = gpt_annot_read(hpo = hpo), + keep_descendants = NULL, + keep_ont_levels = NULL, + metric = "congenital_onset", + fill_lab = gsub("_", " ", metric), + show_plot = TRUE +) +} +\arguments{ +\item{hpo}{Human Phenotype Ontology object, +loaded from \link[KGExplorer]{get_ontology}.} + +\item{gpt_annot}{Output from \link{gpt_annot_read}.} + +\item{keep_descendants}{Terms whose descendants should be kept +(including themselves). + Set to \code{NULL} (default) to skip this filtering step.} + +\item{keep_ont_levels}{Only keep phenotypes at certain \emph{absolute} +ontology levels to keep. +See \link{add_ont_lvl} for details.} + +\item{metric}{Annotation metric to plot.} + +\item{fill_lab}{Fill label in legend.} + +\item{show_plot}{Show the plot.} +} +\value{ +Named list of plot and data. +} +\description{ +Plot annotations from GPT by ancestral HPO branch. +} +\examples{ +out <- gpt_annot_plot_branches() +} diff --git a/tests/testthat/Rplots.pdf b/tests/testthat/Rplots.pdf new file mode 100644 index 0000000..a5f5778 Binary files /dev/null and b/tests/testthat/Rplots.pdf differ diff --git a/tests/testthat/test-gpt_annot_plot_branches.R b/tests/testthat/test-gpt_annot_plot_branches.R new file mode 100644 index 0000000..4c5b41f --- /dev/null +++ b/tests/testthat/test-gpt_annot_plot_branches.R @@ -0,0 +1,7 @@ +test_that("gpt_annot_plot_branches works", { + + out <- gpt_annot_plot_branches() + testthat::expect_true(methods::is(out$plot,"gg")) + testthat::expect_true(methods::is(out$dat,"data.table")) + testthat::expect_true(length(unique(out$dat$ancestor_name))<100) +})