Cell subsampling using TopACeDo¶
%load_ext autotime
import scarf
scarf.__version__
'0.18.2'
time: 925 ms (started: 2021-09-28 17:36:58 +00:00)
1) Installing dependencies¶
We need to install the TopACeDo algorithm to perform subsampling:
!pip install git+https://github.com/fraenkel-lab/pcst_fast.git@deb3236cc26ee9fee77d5af40fac3f12bb753850
!pip install -U topacedo
Collecting git+https://github.com/fraenkel-lab/pcst_fast.git@deb3236cc26ee9fee77d5af40fac3f12bb753850
Cloning https://github.com/fraenkel-lab/pcst_fast.git (to revision deb3236cc26ee9fee77d5af40fac3f12bb753850) to /tmp/pip-req-build-4hh4g97t
Running command git clone -q https://github.com/fraenkel-lab/pcst_fast.git /tmp/pip-req-build-4hh4g97t
Running command git rev-parse -q --verify 'sha^deb3236cc26ee9fee77d5af40fac3f12bb753850'
Running command git fetch -q https://github.com/fraenkel-lab/pcst_fast.git deb3236cc26ee9fee77d5af40fac3f12bb753850
Running command git checkout -q deb3236cc26ee9fee77d5af40fac3f12bb753850
Resolved https://github.com/fraenkel-lab/pcst_fast.git to commit deb3236cc26ee9fee77d5af40fac3f12bb753850
Requirement already satisfied: pybind11>=2.1.0 in /home/docs/checkouts/readthedocs.org/user_builds/scarf/envs/0.18.2/lib/python3.8/site-packages (from pcst-fast==1.0.7) (2.7.1)
Building wheels for collected packages: pcst-fast
Building wheel for pcst-fast (setup.py) ... ?25l-
\
|
/
-
\
|
done
?25h Created wheel for pcst-fast: filename=pcst_fast-1.0.7-cp38-cp38-linux_x86_64.whl size=840707 sha256=5ee9fcb9075d455221eb623ba92055d85f05c4fd36af8cc77d1c09d11742747e
Stored in directory: /home/docs/.cache/pip/wheels/48/f1/74/6bb2e9cc262228dbdc63916ec60f0717574b5f633b6ee30b0a
Successfully built pcst-fast
Installing collected packages: pcst-fast
Successfully installed pcst-fast-1.0.7
Requirement already satisfied: topacedo in /home/docs/checkouts/readthedocs.org/user_builds/scarf/envs/0.18.2/lib/python3.8/site-packages (0.2.7)
time: 15.2 s (started: 2021-09-28 17:36:59 +00:00)
2) Fetching pre-processed data¶
# Loading preanalyzed dataset that was processed in the `basic_tutorial` vignette
scarf.fetch_dataset(
dataset_name='tenx_5K_pbmc_rnaseq',
as_zarr=True,
save_path='scarf_datasets'
)
time: 9.82 s (started: 2021-09-28 17:37:15 +00:00)
ds = scarf.DataStore('scarf_datasets/tenx_5K_pbmc_rnaseq/data.zarr')
ds.plot_layout(
layout_key='RNA_UMAP',
color_by='RNA_cluster'
)
time: 1.01 s (started: 2021-09-28 17:37:24 +00:00)
3) Run TopACeDo downsampler¶
UMAP, clustering and marker identification together allow a good understanding of cellular diversity. However, one can still choose from a plethora of other analysis on the data. For example, identification of cell differentiation trajectories. One of the major challenges to run these analysis could be the size of the data. Scarf performs a topology conserving downsampling of the data based on the cell neighbourhood graph. This downsampling aims to maximize the heterogeneity while sampling cells from the data.
Here we run the TopACeDo downsampling algorithm that leverages Scarf’s KNN graph to perform a manifold preserving subsampling of cells. The subsampler can be invoked directly from Scarf’s DataStore object.
ds.run_topacedo_sampler(
cluster_key='RNA_cluster',
max_sampling_rate=0.1
)
Constructing graph from dendrogram: 0%| | 0/3939 [00:00<?, ?it/s]
Constructing graph from dendrogram: 100%|██████████| 3939/3939 [00:00<00:00, 55222.89it/s]
INFO: 384 cells (9.75%) sub-sampled. Subsample to Seed (165 cells) ratio: 2.327
time: 1.38 s (started: 2021-09-28 17:37:25 +00:00)
As a result of subsampling the subsampled cells are marked True under the cell metadata column RNA_sketched
. We can visualize these cells using plot_layout
ds.plot_layout(
layout_key='RNA_UMAP',
color_by='RNA_cluster',
subselection_key='RNA_sketched'
)
time: 414 ms (started: 2021-09-28 17:37:27 +00:00)
It may also be interesting to visualize the cells that were marked as seed cells
used when PCST was run. These cells are marked under the column RNA_sketch_seeds
.
ds.plot_layout(
layout_key='RNA_UMAP',
color_by='RNA_cluster',
subselection_key='RNA_sketch_seeds'
)
time: 405 ms (started: 2021-09-28 17:37:27 +00:00)
4) Intermediate parameters of downsampling¶
To identify the seed cells, the subsampling algorithm calculates cell densities based on neighbourhood degrees. Regions of higher cell density get a sampling penalty. The neighbourhood degree of individual cells are stored under the column RNA_cell_density
.
ds.plot_layout(
layout_key='RNA_UMAP',
color_by='RNA_cell_density'
)
time: 562 ms (started: 2021-09-28 17:37:28 +00:00)
The dowsampling algorithm also identifies regions of the graph where cells form tightly connected groups by calculating mean shared nearest neighbours of each cell’s nieghbours. The tightly connected regions get a sampling award. These values can be accessed from under the cell metadata column RNA_snn_value
.
ds.plot_layout(
layout_key='RNA_UMAP',
color_by='RNA_snn_value'
)
time: 533 ms (started: 2021-09-28 17:37:28 +00:00)
That is all for this vignette.