STOmics / Stereopy

A toolkit of spatial transcriptomic analysis.
MIT License
197 stars 65 forks source link

module 'stereo.io' has no attribute 'read_h5ad' #153

Closed Jiayi-Zheng closed 1 year ago

Jiayi-Zheng commented 1 year ago

Hello, I installed stereopy in a newly created env with python3.8 with conda create --name st python=3.8 then pip install --use-pep517 stereopy and then do import stereo as st followed by data = st.io.read_h5ad('/usersdata/user/BGI_Stereo/E16.5_E2S6.MOSTA.h5ad')

I tried to read in h5ad file from https://db.cngb.org/stomics/mosta/download/ but it tells me module 'stereo.io' has no attribute 'read_h5ad'

Further check in my stereopy package indicated:

(stereo) [user@hpc02 ~]$ cd /home/user/.conda/envs/stereo/lib/python3.8/site-packages/stereo/io
(stereo) [user@hpc02 io]$ ls
h5ad.py  __init__.py  __pycache__  reader.py  writer.py

Checking the init.py file I read:

#!/usr/bin/env python3
# coding: utf-8
"""
@author: Ping Qiu  qiuping1@genomics.cn
@last modified by: Ping Qiu
@file:__init__.py.py
@time:2021/03/05
"""
from .reader import read_gef, read_gem, read_ann_h5ad, read_stereo_h5ad, anndata_to_stereo, stereo_to_anndata, read_gef_info, read_seurat_h5ad
from .writer import write, write_h5ad, write_mid_gef, update_gef

I then tried data = st.io.read_stereo_h5ad('/usersdata/user/BGI_Stereo/E16.5_E2S6.MOSTA.h5ad') which gives me the following:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/.conda/envs/stereo/lib/python3.8/site-packages/IPython/core/formatters.py:708, in PlainTextFormatter.__call__(self, obj)
    701 stream = StringIO()
    702 printer = pretty.RepresentationPrinter(stream, self.verbose,
    703     self.max_width, self.newline,
    704     max_seq_length=self.max_seq_length,
    705     singleton_pprinters=self.singleton_printers,
    706     type_pprinters=self.type_printers,
    707     deferred_pprinters=self.deferred_printers)
--> 708 printer.pretty(obj)
    709 printer.flush()
    710 return stream.getvalue()

File ~/.conda/envs/stereo/lib/python3.8/site-packages/IPython/lib/pretty.py:410, in RepresentationPrinter.pretty(self, obj)
    407                         return meth(obj, self, cycle)
    408                 if cls is not object \
    409                         and callable(cls.__dict__.get('__repr__')):
--> 410                     return _repr_pprint(obj, self, cycle)
    412     return _default_pprint(obj, self, cycle)
    413 finally:

File ~/.conda/envs/stereo/lib/python3.8/site-packages/IPython/lib/pretty.py:778, in _repr_pprint(obj, p, cycle)
    776 """A pprint that just redirects to the normal repr function."""
    777 # Find newlines and replace them with p.break_()
--> 778 output = repr(obj)
    779 lines = output.splitlines()
    780 with p.group():

File ~/.conda/envs/stereo/lib/python3.8/site-packages/stereo/core/stereo_exp_data.py:537, in StereoExpData.__repr__(self)
    536 def __repr__(self):
--> 537     return self.__str__()

File ~/.conda/envs/stereo/lib/python3.8/site-packages/stereo/core/stereo_exp_data.py:506, in StereoExpData.__str__(self)
    505 def __str__(self):
--> 506     format_str = f"StereoExpData object with n_cells X n_genes = {self.shape[0]} X {self.shape[1]}"
    507     format_str += f"\nbin_type: {self.bin_type}"
    508     if self.bin_type == 'bins':

File ~/.conda/envs/stereo/lib/python3.8/site-packages/stereo/core/stereo_exp_data.py:217, in StereoExpData.shape(self)
    210 @property
    211 def shape(self):
    212     """
    213     Get the shape of expression matrix.
    214 
    215     :return:
    216     """
