mirror of
https://github.com/johrpan/geposan.git
synced 2025-10-26 10:47:25 +01:00
Move species count to analysis
This commit is contained in:
parent
f84d37dd30
commit
33056bfa40
6 changed files with 30 additions and 45 deletions
30
R/ranking.R
30
R/ranking.R
|
|
@ -6,33 +6,18 @@
|
|||
#' @param analysis Analysis object resulting from [analyze()].
|
||||
#' @param weights Named list pairing method names with weighting factors. Only
|
||||
#' 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
|
||||
#' additional columns containing the `score` and the `rank` of each gene. It
|
||||
#' will be ordered by rank.
|
||||
#'
|
||||
#' @export
|
||||
ranking <- function(analysis, weights, min_n_species = 10) {
|
||||
ranking <- function(analysis, weights) {
|
||||
if (!"geposan_analysis" %chin% class(analysis)) {
|
||||
stop("Invalid analyis. Use geposan::analyze().")
|
||||
}
|
||||
|
||||
# Count included species from the preset per gene.
|
||||
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 <- copy(analysis$results)
|
||||
ranking[, score := 0.0]
|
||||
|
||||
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 target The optimization target. It may be one of "mean", "min" or
|
||||
#' "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
|
||||
#' can be used as an argument to [ranking()].
|
||||
#'
|
||||
#' @export
|
||||
optimal_weights <- function(analysis, methods, reference_gene_ids,
|
||||
target = "mean", min_n_species = 10) {
|
||||
target = "mean") {
|
||||
if (!"geposan_analysis" %chin% class(analysis)) {
|
||||
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.
|
||||
target_rank <- function(factors) {
|
||||
data <- ranking(
|
||||
analysis,
|
||||
weights(factors),
|
||||
min_n_species = min_n_species
|
||||
)
|
||||
data <- ranking(analysis, weights(factors))
|
||||
|
||||
result <- data[gene %chin% reference_gene_ids, if (target == "min") {
|
||||
min(rank)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue