theislab / zellkonverter

Conversion between scRNA-seq objects
https://theislab.github.io/zellkonverter/
Other
145 stars 27 forks source link

zellkonverter in conda environment #60

Closed bio-la closed 2 years ago

bio-la commented 2 years ago

Hi, thanks for the awesome tool!

I've installed zellkonverter in a conda environment where I also have python and reticulate. No issues during installation. BiocManager::install("zellkonverter")

when I first try to run it, as described here, zellkonverter will attempt creating a conda env. since I took care of all the dependencies in the environment in which I am running it, would it be possible to default the behaviour described here to my conda env instead?

 When first run, this function will instantiate a conda environment
 containing all of the necessary dependencies. This will not be
 performed on any subsequent run or if any other *zellkonverter*
 function has been run prior to this one.

 Setting 'reader = "R"' will use an experimental native R reader
 instead of reading the file into Python and converting the result.
 This avoids the need for a Python environment and some of the
 issues with conversion but is still under development and is
 likely to return slightly different output.

Changing the reader param doesn't generate my desired outcome, of course, what I want is for basiliskRun to know that it has to take my env instead. would it be possible to do so?

readH5AD
function (file, X_name = NULL, use_hdf5 = FALSE, reader = c("python", 
    "R"), verbose = NULL, ...) 
{
    file <- path.expand(file)
    reader <- match.arg(reader)
    switch(reader, python = basiliskRun(env = zellkonverterAnnDataEnv, 
        fun = .H5ADreader, file = file, X_name = X_name, backed = use_hdf5, 
        verbose = verbose, ...), R = .native_reader(file, backed = use_hdf5, 
        verbose = verbose))
}

thanks!!

lazappi commented 2 years ago

Hi @bio-la

Thanks for giving {zellkonverter} a go! If you are comfortable setting up conda environments, and you have one working with {reticulate} and that environment has the anndata Python package installed then you can use it directly rather than using the {zellkonverter} environment. This is done by using the AnnData2SCE()/SCE2AnnData() functions directly rather than readH5AD()/writeH5AD().

You need to first import anndata and then use it to create an AnnData object which can be converted:

anndata <- reticulate::import("anndata")
adata <- anndata$read_h5ad(h5ad_path)
sce <- zellkonverter::AnnData2SCE(adata)

And the same thing in reverse to save a .h5ad file:

adata <- SCE2AnnData(sce)
adata$write_h5ad(h5ad_path)

(I haven't checked this code so I might have made a mistake somewhere but it should be pretty close)

Doing this should use whatever Python environment has been detected by {reticulate}. Hope that helps!

bio-la commented 2 years ago

thanks @lazappi for the fast reply! your suggested code works, I've just noticed that anndata$read_h5ad expects a specific structure in the anndata and throws an AttributeError on the anndata files I'm trying to read (i did not create them so I need a bit more debugging to figure out what is wrong with their X layer) So for the time being you can close this issue if you want!

thanks again, f

lazappi commented 2 years ago

I would suggest trying to read in the files in Python. That would do you if the issue is with the file itself or something to do with interacting with it through {reticulate}.