saezlab / liana

LIANA: a LIgand-receptor ANalysis frAmework
https://saezlab.github.io/liana/
GNU General Public License v3.0
160 stars 28 forks source link

TypeError: metaclass conflict in liana_tensor_c2c #152

Closed rbutleriii closed 2 months ago

rbutleriii commented 2 months ago

I am running into an error I haven't seen right at the liana_tensor_c2c step in my script:

TIME                
34:22               
[1] 0
Loading `c2c` Conda Environment
Error in py_module_import(module, convert = convert) : 
  TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
Run `reticulate::py_last_error()` for details.
Calls: liana_tensor_c2c -> <Anonymous> -> py_module_import
Execution halted

Not sure if this is an issue with the conda env I built or how it is called. It was built using the env_python_gpu.yml from ccc_protocols in a specific location (too huge to fit in my cluster's home folder allocation).

git clone git@github.com:saezlab/ccc_protocols.git
conda env create \
  --file ccc_protocols/env_setup/env_python_gpu.yml \
  --prefix=/labs/flongo/Tau-PS19_C31_cortex_snRNAseq/liana/c2c
conda config --append envs_dirs /labs/flongo/Tau-PS19_C31_cortex_snRNAseq/liana

In the script, I specify the RETICULATE_PYTHON variable, and pass the env name and set use_available = TRUE. This might be confusing things, as outside R the environment is activated before starting.

rootdir = '/labs/flongo/Tau-PS19_C31_cortex_snRNAseq'
wk_dir = paste0(rootdir, '/liana')
setwd(wk_dir)
data_dir = paste0(rootdir, '/seurat_v3')

# define the liana environment
Sys.setenv(RETICULATE_PYTHON = paste0(wk_dir, '/c2c/bin/python'))
gpu_use = TRUE
if (gpu_use){
    device <- 'cuda:0'
    tensorly <- reticulate::import('tensorly')
    tensorly$set_backend('pytorch')
}else{
    device <- NULL
}

...

# grab Single Cell experiment
sce <- as.SingleCellExperiment(sc, assay = "RNA")
remove(sc)

# basic feature filtering
# sce <- sce[rowSums(counts(sce) >= 1) >= 5, ]

# liana by sample ----------------------------------------
fn = paste(round_num, nameset, cate, de, "tensor_bysample.rds", sep=".")
if (resum == "resume" & file.exists(fn)){
  sce = readRDS(fn)
  print(sprintf("Resuming %s from run by sample, loading...", cate))
}else{
  # Run LIANA by sample
  sce = liana_bysample(
    sce = sce,
    sample_col = "orig.ident",
    idents_col = cate,
    method = "sca",
    expr_prop = 0,
    inplace = TRUE, # saves to sce
    return_all = FALSE, # whether to return non-expressed interactions 
    resource = "MouseConsensus"
  )

  # Plot
  sce %>%
    get_abundance_summary(
      sample_col = "orig.ident",
      idents_col = cate, 
      min_cells = 10, # min cells per sample
      min_samples = 2, # min samples
      min_prop = 0.2
    ) %>%
    plot_abundance_summary() -> p 
  ggsave(
    filename = paste(round_num, nameset, cate, de, "abundance", "png", sep = "."),
    plot = p, 
    dpi = 300, 
    width = 16, 
    height = 16, 
    type = "cairo"
  )

  # # filter non abundant celltypes
  # sce <- filter_nonabundant_celltypes(
    # sce = sce,
    # min_samples = 2, # min samples
    # sample_col = "orig.ident",
    # idents_col = cate
  # )
  # save by_sample
  print(sprintf("Finished %s run by sample, saving...", cate))
  saveRDS(sce, file=fn)
}
system("squeue -j $SLURM_JOBID --Format=TimeUsed") ###########################this line runs and reports the runtime

# tensor c2c ----------------------------------------------------
fn = paste(round_num, nameset, cate, de, "tensor_c2c.rds", sep=".")
if (resum == "resume" & file.exists(fn)){
  sce = readRDS(fn)
  print(sprintf("Resuming %s from tensor c2c, loading...", cate))
}else{
  # Run Tensor c2c #########################################################seems to be where I stop
  sce = liana_tensor_c2c(
    sce = sce,
    score_col = 'LRscore',
    rank = NULL,  # set to None to estimate for you data!
    how = 'outer',  #  defines how the tensor is built
    conda_env = "c2c", # used to pass an existing conda env with cell2cell
    use_available = TRUE, # detect & load cell2cell if available
    device = device
  )
  print(sprintf("Finished %s tensor c2c, saving...", cate))
  saveRDS(sce, file = fn)
}
system("squeue -j $SLURM_JOBID --Format=TimeUsed")
rbutleriii commented 2 months ago

This is more simply reproducible by trying to load it directly with:

library(reticulate, quietly = TRUE)
rootdir = '/labs/flongo/Tau-PS19_C31_cortex_snRNAseq'
wk_dir = paste0(rootdir, '/liana')
use_condaenv("c2c") # same result with 
tensorly <- reticulate::import('tensorly')
c2c <- reticulate::import(module = "cell2cell")
Error in py_module_import(module, convert = convert) :
  TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
Run `reticulate::py_last_error()` for details.

Running it out, we see:

> reticulate::py_last_error()

── Python Exception Message ────────────────────────────────────────────────────────────────────────────────────────────
Traceback (most recent call last):
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 93, in _run_hook
    module = hook()
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 117, in _hook
    return _find_and_load(name, import_)
  File "/oak/stanford/scg/lab_flongo/Tau-PS19_C31_cortex_snRNAseq/liana/c2c/lib/python3.10/site-packages/cell2cell/__init__.py", line 3, in <module>
    from cell2cell import analysis
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 93, in _run_hook
    module = hook()
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 117, in _hook
    return _find_and_load(name, import_)
  File "/oak/stanford/scg/lab_flongo/Tau-PS19_C31_cortex_snRNAseq/liana/c2c/lib/python3.10/site-packages/cell2cell/analysis/__init__.py", line 1, in <module>
    from cell2cell.analysis.cell2cell_pipelines import (initialize_interaction_space, BulkInteractions, SingleCellInteractions)
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 93, in _run_hook
    module = hook()
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 117, in _hook
    return _find_and_load(name, import_)
  File "/oak/stanford/scg/lab_flongo/Tau-PS19_C31_cortex_snRNAseq/liana/c2c/lib/python3.10/site-packages/cell2cell/analysis/cell2cell_pipelines.py", line 6, in <module>
    import scanpy
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 93, in _run_hook
    module = hook()
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 117, in _hook
    return _find_and_load(name, import_)
  File "/home/rrbutler/.local/lib/python3.10/site-packages/scanpy/__init__.py", line 16, in <module>
    from . import plotting as pl
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 93, in _run_hook
    module = hook()
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 117, in _hook
    return _find_and_load(name, import_)
  File "/home/rrbutler/.local/lib/python3.10/site-packages/scanpy/plotting/__init__.py", line 1, in <module>
    from ._anndata import (
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 93, in _run_hook
    module = hook()
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 117, in _hook
    return _find_and_load(name, import_)
  File "/home/rrbutler/.local/lib/python3.10/site-packages/scanpy/plotting/_anndata.py", line 28, in <module>
    from . import _utils
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 93, in _run_hook
    module = hook()
  File "/home/rrbutler/R/x86_64-pc-linux-gnu-library/4.2/reticulate/python/rpytools/loader.py", line 117, in _hook
    return _find_and_load(name, import_)
  File "/home/rrbutler/.local/lib/python3.10/site-packages/scanpy/plotting/_utils.py", line 35, in <module>
    class _AxesSubplot(Axes, axes.SubplotBase, ABC):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

── R Traceback ─────────────────────────────────────────────────────────────────────────────────────────────────────────
    ▆
 1. └─reticulate::import(module = "cell2cell")
 2.   └─reticulate:::py_module_import(module, convert = convert)

I suppose this may be an issue for cell2cell? tried it with 0.7.2 and updating to latest.

rbutleriii commented 2 months ago

Closing this and moving the issue to cell2cell -> https://github.com/earmingol/cell2cell/issues/55