STOmics / Stereopy

A toolkit of spatial transcriptomic analysis.
MIT License
184 stars 64 forks source link

data.tl.cell_cell_communication can't find the cell comunication database #166

Closed Feng-Zhang closed 11 months ago

Feng-Zhang commented 12 months ago

I try to run the code from https://stereopy.readthedocs.io/en/latest/Tutorials/Cell_Cell_Communication.html. When I run

data.tl.cell_cell_communication(
                                analysis_type='statistical',
                                cluster_res_key='leiden',
                                species='MOUSE',
                                database='cellphonedb',
                                res_key='cell_cell_communication'
                                )

it give the error as shown in below:

[2023-09-03 04:29:53][Stereo][2556510][MainThread][139735730447552][main][127][INFO]: species: MOUSE
[2023-09-03 04:29:53][Stereo][2556510][MainThread][139735730447552][main][128][INFO]: database: cellphonedb
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2336, in Engine._wrap_pool_connect(self, fn, connection)
   2335 try:
-> 2336     return fn()
   2337 except dialect.dbapi.Error as e:

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:364, in Pool.connect(self)
    363 if not self._use_threadlocal:
--> 364     return _ConnectionFairy._checkout(self)
    366 try:

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:778, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy)
    777 if not fairy:
--> 778     fairy = _ConnectionRecord.checkout(pool)
    780     fairy._pool = pool

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:495, in _ConnectionRecord.checkout(cls, pool)
    493 @classmethod
    494 def checkout(cls, pool):
--> 495     rec = pool._do_get()
    496     try:

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:241, in NullPool._do_get(self)
    240 def _do_get(self):
--> 241     return self._create_connection()

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:309, in Pool._create_connection(self)
    307 """Called by subclasses to create a new ConnectionRecord."""
--> 309 return _ConnectionRecord(self)

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:440, in _ConnectionRecord.__init__(self, pool, connect)
    439 if connect:
--> 440     self.__connect(first_connect_check=True)
    441 self.finalize_callback = deque()

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:661, in _ConnectionRecord.__connect(self, first_connect_check)
    660     with util.safe_reraise():
--> 661         pool.logger.debug("Error on connect(): %s", e)
    662 else:

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
     67     if not self.warn_only:
---> 68         compat.raise_(
     69             exc_value,
     70             with_traceback=exc_tb,
     71         )
     72 else:

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/util/compat.py:182, in raise_(***failed resolving arguments***)
    181 try:
--> 182     raise exception
    183 finally:
    184     # credit to
    185     # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
    186     # as the __traceback__ object creates a cycle

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:656, in _ConnectionRecord.__connect(self, first_connect_check)
    655 self.starttime = time.time()
--> 656 connection = pool._invoke_creator(self)
    657 pool.logger.debug("Created new connection %r", connection)

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py:114, in DefaultEngineStrategy.create.<locals>.connect(connection_record)
    113             return connection
--> 114 return dialect.connect(*cargs, **cparams)

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/default.py:508, in DefaultDialect.connect(self, *cargs, **cparams)
    506 def connect(self, *cargs, **cparams):
    507     # inherits the docstring from interfaces.Dialect.connect
--> 508     return self.dbapi.connect(*cargs, **cparams)

OperationalError: unable to open database file

The above exception was the direct cause of the following exception:

OperationalError                          Traceback (most recent call last)
Cell In[29], line 1
----> 1 data.tl.cell_cell_communication(
      2                                 analysis_type='statistical',
      3                                 cluster_res_key='leiden',
      4                                 species='MOUSE',
      5                                 database='cellphonedb',
      6                                 res_key='cell_cell_communication'
      7                                 )

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/stereo/algorithm/cell_cell_communication/main.py:130, in CellCellCommunication.main(self, analysis_type, cluster_res_key, micro_envs, species, database, homogene_path, counts_identifiers, subsampling, subsampling_log, subsampling_num_pc, subsampling_num_cells, pca_res_key, separator_cluster, separator_interaction, iterations, threshold, processes, pvalue, result_precision, output_path, means_filename, pvalues_filename, significant_means_filename, deconvoluted_filename, output_format, res_key)
    127 logger.info(f'species: {species.upper()}')
    128 logger.info(f'database: {database}')
