Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save sina-plot .rds files #17

Merged
merged 6 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: tfpscanner
Title: Transmission fitness polymorphism scanner
Version: 0.2.0
Date: 2022-11-09
Version: 0.2.1
Date: 2022-12-06
Author: Erik Volz, Olivia Boyd
Maintainer: Erik Volz <[email protected]>
Description: A pipeline for scanning a SARS-CoV-2 phylogeny for clades with outlying growth
Expand Down Expand Up @@ -39,5 +39,5 @@ Remotes:
SystemRequirements: libopenmpi-dev
Encoding: UTF-8
License: MIT + file LICENSE
RoxygenNote: 7.2.1
RoxygenNote: 7.2.2
Config/testthat/edition: 3
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# tfpscanner 0.2.1 _2022-12-06_

- "Cluster sina plot"s can be saved in either `html` (as an htmlwidget) or `rds` (as a ggplot2
object) files

# tfpscanner 0.2.0 _2022-11-09_

- Breaking change: `htmlwidget`s are no longer saved to file by `treeview()`
Expand Down
53 changes: 40 additions & 13 deletions R/plot_cluster_sina.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#' Create a sina plot against lineage
#'
#' Side-effect: this function creates a plot and stores it to a file.
#'
#' @param pldf Data-frame. The data element of a tree plot.
#' @param output_dir String. The directory within which the plot should be stored.
#' @param varx String. Which variable in \code{pldf} should be plotted on the y-axis.
#' @param mut_regexp String. Regular-expression(s) for restricting the plot to a subset of the rows
#' in \code{pldf}. Only rows with an "allmuts" entry that matches one of these regular expressions
#' will be presented. If NULL, all rows are presented.
#' @param lineage_regexp String. Regular-expression(s) for restricting the lineages that are
#' presented in the plot. Only those rows of \code{pldf} with a "lineage" entry that matches one
#' of these regular expressions will be presented. If NULL, all rows are presented.
#'
#' @return A \code{ggplot2} object storing the sina-cluster plot.

plot_cluster_sina <- function(pldf,
output_dir,
varx = "logistic_growth_rate",
mut_regexp = "S:A222V",
lineage_regexp = NULL) {
Expand All @@ -24,17 +22,46 @@ plot_cluster_sina <- function(pldf,
lineage_regexp = lineage_regexp
)

p1 <- create_cluster_sina_ggplot(sc1, y_lab = varx)
create_cluster_sina_ggplot(sc1, y_lab = varx)
}

g1 <- create_widget(
ggobj = p1,
width_svg = 8,
height_svg = 8
)
#' Save a sina-cluster-plot to a file
#'
#' Saves as either an htmlwidget (in a \code{html} file) or a ggplot object (in a \code{rds} file).
#'
#' @param ggobj \code{ggplot2} object. Contains the plot that is to be saved.
#' @param varx Scalar string. Which variable is depicted in the plot?
#' @param output_dir File path. The directory where the plot will be stored.
#' @param output_format Scalar string (either \code{rds} or \code{html}). In which format should
#' the plot be saved? Default: \code{rds}.
#' @param width_svg,height_svg The width and height of the plot (only used when
#' \code{output_format == "html"}).
#'
#' @return Invisibly returns the file path where the plot was saved

htmlwidgets::saveWidget(g1,
file = as.character(glue::glue("{output_dir}/sina-{varx}.html"))
)
save_sina_plot <- function(ggobj,
varx,
output_dir,
output_format = c("rds", "html"),
width_svg = 8,
height_svg = 8) {
output_format <- match.arg(output_format)

file_path <- file.path(output_dir, glue::glue("sina-{varx}.{output_format}"))

if (output_format == "rds") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this allow saving both as an rds and as an html?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Maybe not yet. If they have produced a cluster-sina ggplot object, then they can save that as an .rds or an .html by calling save_sina_plot(ggobj, ..., output_format = "rds") or save_sina_plot(ggobj, ..., output_format = "html").

But a typical user would be calling the treeview() workflow, rather than the internals. I can add a sina_output_format parameter to treeview() so that they can call treeview(input_env_rds, sina_output_format = "rds") or treeview(input_env_rds, sina_output_format = "html").

Will do that now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my misunderstanding. As both "html" and "rds" in the same runthrough? That isn't implemented here, but can easily be done. Give me a minute

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been rewritten. Now the user can save cluster-sina plots as either .rds, .html or both (default: both). And they can specify which format to output the cluster-sina plots in the arguments to treeview().

So treeview(input_env_rds) will output both .rds and .html versions of sina-logistic_growth_rate and sina-clock_outlier to "./treeview/".

saveRDS(ggobj, file = file_path)
} else {
g1 <- create_widget(
ggobj = ggobj,
width_svg = width_svg,
height_svg = height_svg
)

htmlwidgets::saveWidget(g1, file = file_path)
}

invisible(file_path)
}

#' Reformats data for a tree-plot for presentation in a sina plot
Expand Down
10 changes: 8 additions & 2 deletions R/treeview.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,19 @@ treeview <- function(e0,

suppressWarnings({
for (vn in unique(c("logistic_growth_rate", branch_cols))) {
plot_cluster_sina(
sina_plot <- plot_cluster_sina(
pldf,
output_dir = output_dir,
varx = vn,
mut_regexp = mutations,
lineage_regexp = lineages
)

save_sina_plot(
sina_plot,
varx = vn,
output_dir = output_dir,
output_format = "rds"
)
}
})

Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ generalised
ggiraph
ggplot
ggtree
htmlwidget
htmlwidgets
ide
Ile
Expand Down
8 changes: 4 additions & 4 deletions man/plot_cluster_sina.Rd

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

34 changes: 34 additions & 0 deletions man/save_sina_plot.Rd

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