Skip to content

Commit

Permalink
do some updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanliwei-coder committed Nov 8, 2024
1 parent 211aa88 commit 750d6af
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 172 deletions.
2 changes: 1 addition & 1 deletion docs/source/Tutorials(Multi-sample)/ST_Gears.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
"source": [
"## Running ST-Gears\n",
"\n",
"`start_i` and `end_i` specify computing scope of samples, here are mean to compute sample 8 to sample 11.\n",
"`start_i` and `end_i` specify computing scope of samples, here means to compute sample 8 to sample 11.\n",
"\n",
"For more details refer to [ms_data.tl.st_gears](../content/stereo.algorithm.st_gears.StGears.main.html)."
]
Expand Down

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions docs/source/Tutorials/SpaTrack.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
SpaTrack
====================

Introducion
--------------------

Trajectory inference (TI) provides important insights in understanding cell development and biological process.
However, the integration of transcriptomic profiles and spatial locations to organize spatiotemporal cell orders is currently remaining challenges.
Here we introduce spaTrack, which effectively constructs cell trajectories from an optimal-transport matrix at single cell resolution,
Expand All @@ -10,6 +14,22 @@ as well as reconstruct cell dynamics across multiple tissue sections in a time s
spaTrack models the fate of a cell as a function of expression profile along the time points driven by transcription factors,
which facilitates the identification of key molecular regulators that govern cellular trajectories.

Highlighted features
---------------------

1. reconstructs fine local trajectories from ST data.
2. integrates spatial transition matrix of multiple samples to generate complete trajectories.
3. traces cell trajectory across multiple tissue sections via direct mapping without integrating data.
4. captures the driven factors of differentiation.
5. could be extensively applied on both ST data and scRNA-seq data.
6. requires lower computing memory and loads than RNA-velocity methods, making it a fast and effective option for TI study.

Turorials
---------------------

We provide the following five tutorials as reference cases to illustrate the application of spaTrack in inferring cell trajectories
on ST data of single slices with a single starting point and multiple starting points, as well as ST data of multiple time slices, and scRNA-seq data.

.. nbgallery::

Apply_spaTrack_on_spatial_data_of_axolotl_brain_regeneration_after_injury
Expand Down
1 change: 1 addition & 0 deletions docs/source/content/032_API_StPipeline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ which is compromised of basic preprocessing, embedding, clustering, and so on.
:toctree: .

core.StPipeline
core.StPipeline.set_layer
core.StPipeline.cal_qc
core.StPipeline.filter_cells
core.StPipeline.filter_genes
Expand Down
74 changes: 74 additions & 0 deletions docs/source/content/06_Release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,80 @@ Release Notes

.. role:: small

Version 1.5.0
------------------
1.5.0 : 2024-11-08
~~~~~~~~~~~~~~~~~~~

.. _SpaTrack: ../Tutorials/SpaTrack.html
.. |SpaTrack| replace:: **SpaTrack**

.. _Layer: stereo.core.StPipeline.set_layer.html
.. |Layer| replace:: **Layer**

.. _st.tl.cal_qc: stereo.core.StPipeline.cal_qc.html
.. |st.tl.cal_qc| replace:: `st.tl.cal_qc`

.. _st.tl.filter_cells: stereo.core.StPipeline.filter_cells.html
.. |st.tl.filter_cells| replace:: `st.tl.filter_cells`

.. _st.tl.filter_genes: stereo.core.StPipeline.filter_genes.html
.. |st.tl.filter_genes| replace:: `st.tl.filter_genes`

.. _st.tl.log1p: stereo.core.StPipeline.log1p.html
.. |st.tl.log1p| replace:: `st.tl.log1p`

.. _st.tl.normalize_total: stereo.core.StPipeline.normalize_total.html
.. |st.tl.normalize_total| replace:: `st.tl.normalize_total`

.. _st.tl.scale: stereo.core.StPipeline.scale.html
.. |st.tl.scale| replace:: `st.tl.scale`

.. _st.tl.quantile: stereo.core.StPipeline.quantile.html
.. |st.tl.quantile| replace:: `st.tl.quantile`

.. _st.tl.disksmooth_zscore: stereo.core.StPipeline.disksmooth_zscore.html
.. |st.tl.disksmooth_zscore| replace:: `st.tl.disksmooth_zscore`

.. _st.tl.sctransform: stereo.core.StPipeline.sctransform.html
.. |st.tl.sctransform| replace:: `st.tl.sctransform`

.. _st.tl.highly_variable_genes: stereo.core.StPipeline.highly_variable_genes.html
.. |st.tl.highly_variable_genes| replace:: `st.tl.highly_variable_genes`