--> 130 interactions, genes, complex_composition, complex_expanded = self._get_ref_database(db_path)
    132 counts, meta = self._prepare_data(cluster_res_key)
    134 # 1. preprocess and validate input data
    135 
    136 # 1.1. preprocess and validate meta data (cell name as index, cell type as the only column).
    137 # meta = self._check_meta_data(meta)
    138 
    139 # 1.2. preprocess and validate counts data

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/stereo/algorithm/cell_cell_communication/main.py:364, in CellCellCommunication._get_ref_database(self, db_path)
    361 database_manager.add_repository(ProteinRepository)
    363 # get data form database
--> 364 interactions = database_manager.get_repository('interaction').get_all_expanded(include_gene=False)
    365 genes = database_manager.get_repository('gene').get_all_expanded()  # join gene, protein, multidata
    366 complex_composition = database_manager.get_repository('complex').get_all_compositions()

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/stereo/algorithm/cell_cell_communication/utils/sqlalchemy_repository.py:325, in InteractionRepository.get_all_expanded(self, include_gene, suffixes)
    322 def get_all_expanded(self, include_gene=True, suffixes=('_1', '_2')):
    323     interactions_query = self.database_manager.database.session.query(Interaction)
--> 325     interactions = pd.read_sql(interactions_query.statement, self.database_manager.database.engine)
    327     multidata_expanded: pd.DataFrame = self.database_manager.get_repository('multidata').get_all_expanded(
    328         include_gene)
    330     multidata_expanded = multidata_expanded.astype({'id_multidata': 'int64'})

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/pandas/io/sql.py:590, in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize)
    581     return pandas_sql.read_table(
    582         sql,
    583         index_col=index_col,
   (...)
    587         chunksize=chunksize,
    588     )
    589 else:
--> 590     return pandas_sql.read_query(
    591         sql,
    592         index_col=index_col,
    593         params=params,
    594         coerce_float=coerce_float,
    595         parse_dates=parse_dates,
    596         chunksize=chunksize,
    597     )

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/pandas/io/sql.py:1560, in SQLDatabase.read_query(self, sql, index_col, coerce_float, parse_dates, params, chunksize, dtype)
   1512 """
   1513 Read SQL query into a DataFrame.
   1514 
   (...)
   1556 
   1557 """
   1558 args = _convert_params(sql, params)
-> 1560 result = self.execute(*args)
   1561 columns = result.keys()
   1563 if chunksize is not None:

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/pandas/io/sql.py:1405, in SQLDatabase.execute(self, *args, **kwargs)
   1403 def execute(self, *args, **kwargs):
   1404     """Simple passthrough to SQLAlchemy connectable"""
-> 1405     return self.connectable.execution_options().execute(*args, **kwargs)

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2234, in Engine.execute(self, statement, *multiparams, **params)
   2214 def execute(self, statement, *multiparams, **params):
   2215     """Executes the given construct and returns a
   2216     :class:`_engine.ResultProxy`.
   2217 
   (...)
   2231 
   2232     """
-> 2234     connection = self._contextual_connect(close_with_result=True)
   2235     return connection.execute(statement, *multiparams, **params)

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2302, in Engine._contextual_connect(self, close_with_result, **kwargs)
   2299 def _contextual_connect(self, close_with_result=False, **kwargs):
   2300     return self._connection_cls(
   2301         self,
-> 2302         self._wrap_pool_connect(self.pool.connect, None),
   2303         close_with_result=close_with_result,
   2304         **kwargs
   2305     )

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2339, in Engine._wrap_pool_connect(self, fn, connection)
   2337 except dialect.dbapi.Error as e:
   2338     if connection is None:
-> 2339         Connection._handle_dbapi_exception_noconnection(
   2340             e, dialect, self
   2341         )
   2342     else:
   2343         util.raise_(
   2344             sys.exc_info()[1], with_traceback=sys.exc_info()[2]
   2345         )

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/base.py:1583, in Connection._handle_dbapi_exception_noconnection(cls, e, dialect, engine)
   1581     util.raise_(newraise, with_traceback=exc_info[2], from_=e)
   1582 elif should_wrap:
-> 1583     util.raise_(
   1584         sqlalchemy_exception, with_traceback=exc_info[2], from_=e
   1585     )
   1586 else:
   1587     util.raise_(exc_info[1], with_traceback=exc_info[2])

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/util/compat.py:182, in raise_(***failed resolving arguments***)
    179     exception.__cause__ = replace_context
    181 try:
