| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  | #' Analyze genes based on position data. | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | #' | 
					
						
							| 
									
										
										
										
											2021-11-03 14:17:39 +01:00
										 |  |  | #' @param preset The preset to use which should be created using [preset()]. | 
					
						
							| 
									
										
										
										
											2021-10-19 15:03:10 +02:00
										 |  |  | #' @param progress A function to be called for progress information. The | 
					
						
							|  |  |  | #'   function should accept a number between 0.0 and 1.0 for the current | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  | #'   progress. If no function is provided, a simple text progress bar will be | 
					
						
							|  |  |  | #'   shown. | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | #' | 
					
						
							| 
									
										
										
										
											2021-11-12 10:16:11 +01:00
										 |  |  | #' @returns An object containing the results of the analysis with the following | 
					
						
							|  |  |  | #'   items: | 
					
						
							|  |  |  | #'   \describe{ | 
					
						
							|  |  |  | #'     \item{`preset`}{The preset that was used.} | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  | #'     \item{`scores`}{Table containing all scores for each gene.} | 
					
						
							|  |  |  | #'     \item{`results`}{Results from the different methods including details.} | 
					
						
							| 
									
										
										
										
											2021-11-12 10:16:11 +01:00
										 |  |  | #'   } | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							| 
									
										
										
										
											2021-10-19 15:03:10 +02:00
										 |  |  | analyze <- function(preset, progress = NULL) { | 
					
						
							| 
									
										
										
										
											2021-12-13 09:50:45 +01:00
										 |  |  |     if (!inherits(preset, "geposan_preset")) { | 
					
						
							| 
									
										
										
										
											2021-11-03 14:17:39 +01:00
										 |  |  |         stop("Preset is invalid. Use geposan::preset() to create one.") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |     if (is.null(progress)) { | 
					
						
							|  |  |  |         progress_bar <- progress::progress_bar$new() | 
					
						
							|  |  |  |         progress_bar$update(0.0) | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |         progress <- function(progress_value) { | 
					
						
							|  |  |  |             if (!progress_bar$finished) { | 
					
						
							|  |  |  |                 progress_bar$update(progress_value) | 
					
						
							|  |  |  |                 if (progress_value >= 1.0) { | 
					
						
							|  |  |  |                     progress_bar$terminate() | 
					
						
							| 
									
										
										
										
											2021-11-03 14:17:39 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-21 17:25:44 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-19 15:03:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |     progress_buffer <- 0.0 | 
					
						
							|  |  |  |     method_count <- length(preset$methods) | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |     method_progress <- function(progress_value) { | 
					
						
							|  |  |  |         progress(progress_buffer + progress_value / method_count) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |     scores <- data.table(gene = preset$gene_id) | 
					
						
							|  |  |  |     results <- list() | 
					
						
							| 
									
										
										
										
											2021-10-19 15:03:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |     for (method in preset$methods) { | 
					
						
							|  |  |  |         method_results <- method$func(preset, method_progress) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 14:18:57 +01:00
										 |  |  |         scores <- merge(scores, method_results$scores, by = "gene") | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |         setnames(scores, "score", method$id) | 
					
						
							| 
									
										
										
										
											2021-10-19 15:03:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |         results <- c(results, list(method_results)) | 
					
						
							| 
									
										
										
										
											2021-11-19 15:07:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |         progress_buffer <- progress_buffer + 1 / method_count | 
					
						
							|  |  |  |         progress(progress_buffer) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-19 15:07:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |     structure( | 
					
						
							|  |  |  |         list( | 
					
						
							|  |  |  |             preset = preset, | 
					
						
							|  |  |  |             scores = scores, | 
					
						
							|  |  |  |             results = results | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         class = "geposan_analysis" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-19 15:07:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  | #' Print an analysis object. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @param x The analysis to print. | 
					
						
							|  |  |  | #' @param ... Other parameters. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @seealso [analyze()] | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							|  |  |  | print.geposan_analysis <- function(x, ...) { | 
					
						
							|  |  |  |     cat("geposan analysis:\n\n") | 
					
						
							|  |  |  |     print(x$preset) | 
					
						
							|  |  |  |     cat("\n") | 
					
						
							| 
									
										
										
										
											2021-11-05 14:33:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |     for (result in x$results) { | 
					
						
							|  |  |  |         print(result) | 
					
						
							|  |  |  |         cat("\n") | 
					
						
							| 
									
										
										
										
											2021-11-17 22:57:31 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:01:44 +01:00
										 |  |  |     invisible(x) | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | } |