mirror of
https://github.com/johrpan/geposan.git
synced 2025-10-26 18:57:25 +01:00
ranking: Filter out species with too few species
This commit is contained in:
parent
910eb06fe9
commit
4dda7fa49e
3 changed files with 41 additions and 7 deletions
31
R/ranking.R
31
R/ranking.R
|
|
@ -6,19 +6,33 @@
|
||||||
#' @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) {
|
ranking <- function(analysis, weights, min_n_species = 10) {
|
||||||
if (!"geposan_analysis" %chin% class(analysis)) {
|
if (!"geposan_analysis" %chin% class(analysis)) {
|
||||||
stop("Invalid analyis. Use geposan::analyze().")
|
stop("Invalid analyis. Use geposan::analyze().")
|
||||||
}
|
}
|
||||||
|
|
||||||
ranking <- copy(analysis$results)
|
# Count included species from the preset per gene.
|
||||||
ranking[, score := 0.0]
|
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,
|
||||||
|
.(score = 0.0)
|
||||||
|
]
|
||||||
|
|
||||||
for (method in names(weights)) {
|
for (method in names(weights)) {
|
||||||
weighted <- weights[[method]] * ranking[, ..method]
|
weighted <- weights[[method]] * ranking[, ..method]
|
||||||
|
|
@ -47,13 +61,16 @@ ranking <- function(analysis, weights) {
|
||||||
#' @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") {
|
target = "mean", min_n_species = 10) {
|
||||||
if (!"geposan_analysis" %chin% class(analysis)) {
|
if (!"geposan_analysis" %chin% class(analysis)) {
|
||||||
stop("Invalid analyis. Use geposan::analyze().")
|
stop("Invalid analyis. Use geposan::analyze().")
|
||||||
}
|
}
|
||||||
|
|
@ -71,7 +88,11 @@ 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(analysis, weights(factors))
|
data <- ranking(
|
||||||
|
analysis,
|
||||||
|
weights(factors),
|
||||||
|
min_n_species = min_n_species
|
||||||
|
)
|
||||||
|
|
||||||
data[gene %chin% reference_gene_ids, if (target == "min") {
|
data[gene %chin% reference_gene_ids, if (target == "min") {
|
||||||
min(rank)
|
min(rank)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,13 @@
|
||||||
\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(analysis, methods, reference_gene_ids, target = "mean")
|
optimal_weights(
|
||||||
|
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()}}.}
|
||||||
|
|
@ -15,6 +21,10 @@ optimal_weights(analysis, methods, reference_gene_ids, target = "mean")
|
||||||
|
|
||||||
\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
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,16 @@
|
||||||
\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)
|
ranking(analysis, weights, min_n_species = 10)
|
||||||
}
|
}
|
||||||
\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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue