Interactive Cluster#

For more flexible data mining in downstream analysis of spatial transcriptomics, we recommend using interactive visualization in Stereopy, containing Bokeh tools box, which comes with a number of interactive tools that help report information conveniently, and change plot parameters, like zoom level or range extents. Meanwhile lasso/box selection to generate specifed spatial data is advantageous for customized analysis subsequently.

This web page is generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Reading data#

In this part, we work with the data of mouse olfactory bulb using cell bin (bin size = 40), which has 20277 cells and 28553 genes. Please download our example data.

[ ]:
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
import stereo as st

data_path = './SS200000135TL_D1.tissue.gef'
data = st.io.reader.read_gef(data_path, bin_size=40)
data.tl.cal_qc()

Spatial distribution#

Interactive scatter is an object containing bokeh’s tools and widgets created by Panel. We can use selection tools, lasso or box,to select a certain area specificly, then export it as a new StereoExpData object, which is saved as a property of interactive scatter object named selected_exp_data.

To manipulate data flexiblely, we provide polygon selection which can be edited after drawing a area.

It’s very necessary to read the user guide of poly draw tool and poly edit tool to learn how to draw and edit a polygon. An example of polygon edited selection:

polyEdit.gif

Before click export button to build a new StereoExpData object,you have to click the polygon area so that you can see the selected position x,y data in the table.

[8]:
# lasso/box seletcion when poly_select is False
# polyAnnotator/polyAnnotator edit tools when poly_select is True, it can fine-tune the selection area
# (Double click for start and end points when use polyAnnotator tools)

ins = data.plt.interact_spatial_scatter(width=500, height=500, poly_select=True)
ins.show()
[8]:
[9]:
ins.add_selected_area()
selected_data = ins.selected_exp_data
selected_data
[9]:
<stereo.core.stereo_exp_data.StereoExpData at 0x7f3d792829a0>
[10]:
selected_data.plt.spatial_scatter(dot_size=30)
../_images/Tutorials_Interactive_Cluster_10_0.png

New GEF#

Selected area could be saved in a new GEF file for subsequent analysis.

[12]:
st.io.write_mid_gef(selected_data, './SS200000135TL_D1_selected.tissue.gef')

Clustering#

Use selected data to perform clustering. Spatial distribution visualization color mapped by category of cluster is also interactive. So we can change plot parameters optionally, such as hiding certain cluster, changing color styles.

[13]:
selected_data.tl.quantile()
selected_data.tl.pca(use_highly_genes=False, n_pcs=30, res_key='dim_reduce')
selected_data.tl.neighbors(pca_res_key='dim_reduce', n_neighbors=30)
selected_data.tl.leiden(neighbors_res_key='neighbors', res_key='cluster')

Display the spatial distribution of Leiden clustering.

[14]:
selected_data.plt.cluster_scatter(res_key='cluster', dot_size=18)
[14]:
<AxesSubplot:>
../_images/Tutorials_Interactive_Cluster_18_1.png

When plot interactive clustering scatters, you can choose to hide or show certain clusters by clicking the legend. Additionally, background color, dot color, and dot size all could be reset.

[15]:
selected_data.plt.interact_cluster()
[15]: