Move species count to analysis

This commit is contained in:
Elias Projahn 2021-11-17 22:57:31 +01:00
parent f84d37dd30
commit 33056bfa40
6 changed files with 30 additions and 45 deletions

View file

@ -75,13 +75,23 @@ analyze <- function(preset, progress = NULL) {
total_progress <- total_progress + 1 / method_count total_progress <- total_progress + 1 / method_count
} }
if (!is.null(progress)) { # Count included species from the preset per gene.
progress(1.0) genes_n_species <- geposan::distances[
} species %chin% preset$species_ids,
.(n_species = .N),
by = "gene"
]
results setkey(genes_n_species, "gene")
# Return the results for genes with enough species.
results[genes_n_species[gene, n_species] >= preset$min_n_species]
}) })
if (!is.null(progress)) {
progress(1.0)
}
structure( structure(
list( list(
preset = preset, preset = preset,

View file

@ -14,6 +14,8 @@
#' @param methods Methods to apply. #' @param methods Methods to apply.
#' @param species_ids IDs of species to include. #' @param species_ids IDs of species to include.
#' @param gene_ids IDs of genes to screen. #' @param gene_ids IDs of genes to screen.
#' @param min_n_species Minimum number of orthologs that a gene should have to
#' be included in the analysis.
#' @param reference_gene_ids IDs of reference genes to compare to. #' @param reference_gene_ids IDs of reference genes to compare to.
#' #'
#' @return The preset to use with [analyze()]. #' @return The preset to use with [analyze()].
@ -27,6 +29,7 @@ preset <- function(methods = c(
), ),
species_ids = NULL, species_ids = NULL,
gene_ids = NULL, gene_ids = NULL,
min_n_species = 10,
reference_gene_ids = NULL) { reference_gene_ids = NULL) {
# The included data gets sorted to be able to produce predictable hashes # The included data gets sorted to be able to produce predictable hashes
# for the object later. # for the object later.
@ -35,6 +38,7 @@ preset <- function(methods = c(
methods = sort(methods), methods = sort(methods),
species_ids = sort(species_ids), species_ids = sort(species_ids),
gene_ids = sort(gene_ids), gene_ids = sort(gene_ids),
min_n_species = as.numeric(min_n_species),
reference_gene_ids = sort(reference_gene_ids) reference_gene_ids = sort(reference_gene_ids)
), ),
class = "geposan_preset" class = "geposan_preset"
@ -60,6 +64,8 @@ print.geposan_preset <- function(x, ...) {
length(x$gene_ids) length(x$gene_ids)
)) ))
cat(sprintf("\n Species per gene: \u2265 %i", x$min_n_species))
cat(sprintf( cat(sprintf(
"\n Comparison data: %i reference genes\n", "\n Comparison data: %i reference genes\n",
length(x$reference_gene_ids) length(x$reference_gene_ids)

View file

@ -6,33 +6,18 @@
#' @param analysis Analysis object resulting from [analyze()]. #' @param analysis Analysis object resulting from [analyze()].
#' @param weights Named list pairing method names with weighting factors. Only #' @param weights Named list pairing method names with weighting factors. Only
#' methods that are contained within this list will be included. #' methods that are contained within this list will be included.
#' @param min_n_species Minimum number of required species per gene. Genes that
#' have fewer species will not be included in the ranking.
#' #'
#' @returns A ranking object. The object extends the analysis result with #' @returns A ranking object. The object extends the analysis result with
#' additional columns containing the `score` and the `rank` of each gene. It #' additional columns containing the `score` and the `rank` of each gene. It
#' will be ordered by rank. #' will be ordered by rank.
#' #'
#' @export #' @export
ranking <- function(analysis, weights, min_n_species = 10) { ranking <- function(analysis, weights) {
if (!"geposan_analysis" %chin% class(analysis)) { if (!"geposan_analysis" %chin% class(analysis)) {
stop("Invalid analyis. Use geposan::analyze().") stop("Invalid analyis. Use geposan::analyze().")
} }
# Count included species from the preset per gene. ranking <- copy(analysis$results)
genes_n_species <- geposan::distances[
species %chin% analysis$preset$species_ids,
.(n_species = .N),
by = "gene"
]
setkey(genes_n_species, gene)
# Exclude genes with too few species.
ranking <- analysis$results[
genes_n_species[gene, n_species] >= min_n_species
]
ranking[, score := 0.0] ranking[, score := 0.0]
for (method in names(weights)) { for (method in names(weights)) {
@ -65,16 +50,13 @@ ranking <- function(analysis, weights, min_n_species = 10) {
#' @param reference_gene_ids IDs of the reference genes. #' @param reference_gene_ids IDs of the reference genes.
#' @param target The optimization target. It may be one of "mean", "min" or #' @param target The optimization target. It may be one of "mean", "min" or
#' "max" and results in the respective rank being optimized. #' "max" and results in the respective rank being optimized.
#' @param min_n_species Minimum number of required species per gene. Genes that
#' have fewer species will not be included in the rankings used to find the
#' optimal weights.
#' #'
#' @returns Named list pairing method names with their optimal weights. This #' @returns Named list pairing method names with their optimal weights. This
#' can be used as an argument to [ranking()]. #' can be used as an argument to [ranking()].
#' #'
#' @export #' @export
optimal_weights <- function(analysis, methods, reference_gene_ids, optimal_weights <- function(analysis, methods, reference_gene_ids,
target = "mean", min_n_species = 10) { target = "mean") {
if (!"geposan_analysis" %chin% class(analysis)) { if (!"geposan_analysis" %chin% class(analysis)) {
stop("Invalid analyis. Use geposan::analyze().") stop("Invalid analyis. Use geposan::analyze().")
} }
@ -92,11 +74,7 @@ optimal_weights <- function(analysis, methods, reference_gene_ids,
# Compute the target rank of the reference genes when applying the weights. # Compute the target rank of the reference genes when applying the weights.
target_rank <- function(factors) { target_rank <- function(factors) {
data <- ranking( data <- ranking(analysis, weights(factors))
analysis,
weights(factors),
min_n_species = min_n_species
)
result <- data[gene %chin% reference_gene_ids, if (target == "min") { result <- data[gene %chin% reference_gene_ids, if (target == "min") {
min(rank) min(rank)

View file

@ -4,13 +4,7 @@
\alias{optimal_weights} \alias{optimal_weights}
\title{Find the best weights to rank the results.} \title{Find the best weights to rank the results.}
\usage{ \usage{
optimal_weights( optimal_weights(analysis, methods, reference_gene_ids, target = "mean")
analysis,
methods,
reference_gene_ids,
target = "mean",
min_n_species = 10
)
} }
\arguments{ \arguments{
\item{analysis}{Results from \code{\link[=analyze]{analyze()}} or \code{\link[=ranking]{ranking()}}.} \item{analysis}{Results from \code{\link[=analyze]{analyze()}} or \code{\link[=ranking]{ranking()}}.}
@ -21,10 +15,6 @@ optimal_weights(
\item{target}{The optimization target. It may be one of "mean", "min" or \item{target}{The optimization target. It may be one of "mean", "min" or
"max" and results in the respective rank being optimized.} "max" and results in the respective rank being optimized.}
\item{min_n_species}{Minimum number of required species per gene. Genes that
have fewer species will not be included in the rankings used to find the
optimal weights.}
} }
\value{ \value{
Named list pairing method names with their optimal weights. This Named list pairing method names with their optimal weights. This

View file

@ -8,6 +8,7 @@ preset(
methods = c("clusteriness", "correlation", "neural", "proximity"), methods = c("clusteriness", "correlation", "neural", "proximity"),
species_ids = NULL, species_ids = NULL,
gene_ids = NULL, gene_ids = NULL,
min_n_species = 10,
reference_gene_ids = NULL reference_gene_ids = NULL
) )
} }
@ -18,6 +19,9 @@ preset(
\item{gene_ids}{IDs of genes to screen.} \item{gene_ids}{IDs of genes to screen.}
\item{min_n_species}{Minimum number of orthologs that a gene should have to
be included in the analysis.}
\item{reference_gene_ids}{IDs of reference genes to compare to.} \item{reference_gene_ids}{IDs of reference genes to compare to.}
} }
\value{ \value{

View file

@ -4,16 +4,13 @@
\alias{ranking} \alias{ranking}
\title{Rank the results by computing a score.} \title{Rank the results by computing a score.}
\usage{ \usage{
ranking(analysis, weights, min_n_species = 10) ranking(analysis, weights)
} }
\arguments{ \arguments{
\item{analysis}{Analysis object resulting from \code{\link[=analyze]{analyze()}}.} \item{analysis}{Analysis object resulting from \code{\link[=analyze]{analyze()}}.}
\item{weights}{Named list pairing method names with weighting factors. Only \item{weights}{Named list pairing method names with weighting factors. Only
methods that are contained within this list will be included.} methods that are contained within this list will be included.}
\item{min_n_species}{Minimum number of required species per gene. Genes that
have fewer species will not be included in the ranking.}
} }
\value{ \value{
A ranking object. The object extends the analysis result with A ranking object. The object extends the analysis result with