--> 182     raise exception
    183 finally:
    184     # credit to
    185     # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
    186     # as the __traceback__ object creates a cycle
    187     del exception, replace_context, from_, with_traceback

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2336, in Engine._wrap_pool_connect(self, fn, connection)
   2334 dialect = self.dialect
   2335 try:
-> 2336     return fn()
   2337 except dialect.dbapi.Error as e:
   2338     if connection is None:

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:364, in Pool.connect(self)
    356 """Return a DBAPI connection from the pool.
    357 
    358 The connection is instrumented such that when its
   (...)
    361 
    362 """
    363 if not self._use_threadlocal:
--> 364     return _ConnectionFairy._checkout(self)
    366 try:
    367     rec = self._threadconns.current()

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:778, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy)
    775 @classmethod
    776 def _checkout(cls, pool, threadconns=None, fairy=None):
    777     if not fairy:
--> 778         fairy = _ConnectionRecord.checkout(pool)
    780         fairy._pool = pool
    781         fairy._counter = 0

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:495, in _ConnectionRecord.checkout(cls, pool)
    493 @classmethod
    494 def checkout(cls, pool):
--> 495     rec = pool._do_get()
    496     try:
    497         dbapi_connection = rec.get_connection()

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:241, in NullPool._do_get(self)
    240 def _do_get(self):
--> 241     return self._create_connection()

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:309, in Pool._create_connection(self)
    306 def _create_connection(self):
    307     """Called by subclasses to create a new ConnectionRecord."""
--> 309     return _ConnectionRecord(self)

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:440, in _ConnectionRecord.__init__(self, pool, connect)
    438 self.__pool = pool
    439 if connect:
--> 440     self.__connect(first_connect_check=True)
    441 self.finalize_callback = deque()

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:661, in _ConnectionRecord.__connect(self, first_connect_check)
    659 except Exception as e:
    660     with util.safe_reraise():
--> 661         pool.logger.debug("Error on connect(): %s", e)
    662 else:
    663     if first_connect_check:

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
     66     self._exc_info = None  # remove potential circular references
     67     if not self.warn_only:
---> 68         compat.raise_(
     69             exc_value,
     70             with_traceback=exc_tb,
     71         )
     72 else:
     73     if not compat.py3k and self._exc_info and self._exc_info[1]:
     74         # emulate Py3K's behavior of telling us when an exception
     75         # occurs in an exception handler.

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/util/compat.py:182, in raise_(***failed resolving arguments***)
    179     exception.__cause__ = replace_context
    181 try:
--> 182     raise exception
    183 finally:
    184     # credit to
    185     # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
    186     # as the __traceback__ object creates a cycle
    187     del exception, replace_context, from_, with_traceback

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/pool/base.py:656, in _ConnectionRecord.__connect(self, first_connect_check)
    654 try:
    655     self.starttime = time.time()
--> 656     connection = pool._invoke_creator(self)
    657     pool.logger.debug("Created new connection %r", connection)
    658     self.connection = connection

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py:114, in DefaultEngineStrategy.create.<locals>.connect(connection_record)
    112         if connection is not None:
    113             return connection
--> 114 return dialect.connect(*cargs, **cparams)

File ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/sqlalchemy/engine/default.py:508, in DefaultDialect.connect(self, *cargs, **cparams)
    506 def connect(self, *cargs, **cparams):
    507     # inherits the docstring from interfaces.Dialect.connect
--> 508     return self.dbapi.connect(*cargs, **cparams)

OperationalError: (sqlite3.OperationalError) unable to open database file
(Background on this error at: http://sqlalche.me/e/13/e3q8)

It seems that the database file can't be find.

tanliwei-coder commented 12 months ago

the database should be installed together with stereopy, please to check whether exists in directory ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/stereo/data/algorithm/cell_cell_communication/database

Feng-Zhang commented 12 months ago

There is no ~/bin/miniconda3/envs/stPy/lib/python3.8/site-packages/stereo/data/ directory. How should I fix this issue?

tanliwei-coder commented 11 months ago

Try to reinstall the stereopy: pip install --use-pep517 --force-reinstall stereopy==0.13.0b1

Feng-Zhang commented 11 months ago

Thank you. It works.