|
a |
|
b/R/HighlightGenes.R |
|
|
1 |
#' Add Highlights for Genes on a Phylogenetic Tree |
|
|
2 |
#' |
|
|
3 |
#' This function adds highlights for specified genes on a phylogenetic tree object. |
|
|
4 |
#' |
|
|
5 |
#' @importFrom ggtree geom_point2 geom_hilight aes |
|
|
6 |
#' @importFrom dplyr filter select pull |
|
|
7 |
#' @importFrom rlang .data |
|
|
8 |
#' @param ggtree_obj A ggtree object representing the phylogenetic tree. |
|
|
9 |
#' @param genes_to_highlight A data frame containing gene names and corresponding colors to highlight. |
|
|
10 |
#' @param hilight_extend Numeric value indicating the extension length for highlights. |
|
|
11 |
#' @return A `ggtree` object with added highlights for specified genes. |
|
|
12 |
#' @export |
|
|
13 |
#' |
|
|
14 |
#' @examples |
|
|
15 |
#' plot_file <- system.file("extdata", "tree_plot.rds", package = "TransProR") |
|
|
16 |
#' p2_plot <- readRDS(plot_file) |
|
|
17 |
#' |
|
|
18 |
#' selected_genes_deseq2_file <- system.file("extdata", |
|
|
19 |
#' "selected_genes_deseq2.rds", |
|
|
20 |
#' package = "TransProR") |
|
|
21 |
#' selected_genes_deseq2 <- readRDS(selected_genes_deseq2_file) |
|
|
22 |
#' |
|
|
23 |
#' Diff_deseq2_file <- system.file("extdata", "Diff_deseq2.rds", package = "TransProR") |
|
|
24 |
#' Diff_deseq2 <- readRDS(Diff_deseq2_file) |
|
|
25 |
#' |
|
|
26 |
#' result_deseq2 <- gene_color(selected_genes_deseq2, Diff_deseq2, "#0000EE", "#fc4746") |
|
|
27 |
#' |
|
|
28 |
#' add_gene_highlights_p3 <- highlight_genes(p2_plot, result_deseq2, hilight_extend = 26) |
|
|
29 |
highlight_genes <- function(ggtree_obj, genes_to_highlight, hilight_extend = 18) { |
|
|
30 |
# Ensure that the first argument is a `ggtree` object |
|
|
31 |
if (!inherits(ggtree_obj, "ggtree")) { |
|
|
32 |
stop("The first argument must be a ggtree object.") |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
# Ensure that the second argument is a data frame |
|
|
36 |
if (!("data.frame" %in% class(genes_to_highlight))) { |
|
|
37 |
stop("The second argument must be a data frame.") |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
if (!requireNamespace("systemfonts", quietly = TRUE)) { |
|
|
41 |
stop("ggplot2 is required to use the function. Please install it.", call. = FALSE) |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
# Extract the data from the tree object and ensure it is a data frame |
|
|
45 |
tree_data <- as.data.frame(ggtree_obj$data) |
|
|
46 |
|
|
|
47 |
# Map gene names and colors to nodes in the tree and create geom_hilight and geom_point2 layers for each node |
|
|
48 |
highlight_commands <- lapply(1:nrow(genes_to_highlight), function(i) { |
|
|
49 |
gene <- genes_to_highlight$Symble[i] |
|
|
50 |
color <- genes_to_highlight$color[i] |
|
|
51 |
node <- dplyr::filter(tree_data, .data$label == gene) %>% |
|
|
52 |
dplyr::select(node) %>% |
|
|
53 |
dplyr::pull() |
|
|
54 |
if (!is.na(node)) { |
|
|
55 |
list( |
|
|
56 |
ggtree::geom_hilight(node = node, fill = color, alpha = 0.3, extend = hilight_extend), |
|
|
57 |
ggtree::geom_point2(ggtree::aes(subset = (.data$label == gene)), color = color, size = 2, alpha = 0.6) |
|
|
58 |
) |
|
|
59 |
} else { |
|
|
60 |
warning(paste("Gene", gene, "not found in the ggtree object.")) |
|
|
61 |
NULL |
|
|
62 |
} |
|
|
63 |
}) |
|
|
64 |
|
|
|
65 |
# Remove NULL elements from `highlight_commands` since they may exist |
|
|
66 |
highlight_commands <- Filter(Negate(is.null), highlight_commands) |
|
|
67 |
|
|
|
68 |
# Apply the commands to the `ggtree object` |
|
|
69 |
ggtree_obj <- ggtree_obj + do.call(c, highlight_commands) |
|
|
70 |
|
|
|
71 |
return(ggtree_obj) |
|
|
72 |
} |