--> 217     return self.exp_matrix.shape

AttributeError: 'NoneType' object has no attribute 'shape'

For reference:

(stereo) [user@hpc02 io]$ pip list
Package                       Version
----------------------------- -----------
adjustText                    0.7.3
aiohttp                       3.8.5
aiosignal                     1.3.1
alabaster                     0.7.13
anndata                       0.7.5
annoy                         1.17.1
arboreto                      0.1.6
args                          0.1.0
asciitree                     0.3.3
asttokens                     2.2.1
async-timeout                 4.0.3
attrs                         23.1.0
Babel                         2.12.1
backcall                      0.2.0
backports.functools-lru-cache 1.6.5
bbmix                         0.2.1
bgzip                         0.4.0
biofluff                      3.0.4
biopython                     1.79
biothings-client              0.2.6
bleach                        6.0.0
blosc2                        2.0.0
bokeh                         2.4.3
boltons                       23.0.0
Brotli                        1.0.9
celloracle                    0.10.15
certifi                       2023.7.22
charset-normalizer            2.0.12
click                         8.1.6
clint                         0.5.1
cloudpickle                   2.2.1
colorcet                      2.0.6
coloredlogs                   15.0.1
comm                          0.1.4
ConfigArgParse                1.5.3
configparser                  5.3.0
connection-pool               0.0.3
contourpy                     1.0.7
coverage                      7.0.3
ctxcore                       0.2.0
cutadapt                      4.1
cycler                        0.11.0
Cython                        3.0.0
cytoolz                       0.12.2
dask                          2022.11.1
dask-image                    2023.3.0
datashader                    0.14.1rc1
datashape                     0.5.2
datrie                        0.8.2
debugpy                       1.6.8
decorator                     5.1.1
dill                          0.3.7
diskcache                     5.4.0
distributed                   2022.11.1
dnaio                         0.9.1
docopt                        0.6.2
docrep                        0.3.2
docutils                      0.20.1
dpath                         2.0.6
dunamai                       1.16.0
entrypoints                   0.4
exceptiongroup                1.0.0rc9
executing                     1.2.0
fa2                           0.3.5
fasteners                     0.18
fbpca                         1.0
flatbuffers                   23.5.26
fonttools                     4.38.0
frozendict                    2.3.8
frozenlist                    1.4.0
fsspec                        2023.6.0
gefpy                         0.7.5
genomepy                      0.14.0
geojson                       3.0.1
geosketch                     1.2
get_version                   3.5.4
gimmemotifs                   0.17.0
gitdb                         4.0.9
GitPython                     3.1.27
goatools                      1.3.1
gtfparse                      1.2.1
h5py                          3.7.0
harmonypy                     0.0.6
holoviews                     1.14.5
hotspotsc                     1.1.1
HTSeq                         2.0.2
humanfriendly                 10.0
hvplot                        0.7.3
hypothesis                    6.56.3
idna                          3.4
igraph                        0.9.11
imageio                       2.9.0
imagesize                     1.4.1
importlib-metadata            6.8.0
inflate64                     0.3.1
inflect                       7.0.0
interlap                      0.2.7
ipykernel                     6.25.1
ipython                       8.12.0
isal                          1.0.1
iteround                      1.0.4
jedi                          0.19.0
Jinja2                        3.0.3
joblib                        1.0.1
jupyter_client                8.3.0
jupyter_core                  4.12.0
KDEpy                         1.1.0
keras                         2.7.0
kiwisolver                    1.4.4
kneed                         0.8.1
legacy-api-wrap               1.2
leidenalg                     0.8.10
llvmlite                      0.39.1
locket                        1.0.0
logomaker                     0.8
loguru                        0.6.0
loompy                        3.0.7
louvain                       0.7.1
maegatk                       0.1.1
mamba                         0.11.2
Markdown                      3.4.4
MarkupSafe                    2.0.1
matplotlib                    3.5.2
matplotlib-inline             0.1.6
matplotlib-scalebar           0.8.1
mpmath                        1.3.0
mquad                         0.1.6b0
msgpack                       1.0.5
multidict                     6.0.4
multipledispatch              1.0.0
multiprocessing-on-dill       3.5.0a4
multivolumefile               0.2.3
mygene                        3.2.2
mysql-connector-python        8.0.32
natsort                       7.1.1
nest-asyncio                  1.5.6
networkx                      3.1
norns                         0.1.6
numba                         0.56.4
numcodecs                     0.11.0
numexpr                       2.8.5
numpy                         1.21.6
numpy-groupies                0.9.19
omnipath                      1.0.7
onnxruntime                   1.14.1
opencv-python                 4.5.5.64
optparse-pretty               0.1.1
packaging                     21.3
palettable                    3.3.0
pandas                        1.5.3
panel                         0.13.1
param                         1.12.2
parso                         0.8.3
partd                         1.4.0
patsy                         0.5.3
pexpect                       4.8.0
PhenoGraph                    1.5.7
phylovelo                     0.1
pickleshare                   0.7.5
Pillow                        9.2.0
PIMS                          0.6.1
pip                           23.2.1
plac                          1.3.5
POT                           0.8.1.0
prompt-toolkit                3.0.39
protobuf                      3.20.3
psutil                        5.9.5
ptyprocess                    0.7.0
PuLP                          2.6.0
pure-eval                     0.2.2
py-cpuinfo                    9.0.0
py7zr                         0.20.2
pyarrow                       8.0.0
pybcj                         1.0.1
pybedtools                    0.9.0
pyBigWig                      0.3.18
pycryptodomex                 3.16.0
pyct                          0.5.0
pydantic                      1.10.12
pydot                         1.4.2
pyfaidx                       0.7.1
Pygments                      2.16.1
pynndescent                   0.5.8
pyparsing                     3.1.1
pyppmd                        1.0.0
pysam                         0.19.1
pyscenic                      0.12.1
python-dateutil               2.8.2
python-igraph                 0.9.9
pytz                          2023.3
pyviz-comms                   2.3.2
PyWavelets                    1.4.1
PyYAML                        6.0
pyzmq                         25.1.1
pyzstd                        0.15.3
qnorm                         0.8.1
ratelimiter                   1.2.0.post0
requests                      2.26.0
retry                         0.9.2
retrying                      1.3.4
ruamel.yaml                   0.17.21
ruamel.yaml.clib              0.2.6
scanorama                     1.7.3
scanpy                        1.9.3
scikit-image                  0.19.0
scikit-learn                  1.0.1
scipy                         1.7.3
seaborn                       0.11.2
session-info                  1.0.0
setuptools                    59.8.0
setuptools-scm                7.1.0
shapely                       2.0.1
sinto                         0.9.0
six                           1.16.0
sklearn                       0.0
slicerator                    1.1.0
slideio                       0.5.225
smart-open                    6.0.0
smmap                         5.0.0
snakemake                     7.12.1
snowballstemmer               2.2.0
sortedcontainers              2.4.0
spatialpandas                 0.4.8
Sphinx                        7.1.2
sphinxcontrib-applehelp       1.0.4
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        2.0.1
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.5
SQLAlchemy                    1.3.24
squidpy                       1.2.2
stack-data                    0.6.2
statsmodels                   0.12.1
stdlib-list                   0.8.0
stereopy                      0.12.1
stopit                        1.1.2
sympy                         1.12
tables                        3.8.0
tabulate                      0.8.10
tblib                         2.0.0
tbsp                          1.0.0
texttable                     1.6.4
threadpoolctl                 3.2.0
tifffile                      2021.11.2
tomli                         2.0.1
toolz                         0.12.0
toposort                      1.7
tornado                       6.1
tqdm                          4.66.1
traitlets                     5.9.0
typing_extensions             4.3.0
umap-learn                    0.5.1
urllib3                       1.26.9
validators                    0.21.2
velocyto                      0.17.17
vireoSNP                      0.5.7
wcwidth                       0.2.6
webencodings                  0.5.1
wheel                         0.41.1
wrapt                         1.15.0
xarray                        0.20.1
xdg                           5.1.1
xgboost                       1.7.3
xopen                         1.6.0
xxhash                        3.2.0
yarl                          1.9.2
yte                           1.5.1
zarr                          2.16.0
zict                          3.0.0
zipp                          3.16.2

