|
a |
|
b/R/EnrichCirclize.R |
|
|
1 |
#' Adjust and Export Pathway Analysis Results |
|
|
2 |
#' |
|
|
3 |
#' This function processes a dataframe containing fgsea results. It adjusts pathway names |
|
|
4 |
#' by removing underscores, converting to lowercase, then capitalizing the first letter, |
|
|
5 |
#' and joining the components with spaces. It selects and merges the top upregulated |
|
|
6 |
#' and downregulated pathways based on enrichment score (ES) and p-value. |
|
|
7 |
#' |
|
|
8 |
#' @param fgseaRes Dataframe containing fgsea results with columns 'pathway', 'ES', and 'pval'. |
|
|
9 |
#' @param nTop Integer, number of top pathways to select based on the p-value. |
|
|
10 |
#' @return A vector containing combined top upregulated and downregulated pathways. |
|
|
11 |
#' @importFrom Hmisc capitalize |
|
|
12 |
#' @export |
|
|
13 |
#' @examples |
|
|
14 |
#' # Create a synthetic fgseaRes dataframe |
|
|
15 |
#'fgseaRes <- data.frame( |
|
|
16 |
#' pathway = c("KEGG_APOPTOSIS", |
|
|
17 |
#' "GO_CELL_CYCLE", |
|
|
18 |
#' "REACTOME_DNA_REPAIR", |
|
|
19 |
#' "KEGG_METABOLISM", |
|
|
20 |
#' "GO_TRANSPORT"), |
|
|
21 |
#' ES = c(0.45, -0.22, 0.56, -0.35, 0.33), |
|
|
22 |
#' pval = c(0.001, 0.02, 0.0003, 0.05, 0.01) |
|
|
23 |
#') |
|
|
24 |
#' |
|
|
25 |
#' # Run the function to get top pathways |
|
|
26 |
#'result <- adjust_export_pathway(fgseaRes = fgseaRes, nTop = 2) |
|
|
27 |
#' |
|
|
28 |
adjust_export_pathway <- function(fgseaRes, nTop = 10) { |
|
|
29 |
# Adjust pathway names |
|
|
30 |
fgseaRes$pathway <- as.character(fgseaRes$pathway) |
|
|
31 |
for(i in 1:nrow(fgseaRes)){ |
|
|
32 |
message("Processing row ", i) |
|
|
33 |
term = fgseaRes$pathway[i] |
|
|
34 |
### 1. Split the string |
|
|
35 |
term = unlist(strsplit(term, split="_", fixed=TRUE))[-1] |
|
|
36 |
### 2. Convert to lowercase, then capitalize the first letter |
|
|
37 |
term = Hmisc::capitalize(tolower(term)) |
|
|
38 |
### 3. Concatenate with spaces |
|
|
39 |
term = paste(term, collapse=" ") |
|
|
40 |
### 4. Data export |
|
|
41 |
fgseaRes$pathway[i] = term |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
# Select top upregulated pathways |
|
|
45 |
topPathwaysUp <- fgseaRes[fgseaRes$ES > 0,][order(fgseaRes$pval[fgseaRes$ES > 0]), 'pathway'][1:nTop] |
|
|
46 |
|
|
|
47 |
# Select top downregulated pathways |
|
|
48 |
topPathwaysDown <- fgseaRes[fgseaRes$ES < 0,][order(fgseaRes$pval[fgseaRes$ES < 0]), 'pathway'][1:nTop] |
|
|
49 |
|
|
|
50 |
# Combine top pathways and convert any potential list to a vector |
|
|
51 |
combinedPathways <- unlist(c(topPathwaysUp, rev(topPathwaysDown)), use.names = FALSE) |
|
|
52 |
|
|
|
53 |
return(list(combinedPathways = combinedPathways, fgseaRes = fgseaRes)) |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
#' Randomly Select Pathways with Limited Word Count |
|
|
62 |
#' |
|
|
63 |
#' This function randomly selects a specified number of pathways from a given list, ensuring that each selected pathway name does not exceed a specified number of words. It filters out pathways with names longer than the specified word limit before making the selection. |
|
|
64 |
#' |
|
|
65 |
#' @param pathways Character vector of pathways. |
|
|
66 |
#' @param max_words Integer, maximum number of words allowed in the pathway name. |
|
|
67 |
#' @param num_select Integer, number of pathways to randomly select. |
|
|
68 |
#' @return A character vector of selected pathways. |
|
|
69 |
#' @export |
|
|
70 |
#' @examples |
|
|
71 |
#' pathway_list <- c("pathway_one response to stimulus", |
|
|
72 |
#' "pathway_two cell growth and death", |
|
|
73 |
#' "pathway_three regulation of cellular process", |
|
|
74 |
#' "pathway_four metabolic process") |
|
|
75 |
#' selected_pathways <- selectPathways(pathway_list, max_words = 5, num_select = 2) |
|
|
76 |
#' |
|
|
77 |
selectPathways <- function(pathways, max_words = 10, num_select = 10) { |
|
|
78 |
# Check input |
|
|
79 |
if (!is.character(pathways)) { |
|
|
80 |
stop("Please provide a character vector of pathways.") |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
# Filter pathways with word count not exceeding max_words |
|
|
84 |
filtered_pathways <- pathways[sapply(pathways, function(x) length(strsplit(x, " ")[[1]]) <= max_words)] |
|
|
85 |
|
|
|
86 |
# Randomly select num_select pathways from the filtered list |
|
|
87 |
if (length(filtered_pathways) >= num_select) { |
|
|
88 |
selected_pathways <- sample(filtered_pathways, num_select) |
|
|
89 |
} else { |
|
|
90 |
warning("Not enough pathways with <= ", max_words, " words. Returning as many as possible.") |
|
|
91 |
selected_pathways <- sample(filtered_pathways, length(filtered_pathways)) |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
return(selected_pathways) |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
#' Draw Dual-Sided Legends on a Plot |
|
|
101 |
#' |
|
|
102 |
#' This function creates two sets of legends, one on the left and one on the right side of a plot. |
|
|
103 |
#' It displays color-coded legends with labels corresponding to different data categories. |
|
|
104 |
#' Each legend entry consists of a colored rectangle and a text label. The left side legend has |
|
|
105 |
#' text aligned to the right of the color block, while the right side legend has text aligned |
|
|
106 |
#' to the left of the color block. |
|
|
107 |
#' |
|
|
108 |
#' @param labels Vector of labels for the legends. |
|
|
109 |
#' @param colors Vector of colors corresponding to the labels. |
|
|
110 |
#' @param legend_width The width of each legend viewport expressed in grid units. |
|
|
111 |
#' @param x_positions Numeric vector of length 2 specifying the x-positions of the left and right legends. |
|
|
112 |
#' @param y_position The y-position common for both legends, expressed as a fraction of the plot height. |
|
|
113 |
#' @param just_positions List of two vectors, each specifying the horizontal and vertical justification for the legends. |
|
|
114 |
#' @param text_alignments List of two character strings specifying text alignments for the legends ('left' or 'right'). |
|
|
115 |
#' @param font_size Numeric value specifying the font size for the legend labels. |
|
|
116 |
#' @return Invisible. This function is called for its side effects of drawing legends on a plot. |
|
|
117 |
#' @importFrom grid pushViewport viewport grid.roundrect grid.text upViewport unit |
|
|
118 |
#' @export |
|
|
119 |
#' @examples |
|
|
120 |
#' labels <- c("Label1", "Label2", "Label3", "Label4", "Label5", "Label6") |
|
|
121 |
#' colors <- c("#ff0000", "#00ff00", "#0000ff", "#ffff00", "#ff00ff", "#00ffff") |
|
|
122 |
#' |
|
|
123 |
#' # Convert to 'unit' objects for grid |
|
|
124 |
#' grid::grid.roundrect( |
|
|
125 |
#' x = grid::unit(0.5, "npc"), # "npc" stands for normalized parent coordinates |
|
|
126 |
#' y = grid::unit(0.5, "npc"), |
|
|
127 |
#' width = grid::unit(0.1, "npc"), |
|
|
128 |
#' height = grid::unit(0.05, "npc"), |
|
|
129 |
#' gp = grid::gpar(fill = "red"), |
|
|
130 |
#' r = grid::unit(0.1, "npc") # rounding radius |
|
|
131 |
#' ) |
|
|
132 |
#' |
|
|
133 |
#' # Example of drawing legends with specific labels and colors |
|
|
134 |
#' drawLegends(labels, colors, grid::unit(2, "cm"), c(0.225, 0.75), 0.5, |
|
|
135 |
#' list(c("left", "center"), c("right", "center")), |
|
|
136 |
#' list("right", "left"), 10) |
|
|
137 |
#' |
|
|
138 |
drawLegends <- function(labels, colors, legend_width, x_positions, y_position, just_positions, text_alignments, font_size) { |
|
|
139 |
half_length <- length(labels) / 2 |
|
|
140 |
legend_height <- grid::unit(1, "lines") * half_length |
|
|
141 |
|
|
|
142 |
# Draw left-side legend |
|
|
143 |
grid::pushViewport(grid::viewport( |
|
|
144 |
width = legend_width, |
|
|
145 |
height = legend_height, |
|
|
146 |
x = x_positions[1], |
|
|
147 |
y = y_position, |
|
|
148 |
just = just_positions[[1]] |
|
|
149 |
)) |
|
|
150 |
for (i in seq_len(half_length)) { |
|
|
151 |
grid::grid.roundrect( |
|
|
152 |
x = grid::unit(1, "npc") - grid::unit(0.5, "cm"), |
|
|
153 |
y = grid::unit(1, "npc") - grid::unit(i / half_length, "npc") + grid::unit(0.5 / half_length, "npc"), |
|
|
154 |
width = grid::unit(0.7, "cm"), |
|
|
155 |
height = grid::unit(0.9 / half_length, "npc"), |
|
|
156 |
gp = grid::gpar(fill = colors[i], col = NA), |
|
|
157 |
r = grid::unit(0.3, "snpc") |
|
|
158 |
) |
|
|
159 |
grid::grid.text( |
|
|
160 |
labels[i], |
|
|
161 |
x = grid::unit(1, "npc") - grid::unit(1, "cm"), |
|
|
162 |
y = grid::unit(1, "npc") - grid::unit(i / half_length, "npc") + grid::unit(0.5 / half_length, "npc"), |
|
|
163 |
gp = grid::gpar(col = colors[i], fontsize = font_size), |
|
|
164 |
just = text_alignments[[1]] |
|
|
165 |
) |
|
|
166 |
} |
|
|
167 |
grid::upViewport() |
|
|
168 |
|
|
|
169 |
# Draw right-side legend |
|
|
170 |
grid::pushViewport(grid::viewport( |
|
|
171 |
width = legend_width, |
|
|
172 |
height = legend_height, |
|
|
173 |
x = x_positions[2], |
|
|
174 |
y = y_position, |
|
|
175 |
just = just_positions[[2]] |
|
|
176 |
)) |
|
|
177 |
for (i in (half_length + 1):length(labels)) { |
|
|
178 |
grid::grid.roundrect( |
|
|
179 |
x = grid::unit(1, "npc") - grid::unit(0.6, "cm"), |
|
|
180 |
y = grid::unit(1, "npc") - grid::unit((i - half_length) / half_length, "npc") + grid::unit(0.5 / half_length, "npc"), |
|
|
181 |
width = grid::unit(0.7, "cm"), |
|
|
182 |
height = grid::unit(0.9 / half_length, "npc"), |
|
|
183 |
gp = grid::gpar(fill = colors[i], col = NA), |
|
|
184 |
r = grid::unit(0.3, "snpc") |
|
|
185 |
) |
|
|
186 |
grid::grid.text( |
|
|
187 |
labels[i], |
|
|
188 |
x = grid::unit(1, "npc") - grid::unit(0.1, "cm"), |
|
|
189 |
y = grid::unit(1, "npc") - grid::unit((i - half_length) / half_length, "npc") + grid::unit(0.5 / half_length, "npc"), |
|
|
190 |
gp = grid::gpar(col = colors[i], fontsize = font_size), |
|
|
191 |
just = text_alignments[[2]] |
|
|
192 |
) |
|
|
193 |
} |
|
|
194 |
grid::upViewport() |
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
|
|
|
198 |
#' Draw Chord Diagram with Legends |
|
|
199 |
#' |
|
|
200 |
#' This function creates a chord diagram from a specified dataframe and draws two sets of legends for it. |
|
|
201 |
#' It adjusts the track height of the chord diagram to optimize space and uses specified colors for the grid. |
|
|
202 |
#' Legends are drawn at specified positions with configurable text alignments and font sizes. |
|
|
203 |
#' |
|
|
204 |
#' @param all_combined_df A dataframe containing the matrix for the chord diagram. |
|
|
205 |
#' @param original_colors A vector of colors for the grid columns of the chord diagram. |
|
|
206 |
#' @param labels A vector of labels for the first legend. |
|
|
207 |
#' @param colors A vector of colors corresponding to the first legend's labels. |
|
|
208 |
#' @param labels2 A vector of labels for the second legend. |
|
|
209 |
#' @param colors2 A vector of colors corresponding to the second legend's labels. |
|
|
210 |
#' @param font_size The font size used for legend texts, defaults to 10. |
|
|
211 |
#' @return Invisible, primarily used for its side effects of drawing on a graphics device. |
|
|
212 |
#' @importFrom circlize chordDiagram |
|
|
213 |
#' @importFrom grid unit |
|
|
214 |
#' @importFrom graphics strwidth |
|
|
215 |
#' @export |
|
|
216 |
#' @examples |
|
|
217 |
#' # Sample Chord Diagram Matrix |
|
|
218 |
#' all_combined_df <- data.frame( |
|
|
219 |
#' A = c(10, 20, 30), |
|
|
220 |
#' B = c(15, 25, 35), |
|
|
221 |
#' C = c(5, 10, 15) |
|
|
222 |
#' ) |
|
|
223 |
#' rownames(all_combined_df) <- c("A", "B", "C") |
|
|
224 |
#' |
|
|
225 |
#' # Colors for the grid of the chord diagram (corresponding to columns of the matrix) |
|
|
226 |
#' original_colors <- c("red", "green", "blue") |
|
|
227 |
#' |
|
|
228 |
#' # Name the colors according to the sectors (A, B, C) |
|
|
229 |
#' names(original_colors) <- colnames(all_combined_df) |
|
|
230 |
#' |
|
|
231 |
#' # Labels and Colors for the First Legend |
|
|
232 |
#' labels <- c("Label 1", "Label 2", "Label 3") |
|
|
233 |
#' colors <- c("yellow", "purple", "cyan") |
|
|
234 |
#' |
|
|
235 |
#' # Labels and Colors for the Second Legend |
|
|
236 |
#' labels2 <- c("Label A", "Label B", "Label C") |
|
|
237 |
#' colors2 <- c("orange", "pink", "brown") |
|
|
238 |
#' |
|
|
239 |
#' # Font size for the legend texts (optional, default is 10) |
|
|
240 |
#' font_size <- 10 |
|
|
241 |
#' |
|
|
242 |
#' # Call the enrichment_circlize function with the sample data |
|
|
243 |
#' # This is just an example; the plot will be rendered in an appropriate graphics context |
|
|
244 |
#' # such as RStudio's plot pane or an external plotting window. |
|
|
245 |
#' plot1 <- enrichment_circlize(all_combined_df, |
|
|
246 |
#' original_colors, |
|
|
247 |
#' labels, |
|
|
248 |
#' colors, |
|
|
249 |
#' labels2, |
|
|
250 |
#' colors2, |
|
|
251 |
#' font_size |
|
|
252 |
#' ) |
|
|
253 |
#' |
|
|
254 |
enrichment_circlize <- function(all_combined_df, original_colors, labels, colors, |
|
|
255 |
labels2, colors2, font_size = 10) { |
|
|
256 |
|
|
|
257 |
# Calculate adjusted height for the chord diagram |
|
|
258 |
max_height <- max(graphics::strwidth(unlist(dimnames(all_combined_df)), "inches")) * 1.2 |
|
|
259 |
|
|
|
260 |
# Draw the chord diagram |
|
|
261 |
circlize::chordDiagram(all_combined_df, grid.col = original_colors, annotationTrack = "grid", |
|
|
262 |
directional = -1, direction.type = c("diffHeight", "arrows"), |
|
|
263 |
link.arr.type = "big.arrow", preAllocateTracks = list(track.height = max_height)) |
|
|
264 |
|
|
|
265 |
# Draw the first set of legends |
|
|
266 |
legend_width <- grid::unit(2, "cm") |
|
|
267 |
x_positions <- c(0.225, 0.75) |
|
|
268 |
y_position <- 0.5 |
|
|
269 |
just_positions <- list(c("left", "center"), c("right", "center")) |
|
|
270 |
text_alignments <- list("right", "left") |
|
|
271 |
|
|
|
272 |
drawLegends(labels, colors, legend_width, x_positions, y_position, just_positions, text_alignments, font_size) |
|
|
273 |
|
|
|
274 |
# Draw the second set of legends |
|
|
275 |
x_positions2 <- c(0.3, 0.68) |
|
|
276 |
y_position2 <- 0.7 |
|
|
277 |
|
|
|
278 |
drawLegends(labels2, colors2, legend_width, x_positions2, y_position2, just_positions, text_alignments, font_size) |
|
|
279 |
} |