Skip to content

Commit

Permalink
refac: function for pulling heatmap genotype data from ggtree object
Browse files Browse the repository at this point in the history
  • Loading branch information
russHyde committed Nov 7, 2022
1 parent a28f0d2 commit d630c3c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
26 changes: 26 additions & 0 deletions R/plot_tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,32 @@ append_interactivity_data <- function(ggobj,
ggobj
}

#' Extract the data about viral genotypes from a \code{ggtree} object
#'
#' @param ggobj A \code{ggtree} object, as generated by \code{append_interactivity_data}. The
#' \code{data} entry for this object should contain the columns "node", "label" and a column for
#' each of the \code{mut_regex} values.
#' @param n_leaves Scalar integer. The number of leaves in the tree.
#' @param mut_regex String. Regular expression defining the mutations under study here. This
#' should be a subset of the column-names in \code{ggobj$data}.
#'
#' @return Data-frame.

extract_genotype_data <- function(ggobj,
n_leaves,
mut_regex) {
genotype <- as.data.frame(
ggobj$data[
ggobj$data$node <= n_leaves,
c("label", mut_regex)
]
)
rownames(genotype) <- genotype$label
genotype <- genotype[, -1, drop = FALSE]

genotype
}

#' Adds a heatmap to the right of a ggtree object
#'
#' @param ggobj A ggtree object.
Expand Down
11 changes: 4 additions & 7 deletions R/treeview.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,11 @@ treeview <- function(e0,
mut_regex = mut_regex
)

genotype <- as.data.frame(
gtr1.1$data[
gtr1.1$data$node <= ape::Ntip(tr2),
c("label", mut_regex)
]
genotype <- extract_genotype_data(
ggobj = gtr1.1,
n_leaves = ape::Ntip(tr2),
mut_regex = mut_regex
)
rownames(genotype) <- genotype$label
genotype <- genotype[, -1, drop = FALSE]

gtr1.2 <- append_heatmap(
ggobj = gtr1.1,
Expand Down
24 changes: 24 additions & 0 deletions man/extract_genotype_data.Rd

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

0 comments on commit d630c3c

Please sign in to comment.