One more question... The manual tutorial talked about how to get result from the gef file. I'm trying to visualize the E16.5 E1S3 whole embryo data. Could kindly point me to any tutorial or instruction on how to assemble datasets regarding E1S3 in https://db.cngb.org/stomics/mosta/download/? For example, how to embed the tiff image file into the h5ad file. I didn't seem to find relevant instruction from the stereopy tutorial.

Sorry another really simple question... How do I extract cells information? I have a StereoExpData object called data. If I do data.cells it tells me <stereo.core.cell.Cell at 0x7f756fd85970>. But how do I visualize it or convert it to a pandas dataframe? my data object looks like:

StereoExpData object with n_cells X n_genes = 35998 X 24302
bin_type: bins
bin_size: 50
offset_x = 0
offset_y = 0
cells: ['cell_name']
genes: ['gene_name']
>data.cells['cell_name']
KeyError                                  Traceback (most recent call last)
File ~/.conda/envs/stereo/lib/python3.8/site-packages/pandas/core/indexes/base.py:3802, in Index.get_loc(self, key, method, tolerance)
   3801 try:
-> 3802     return self._engine.get_loc(casted_key)
   3803 except KeyError as err:

File ~/.conda/envs/stereo/lib/python3.8/site-packages/pandas/_libs/index.pyx:138, in pandas._libs.index.IndexEngine.get_loc()

