Skip to content

implement from_SingleCellExperiment() - #72

Merged
rcannood merged 3 commits into
mainfrom
issue-59-from_SingleCellExperiment
May 11, 2023
Merged

rcannood merged 3 commits into
mainfrom
issue-59-from_SingleCellExperiment

Conversation

@mtmorgan

@mtmorgan mtmorgan commented May 5, 2023

Copy link
Copy Markdown
Contributor

@mtmorgan
mtmorgan requested a review from rcannood May 5, 2023 18:17
@mtmorgan

mtmorgan commented May 5, 2023 •

Copy link
Copy Markdown
Contributor Author

from_SingleCellExperiment() tries to be completely generic, but assumes that the initialize() method of all classes accept X, obs, var, obs_names, var_names, layers, and perhaps additional arguments (e.g., path, for on-disk representations.

This is a different approach from creating an empty object and filling it in #65, in particular it doesn't require a .create_empty...() for each class and allows the from_*() methods to be implemented independent of the class definition of the output class.

Also, followed from_Seurat() and used "InMemory" for output class, but I think it would be better to be consistent and use "InMemoryAnnData".

@mtmorgan
mtmorgan force-pushed the issue-59-from_SingleCellExperiment branch from 36490d1 to 5ec34d2 Compare May 6, 2023 16:26
@rcannood

rcannood commented May 8, 2023

Copy link
Copy Markdown
Member

That's an interesting idea! Using ... would solve the technical issue of how to pass backend specific

From a documentation perspective I never really liked when functions use ... to pass arguments to other functions, because it requires you to recursively go through documentation pages of other functions to find back the documentation that you need.

Solution 1: helper functions

You could address that by adding a lot of helper functions, e.g.:

from_SingleCellExperiment(sce, output_class = c("InMemoryAnnData", "HDF5AnnData", ...) {
  stopifnot(
    inherits(sce, "SingleCellExperiment")
  )
  output_class <- match.arg(output_class)

  generator <- switch(
    output_class,
    "InMemoryAnnData" = InMemoryAnnData,
    "HDF5AnnData" = HDF5AnnData
  )
  from_SingleCellExperiment_impl(sce, generator, ...)
}

#' @rdname from_SingleCellExperiment
from_SingleCellExperiment_to_HDF5AnnData <- function(sce, path) {
  from_SingleCellExperiment(sce = sce, output_class = "HDF5AnnData", path = path)
}

#' @rdname from_SingleCellExperiment
from_SingleCellExperiment_to_InMemoryAnnData <- function(sce) {
  from_SingleCellExperiment(sce = sce, output_class = "InMemoryAnnData")
}

(Not saying that this is a good idea, but it would solve my main annoyance with how ... arguments are documented)

Solution 2

Simply add some documentation to from_SingleCellExperiment:

#' @param output_class character(1) abbreviated name for the AnnData
#'  class (e.g., `"InMemory"` for an `InMemoryAnnData` object) to
#'  create from `sce`. See "Details" for a list of supported output classes and
#'  corresponding additional arguments.
#' @param ... additional arguments passed to the generator function
#'  (e.g., `InMemoryAnnData$new()`) of `output_class`. See "Details" for a list 
#'  of supported output classes and corresponding additional arguments.
#'
#' @details
#' Overview of possible output classes and corresponding generator functions:
#'
#'   * `"InMemoryAnnData"`: A class for storing the data in memory. Extra arguments: none.
#'   * `"HDFAnnData"`: A class for storing the data in memory. Extra arguments: path.

@mtmorgan

mtmorgan commented May 8, 2023

Copy link
Copy Markdown
Contributor Author

I'm in favor of 'documenting the dots'.

Comment thread R/SingleCellExperiment.R Resolved
rcannood added 2 commits May 11, 2023 11:03
* use get_generator() helper function

* might as well combine

* if no assay is found, X should be NULL

* fix test
@rcannood
rcannood merged commit 285636c into main May 11, 2023
@rcannood
rcannood deleted the issue-59-from_SingleCellExperiment branch May 11, 2023 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement from_SingleCellExperiment

2 participants