| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | # Find genes by training a neural network on reference position data. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # @param seed A seed to get reproducible results. | 
					
						
							| 
									
										
										
										
											2021-11-16 16:23:03 +01:00
										 |  |  | neural <- function(preset, | 
					
						
							|  |  |  |                    use_positions = FALSE, | 
					
						
							|  |  |  |                    progress = NULL, | 
					
						
							|  |  |  |                    seed = 448077) { | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  |     species_ids <- preset$species_ids | 
					
						
							| 
									
										
										
										
											2021-10-21 17:25:44 +02:00
										 |  |  |     gene_ids <- preset$gene_ids | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  |     reference_gene_ids <- preset$reference_gene_ids | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |     cached( | 
					
						
							|  |  |  |         "neural", | 
					
						
							| 
									
										
										
										
											2021-11-16 16:23:03 +01:00
										 |  |  |         c(species_ids, gene_ids, reference_gene_ids, use_positions), | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |         { # nolint | 
					
						
							|  |  |  |             set.seed(seed) | 
					
						
							|  |  |  |             gene_count <- length(gene_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Prefilter distances by species. | 
					
						
							|  |  |  |             distances <- geposan::distances[species %chin% species_ids] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Input data for the network. This contains the gene ID as an | 
					
						
							|  |  |  |             # identifier as well as the per-species gene distances as input | 
					
						
							|  |  |  |             # variables. | 
					
						
							|  |  |  |             data <- data.table(gene = gene_ids) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Buffer to keep track of species included in the computation. | 
					
						
							|  |  |  |             # Species from `species_ids` may be excluded if they don't have | 
					
						
							|  |  |  |             # enough data. | 
					
						
							|  |  |  |             species_ids_included <- NULL | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-14 19:27:11 +01:00
										 |  |  |             # Make a column containing positions for each species. | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             for (species_id in species_ids) { | 
					
						
							| 
									
										
										
										
											2021-11-16 16:23:03 +01:00
										 |  |  |                 species_data <- if (use_positions) { | 
					
						
							|  |  |  |                     setnames(distances[ | 
					
						
							|  |  |  |                         species == species_id, | 
					
						
							|  |  |  |                         .(gene, position) | 
					
						
							|  |  |  |                     ], "position", "distance") | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     distances[ | 
					
						
							|  |  |  |                         species == species_id, | 
					
						
							|  |  |  |                         .(gene, distance) | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 # Only include species with at least 25% known values. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-14 19:27:11 +01:00
										 |  |  |                 species_data <- stats::na.omit(species_data) | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-14 19:27:11 +01:00
										 |  |  |                 if (nrow(species_data) >= 0.25 * gene_count) { | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |                     species_ids_included <- c(species_ids_included, species_id) | 
					
						
							| 
									
										
										
										
											2021-11-14 19:27:11 +01:00
										 |  |  |                     data <- merge(data, species_data, all.x = TRUE) | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     # Replace missing data with mean values. The neural network | 
					
						
							|  |  |  |                     # can't handle NAs in a meaningful way. Choosing extreme | 
					
						
							|  |  |  |                     # values here would result in heavily biased results. | 
					
						
							|  |  |  |                     # Therefore, the mean value is chosen as a compromise. | 
					
						
							|  |  |  |                     # However, this will of course lessen the significance of | 
					
						
							|  |  |  |                     # the results. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 16:23:03 +01:00
										 |  |  |                     mean_distance <- round(species_data[, mean(distance)]) | 
					
						
							|  |  |  |                     data[is.na(distance), distance := mean_distance] | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     # Name the new column after the species. | 
					
						
							| 
									
										
										
										
											2021-11-16 16:23:03 +01:00
										 |  |  |                     setnames(data, "distance", species_id) | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             # Extract the reference genes. | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             reference_data <- data[gene %chin% reference_gene_ids] | 
					
						
							|  |  |  |             reference_data[, neural := 1.0] | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             # Take out random samples from the remaining genes. This is another | 
					
						
							|  |  |  |             # compromise with a negative impact on significance. Because there | 
					
						
							|  |  |  |             # is no information on genes with are explicitely *not* TPE-OLD | 
					
						
							|  |  |  |             # genes, we have to assume that a random sample of genes has a low | 
					
						
							|  |  |  |             # probability of including TPE-OLD genes. | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             without_reference_data <- data[!gene %chin% reference_gene_ids] | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             reference_samples <- without_reference_data[ | 
					
						
							|  |  |  |                 sample( | 
					
						
							|  |  |  |                     nrow(without_reference_data), | 
					
						
							|  |  |  |                     nrow(reference_data) | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ] | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             reference_samples[, neural := 0.0] | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             # Merge training data. The training data includes all reference | 
					
						
							|  |  |  |             # genes as well as an equal number of random sample genes. | 
					
						
							|  |  |  |             training_data <- rbindlist(list(reference_data, reference_samples)) | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             # Construct and train the neural network. | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             nn_formula <- stats::as.formula(sprintf( | 
					
						
							|  |  |  |                 "neural~%s", | 
					
						
							|  |  |  |                 paste(species_ids_included, collapse = "+") | 
					
						
							|  |  |  |             )) | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             layer1 <- length(species_ids) * 0.66 | 
					
						
							|  |  |  |             layer2 <- layer1 * 0.66 | 
					
						
							|  |  |  |             layer3 <- layer2 * 0.66 | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             nn <- neuralnet::neuralnet( | 
					
						
							|  |  |  |                 nn_formula, | 
					
						
							|  |  |  |                 training_data, | 
					
						
							|  |  |  |                 hidden = c(layer1, layer2, layer3), | 
					
						
							|  |  |  |                 linear.output = FALSE | 
					
						
							| 
									
										
										
										
											2021-10-21 17:25:44 +02:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             if (!is.null(progress)) { | 
					
						
							| 
									
										
										
										
											2021-11-16 12:18:03 +01:00
										 |  |  |                 progress(0.33) | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             # Apply the neural network. | 
					
						
							|  |  |  |             data[, score := neuralnet::compute(nn, data)$net.result] | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 12:18:03 +01:00
										 |  |  |             # Reconstruct and run the neural network for each training gene | 
					
						
							|  |  |  |             # by training it without the respective gene. | 
					
						
							|  |  |  |             for (gene_id in training_data$gene) { | 
					
						
							|  |  |  |                 nn <- neuralnet::neuralnet( | 
					
						
							|  |  |  |                     nn_formula, | 
					
						
							|  |  |  |                     training_data[gene != gene_id], | 
					
						
							|  |  |  |                     hidden = c(layer1, layer2, layer3), | 
					
						
							|  |  |  |                     linear.output = FALSE | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 data[ | 
					
						
							|  |  |  |                     gene == gene_id, | 
					
						
							|  |  |  |                     score := neuralnet::compute( | 
					
						
							|  |  |  |                         nn, | 
					
						
							|  |  |  |                         training_data[gene == gene_id] | 
					
						
							|  |  |  |                     )$net.result | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             if (!is.null(progress)) { | 
					
						
							|  |  |  |                 progress(1.0) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-19 15:03:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |             data[, .(gene, score)] | 
					
						
							| 
									
										
										
										
											2021-10-21 17:25:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-05 19:49:54 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2021-10-19 13:39:55 +02:00
										 |  |  | } |