| 
									
										
										
										
											2021-10-20 15:34:52 +02:00
										 |  |  | # Construct UI for the methods editor. | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  | methods_ui <- function(id) { | 
					
						
							|  |  |  |     verticalLayout( | 
					
						
							|  |  |  |         h3("Methods"), | 
					
						
							| 
									
										
										
										
											2022-01-25 13:05:04 +01:00
										 |  |  |         selectInput( | 
					
						
							|  |  |  |             NS(id, "optimization_genes"), | 
					
						
							|  |  |  |             "Genes to optimize for", | 
					
						
							|  |  |  |             choices = list( | 
					
						
							|  |  |  |                 "Reference genes" = "reference", | 
					
						
							|  |  |  |                 "Comparison genes" = "comparison" | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |         selectInput( | 
					
						
							|  |  |  |             NS(id, "optimization_target"), | 
					
						
							|  |  |  |             "Optimization target", | 
					
						
							|  |  |  |             choices = list( | 
					
						
							| 
									
										
										
										
											2022-01-25 13:05:04 +01:00
										 |  |  |                 "Mean rank" = "mean", | 
					
						
							|  |  |  |                 "Median rank" = "median", | 
					
						
							|  |  |  |                 "First rank" = "min", | 
					
						
							|  |  |  |                 "Last rank" = "max", | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |                 "Customize weights" = "custom" | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |         lapply(methods, function(method) { | 
					
						
							|  |  |  |             verticalLayout( | 
					
						
							|  |  |  |                 checkboxInput( | 
					
						
							|  |  |  |                     NS(id, method$id), | 
					
						
							|  |  |  |                     span( | 
					
						
							|  |  |  |                         method$description, | 
					
						
							| 
									
										
										
										
											2021-12-15 12:41:12 +01:00
										 |  |  |                         class = "control-label" | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |                     ), | 
					
						
							|  |  |  |                     value = TRUE | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |                 sliderInput( | 
					
						
							|  |  |  |                     NS(id, sprintf("%s_weight", method$id)), | 
					
						
							|  |  |  |                     NULL, | 
					
						
							| 
									
										
										
										
											2021-11-16 15:20:42 +01:00
										 |  |  |                     min = -1.0, | 
					
						
							|  |  |  |                     max = 1.0, | 
					
						
							|  |  |  |                     step = 0.01, | 
					
						
							|  |  |  |                     value = 1.0 | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |                 ) | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 15:34:52 +02:00
										 |  |  | # Construct server for the methods editor. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # @param analysis The reactive containing the results to be weighted. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # @return A reactive containing the weighted results. | 
					
						
							| 
									
										
										
										
											2022-01-25 13:05:04 +01:00
										 |  |  | methods_server <- function(id, analysis, comparison_gene_ids) { | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |     moduleServer(id, function(input, output, session) { | 
					
						
							|  |  |  |         # Observe each method's enable button and synchronise the slider state. | 
					
						
							|  |  |  |         lapply(methods, function(method) { | 
					
						
							| 
									
										
										
										
											2021-12-17 13:14:42 +01:00
										 |  |  |             observeEvent(input[[method$id]], { | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |                 shinyjs::toggleState( | 
					
						
							|  |  |  |                     sprintf("%s_weight", method$id), | 
					
						
							| 
									
										
										
										
											2021-12-17 13:14:42 +01:00
										 |  |  |                     condition = input[[method$id]] | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             shinyjs::onclick(sprintf("%s_weight", method$id), { | 
					
						
							|  |  |  |                 updateSelectInput( | 
					
						
							|  |  |  |                     session, | 
					
						
							|  |  |  |                     "optimization_target", | 
					
						
							|  |  |  |                     selected = "custom" | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |                 ) | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 15:38:46 +01:00
										 |  |  |         reactive({ | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |             analysis <- analysis() | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |             weights <- NULL | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 13:05:04 +01:00
										 |  |  |             gene_ids <- if (input$optimization_genes == "comparison") { | 
					
						
							|  |  |  |                 comparison_gene_ids() | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 analysis$preset$reference_gene_ids | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (length(gene_ids) < 1 | input$optimization_target == "custom") { | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |                 for (method in methods) { | 
					
						
							|  |  |  |                     if (input[[method$id]]) { | 
					
						
							|  |  |  |                         weight <- input[[sprintf("%s_weight", method$id)]] | 
					
						
							|  |  |  |                         weights[[method$id]] <- weight | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 withProgress(message = "Optimizing weights", { | 
					
						
							|  |  |  |                     setProgress(0.2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     included_methods <- NULL | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     for (method in methods) { | 
					
						
							|  |  |  |                         if (input[[method$id]]) { | 
					
						
							|  |  |  |                             included_methods <- c(included_methods, method$id) | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     weights <- geposan::optimal_weights( | 
					
						
							|  |  |  |                         analysis, | 
					
						
							|  |  |  |                         included_methods, | 
					
						
							| 
									
										
										
										
											2022-01-25 13:05:04 +01:00
										 |  |  |                         gene_ids, | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |                         target = input$optimization_target | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     for (method_id in names(weights)) { | 
					
						
							|  |  |  |                         updateSliderInput( | 
					
						
							|  |  |  |                             session, | 
					
						
							|  |  |  |                             sprintf("%s_weight", method_id), | 
					
						
							|  |  |  |                             value = weights[[method_id]] | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:52:14 +01:00
										 |  |  |             geposan::ranking(analysis, weights) | 
					
						
							| 
									
										
										
										
											2021-10-19 16:44:29 +02:00
										 |  |  |         }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | } |