From 8e23f586571ea545e17acc13e0c2a4f27377bb39 Mon Sep 17 00:00:00 2001 From: Paul Hoffman Date: Tue, 18 Apr 2023 15:27:55 -0400 Subject: [PATCH] Use template for docs --- apis/r/R/SOMAExperimentAxisQuery.R | 108 +++++++---------------------- 1 file changed, 26 insertions(+), 82 deletions(-) diff --git a/apis/r/R/SOMAExperimentAxisQuery.R b/apis/r/R/SOMAExperimentAxisQuery.R index 5a72b1c0d4..576fd4c131 100644 --- a/apis/r/R/SOMAExperimentAxisQuery.R +++ b/apis/r/R/SOMAExperimentAxisQuery.R @@ -174,8 +174,7 @@ SOMAExperimentAxisQuery <- R6::R6Class( #' @template param-x-layers-v3 #' @template param-obs-index #' @template param-var-index - #' @param obs_column_names Names of columns in \code{obs} to add as - #' cell-level meta data; by default, loads all columns + #' @template param-obs-column-names #' @template param-var-column-names #' @param obsm_layers Names of arrays in \code{obsm} to load in as the #' cell embeddings; pass \code{FALSE} to suppress loading in any @@ -269,75 +268,26 @@ SOMAExperimentAxisQuery <- R6::R6Class( skip_reducs <- TRUE } if (!skip_reducs) { - names(ms_embed) <- .anndata_to_seurat_reduc(ms_embed) if (isTRUE(obsm_layers)) { obsm_layers <- NULL } - obsm_layers <- obsm_layers %||% ms_embed - # Match loadings to embeddings - ms_load <- tryCatch(expr = self$ms$varm$names(), error = null) - if (isTRUE(varm_layers)) { - varm_layers <- NULL - } else if (rlang::is_na(varm_layers)) { - varm_layers <- FALSE - } - if (is.null(ms_load) && !isFALSE(varm_layers)) { - warning("No loadings found", call. = FALSE, immediate. = TRUE) - varm_layers <- FALSE - } - if (!isFALSE(varm_layers)) { - names(ms_load) <- ms_embed[.anndata_to_seurat_reduc(ms_load, 'loadings')] - varm_layers <- varm_layers %||% ms_load - reduc_misisng <- setdiff(x = names(varm_layers), y = names(ms_load)) - if (length(reduc_misisng) == length(varm_layers)) { - warning( - "None of the reductions specified in 'varm_layers' can be found", - call. = FALSE, - immediate. = TRUE - ) - varm_layers <- FALSE - } else if (length(reduc_misisng)) { - warning( - paste( - strwrap(paste( - "The reductions for the following loadings cannot be found in 'varm':", - sQuote(varm_layers[reduc_misisng]), - collapse = ', ' - )), - collapse = '\n' - ), - call. = FALSE, - immediate. = TRUE - ) - varm_layers <- varm_layers[!names(varm_layers) %in% reduc_misisng] - } - } - # Read in reductions and add to `object` - for (embed in obsm_layers) { - if (embed %in% names(ms_embed)) { - embed <- ms_embed[embed] + reductions <- tryCatch( + expr = .get_seurat_reductions( + query = self, + obsm_layers = obsm_layers, + varm_layers = varm_layers, + obs_index = obs_index, + var_index = var_index + ), + error = function(e) { + warning(conditionMessage(e), call. = FALSE, immediate. = TRUE) + return(NULL) } - rname <- .anndata_to_seurat_reduc(embed) - reduc <- tryCatch( - expr = self$to_seurat_reduction( - obsm_layer = embed, - varm_layer = ifelse( - embed %in% names(varm_layers), - yes = varm_layers[embed], - no = FALSE - ), - obs_index = obs_index, - var_index = var_index - ), - error = function(e) { - warning(conditionMessage(e), call. = FALSE, immediate. = TRUE) - return(NULL) - } - ) - if (is.null(reduc)) { - next + ) + if (length(reductions)) { + for (reduc in names(reductions)) { + object[[reduc]] <- reductions[[reduc]] } - object[[rname]] <- reduc } } # Load in graphs @@ -353,19 +303,15 @@ SOMAExperimentAxisQuery <- R6::R6Class( if (isTRUE(obsp_layers)) { obsp_layers <- NULL } - obsp_layers <- obsp_layers %||% ms_graphs - for (grph in obsp_layers) { - mat <- tryCatch( - expr = self$to_seurat_graph(obsp_layer = grph, obs_index = obs_index), - error = function(e) { - warning(conditionMessage(e), call. = FALSE, immediate. = TRUE) - return(NULL) - } - ) - if (is.null(mat)) { - next + graphs <- .get_seurat_graphs( + query = self, + obsp_layers = obsp_layers, + obs_index = obs_index + ) + if (length(graphs)) { + for (grph in names(graphs)) { + object[[grph]] <- graphs[[grph]] } - object[[grph]] <- mat } } # Validate and return @@ -592,8 +538,7 @@ SOMAExperimentAxisQuery <- R6::R6Class( )), collapse = '\n' ), - call. = FALSE, - immediate. = TRUE + call. = FALSE ) embed$read_dense_matrix(unname(coords)) } else { @@ -642,8 +587,7 @@ SOMAExperimentAxisQuery <- R6::R6Class( )), collapse = '\n' ), - call. = FALSE, - immediate. = TRUE + call. = FALSE ) loads$read_dense_matrix(unname(coords)) } else {