.. _st.tl.pca: stereo.core.StPipeline.pca.html
.. |st.tl.pca| replace:: `st.tl.pca`

.. _st.tl.find_marker_genes: stereo.core.StPipeline.find_marker_genes.html
.. |st.tl.find_marker_genes| replace:: `st.tl.find_marker_genes`

.. _st.plt.spatial_scatter: stereo.plots.PlotCollection.spatial_scatter.html
.. |st.plt.spatial_scatter| replace:: `st.plt.spatial_scatter`

Features:

1. Addition of new algorithm |SpaTrack|_ for trajectory inference.
2. Addition of |Layer|_ for saving expression matrices at different analysis stages, the functions that can use expression matrices in **Layer** as following:
* |st.tl.cal_qc|_
* |st.tl.filter_cells|_
* |st.tl.filter_genes|_
* |st.tl.log1p|_
* |st.tl.normalize_total|_
* |st.tl.scale|_
* |st.tl.quantile|_
* |st.tl.disksmooth_zscore|_
* |st.tl.sctransform|_
* |st.tl.highly_variable_genes|_
* |st.tl.pca|_
* |st.tl.find_marker_genes|_
3. Merger of multiple samples can merge some analysis result in every single samples when data type is **StereoExpData**.
4. |st.plt.spatial_scatter|_ supports setting base image as background to display simultaneously on the plot.

BUG Fixes:

1. Fixed the problem that the proportion of chondriogenes was calculated incorrectly when input data contains **geneID**.
2. Fixed the problem that saving **MSData** into **h5mu** was failed after running `st.tl.highly_variable_genes`.

Version 1.4.0
------------------
1.4.0 : 2024-09-05
Expand Down
93 changes: 74 additions & 19 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,80 @@ Workflow
Latest Additions
------------------

Version 1.5.0
~~~~~~~~~~~~~~~~~~~
1.5.0 : 2024-11-08

.. _SpaTrack: Tutorials/SpaTrack.html
.. |SpaTrack| replace:: **SpaTrack**

.. _Layer: content/stereo.core.StPipeline.set_layer.html
.. |Layer| replace:: **Layer**

.. _st.tl.cal_qc: content/stereo.core.StPipeline.cal_qc.html
.. |st.tl.cal_qc| replace:: `st.tl.cal_qc`

.. _st.tl.filter_cells: content/stereo.core.StPipeline.filter_cells.html
.. |st.tl.filter_cells| replace:: `st.tl.filter_cells`

.. _st.tl.filter_genes: content/stereo.core.StPipeline.filter_genes.html
.. |st.tl.filter_genes| replace:: `st.tl.filter_genes`

.. _st.tl.log1p: content/stereo.core.StPipeline.log1p.html
.. |st.tl.log1p| replace:: `st.tl.log1p`

.. _st.tl.normalize_total: content/stereo.core.StPipeline.normalize_total.html
.. |st.tl.normalize_total| replace:: `st.tl.normalize_total`

.. _st.tl.scale: content/stereo.core.StPipeline.scale.html
.. |st.tl.scale| replace:: `st.tl.scale`

.. _st.tl.quantile: content/stereo.core.StPipeline.quantile.html
.. |st.tl.quantile| replace:: `st.tl.quantile`

.. _st.tl.disksmooth_zscore: content/stereo.core.StPipeline.disksmooth_zscore.html
.. |st.tl.disksmooth_zscore| replace:: `st.tl.disksmooth_zscore`

.. _st.tl.sctransform: content/stereo.core.StPipeline.sctransform.html
.. |st.tl.sctransform| replace:: `st.tl.sctransform`

.. _st.tl.highly_variable_genes: content/stereo.core.StPipeline.highly_variable_genes.html
.. |st.tl.highly_variable_genes| replace:: `st.tl.highly_variable_genes`

.. _st.tl.pca: content/stereo.core.StPipeline.pca.html
.. |st.tl.pca| replace:: `st.tl.pca`

.. _st.tl.find_marker_genes: content/stereo.core.StPipeline.find_marker_genes.html
.. |st.tl.find_marker_genes| replace:: `st.tl.find_marker_genes`

.. _st.plt.spatial_scatter: content/stereo.plots.PlotCollection.spatial_scatter.html
.. |st.plt.spatial_scatter| replace:: `st.plt.spatial_scatter`

Features:

