[e25014]: / R / CircosFruits.R

Download this file

360 lines (336 with data), 14.1 kB

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