Cell subsampling using TopACeDo#

%load_ext autotime

import scarf
scarf.__version__
'0.19.8'
time: 942 ms (started: 2022-08-15 17:11:40 +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-2uvmavia
  Running command git clone --filter=blob:none --quiet https://github.com/fraenkel-lab/pcst_fast.git /tmp/pip-req-build-2uvmavia
  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
  Preparing metadata (setup.py) ... ?25l-
 done
?25hRequirement already satisfied: pybind11>=2.1.0 in /home/docs/checkouts/readthedocs.org/user_builds/scarf/envs/0.19.8/lib/python3.8/site-packages (from pcst-fast==1.0.7) (2.10.0)
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=882328 sha256=adf0a266114f2c70c6a86bd13f72947e4a327c6e6f4b1c8d7aa93c0c40b3fac6
  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.19.8/lib/python3.8/site-packages (0.2.7)
time: 16.3 s (started: 2022-08-15 17:11:41 +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: 10.2 s (started: 2022-08-15 17:11:57 +00:00)
ds = scarf.DataStore('scarf_datasets/tenx_5K_pbmc_rnaseq/data.zarr')

ds.plot_layout(
    layout_key='RNA_UMAP',
    color_by='RNA_cluster'
)
../_images/f8db713bc119a402fde56441f0fe1c0e1f170662a69bc149559afd938c1518df.png
time: 1.56 s (started: 2022-08-15 17:12:07 +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, 60136.51it/s]
INFO: 384 cells (9.75%) sub-sampled. Subsample to Seed (165 cells) ratio: 2.327
time: 1.36 s (started: 2022-08-15 17:12:09 +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'
)
../_images/340d8ddb80ca9c2c7a3ac711d5f03601a2424083ec71041da31d8143d05f7ccb.png
time: 398 ms (started: 2022-08-15 17:12:10 +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'
)
../_images/30ac0f78bd9a67506b8be9bbf75d4d37ff84a920fb63675e120682a48ac63a71.png
time: 388 ms (started: 2022-08-15 17:12:11 +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'
)
../_images/b5ee6cbccf0cb7eb9f4212dc6c353466f0c2bb17cfa76c34f4ff57657efd30cd.png
time: 562 ms (started: 2022-08-15 17:12:11 +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'
)
../_images/c109d2535cbf60e2093f62e8b345cc1e789caede735d083e13f6062881275c82.png
time: 538 ms (started: 2022-08-15 17:12:12 +00:00)

That is all for this vignette.