1. Addition of new algorithm |SpaTrack|_ for trajectory inference.
2. Addition of |Layer|_ for saving expression matrices at different analysis stages, the functions that can use expression matrices in **Layer** as following:
* |st.tl.cal_qc|_
* |st.tl.filter_cells|_
* |st.tl.filter_genes|_
* |st.tl.log1p|_
* |st.tl.normalize_total|_
* |st.tl.scale|_
* |st.tl.quantile|_
* |st.tl.disksmooth_zscore|_
* |st.tl.sctransform|_
* |st.tl.highly_variable_genes|_
* |st.tl.pca|_
* |st.tl.find_marker_genes|_
3. Merger of multiple samples can merge some analysis result in every single samples when data type is **StereoExpData**.
4. |st.plt.spatial_scatter|_ supports setting base image as background to display simultaneously on the plot.

BUG Fixes:

1. Fixed the problem that the proportion of chondriogenes was calculated incorrectly when input data contains **geneID**.
2. Fixed the problem that saving **MSData** into **h5mu** was failed after running `st.tl.highly_variable_genes`.


Version 1.4.0
~~~~~~~~~~~~~~~~~~~
1.4.0 : 2024-09-05
Expand Down Expand Up @@ -116,25 +190,6 @@ BUG Fixes:
2. Fixed the problem that `st.io.read_gef` is incompatible with those **GEF** files that contain **gene names** ending with **'_{number}'** (like **'ABC_123'**).
3. Upgraded **gefpy** to latest for fixing the error that **gene names** are lost after running **CellCorrection**.

Version 1.3.0
~~~~~~~~~~~~~~~~~~~
1.3.0 : 2024-05-31

Features:

1. Addition of `MSData.tl.st_gears <Tutorials(Multi-sample)/ST_Gears.html>`_ for spatial alignment of **Multi-sample**.
2. `High Resolution Matrix Export <Tutorials/High_Resolution_Export.html>`_ can support both **GEF** and **GEM** files.
3. Addition of parameters `min_count` and `max_count` for `st.tl.filter_genes <content/stereo.core.StPipeline.filter_genes.html>`_.
4. `MSData.integrate <content/stereo.core.ms_data.MSData.integrate.html>`_ can be compatible with sparse matrix when `MSData.var_type` is `union`.
5. Addition of `MSData.tl.set_scope_and_mode <content/stereo.core.ms_pipeline.MSDataPipeLine.set_scope_and_mode.html>`_ to set `scope` and `mode` globally on **Multi-sample** analysis.
6. Addition of `MSData.plt.ms_spatial_scatter <content/stereo.plots.PlotMsSpatialScatter.ms_spatial_scatter.html>`_ to plot spatial scatter plot for each **sample** in **Multi-sample** separately.

BUG Fixes:

1. Fixed the problem that `st.io.read_gem` is incompatible with **GEM** files containing **geneID**.
2. Fixed the bug of losing part of metadata when writing **StereoExpData** / **MSData** into **Stereo-h5ad** or **h5ms** file.
3. Fixed the incompatibility problem with **AnnData** when performing `st.tl.sctransform`.


