| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  | #' Compare a set of genes with the ranking. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @param ranking A ranking created using [ranking()]. | 
					
						
							|  |  |  | #' @param comparison_gene_ids IDs of the genes of interest. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @returns A comparison object with the following items: | 
					
						
							|  |  |  | #'   \describe{ | 
					
						
							| 
									
										
										
										
											2022-01-26 10:56:35 +01:00
										 |  |  | #'     \item{`quantiles`}{A `data.table` containing quantile values for the | 
					
						
							|  |  |  | #'       score, rank and percentile of the comparison genes. | 
					
						
							|  |  |  | #'     } | 
					
						
							|  |  |  | #'     \item{`mean_score`}{The mean score of the genes.} | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  | #'     \item{`mean_rank`}{The mean rank of the genes.} | 
					
						
							| 
									
										
										
										
											2022-01-26 10:56:35 +01:00
										 |  |  | #'     \item{`mean_percentile`}{The mean percentile of the genes.} | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  | #'     \item{`p_value`}{p-value for the null hypothesis that the comparison | 
					
						
							|  |  |  | #'       genes do _not_ rank better than other genes. In other words: A low | 
					
						
							|  |  |  | #'       p-value means that the comparison genes rank particularly high.} | 
					
						
							|  |  |  | #'   } | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							|  |  |  | compare <- function(ranking, comparison_gene_ids) { | 
					
						
							| 
									
										
										
										
											2021-12-13 09:50:45 +01:00
										 |  |  |     if (!inherits(ranking, "geposan_ranking")) { | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  |         stop("Invalid ranking. Use geposan::ranking().") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     comparison_ranking <- ranking[gene %chin% comparison_gene_ids] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 10:56:35 +01:00
										 |  |  |     quantiles <- data.table( | 
					
						
							|  |  |  |         quantile = c("0%", "25%", "50%", "75%", "100%"), | 
					
						
							|  |  |  |         score = stats::quantile(comparison_ranking[, score]), | 
					
						
							|  |  |  |         rank = stats::quantile( | 
					
						
							|  |  |  |             comparison_ranking[, rank], | 
					
						
							|  |  |  |             probs = seq(1, 0, -0.25) | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         percentile = stats::quantile(comparison_ranking[, percentile]) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 13:22:57 +01:00
										 |  |  |     p_value <- stats::wilcox.test( | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  |         x = comparison_ranking[, score], | 
					
						
							|  |  |  |         y = ranking[!gene %chin% comparison_gene_ids, score], | 
					
						
							|  |  |  |         alternative = "greater" | 
					
						
							|  |  |  |     )$p.value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     structure( | 
					
						
							|  |  |  |         list( | 
					
						
							| 
									
										
										
										
											2022-01-26 10:56:35 +01:00
										 |  |  |             quantiles = quantiles, | 
					
						
							|  |  |  |             mean_score = comparison_ranking[, mean(score)], | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  |             mean_rank = comparison_ranking[, mean(rank)], | 
					
						
							| 
									
										
										
										
											2022-01-26 10:56:35 +01:00
										 |  |  |             mean_percentile = comparison_ranking[, mean(percentile)], | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  |             p_value = p_value | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         class = "geposan_comparison" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #' S3 method to print a comparison object. | 
					
						
							|  |  |  | #' | 
					
						
							| 
									
										
										
										
											2021-11-06 13:22:57 +01:00
										 |  |  | #' @param x The comparison to print. | 
					
						
							|  |  |  | #' @param ... Other parameters. | 
					
						
							|  |  |  | #' | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  | #' @seealso [compare()] | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' @export | 
					
						
							| 
									
										
										
										
											2021-11-06 13:22:57 +01:00
										 |  |  | print.geposan_comparison <- function(x, ...) { | 
					
						
							| 
									
										
										
										
											2022-01-26 10:56:35 +01:00
										 |  |  |     cat("geposan comparison:\n\n") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     quantiles_formatted <- x$quantiles[, .( | 
					
						
							|  |  |  |         "Quantile" = quantile, | 
					
						
							|  |  |  |         "Score" = round(score, 3), | 
					
						
							|  |  |  |         "Rank" = rank, | 
					
						
							|  |  |  |         "Percentile" = paste0( | 
					
						
							|  |  |  |             format(round(percentile * 100, 1), nsmall = 1), | 
					
						
							|  |  |  |             "%" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     )] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print(quantiles_formatted, row.names = FALSE) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  |     cat(sprintf( | 
					
						
							| 
									
										
										
										
											2022-01-26 10:56:35 +01:00
										 |  |  |         paste0( | 
					
						
							|  |  |  |             "\n  Mean score: %.3f", | 
					
						
							|  |  |  |             "\n  Mean rank: %.1f", | 
					
						
							|  |  |  |             "\n  Mean percentile: %.1f%%", | 
					
						
							|  |  |  |             "\n  p-value for better scores: %.4f\n" | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  |         ), | 
					
						
							| 
									
										
										
										
											2022-01-26 10:56:35 +01:00
										 |  |  |         x$mean_score, | 
					
						
							| 
									
										
										
										
											2021-11-06 13:22:57 +01:00
										 |  |  |         x$mean_rank, | 
					
						
							| 
									
										
										
										
											2022-01-26 10:56:35 +01:00
										 |  |  |         x$mean_percentile * 100, | 
					
						
							| 
									
										
										
										
											2021-11-06 13:22:57 +01:00
										 |  |  |         x$p_value | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  |     )) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 13:22:57 +01:00
										 |  |  |     invisible(x) | 
					
						
							| 
									
										
										
										
											2021-11-05 19:08:50 +01:00
										 |  |  | } |