File ~/.conda/envs/stereo/lib/python3.8/site-packages/pandas/_libs/index.pyx:165, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/hashtable_class_helper.pxi:5745, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas/_libs/hashtable_class_helper.pxi:5753, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'cell_name'

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

KeyError                                  Traceback (most recent call last)
Cell In[45], line 1
----> 1 data.cells['cell_name']

File ~/.conda/envs/stereo/lib/python3.8/site-packages/stereo/core/cell.py:50, in Cell.__getitem__(self, key)
     49 def __getitem__(self, key):
---> 50     return self._obs[key]

File ~/.conda/envs/stereo/lib/python3.8/site-packages/pandas/core/frame.py:3807, in DataFrame.__getitem__(self, key)
   3805 if self.columns.nlevels > 1:
   3806     return self._getitem_multilevel(key)
-> 3807 indexer = self.columns.get_loc(key)
   3808 if is_integer(indexer):
   3809     indexer = [indexer]

File ~/.conda/envs/stereo/lib/python3.8/site-packages/pandas/core/indexes/base.py:3804, in Index.get_loc(self, key, method, tolerance)
   3802     return self._engine.get_loc(casted_key)
   3803 except KeyError as err:
-> 3804     raise KeyError(key) from err
   3805 except TypeError:
   3806     # If we have a listlike key, _check_indexing_error will raise
   3807     #  InvalidIndexError. Otherwise we fall through and re-raise
   3808     #  the TypeError.
   3809     self._check_indexing_error(key)

KeyError: 'cell_name'
tanliwei-coder commented 1 year ago

Hi!! The function read_h5ad is added from v0.13.0b1, you need to explicitly specify the version when you install stereopy because it is just a beta build.

You can run the command: pip install --use-pep517 stereopy==0.13.0b1

Jiayi-Zheng commented 1 year ago

@tanliwei-coder Thank you so much for your reply! Could you maybe help me out on my two other questions as well? I didn't seem to find a function annotation of all the available functions in sterepy package... Not sure if it's I didn't look into the right place?

tanliwei-coder commented 1 year ago

About your second question, you can get the cells infomation by running data.cells.to_df().

About the first, i need more investigation and will reply to you as soon as possible.