.. toctree::
:titlesonly:
Expand Down
7 changes: 3 additions & 4 deletions stereo/algorithm/get_niche.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def main(
assert cluster_1 != cluster_2, "cluster_1 can not equal to cluster_2."

data_full = self.stereo_exp_data
cluster = self.pipeline_res[cluster_res_key]
data_1, _ = filter_by_clusters(data_full, cluster_res=cluster, groups=cluster_1, inplace=False)
data_2, _ = filter_by_clusters(data_full, cluster_res=cluster, groups=cluster_2, inplace=False)
data_1 = filter_by_clusters(data_full, cluster_res_key=cluster_res_key, groups=cluster_1, inplace=False)
data_2 = filter_by_clusters(data_full, cluster_res_key=cluster_res_key, groups=cluster_2, inplace=False)

coord_1 = data_1.position.astype(np.int64)
coord_2 = data_2.position.astype(np.int64)
Expand Down Expand Up @@ -76,7 +75,7 @@ def main(
neighbors = np.zeros((n1, n12), dtype=int)
shift = np.zeros(n1, dtype=np.float64)

cluster_label = cluster['group']
cluster_label = self.pipeline_res[cluster_res_key]['group']
info_entropy = np.zeros(n1, dtype=np.float64)

for i in range(n1):
Expand Down
2 changes: 2 additions & 0 deletions stereo/core/ms_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ class _MSDataStruct(object):
def __check_data_list(self, data_list):
if not isinstance(data_list, list):
raise TypeError('data_list must be a list object')
if len(data_list) == 0:
return data_list
first_data = data_list[0]
for data in data_list[1:]:
if type(data) != type(first_data):
Expand Down
2 changes: 1 addition & 1 deletion stereo/core/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ def _get_marker_genes_res(self, name):
'control_groups': marker_genes_result['params']['reference'],
'corr_method': marker_genes_result['params']['corr_method'],
'use_raw': marker_genes_result['params']['use_raw'],
'layer': marker_genes_result['params']['layer']
'layer': marker_genes_result['params']['layer'] if 'layer' in marker_genes_result['params'] else None,
}
if 'marker_genes_res_key' in marker_genes_result['params']:
marker_genes_result_reconstructed['parameters']['marker_genes_res_key'] = \
Expand Down
2 changes: 1 addition & 1 deletion stereo/plots/plot_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ def umap(
height=height,
**kwargs)
else:
self.data.array2sparse()
# self.data.array2sparse()
if gene_names is None:
raise ValueError('gene name must be set if cluster_key is None')
if isinstance(gene_names, str):
Expand Down
56 changes: 29 additions & 27 deletions stereo/plots/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,24 @@ def base_scatter(
""" # noqa
if not ax:
# if width is None or height is None:
# figsize = (7, 7)
# else:
# width = width / 100 if width >= 100 else 7
# height = height / 100 if height >= 100 else 7
# figsize = (width, height)
min_x, max_x = np.min(x), np.max(x)
min_y, max_y = np.min(y), np.max(y)
data_width = (max_x - min_x) / 100
data_height = (max_y - min_y) / 100
default_width = 7
default_height = 7 * data_height / data_width
if width is None:
width = 7
width = default_width
else:
width = width / 100 if width >= 100 else 7
width = width / 100 if width >= 100 else default_width
if height is None:
height = 7
height = default_height
else:
height = height / 100 if height >= 100 else 7
height = height / 100 if height >= 100 else default_height
_, ax = plt.subplots(figsize=(width, height))
dot_size = PLOT_SCATTER_SIZE_FACTOR / len(hue) if dot_size is None else dot_size
# add a color bar


if invert_y:
ax.invert_yaxis()
Expand Down Expand Up @@ -276,27 +277,17 @@ def base_scatter(
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
ax.figure.colorbar(sm)
# if ax.legend_ is not None:
# ax.legend_.remove()
else:
from natsort import natsorted
import collections
g = natsorted(set(hue))
if hue_order is None:
hue_order = g
# if isinstance(palette, (dict, collections.OrderedDict)):
# palette = [palette[i] for i in g if i in palette]
# if len(palette) < len(g):
# colors = stereo_conf.get_colors(palette, n=len(g))
# else:
# colors = palette
# color_dict = collections.OrderedDict(dict([(g[i], colors[i]) for i in range(len(g))]))
colors = stereo_conf.get_colors(palette, n=len(g), order=hue_order)
color_dict = dict(zip(hue_order, colors))
sns.scatterplot(x=x, y=y, hue=hue, hue_order=hue_order, linewidth=0, marker=marker,
palette=color_dict, size=hue, sizes=(dot_size, dot_size), ax=ax, alpha=foreground_alpha)
handles, labels = ax.get_legend_handles_labels()
# ax.legend_.remove()
legd = ax.legend(handles, labels, ncol=legend_ncol, bbox_to_anchor=(1.02, 1),
loc='upper left', borderaxespad=0, frameon=False)
for lh in legd.legendHandles:
Expand Down Expand Up @@ -372,14 +363,21 @@ def multi_scatter(
hue_length = len(hue) if isinstance(hue, list) else hue.shape[0]
ncols = min(ncols, hue_length)
nrows = np.ceil(hue_length / ncols).astype(int)
# each panel will have the size of rcParams['figure.figsize']
if width is None or height is None:
figsize = (ncols * 10, nrows * 8)
min_x, max_x = np.min(x), np.max(x)
min_y, max_y = np.min(y), np.max(y)
data_width = (max_x - min_x) / 100
data_height = (max_y - min_y) / 100
default_width = 8 + 2
default_height = 8 * data_height / data_width
if width is None:
width = ncols * default_width
else:
width = width / 100 if width >= 100 else ncols * default_width
if height is None:
height = nrows * default_height
else:
width = width / 100 if width >= 100 else ncols * 8
height = height / 100 if height >= 100 else nrows * 8
figsize = (width, height)
fig = plt.figure(figsize=figsize)
height = height / 100 if height >= 100 else nrows * default_height
fig = plt.figure(figsize=(width, height))
left = 0.2 / ncols
bottom = 0.13 / nrows
axs = gridspec.GridSpec(
Expand Down Expand Up @@ -412,6 +410,10 @@ def multi_scatter(
data_resolution=data_resolution,
**kwargs
)
# if width is not None:
# fig.set_figwidth(width)
# if height is not None:
# fig.set_figheight(height)
return fig


Expand Down

0 comments on commit 750d6af

Please sign in to comment.