Skip to content

Commit

Permalink
add gpt_annot_plot_branches [\nocache]
Browse files Browse the repository at this point in the history
  • Loading branch information
bschilder committed May 22, 2024
1 parent 7907a3a commit ed4b070
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions R/gpt_annot_plot_branches.R
Original file line number Diff line number Diff line change
@@ -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))
}
45 changes: 45 additions & 0 deletions man/gpt_annot_plot_branches.Rd

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

Binary file added tests/testthat/Rplots.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/testthat/test-gpt_annot_plot_branches.R
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit ed4b070

Please sign in to comment.