|
a |
|
b/R/CircosFruits.R |
|
|
1 |
#' Create a base plot with gene expression data on a phylogenetic tree |
|
|
2 |
#' |
|
|
3 |
#' This function creates a base plot using 'ggtree' and 'ggtreeExtra' libraries, adding gene expression |
|
|
4 |
#' data as colored tiles to the plot. It allows for dynamic coloring of the genes and includes |
|
|
5 |
#' adjustments for alpha transparency based on the expression value. |
|
|
6 |
#' |
|
|
7 |
#' @importFrom ggplot2 aes scale_fill_manual scale_alpha_continuous guide_legend |
|
|
8 |
#' @param p A ggtree plot object to which the data will be added. |
|
|
9 |
#' @param data A data frame containing gene expression data with columns for Samples, Genes, and Values. |
|
|
10 |
#' @param gene_colors A named vector of colors for genes. |
|
|
11 |
#' @param gene_label A character string used as a label in the legend for the genes. Default is "Gene". |
|
|
12 |
#' @return A `ggtree` plot object with the gene expression data added. |
|
|
13 |
#' @export |
|
|
14 |
#' |
|
|
15 |
#' @examples |
|
|
16 |
#' \donttest{ |
|
|
17 |
#' # Check and load required packages |
|
|
18 |
#' if (requireNamespace("ggtreeExtra", quietly = TRUE) && |
|
|
19 |
#' requireNamespace("ggplot2", quietly = TRUE)) { |
|
|
20 |
#' library(ggtreeExtra) |
|
|
21 |
#' library(ggplot2) |
|
|
22 |
#' |
|
|
23 |
#' file_path <- system.file("extdata", "p_tree_test.rds", package = "TransProR") |
|
|
24 |
#' p <- readRDS(file_path) |
|
|
25 |
#' |
|
|
26 |
#' # Create gene expression data frame |
|
|
27 |
#' expression_data <- data.frame( |
|
|
28 |
#' Sample = rep(c("Species_A", "Species_B", "Species_C", "Species_D"), each = 5), |
|
|
29 |
#' Gene = rep(paste0("Gene", 1:5), times = 4), |
|
|
30 |
#' Value = runif(20, min = 0, max = 1) # Randomly generate expression values between 0 and 1 |
|
|
31 |
#' ) |
|
|
32 |
#' |
|
|
33 |
#' # Define gene colors (named vector) |
|
|
34 |
#' gene_colors <- c( |
|
|
35 |
#' Gene1 = "#491588", |
|
|
36 |
#' Gene2 = "#301b8d", |
|
|
37 |
#' Gene3 = "#1a237a", |
|
|
38 |
#' Gene4 = "#11479c", |
|
|
39 |
#' Gene5 = "#0a5797" |
|
|
40 |
#' ) |
|
|
41 |
#' |
|
|
42 |
#' # Call create_base_plot function to add gene expression data |
|
|
43 |
#' p <- create_base_plot(p, expression_data, gene_colors) |
|
|
44 |
#' } else { |
|
|
45 |
#' message("Required packages 'ggtreeExtra' and 'ggplot2' are not installed.") |
|
|
46 |
#' } |
|
|
47 |
#' } |
|
|
48 |
#' |
|
|
49 |
create_base_plot <- function(p, data, gene_colors, gene_label="Gene") { |
|
|
50 |
# Define local variables |
|
|
51 |
Sample <- data$Sample |
|
|
52 |
value <- data$value |
|
|
53 |
Gene <- data$Gene |
|
|
54 |
if (!requireNamespace("ggtreeExtra", quietly = TRUE)) { |
|
|
55 |
stop("ggtreeExtra is required for using create_base_plot. Please install it.", call. = FALSE) |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
if (!requireNamespace("ggplot2", quietly = TRUE)) { |
|
|
59 |
stop("ggplot2 is required to use geom_tile. Please install it.", call. = FALSE) |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
p <- p + |
|
|
63 |
ggtreeExtra::geom_fruit( |
|
|
64 |
data=data, |
|
|
65 |
geom="geom_tile", |
|
|
66 |
mapping=ggplot2::aes(y=Sample, alpha=value, x=Gene, fill=Gene), |
|
|
67 |
offset=0.001, |
|
|
68 |
pwidth=2 |
|
|
69 |
) + |
|
|
70 |
ggplot2::scale_fill_manual( |
|
|
71 |
name=gene_label, |
|
|
72 |
values=gene_colors, |
|
|
73 |
guide=ggplot2::guide_legend(keywidth=0.65, keyheight=0.35, order=1) |
|
|
74 |
) + |
|
|
75 |
# Assuming the function 'adjust_alpha_scale' is defined elsewhere to adjust alpha scale based on the expression values |
|
|
76 |
adjust_alpha_scale(data, gene_label) |
|
|
77 |
return(p) |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
#' Add a boxplot layer to a `ggtree` plot |
|
|
83 |
#' |
|
|
84 |
#' This function adds a boxplot layer to an existing `ggtree` plot object using ggtreeExtra's geom_fruit for boxplots. |
|
|
85 |
#' It is primarily used to display statistical summaries of the data related to gene expressions or other metrics. |
|
|
86 |
#' |
|
|
87 |
#' @importFrom ggplot2 aes |
|
|
88 |
#' @param p An existing ggtree plot object. |
|
|
89 |
#' @param data A data frame containing the data to be plotted. Expected to have columns for 'Sample' and 'value'. |
|
|
90 |
#' @param fill_color A character string specifying the fill color for the boxplots. Default is "#f28131". |
|
|
91 |
#' @param alpha Numeric value for the transparency of the boxplots. Default is 0.6. |
|
|
92 |
#' @param offset Numeric value, the position of the boxplot on the x-axis relative to its gene name. Default is 0.22. |
|
|
93 |
#' @param pwidth Numeric value, the width of the boxplot. Default is 0.5. |
|
|
94 |
#' @return A `ggtree` plot object with the added boxplot layer. |
|
|
95 |
#' @export |
|
|
96 |
#' |
|
|
97 |
#' @examples |
|
|
98 |
#' \donttest{ |
|
|
99 |
#' # Check and load required packages |
|
|
100 |
#' if (requireNamespace("ggtreeExtra", quietly = TRUE) && |
|
|
101 |
#' requireNamespace("ggplot2", quietly = TRUE)) { |
|
|
102 |
#' library(ggtreeExtra) |
|
|
103 |
#' library(ggplot2) |
|
|
104 |
#' |
|
|
105 |
#' file_path <- system.file("extdata", "p_tree_test.rds", package = "TransProR") |
|
|
106 |
#' p <- readRDS(file_path) |
|
|
107 |
#' |
|
|
108 |
#' # Create boxplot data frame |
|
|
109 |
#' boxplot_data <- data.frame( |
|
|
110 |
#' Sample = rep(c("Species_A", "Species_B", "Species_C", "Species_D"), each = 30), |
|
|
111 |
#' value = c( |
|
|
112 |
#' rnorm(30, mean = 5, sd = 1), # Data for Species_A |
|
|
113 |
#' rnorm(30, mean = 7, sd = 1.5), # Data for Species_B |
|
|
114 |
#' rnorm(30, mean = 6, sd = 1.2), # Data for Species_C |
|
|
115 |
#' rnorm(30, mean = 8, sd = 1.3) # Data for Species_D |
|
|
116 |
#' ) |
|
|
117 |
#' ) |
|
|
118 |
#' |
|
|
119 |
#' # Call add_boxplot function to add boxplot layer |
|
|
120 |
#' p_with_boxplot <- add_boxplot(p, boxplot_data) |
|
|
121 |
#' } else { |
|
|
122 |
#' message("Required packages 'ggtreeExtra' and 'ggplot2' are not installed.") |
|
|
123 |
#' } |
|
|
124 |
#' } |
|
|
125 |
#' |
|
|
126 |
add_boxplot <- function(p, data, fill_color="#f28131", alpha=0.6, offset=0.22, pwidth=0.5) { |
|
|
127 |
# Define local variables |
|
|
128 |
Sample <- data$Sample |
|
|
129 |
value <- data$value |
|
|
130 |
if (!requireNamespace("ggtreeExtra", quietly = TRUE)) { |
|
|
131 |
stop("ggtreeExtra is required for using create_base_plot. Please install it.", call. = FALSE) |
|
|
132 |
} |
|
|
133 |
|
|
|
134 |
if (!requireNamespace("ggplot2", quietly = TRUE)) { |
|
|
135 |
stop("ggplot2 is required to use geom_boxplot. Please install it.", call. = FALSE) |
|
|
136 |
} |
|
|
137 |
|
|
|
138 |
p + ggtreeExtra::geom_fruit( |
|
|
139 |
data=data, |
|
|
140 |
geom="geom_boxplot", |
|
|
141 |
mapping=ggplot2::aes(y=Sample, x=value), |
|
|
142 |
fill=fill_color, |
|
|
143 |
alpha=alpha, |
|
|
144 |
offset=offset, |
|
|
145 |
pwidth=pwidth, |
|
|
146 |
size=0.05, |
|
|
147 |
outlier.size=0.3, |
|
|
148 |
outlier.stroke=0.06, |
|
|
149 |
outlier.shape=21, |
|
|
150 |
show.legend=FALSE |
|
|
151 |
) |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
#' Add a new tile layer with dynamic scales to a `ggtree` plot |
|
|
158 |
#' |
|
|
159 |
#' This function adds a new tile layer to an existing `ggtree` plot object, allowing for separate scales for fill |
|
|
160 |
#' and alpha transparency. This is useful when you want to add additional data layers without interfering with |
|
|
161 |
#' the existing scales in the plot. It utilizes the 'ggnewscale' package to reset scales for new layers. |
|
|
162 |
#' |
|
|
163 |
#' @importFrom ggplot2 aes scale_fill_manual guide_legend |
|
|
164 |
#' @importFrom ggnewscale new_scale |
|
|
165 |
#' @param p An existing ggtree plot object. |
|
|
166 |
#' @param data A data frame containing the data to be plotted. Expected to have columns for 'Sample', 'Gene', and 'value'. |
|
|
167 |
#' @param gene_colors A named vector of colors for genes. |
|
|
168 |
#' @param gene_label A character string used as a label in the legend for the genes. |
|
|
169 |
#' @param alpha_value A numeric or named vector for setting the alpha scale based on values. |
|
|
170 |
#' @param offset Numeric value, the position of the tile on the x-axis relative to its gene name. Default is 0.02. |
|
|
171 |
#' @param pwidth Numeric value, the width of the tile. Default is 2. |
|
|
172 |
#' @return A `ggtree` plot object with the added tile layer and new scales. |
|
|
173 |
#' @export |
|
|
174 |
#' |
|
|
175 |
#' @examples |
|
|
176 |
#' \donttest{ |
|
|
177 |
#' # Check and load required packages |
|
|
178 |
#' if (requireNamespace("ggtreeExtra", quietly = TRUE) && |
|
|
179 |
#' requireNamespace("ggplot2", quietly = TRUE) && |
|
|
180 |
#' requireNamespace("ggnewscale", quietly = TRUE)) { |
|
|
181 |
#' library(ggtreeExtra) |
|
|
182 |
#' library(ggplot2) |
|
|
183 |
#' library(ggnewscale) |
|
|
184 |
#' |
|
|
185 |
#' file_path <- system.file("extdata", "p_tree_test.rds", package = "TransProR") |
|
|
186 |
#' p <- readRDS(file_path) |
|
|
187 |
#' |
|
|
188 |
#' # Create new expression data |
|
|
189 |
#' new_expression_data <- data.frame( |
|
|
190 |
#' Sample = rep(c("Species_A", "Species_B", "Species_C", "Species_D"), each = 3), |
|
|
191 |
#' Gene = rep(c("Gene6", "Gene7", "Gene8"), times = 4), |
|
|
192 |
#' Value = runif(12, min = 0, max = 1) # Randomly generate expression values between 0 and 1 |
|
|
193 |
#' ) |
|
|
194 |
#' |
|
|
195 |
#' # Define new gene colors |
|
|
196 |
#' new_gene_colors <- c( |
|
|
197 |
#' Gene6 = "#0b5f63", |
|
|
198 |
#' Gene7 = "#074d41", |
|
|
199 |
#' Gene8 = "#1f5e27" |
|
|
200 |
#' ) |
|
|
201 |
#' |
|
|
202 |
#' # Define gene label and alpha values |
|
|
203 |
#' gene_label <- "New Genes" |
|
|
204 |
#' alpha_value <- c(0.3, 0.9) |
|
|
205 |
#' |
|
|
206 |
#' # Add new tile layer |
|
|
207 |
#' p_with_new_layer <- add_new_tile_layer( |
|
|
208 |
#' p, |
|
|
209 |
#' new_expression_data, |
|
|
210 |
#' new_gene_colors, |
|
|
211 |
#' gene_label, |
|
|
212 |
#' alpha_value, |
|
|
213 |
#' offset = 0.02, |
|
|
214 |
#' pwidth = 2 |
|
|
215 |
#' ) |
|
|
216 |
#' } else { |
|
|
217 |
#' message("Required packages 'ggtreeExtra', 'ggplot2', and 'ggnewscale' are not installed.") |
|
|
218 |
#' } |
|
|
219 |
#' } |
|
|
220 |
#' |
|
|
221 |
add_new_tile_layer <- function(p, data, gene_colors, gene_label, alpha_value=c(0.3, 0.9), offset=0.02, pwidth=2) { |
|
|
222 |
# Define local variables |
|
|
223 |
Sample <- data$Sample |
|
|
224 |
value <- data$value |
|
|
225 |
Gene <- data$Gene |
|
|
226 |
if (!requireNamespace("ggtreeExtra", quietly = TRUE)) { |
|
|
227 |
stop("ggtreeExtra is required for using create_base_plot. Please install it.", call. = FALSE) |
|
|
228 |
} |
|
|
229 |
|
|
|
230 |
if (!requireNamespace("ggplot2", quietly = TRUE)) { |
|
|
231 |
stop("ggplot2 is required to use geom_tile. Please install it.", call. = FALSE) |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
p + ggnewscale::new_scale("alpha") + ggnewscale::new_scale("fill") + |
|
|
235 |
ggtreeExtra::geom_fruit( |
|
|
236 |
data=data, |
|
|
237 |
geom="geom_tile", |
|
|
238 |
mapping=ggplot2::aes(y=Sample, alpha=value, x=Gene, fill=Gene), |
|
|
239 |
offset=offset, |
|
|
240 |
pwidth=pwidth |
|
|
241 |
) + |
|
|
242 |
ggplot2::scale_fill_manual( |
|
|
243 |
name=gene_label, |
|
|
244 |
values=gene_colors, |
|
|
245 |
guide=ggplot2::guide_legend(keywidth=0.65, keyheight=0.35, order=1) |
|
|
246 |
) + |
|
|
247 |
adjust_alpha_scale(data, gene_label, alpha_value) # Assuming function signature and usage |
|
|
248 |
} |
|
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
|
253 |
#' Add multiple layers to a `ggtree` plot for visualizing gene expression and enrichment data |
|
|
254 |
#' |
|
|
255 |
#' This function sequentially adds multiple layers to a `ggtree` plot, including gene expression data, boxplots for statistical |
|
|
256 |
#' summaries, and additional tile layers for pathway enrichment scores from SSGSEA and GSVA analyses. It utilizes separate |
|
|
257 |
#' functions for adding each type of layer and allows for the specification of gene colors as well as adjustments in aesthetics |
|
|
258 |
#' for each layer. The function is designed to work with specific data structures and assumes all functions for adding layers |
|
|
259 |
#' are defined and available. |
|
|
260 |
#' |
|
|
261 |
#' @param p A `ggtree` plot object to which the data and layers will be added. |
|
|
262 |
#' @param long_format_HeatdataDeseq A data frame containing gene expression data with columns for `Samples`, `Genes`, and `Values`. |
|
|
263 |
#' @param ssgsea_kegg_HeatdataDeseq A data frame containing SSGSEA analysis results with columns for `Samples`, `Genes`, and `Values`. |
|
|
264 |
#' @param gsva_kegg_HeatdataDeseq A data frame containing GSVA analysis results with columns for `Samples`, `Genes`, and `Values`. |
|
|
265 |
#' @param gene_colors A named vector of colors for genes, used for coloring tiles in different layers. |
|
|
266 |
#' @return A `ggtree` plot object with multiple layers added for comprehensive visualization. |
|
|
267 |
#' @export |
|
|
268 |
#' |
|
|
269 |
#' @examples |
|
|
270 |
#' \donttest{ |
|
|
271 |
#' # Check and load required packages |
|
|
272 |
#' if (requireNamespace("ggtreeExtra", quietly = TRUE) && |
|
|
273 |
#' requireNamespace("ggplot2", quietly = TRUE)) { |
|
|
274 |
#' library(ggtreeExtra) |
|
|
275 |
#' library(ggplot2) |
|
|
276 |
#' |
|
|
277 |
#' # Example data for gene expression, SSGSEA, and GSVA |
|
|
278 |
#' file_path <- system.file("extdata", "p_tree_test.rds", package = "TransProR") |
|
|
279 |
#' p <- readRDS(file_path) |
|
|
280 |
#' |
|
|
281 |
#' # Create gene expression data frame (long_format_HeatdataDeseq) |
|
|
282 |
#' long_format_HeatdataDeseq <- data.frame( |
|
|
283 |
#' Sample = rep(c("Species_A", "Species_B", "Species_C", "Species_D"), each = 5), |
|
|
284 |
#' Genes = rep(paste0("Gene", 1:5), times = 4), |
|
|
285 |
#' Value = runif(20, min = 0, max = 1) # Randomly generate expression values between 0 and 1 |
|
|
286 |
#' ) |
|
|
287 |
#' |
|
|
288 |
#' # Create SSGSEA analysis results data frame (ssgsea_kegg_HeatdataDeseq) |
|
|
289 |
#' ssgsea_kegg_HeatdataDeseq <- data.frame( |
|
|
290 |
#' Sample = rep(c("Species_A", "Species_B", "Species_C", "Species_D"), each = 3), |
|
|
291 |
#' Genes = rep(c("Pathway1", "Pathway2", "Pathway3"), times = 4), |
|
|
292 |
#' Value = runif(12, min = 0, max = 1) # Randomly generate enrichment scores between 0 and 1 |
|
|
293 |
#' ) |
|
|
294 |
#' |
|
|
295 |
#' # Create GSVA analysis results data frame (gsva_kegg_HeatdataDeseq) |
|
|
296 |
#' gsva_kegg_HeatdataDeseq <- data.frame( |
|
|
297 |
#' Sample = rep(c("Species_A", "Species_B", "Species_C", "Species_D"), each = 4), |
|
|
298 |
#' Genes = rep(c("PathwayA", "PathwayB", "PathwayC", "PathwayD"), times = 4), |
|
|
299 |
#' Value = runif(16, min = 0, max = 1) # Randomly generate enrichment scores between 0 and 1 |
|
|
300 |
#' ) |
|
|
301 |
#' |
|
|
302 |
#' # Define gene and pathway colors (named vector), including all genes and pathways |
|
|
303 |
#' gene_colors <- c( |
|
|
304 |
#' # Genes for gene expression |
|
|
305 |
#' Gene1 = "#491588", |
|
|
306 |
#' Gene2 = "#301b8d", |
|
|
307 |
#' Gene3 = "#1a237a", |
|
|
308 |
#' Gene4 = "#11479c", |
|
|
309 |
#' Gene5 = "#0a5797", |
|
|
310 |
#' # Pathways for SSGSEA |
|
|
311 |
#' Pathway1 = "#0b5f63", |
|
|
312 |
#' Pathway2 = "#074d41", |
|
|
313 |
#' Pathway3 = "#1f5e27", |
|
|
314 |
#' # Pathways for GSVA |
|
|
315 |
#' PathwayA = "#366928", |
|
|
316 |
#' PathwayB = "#827729", |
|
|
317 |
#' PathwayC = "#a1d99b", |
|
|
318 |
#' PathwayD = "#c7e9c0" |
|
|
319 |
#' ) |
|
|
320 |
#' |
|
|
321 |
#' # Call circos_fruits function to add multiple layers |
|
|
322 |
#' final_plot <- circos_fruits( |
|
|
323 |
#' p, |
|
|
324 |
#' long_format_HeatdataDeseq, |
|
|
325 |
#' ssgsea_kegg_HeatdataDeseq, |
|
|
326 |
#' gsva_kegg_HeatdataDeseq, |
|
|
327 |
#' gene_colors |
|
|
328 |
#' ) |
|
|
329 |
#' } else { |
|
|
330 |
#' message("Required packages 'ggtreeExtra' and 'ggplot2' are not installed.") |
|
|
331 |
#' } |
|
|
332 |
#' } |
|
|
333 |
#' |
|
|
334 |
circos_fruits <- function(p, long_format_HeatdataDeseq, ssgsea_kegg_HeatdataDeseq, gsva_kegg_HeatdataDeseq, gene_colors) { |
|
|
335 |
if (!requireNamespace("ggtreeExtra", quietly = TRUE)) { |
|
|
336 |
stop("ggtreeExtra is required for using create_base_plot. Please install it.", call. = FALSE) |
|
|
337 |
} |
|
|
338 |
|
|
|
339 |
if (!requireNamespace("ggplot2", quietly = TRUE)) { |
|
|
340 |
stop("ggplot2 is required to use geom_tile. Please install it.", call. = FALSE) |
|
|
341 |
} |
|
|
342 |
# Create the base plot with gene expression data |
|
|
343 |
p1 <- create_base_plot(p, long_format_HeatdataDeseq, gene_colors) |
|
|
344 |
|
|
|
345 |
# Add a boxplot layer to the base plot |
|
|
346 |
p2 <- add_boxplot(p1, long_format_HeatdataDeseq) |
|
|
347 |
|
|
|
348 |
# Add a new tile layer for SSGSEA data |
|
|
349 |
p3 <- add_new_tile_layer(p2, ssgsea_kegg_HeatdataDeseq, gene_colors, "Ssgsea Term") |
|
|
350 |
|
|
|
351 |
# Add another boxplot layer with specific aesthetic adjustments |
|
|
352 |
p4 <- add_boxplot(p3, ssgsea_kegg_HeatdataDeseq, fill_color="#f28131", alpha=0.65, offset=0.32) |
|
|
353 |
|
|
|
354 |
# Add a new tile layer for GSVA data with specific alpha and offset adjustments |
|
|
355 |
p5 <- add_new_tile_layer(p4, gsva_kegg_HeatdataDeseq, gene_colors, "Gsva Term", alpha_value=c(0.3, 0.9), offset=0.02) |
|
|
356 |
|
|
|
357 |
# Return the final plot |
|
|
358 |
return(p5) |
|
|
359 |
} |