| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  | #' Base color for the plots. | 
					
						
							|  |  |  | #' @noRd | 
					
						
							|  |  |  | base_color <- function() "#1964bf" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #' Transparent version of the base color. | 
					
						
							|  |  |  | #' @noRd | 
					
						
							|  |  |  | base_color_transparent <- function() "#1964bf80" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #' Color palette for gene sets. | 
					
						
							|  |  |  | #' @noRd | 
					
						
							|  |  |  | gene_set_color <- function(index) { | 
					
						
							|  |  |  |     c("#FF7F00", "#4DAF4A", "#984EA3")[index] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-15 15:12:13 +01:00
										 |  |  | #' Plot gene positions. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' This function requires the package `plotly`. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @param species_ids IDs of species to show in the plot. | 
					
						
							|  |  |  | #' @param gene_sets A list of gene sets (containing vectors of gene IDs) that | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  | #'   will be highlighted in the plot. The names will be used as labels. | 
					
						
							| 
									
										
										
										
											2021-11-15 15:12:13 +01:00
										 |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  | plot_positions <- function(species_ids, gene_sets) { | 
					
						
							| 
									
										
										
										
											2021-11-15 15:12:13 +01:00
										 |  |  |     if (!requireNamespace("plotly", quietly = TRUE)) { | 
					
						
							|  |  |  |         stop("Please install \"plotly\" to use this function.") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |     # Prefilter data by species. | 
					
						
							|  |  |  |     data <- geposan::distances[species %chin% species_ids] | 
					
						
							| 
									
										
										
										
											2021-11-15 15:12:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:33:51 +01:00
										 |  |  |     species_max_distance <- data[, | 
					
						
							|  |  |  |         .(max_distance = max(distance)), | 
					
						
							|  |  |  |         by = species | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2021-11-15 15:12:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |     # Prefilter species. | 
					
						
							| 
									
										
										
										
											2021-11-15 15:12:13 +01:00
										 |  |  |     species <- geposan::species[id %chin% species_ids] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |     plot <- plotly::plot_ly() |> | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         plotly::layout( | 
					
						
							|  |  |  |             xaxis = list( | 
					
						
							|  |  |  |                 title = "Species", | 
					
						
							|  |  |  |                 tickvals = species$id, | 
					
						
							|  |  |  |                 ticktext = species$name | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2021-12-03 11:33:51 +01:00
										 |  |  |             yaxis = list(title = "Distance to telomeres [Bp]"), | 
					
						
							|  |  |  |             bargap = 0.9 | 
					
						
							| 
									
										
										
										
											2022-05-17 21:48:30 +02:00
										 |  |  |         ) |> | 
					
						
							|  |  |  |         plotly::add_bars( | 
					
						
							| 
									
										
										
										
											2021-12-03 11:33:51 +01:00
										 |  |  |             data = species_max_distance, | 
					
						
							|  |  |  |             x = ~species, | 
					
						
							|  |  |  |             y = ~max_distance, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |             name = "All genes", | 
					
						
							|  |  |  |             marker = list(color = base_color()) | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (length(gene_sets) > 0) { | 
					
						
							|  |  |  |         # Include gene information which will be used for labeling | 
					
						
							|  |  |  |         gene_set_data <- merge( | 
					
						
							|  |  |  |             data[gene %chin% unlist(gene_sets)], | 
					
						
							|  |  |  |             geposan::genes, | 
					
						
							|  |  |  |             by.x = "gene", | 
					
						
							|  |  |  |             by.y = "id" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |         index <- 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         for (gene_set_name in names(gene_sets)) { | 
					
						
							|  |  |  |             gene_set <- gene_sets[[gene_set_name]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             plot <- plot |> plotly::add_markers( | 
					
						
							|  |  |  |                 data = gene_set_data[gene %chin% gene_set], | 
					
						
							|  |  |  |                 x = ~species, | 
					
						
							|  |  |  |                 y = ~distance, | 
					
						
							|  |  |  |                 text = ~name, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |                 name = gene_set_name, | 
					
						
							|  |  |  |                 marker = list( | 
					
						
							|  |  |  |                     size = 10, | 
					
						
							|  |  |  |                     color = gene_set_color(index) | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             index <- index + 1 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-15 15:12:13 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |     plot | 
					
						
							| 
									
										
										
										
											2021-11-15 15:12:13 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:28:03 +01:00
										 |  |  | #' Plot a side-by-side comparison of multiple rankings. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' Each ranking's scores will be shown as a vertical violin plot without any | 
					
						
							|  |  |  | #' additional markings. The gene sets will be shown as markers on top of the | 
					
						
							|  |  |  | #' density visualization. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' This function requires the package `plotly`. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @param rankings A named list of rankings to display. The names will be shown | 
					
						
							|  |  |  | #'   as labels in the plot. | 
					
						
							|  |  |  | #' @param gene_sets A named list of vectors of gene IDs to highlight. The names | 
					
						
							|  |  |  | #'   will be used to distinguish the sets and in the legend. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							|  |  |  | plot_rankings <- function(rankings, gene_sets) { | 
					
						
							|  |  |  |     if (!requireNamespace("plotly", quietly = TRUE)) { | 
					
						
							|  |  |  |         stop("Please install \"plotly\" to use this function.") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |     plot <- plotly::plot_ly() |> | 
					
						
							| 
									
										
										
										
											2021-12-02 15:28:03 +01:00
										 |  |  |         plotly::layout( | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |             xaxis = list(tickvals = names(rankings)), | 
					
						
							| 
									
										
										
										
											2021-12-02 15:28:03 +01:00
										 |  |  |             yaxis = list(title = "Score") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     is_first <- TRUE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (ranking_name in names(rankings)) { | 
					
						
							|  |  |  |         ranking <- rankings[[ranking_name]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         plot <- plot |> plotly::add_trace( | 
					
						
							|  |  |  |             data = ranking, | 
					
						
							|  |  |  |             x = ranking_name, | 
					
						
							|  |  |  |             y = ~score, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |             name = "All genes", | 
					
						
							| 
									
										
										
										
											2021-12-02 15:28:03 +01:00
										 |  |  |             type = "violin", | 
					
						
							|  |  |  |             spanmode = "hard", | 
					
						
							|  |  |  |             points = FALSE, | 
					
						
							|  |  |  |             showlegend = is_first, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |             hoverinfo = "skip", | 
					
						
							|  |  |  |             line = list(color = base_color()), | 
					
						
							|  |  |  |             fillcolor = base_color_transparent() | 
					
						
							| 
									
										
										
										
											2021-12-02 15:28:03 +01:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         if (length(gene_sets) > 0) { | 
					
						
							|  |  |  |             gene_set_data <- merge( | 
					
						
							|  |  |  |                 ranking[gene %chin% unlist(gene_sets)], | 
					
						
							|  |  |  |                 geposan::genes, | 
					
						
							|  |  |  |                 by.x = "gene", | 
					
						
							|  |  |  |                 by.y = "id" | 
					
						
							| 
									
										
										
										
											2021-12-02 15:28:03 +01:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |             index <- 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |             for (gene_set_name in names(gene_sets)) { | 
					
						
							|  |  |  |                 gene_set <- gene_sets[[gene_set_name]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 plot <- plot |> plotly::add_markers( | 
					
						
							|  |  |  |                     data = gene_set_data[gene %chin% gene_set], | 
					
						
							|  |  |  |                     x = ranking_name, | 
					
						
							|  |  |  |                     y = ~score, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |                     name = gene_set_name, | 
					
						
							| 
									
										
										
										
											2022-01-13 11:39:33 +01:00
										 |  |  |                     text = ~name, | 
					
						
							|  |  |  |                     customdata = ~percentile, | 
					
						
							|  |  |  |                     hovertemplate = paste0( | 
					
						
							|  |  |  |                         "<b>%{text}</b><br>", | 
					
						
							|  |  |  |                         "Score: %{y:.3}<br>", | 
					
						
							|  |  |  |                         "Percentile: %{customdata:.2%}", | 
					
						
							|  |  |  |                         "<extra></extra>" | 
					
						
							|  |  |  |                     ), | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |                     showlegend = is_first, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |                     marker = list( | 
					
						
							|  |  |  |                         size = 10, | 
					
						
							|  |  |  |                         color = gene_set_color(index) | 
					
						
							|  |  |  |                     ) | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 index <- index + 1 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-02 15:28:03 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         is_first <- FALSE | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     plot | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  | #' Plot a ranking as a scatter plot of scores. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' This function requires the package `plotly`. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @param ranking The ranking to visualize. | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  | #' @param gene_sets A named list of gene sets (containing vectors of gene IDs) | 
					
						
							|  |  |  | #'   that will be highlighted in the plot. The names will be used in the legend. | 
					
						
							|  |  |  | #' @param max_rank The maximum rank of included genes. All genes that are ranked | 
					
						
							|  |  |  | #'   lower will appear greyed out. | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  | #' | 
					
						
							|  |  |  | #' @seealso ranking() | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  | plot_scores <- function(ranking, gene_sets = NULL, max_rank = NULL) { | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  |     if (!requireNamespace("plotly", quietly = TRUE)) { | 
					
						
							|  |  |  |         stop("Please install \"plotly\" to use this function.") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 11:17:11 +01:00
										 |  |  |     # To speed up rendering, don't show every single gene. | 
					
						
							| 
									
										
										
										
											2021-12-29 16:23:35 +01:00
										 |  |  |     n_ranks <- nrow(ranking) | 
					
						
							|  |  |  |     sample_ranking <- ranking[seq(1, n_ranks, 5)] | 
					
						
							| 
									
										
										
										
											2021-12-06 11:17:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |     plot <- plotly::plot_ly() |> | 
					
						
							| 
									
										
										
										
											2021-12-06 11:17:11 +01:00
										 |  |  |         plotly::add_lines( | 
					
						
							|  |  |  |             data = sample_ranking, | 
					
						
							| 
									
										
										
										
											2021-12-29 16:23:35 +01:00
										 |  |  |             x = ~percentile, | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  |             y = ~score, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |             name = "All genes", | 
					
						
							| 
									
										
										
										
											2021-12-06 11:17:11 +01:00
										 |  |  |             hoverinfo = "skip", | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |             line = list(width = 4, color = base_color()) | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  |         ) |> | 
					
						
							|  |  |  |         plotly::layout( | 
					
						
							| 
									
										
										
										
											2021-12-29 16:23:35 +01:00
										 |  |  |             xaxis = list( | 
					
						
							|  |  |  |                 title = "Percentile", | 
					
						
							|  |  |  |                 tickformat = ".0%" | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  |             yaxis = list(title = "Score") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (length(gene_sets) > 0) { | 
					
						
							|  |  |  |         # Include gene information which will be used for labeling | 
					
						
							| 
									
										
										
										
											2021-11-06 13:22:57 +01:00
										 |  |  |         gene_set_data <- merge( | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |             ranking[gene %chin% unlist(gene_sets)], | 
					
						
							| 
									
										
										
										
											2021-11-06 13:22:57 +01:00
										 |  |  |             geposan::genes, | 
					
						
							|  |  |  |             by.x = "gene", | 
					
						
							|  |  |  |             by.y = "id" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |         index <- 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         for (gene_set_name in names(gene_sets)) { | 
					
						
							|  |  |  |             gene_set <- gene_sets[[gene_set_name]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             plot <- plot |> plotly::add_markers( | 
					
						
							|  |  |  |                 data = gene_set_data[gene %chin% gene_set], | 
					
						
							| 
									
										
										
										
											2021-12-29 16:23:35 +01:00
										 |  |  |                 x = ~percentile, | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |                 y = ~score, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |                 name = gene_set_name, | 
					
						
							| 
									
										
										
										
											2021-12-29 21:15:50 +01:00
										 |  |  |                 text = ~name, | 
					
						
							|  |  |  |                 customdata = ~rank, | 
					
						
							|  |  |  |                 hovertemplate = paste0( | 
					
						
							|  |  |  |                     "<b>%{text}</b><br>", | 
					
						
							|  |  |  |                     "Score: %{y:.3}<br>", | 
					
						
							|  |  |  |                     "Rank: %{customdata}<br>", | 
					
						
							|  |  |  |                     "Percentile: %{x:.2%}", | 
					
						
							|  |  |  |                     "<extra></extra>" | 
					
						
							|  |  |  |                 ), | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |                 marker = list( | 
					
						
							|  |  |  |                     size = 10, | 
					
						
							|  |  |  |                     color = gene_set_color(index) | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             index <- index + 1 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-15 09:21:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!is.null(max_rank)) { | 
					
						
							|  |  |  |         first_not_included_rank <- max_rank + 1 | 
					
						
							|  |  |  |         last_rank <- ranking[, .N] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (first_not_included_rank <= last_rank) { | 
					
						
							|  |  |  |             plot <- plot |> plotly::layout( | 
					
						
							|  |  |  |                 shapes = list( | 
					
						
							|  |  |  |                     type = "rect", | 
					
						
							|  |  |  |                     fillcolor = "black", | 
					
						
							|  |  |  |                     opacity = 0.1, | 
					
						
							| 
									
										
										
										
											2021-12-29 16:23:35 +01:00
										 |  |  |                     x0 = 1 - first_not_included_rank / n_ranks, | 
					
						
							|  |  |  |                     x1 = 1 - last_rank / n_ranks, | 
					
						
							| 
									
										
										
										
											2021-11-15 09:21:37 +01:00
										 |  |  |                     y0 = 0.0, | 
					
						
							|  |  |  |                     y1 = 1.0 | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     plot | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #' Visualize a ranking by comparing gene sets in a boxplot. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' This function requires the package `plotly`. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @param ranking The ranking to visualize. | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  | #' @param gene_sets A named list of gene sets (containing vectors of gene IDs) | 
					
						
							|  |  |  | #'   that will be shown as separate boxes. The names will be used as labels. | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  | #' | 
					
						
							|  |  |  | #' @seealso ranking() | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  | plot_boxplot <- function(ranking, gene_sets = NULL) { | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  |     if (!requireNamespace("plotly", quietly = TRUE)) { | 
					
						
							|  |  |  |         stop("Please install \"plotly\" to use this function.") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |     plot <- plotly::plot_ly() |> | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         plotly::add_boxplot( | 
					
						
							|  |  |  |             data = ranking, | 
					
						
							|  |  |  |             x = "All genes", | 
					
						
							|  |  |  |             y = ~score, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |             name = "All genes", | 
					
						
							|  |  |  |             showlegend = FALSE, | 
					
						
							|  |  |  |             line = list(color = base_color()) | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         ) |> | 
					
						
							|  |  |  |         plotly::layout( | 
					
						
							|  |  |  |             xaxis = list(tickvals = c("All genes", names(gene_sets))), | 
					
						
							|  |  |  |             yaxis = list(title = "Score") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (length(gene_sets) > 0) { | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |         index <- 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         for (gene_set_name in names(gene_sets)) { | 
					
						
							|  |  |  |             gene_set <- gene_sets[[gene_set_name]] | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |             plot <- plot |> plotly::add_boxplot( | 
					
						
							|  |  |  |                 data = ranking[gene %chin% gene_set], | 
					
						
							|  |  |  |                 x = gene_set_name, | 
					
						
							|  |  |  |                 y = ~score, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |                 name = gene_set_name, | 
					
						
							|  |  |  |                 showlegend = FALSE, | 
					
						
							|  |  |  |                 line = list(color = gene_set_color(index)) | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             index <- index + 1 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 17:23:18 +01:00
										 |  |  |     plot | 
					
						
							| 
									
										
										
										
											2021-11-06 13:03:36 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-22 14:10:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #' Show the distribution of scores across chromosomes. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' This function requires the package `plotly`. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @param ranking The ranking to visualize. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @seealso ranking() | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							|  |  |  | plot_chromosomes <- function(ranking) { | 
					
						
							|  |  |  |     if (!requireNamespace("plotly", quietly = TRUE)) { | 
					
						
							|  |  |  |         stop("Please install \"plotly\" to use this function.") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data <- merge(ranking, geposan::genes, by.x = "gene", by.y = "id") | 
					
						
							|  |  |  |     data <- data[, .(score = mean(score)), by = "chromosome"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Get an orderable integer from a chromosome name. | 
					
						
							|  |  |  |     chromosome_index <- function(chromosome) { | 
					
						
							|  |  |  |         index <- suppressWarnings(as.integer(chromosome)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ifelse( | 
					
						
							|  |  |  |             !is.na(index), | 
					
						
							|  |  |  |             index, | 
					
						
							|  |  |  |             ifelse( | 
					
						
							|  |  |  |                 chromosome == "X", | 
					
						
							|  |  |  |                 998, | 
					
						
							|  |  |  |                 999 | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data[, index := chromosome_index(chromosome)] | 
					
						
							|  |  |  |     setorder(data, "index") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     plotly::plot_ly( | 
					
						
							|  |  |  |         data = data, | 
					
						
							|  |  |  |         x = ~chromosome, | 
					
						
							|  |  |  |         y = ~score, | 
					
						
							| 
									
										
										
										
											2022-03-01 15:49:04 +01:00
										 |  |  |         type = "bar", | 
					
						
							|  |  |  |         marker = list(color = base_color()) | 
					
						
							| 
									
										
										
										
											2021-11-22 14:10:08 +01:00
										 |  |  |     ) |> | 
					
						
							|  |  |  |         plotly::layout( | 
					
						
							|  |  |  |             xaxis = list( | 
					
						
							|  |  |  |                 title = "Chromosome", | 
					
						
							|  |  |  |                 categoryorder = "array", | 
					
						
							|  |  |  |                 categoryarray = ~chromosome | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             yaxis = list(title = "Mean score") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-05-17 21:48:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #' Plot scores in relation to chromosomal position of genes. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @param ranking The ranking to visualize. | 
					
						
							|  |  |  | #' @param chromosome_name The chromosome to visualize. | 
					
						
							| 
									
										
										
										
											2022-05-18 17:29:35 +02:00
										 |  |  | #' @param gene_sets Named list of vectors of genes to highlight. The list names | 
					
						
							|  |  |  | #'   will be used as labels. | 
					
						
							| 
									
										
										
										
											2022-05-17 21:48:30 +02:00
										 |  |  | #' | 
					
						
							|  |  |  | #' @return A `plotly` figure. | 
					
						
							|  |  |  | #' @seealso ranking() | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							| 
									
										
										
										
											2022-05-18 17:29:35 +02:00
										 |  |  | plot_scores_by_position <- function(ranking, | 
					
						
							|  |  |  |                                     chromosome_name, | 
					
						
							|  |  |  |                                     gene_sets = NULL) { | 
					
						
							| 
									
										
										
										
											2022-05-17 21:48:30 +02:00
										 |  |  |     if (!requireNamespace("plotly", quietly = TRUE)) { | 
					
						
							|  |  |  |         stop("Please install \"plotly\" to use this function.") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     chromosome_name_ <- chromosome_name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data <- merge( | 
					
						
							|  |  |  |         ranking, | 
					
						
							|  |  |  |         geposan::distances[ | 
					
						
							|  |  |  |             species == "hsapiens" & | 
					
						
							|  |  |  |                 chromosome_name == chromosome_name_ | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         by = "gene" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-19 17:09:19 +02:00
										 |  |  |     data <- merge( | 
					
						
							|  |  |  |         data, | 
					
						
							|  |  |  |         geposan::genes, | 
					
						
							|  |  |  |         by.x = "gene", | 
					
						
							|  |  |  |         by.y = "id" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-18 17:29:35 +02:00
										 |  |  |     data[, `:=`(gene_set = "All genes", color = base_color())] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     index <- 1 | 
					
						
							|  |  |  |     for (gene_set_name in names(gene_sets)) { | 
					
						
							|  |  |  |         gene_set_genes <- gene_sets[[gene_set_name]] | 
					
						
							|  |  |  |         data[ | 
					
						
							|  |  |  |             gene %chin% gene_set_genes, | 
					
						
							|  |  |  |             `:=`( | 
					
						
							|  |  |  |                 gene_set = gene_set_name, | 
					
						
							|  |  |  |                 color = gene_set_color(index) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         index <- index + 1 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-17 21:48:30 +02:00
										 |  |  |     plotly::plot_ly() |> | 
					
						
							|  |  |  |         plotly::add_markers( | 
					
						
							|  |  |  |             data = data, | 
					
						
							|  |  |  |             x = ~start_position, | 
					
						
							|  |  |  |             y = ~score, | 
					
						
							| 
									
										
										
										
											2022-05-18 17:29:35 +02:00
										 |  |  |             name = ~gene_set, | 
					
						
							| 
									
										
										
										
											2022-05-19 17:09:19 +02:00
										 |  |  |             text = ~name, | 
					
						
							|  |  |  |             hovertemplate = paste0( | 
					
						
							|  |  |  |                 "<b>%{text}</b><br>", | 
					
						
							|  |  |  |                 "Position: %{x:.0} Bp<br>", | 
					
						
							|  |  |  |                 "Score: %{y:.3}<br>", | 
					
						
							|  |  |  |                 "<extra></extra>" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-05-17 21:48:30 +02:00
										 |  |  |         ) |> | 
					
						
							|  |  |  |         plotly::layout( | 
					
						
							|  |  |  |             xaxis = list(title = "Position (Bp)"), | 
					
						
							|  |  |  |             yaxis = list(title = "Score") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | } |