Skip to content

Commit

Permalink
Robust fallback mechanism (#69)
Browse files Browse the repository at this point in the history
* testload the scvelo and anndata packages

* Prefix all functions

---------

Co-authored-by: Kevin Rue-Albrecht <[email protected]>
  • Loading branch information
multimeric and kevinrue authored Jan 29, 2024
1 parent 0883bf4 commit c798bc5
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions R/scvelo.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
#' containing the velocity vectors for each cell.
#' }
#' The output will always have number of columns equal to the number of cells supplied in \code{x},
#' though the number of rows will depend on whether any subsetting (if \code{subset.row} is supplied)
#' though the number of rows will depend on whether any subsetting (if \code{subset.row} is supplied)
#' or feature selection (if \code{use.theirs=TRUE}) was performed.
#'
#' @examples
Expand All @@ -123,8 +123,8 @@
#' out <- scvelo(list(X=spliced, spliced=spliced, unspliced=unspliced))
#'
#' # make scvelo use 10 rather than the default 30 neighbors to compute moments for velocity estimation:
#' out <- scvelo(list(X=spliced, spliced=spliced, unspliced=unspliced),
#' scvelo.params=list(moments=list(n_neighbors=10L)))
#' out <- scvelo(list(X=spliced, spliced=spliced, unspliced=unspliced),
#' scvelo.params=list(moments=list(n_neighbors=10L)))
#'
#' @references
#' Bergen, V., Lange, M., Peidli, S. et al. Generalizing RNA velocity to transient cell states through dynamical modeling. Nat Biotechnol 38, 1408–1414 (2020). \url{https://doi.org/10.1038/s41587-020-0591-3}
Expand Down Expand Up @@ -175,28 +175,25 @@ NULL
X=X, spliced=spliced, unspliced=unspliced,
use.theirs=use.theirs, mode=mode,
scvelo.params=scvelo.params,
dimred=dimred)
dimred=dimred, testload = c("scvelo", "anndata"))

output
}

#' @importFrom reticulate import
#' @importFrom DelayedArray is_sparse t
#' @importFrom zellkonverter AnnData2SCE
.run_scvelo <- function(X, spliced, unspliced, use.theirs=FALSE, mode='dynamical', scvelo.params=list(), dimred=NULL) {
X <- t(.make_np_friendly(X))
spliced <- t(.make_np_friendly(spliced))
unspliced <- t(.make_np_friendly(unspliced))
X <- t(velociraptor:::.make_np_friendly(X))
spliced <- t(velociraptor:::.make_np_friendly(spliced))
unspliced <- t(velociraptor:::.make_np_friendly(unspliced))

and <- import("anndata")
scv <- import("scvelo")
and <- reticulate::import("anndata")
scv <- reticulate::import("scvelo")
adata <- and$AnnData(X, layers=list(spliced=spliced, unspliced=unspliced))
adata$obs_names <- rownames(spliced)
adata$var_names <- colnames(spliced)

## A supplied dimred will be used even if use.theirs=TRUE
if (!is.null(dimred)) {
dimred <- .make_np_friendly(dimred)
dimred <- velociraptor:::.make_np_friendly(dimred)
adata$obsm <- list(X_pca = dimred)
}

Expand All @@ -223,7 +220,7 @@ NULL

do.call(scv$tl$velocity_confidence, c(list(data=adata), scvelo.params$velocity_confidence))

AnnData2SCE(adata)
zellkonverter::AnnData2SCE(adata)
}

#' @export
Expand Down

0 comments on commit c798bc5

Please sign in to comment.