Implement all methods using positions additionally

This commit is contained in:
Elias Projahn 2021-11-05 19:49:54 +01:00
parent 9cbc127177
commit cfc5e7a6bf
5 changed files with 222 additions and 167 deletions

View file

@ -35,7 +35,19 @@ analyze <- function(preset, progress = NULL) {
"clusteriness" = clusteriness, "clusteriness" = clusteriness,
"correlation" = correlation, "correlation" = correlation,
"proximity" = proximity, "proximity" = proximity,
"neural" = neural "neural" = neural,
"clusteriness_positions" = function(...) {
clusteriness(..., use_positions = TRUE)
},
"correlation_positions" = function(...) {
correlation(..., use_positions = TRUE)
},
"proximity_positions" = function(...) {
proximity(..., use_positions = TRUE)
},
"neural_positions" = function(...) {
neural(..., use_positions = TRUE)
}
) )
results <- cached("analysis", preset, { results <- cached("analysis", preset, {
@ -50,7 +62,11 @@ analyze <- function(preset, progress = NULL) {
} }
} }
method_results <- methods[[method_id]](preset, method_progress) method_results <- methods[[method_id]](
preset,
progress = method_progress
)
setnames(method_results, "score", method_id) setnames(method_results, "score", method_id)
results <- merge( results <- merge(

View file

@ -36,11 +36,11 @@ clusteriness_priv <- function(data, height = 1000000) {
} }
# Process genes clustering their distance to telomeres. # Process genes clustering their distance to telomeres.
clusteriness <- function(preset, progress = NULL) { clusteriness <- function(preset, use_positions = FALSE, progress = NULL) {
species_ids <- preset$species_ids species_ids <- preset$species_ids
gene_ids <- preset$gene_ids gene_ids <- preset$gene_ids
cached("clusteriness", c(species_ids, gene_ids), { cached("clusteriness", c(species_ids, gene_ids, use_positions), {
results <- data.table(gene = gene_ids) results <- data.table(gene = gene_ids)
# Prefilter the input data by species. # Prefilter the input data by species.
@ -54,7 +54,13 @@ clusteriness <- function(preset, progress = NULL) {
# Perform the cluster analysis for one gene. # Perform the cluster analysis for one gene.
compute <- function(gene_id) { compute <- function(gene_id) {
score <- clusteriness_priv(distances[gene_id, distance]) data <- if (use_positions) {
distances[gene_id, position]
} else {
distances[gene_id, distance]
}
score <- clusteriness_priv(data)
if (!is.null(progress)) { if (!is.null(progress)) {
genes_done <<- genes_done + 1 genes_done <<- genes_done + 1

View file

@ -1,30 +1,40 @@
# Compute the mean correlation coefficient comparing gene distances with a set # Compute the mean correlation coefficient comparing gene distances with a set
# of reference genes. # of reference genes.
correlation <- function(preset, progress = NULL) { correlation <- function(preset, use_positions = FALSE, progress = NULL) {
species_ids <- preset$species_ids species_ids <- preset$species_ids
gene_ids <- preset$gene_ids gene_ids <- preset$gene_ids
reference_gene_ids <- preset$reference_gene_ids reference_gene_ids <- preset$reference_gene_ids
cached("correlation", c(species_ids, gene_ids, reference_gene_ids), { cached(
"correlation",
c(species_ids, gene_ids, reference_gene_ids, use_positions),
{ # nolint
# Prefilter distances by species. # Prefilter distances by species.
distances <- geposan::distances[species %chin% species_ids] distances <- geposan::distances[species %chin% species_ids]
# Tranform data to get species as rows and genes as columns. We # Tranform data to get species as rows and genes as columns. We
# construct columns per species, because it requires fewer iterations, # construct columns per species, because it requires fewer
# and transpose the table afterwards. # iterations, and transpose the table afterwards.
data <- data.table(gene = gene_ids) data <- data.table(gene = gene_ids)
# Make a column containing distance data for each species. # Make a column containing distance data for each species.
for (species_id in species_ids) { for (species_id in species_ids) {
species_distances <- distances[ species_data <- if (use_positions) {
setnames(distances[
species == species_id,
.(gene, position)
], "position", "distance")
} else {
distances[
species == species_id, species == species_id,
.(gene, distance) .(gene, distance)
] ]
data <- merge(data, species_distances, all.x = TRUE)
setnames(data, "distance", species_id)
} }
}
data <- merge(data, species_data, all.x = TRUE)
setnames(data, "distance", species_id)
# Transpose to the desired format. # Transpose to the desired format.
data <- transpose(data, make.names = "gene") data <- transpose(data, make.names = "gene")
@ -54,8 +64,8 @@ correlation <- function(preset, progress = NULL) {
if (!is.null(progress)) progress(0.66) if (!is.null(progress)) progress(0.66)
# Compute the final score as the mean of known correlation scores. # Compute the final score as the mean of known correlation scores.
# Negative correlations will correctly lessen the score, which will be # Negative correlations will correctly lessen the score, which will
# clamped to zero as its lower bound. Genes with no possible # be clamped to zero as its lower bound. Genes with no possible
# correlations at all will be assumed to have a score of 0.0. # correlations at all will be assumed to have a score of 0.0.
compute_score <- function(scores) { compute_score <- function(scores) {
@ -75,5 +85,6 @@ correlation <- function(preset, progress = NULL) {
] ]
results[, .(gene, score)] results[, .(gene, score)]
}) }
)
} }

View file

@ -1,46 +1,62 @@
# Find genes by training a neural network on reference position data. # Find genes by training a neural network on reference position data.
# #
# @param seed A seed to get reproducible results. # @param seed A seed to get reproducible results.
neural <- function(preset, progress = NULL, seed = 448077) { neural <- function(preset,
use_positions = FALSE,
progress = NULL,
seed = 448077) {
species_ids <- preset$species_ids species_ids <- preset$species_ids
gene_ids <- preset$gene_ids gene_ids <- preset$gene_ids
reference_gene_ids <- preset$reference_gene_ids reference_gene_ids <- preset$reference_gene_ids
cached("neural", c(species_ids, gene_ids, reference_gene_ids), { cached(
"neural",
c(species_ids, gene_ids, reference_gene_ids, use_positions),
{ # nolint
set.seed(seed) set.seed(seed)
gene_count <- length(gene_ids) gene_count <- length(gene_ids)
# Prefilter distances by species. # Prefilter distances by species.
distances <- geposan::distances[species %chin% species_ids] distances <- geposan::distances[species %chin% species_ids]
# Input data for the network. This contains the gene ID as an identifier # Input data for the network. This contains the gene ID as an
# as well as the per-species gene distances as input variables. # identifier as well as the per-species gene distances as input
# variables.
data <- data.table(gene = gene_ids) data <- data.table(gene = gene_ids)
# Buffer to keep track of species included in the computation. Species # Buffer to keep track of species included in the computation.
# from `species_ids` may be excluded if they don't have enough data. # Species from `species_ids` may be excluded if they don't have
# enough data.
species_ids_included <- NULL species_ids_included <- NULL
# Make a column containing distance data for each species. # Make a column containing distance data for each species.
for (species_id in species_ids) { for (species_id in species_ids) {
species_distances <- distances[ species_data <- if (use_positions) {
setnames(distances[
species == species_id,
.(gene, position)
], "position", "distance")
} else {
distances[
species == species_id, species == species_id,
.(gene, distance) .(gene, distance)
] ]
}
# Only include species with at least 25% known values. # Only include species with at least 25% known values.
species_distances <- stats::na.omit(species_distances) species_distances <- stats::na.omit(species_data)
if (nrow(species_distances) >= 0.25 * gene_count) { if (nrow(species_distances) >= 0.25 * gene_count) {
species_ids_included <- c(species_ids_included, species_id) species_ids_included <- c(species_ids_included, species_id)
data <- merge(data, species_distances, all.x = TRUE) data <- merge(data, species_distances, all.x = TRUE)
# Replace missing data with mean values. The neural network # Replace missing data with mean values. The neural network
# can't handle NAs in a meaningful way. Choosing extreme values # can't handle NAs in a meaningful way. Choosing extreme
# here would result in heavily biased results. Therefore, the # values here would result in heavily biased results.
# mean value is chosen as a compromise. However, this will of # Therefore, the mean value is chosen as a compromise.
# course lessen the significance of the results. # However, this will of course lessen the significance of
# the results.
mean_distance <- round(species_distances[, mean(distance)]) mean_distance <- round(species_distances[, mean(distance)])
data[is.na(distance), distance := mean_distance] data[is.na(distance), distance := mean_distance]
@ -56,10 +72,10 @@ neural <- function(preset, progress = NULL, seed = 448077) {
reference_data[, neural := 1.0] reference_data[, neural := 1.0]
# Take out random samples from the remaining genes. This is another # Take out random samples from the remaining genes. This is another
# compromise with a negative impact on significance. Because there is # compromise with a negative impact on significance. Because there
# no information on genes with are explicitely *not* TPE-OLD genes, we # is no information on genes with are explicitely *not* TPE-OLD
# have to assume that a random sample of genes has a low probability of # genes, we have to assume that a random sample of genes has a low
# including TPE-OLD genes. # probability of including TPE-OLD genes.
without_reference_data <- data[!gene %chin% reference_gene_ids] without_reference_data <- data[!gene %chin% reference_gene_ids]
@ -72,8 +88,8 @@ neural <- function(preset, progress = NULL, seed = 448077) {
reference_samples[, neural := 0.0] reference_samples[, neural := 0.0]
# Merge training data. The training data includes all reference genes as # Merge training data. The training data includes all reference
# well as an equal number of random sample genes. # genes as well as an equal number of random sample genes.
training_data <- rbindlist(list(reference_data, reference_samples)) training_data <- rbindlist(list(reference_data, reference_samples))
# Construct and train the neural network. # Construct and train the neural network.
@ -96,8 +112,8 @@ neural <- function(preset, progress = NULL, seed = 448077) {
if (!is.null(progress)) { if (!is.null(progress)) {
# We do everything in one go, so it's not possible to report # We do everything in one go, so it's not possible to report
# detailed progress information. As the method is relatively quick, # detailed progress information. As the method is relatively
# this should not be a problem. # quick, this should not be a problem.
progress(0.5) progress(0.5)
} }
@ -110,5 +126,6 @@ neural <- function(preset, progress = NULL, seed = 448077) {
} }
data[, .(gene, score)] data[, .(gene, score)]
}) }
)
} }

View file

@ -2,21 +2,26 @@
# #
# A score will be given to each gene such that 0.0 corresponds to the maximal # A score will be given to each gene such that 0.0 corresponds to the maximal
# mean distance across all genes and 1.0 corresponds to a distance of 0. # mean distance across all genes and 1.0 corresponds to a distance of 0.
proximity <- function(preset, progress = NULL) { proximity <- function(preset, use_positions = FALSE, progress = NULL) {
species_ids <- preset$species_ids species_ids <- preset$species_ids
gene_ids <- preset$gene_ids gene_ids <- preset$gene_ids
cached("proximity", c(species_ids, gene_ids), { cached("proximity", c(species_ids, gene_ids, use_positions), {
# Prefilter distances by species and gene. # Prefilter distances by species and gene.
distances <- geposan::distances[ data <- geposan::distances[
species %chin% preset$species_ids & gene %chin% preset$gene_ids species %chin% preset$species_ids & gene %chin% preset$gene_ids
] ]
# Compute the score as described above. # Compute the score as described above.
distances <- distances[, .(mean_distance = mean(distance)), by = "gene"] data <- if (use_positions) {
max_distance <- distances[, max(mean_distance)] data[, .(mean_distance = mean(position)), by = "gene"]
distances[, score := 1 - mean_distance / max_distance] } else {
data[, .(mean_distance = mean(distance)), by = "gene"]
}
max_distance <- data[, max(mean_distance)]
data[, score := 1 - mean_distance / max_distance]
if (!is.null(progress)) { if (!is.null(progress)) {
# We do everything in one go, so it's not possible to report # We do everything in one go, so it's not possible to report
@ -25,6 +30,6 @@ proximity <- function(preset, progress = NULL) {
progress(1.0) progress(1.0)
} }
distances[, .(gene, score)] data[, .(gene, score)]
}) })
} }