Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed the error when use SCT run scvi and set multiple threads #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions R/scVI.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ NULL
#' @param new.reduction Name under which to store resulting DimReduc object
#' @param ndims Dimensionality of the latent space
#' @param nlayers Number of hidden layers used for encoder and decoder NNs
#' @param gene_likelihood Distribution to use for modelling expression
#' @param gene_likelihood Distribution to use for modelling expression
#' data: {"zinb", "nb", "poisson"}
#' @param max_epochs Number of passes through the dataset taken while
#' training the model
#' @param num_threads The number of threads PyTorch will use
#' @param ... Unused - currently just capturing parameters passed in from
#' \code{Seurat::IntegrateLayers} intended for other integration methods
#'
Expand Down Expand Up @@ -66,8 +67,9 @@ scVIIntegration <- function(
nlayers = 2,
gene_likelihood = "nb",
max_epochs = NULL,
num_threads = NULL,
...) {

# import python methods from specified conda env
reticulate::use_condaenv(conda_env, required = TRUE)
sc <- reticulate::import("scanpy", convert = FALSE)
Expand All @@ -90,16 +92,20 @@ scVIIntegration <- function(
# it also expects the raw counts matrix
# TODO: avoid hardcoding this - users can rename their layers arbitrarily
# so there's no gauruntee that the usual naming conventions will be followed
object <- JoinLayers(object = object, layers = "counts")
if (!inherits(object, what = "SCTAssay")) {
object <- JoinLayers(object = object, layers = "counts")
}
# setup an `AnnData` python instance
adata <- sc$AnnData(
X = scipy$sparse$csr_matrix(
# TODO: avoid hardcoding per comment above
Matrix::t(LayerData(object, layer = "counts")[features, ])
),
obs = batches,
var = object[[]][features, ]
var = if (inherits(object, what = "SCTAssay")) {reticulate::r_to_py(NULL)} else {object[[]][features, ]}
)

scvi$settings$num_threads = if (is.null(num_threads)) {reticulate::r_to_py(num_threads)} else {as.integer(num_threads)}
scvi$model$SCVI$setup_anndata(adata, batch_key = "batch")

# initialize and train the model
Expand All @@ -124,7 +130,7 @@ scVIIntegration <- function(
# build a `DimReduc` instance
suppressWarnings(
latent.dr <- CreateDimReducObject(
embeddings = latent,
embeddings = latent,
key = new.reduction
)
)
Expand All @@ -143,15 +149,15 @@ attr(x = scVIIntegration, which = "Seurat.method") <- "integration"
#' \code{object}. For \code{SCTAssay}s, batches are split using their
#' model identifiers. For \code{StdAssays}, batches are split by layer.
#'
#' Internal - essentially the same as \code{Seurat:::CreateIntegrationGroups}
#' Internal - essentially the same as \code{Seurat:::CreateIntegrationGroups}
#' except that it does not take in a `scale.layer` param.
#'
#' @noRd
#'
#' @param object A \code{SCTAssay} or \code{StdAssays} instance.
#' @param layers Layers in \code{object} to integrate.
#'
#' @return A dataframe indexed on the cell identifiers from \code{object} -
#' @return A dataframe indexed on the cell identifiers from \code{object} -
#' the dataframe contains a single column, "batch", indicating the ...
.FindBatches <- function(object, layers) {
# if an `SCTAssay` is passed in it's expected that the transformation
Expand Down
5 changes: 4 additions & 1 deletion man/scVIIntegration.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.