Jupyter Notebook

Spatial

Note

Please see:

This page is for now just a stub.

# !pip install 'lamindb[jupyter,bionty]'
!lamin init --storage ./test-spatial --schema bionty
Hide code cell output
→ connected lamindb: testuser1/test-spatial
import lamindb as ln
import bionty as bt
import matplotlib.pyplot as plt
import scanpy as sc

ln.track("daeFs3PkquDW0000")
Hide code cell output
→ connected lamindb: testuser1/test-spatial
→ notebook imports: bionty==0.52.0 lamindb==0.76.14 matplotlib==3.9.2 scanpy==1.10.3
→ created Transform('daeFs3Pk'), started new Run('bkf30Tv1') at 2024-10-19 07:31:25 UTC

An example spatial dataset

Here, we have a spatial gene expression dataset measured using Visium from Suo22.

This collection contains two parts:

  1. a high-res image of a slice of fetal liver

  2. a single cell expression dataset in .h5ad

img_path = ln.core.datasets.file_tiff_suo22()
img = plt.imread(img_path)
plt.imshow(img)
plt.show()
_images/0541a7fc63c95f9169486b46d0dd0700bb47268bca9cff61d3f4861a33afed78.png
adata = ln.core.datasets.anndata_suo22_Visium10X()
# subset to the same image
adata = adata[adata.obs["img_id"] == "F121_LP1_4LIV"].copy()
adata
Hide code cell output
AnnData object with n_obs × n_vars = 3027 × 191
    obs: 'in_tissue', 'array_row', 'array_col', 'sample', 'n_genes_by_counts', 'log1p_n_genes_by_counts', 'total_counts', 'log1p_total_counts', 'pct_counts_in_top_50_genes', 'pct_counts_in_top_100_genes', 'pct_counts_in_top_200_genes', 'pct_counts_in_top_500_genes', 'mt_frac', 'img_id', 'EXP_id', 'Organ', 'Fetal_id', 'SN', 'Visium_Area_id', 'Age_PCW', 'Digestion time', 'paths', 'sample_id', '_scvi_batch', '_scvi_labels', '_indices', 'total_cell_abundance'
    var: 'feature_types', 'genome', 'SYMBOL', 'mt'
    obsm: 'NMF', 'means_cell_abundance_w_sf', 'q05_cell_abundance_w_sf', 'q95_cell_abundance_w_sf', 'spatial', 'stds_cell_abundance_w_sf'
# plot where CD45+ leukocytes are in the slice
sc.pl.scatter(adata, "array_row", "array_col", color="ENSG00000081237")
_images/a3e15a761d82118b78d4de32c540fa6b3a25345ef2ed91146f7313e8c30cb7b3.png

Validate annotations

We’ll register the single-cell data and the image as a Collection.

curate = ln.Curator.from_anndata(
    adata,
    var_index=bt.Gene.ensembl_gene_id,
    categoricals={"sample": ln.ULabel.name},
    organism="human",
)
Hide code cell output
✓ added 1 record with Feature.name for columns: 'sample'
26 non-validated values are not saved in Feature.name: ['pct_counts_in_top_500_genes', 'log1p_total_counts', '_scvi_batch', 'array_row', 'pct_counts_in_top_100_genes', 'array_col', 'paths', 'log1p_n_genes_by_counts', '_scvi_labels', '_indices', 'pct_counts_in_top_200_genes', 'Fetal_id', 'n_genes_by_counts', 'img_id', 'Visium_Area_id', 'Age_PCW', 'total_counts', 'Organ', 'mt_frac', 'in_tissue', 'sample_id', 'pct_counts_in_top_50_genes', 'EXP_id', 'Digestion time', 'SN', 'total_cell_abundance']!
      → to lookup values, use lookup().columns
      → to save, run add_new_from_columns
curate.validate()
Hide code cell output
• saving validated records of 'var_index'
• saving validated terms of 'sample'
! 1 non-validated values are not saved in ULabel.name: ['WSSS_F_IMMsp9838712']!
      → to lookup values, use lookup().sample
      → to save, run .add_new_from('sample')
✓ var_index is validated against Gene.ensembl_gene_id
• mapping sample on ULabel.name
!    1 terms is not validated: 'WSSS_F_IMMsp9838712'
      → fix typos, remove non-existent values, or save terms via .add_new_from('sample')
False
curate.add_new_from("sample")
Hide code cell output
✓ added 1 record with ULabel.name for sample: 'WSSS_F_IMMsp9838712'
curate.validate()
Hide code cell output
• saving validated records of 'var_index'
• saving validated terms of 'sample'
✓ var_index is validated against Gene.ensembl_gene_id
✓ sample is validated against ULabel.name
True

Register curated artifact

artifact_ad = curate.save_artifact(description="Suo22 Visium10X image F121_LP1_4LIV")
Hide code cell output
!    26 unique terms (96.30%) are not validated for name: in_tissue, array_row, array_col, n_genes_by_counts, log1p_n_genes_by_counts, total_counts, log1p_total_counts, pct_counts_in_top_50_genes, pct_counts_in_top_100_genes, pct_counts_in_top_200_genes, pct_counts_in_top_500_genes, mt_frac, img_id, EXP_id, Organ, Fetal_id, SN, Visium_Area_id, Age_PCW, Digestion time, ...
artifact_ad.describe()
Hide code cell output
Artifact(uid='XAYh5rd1HtpnmdOd0000', is_latest=True, description='Suo22 Visium10X image F121_LP1_4LIV', suffix='.h5ad', type='dataset', size=9743793, hash='-mhaV6jtrbJtLHCIphbrHg', n_observations=3027, _hash_type='md5', _accessor='AnnData', visibility=1, _key_is_virtual=True, created_at=2024-10-19 07:31:37 UTC)
  Provenance
    .storage = '/home/runner/work/lamin-usecases/lamin-usecases/docs/test-spatial'
    .transform = 'Spatial'
    .run = 2024-10-19 07:31:25 UTC
    .created_by = 'testuser1'
  Labels
    .ulabels = 'WSSS_F_IMMsp9838712'
  Features
    'sample' = 'WSSS_F_IMMsp9838712'
  Feature sets
    'var' = 'CD99', 'CD38', 'CDC27', 'CD79B', 'CDKL5', 'CDK11A', 'CD9', 'CD4', 'CD22', 'CD6', 'CD74', 'CD44', 'CDH1', 'CDK17', 'CDH3', 'CDK13', 'CD84', 'CDC42', 'CDH19', 'CD5L'
    'obs' = 'sample'

Register a collection

artifact_img = ln.Artifact(img_path, description="Suo22 image F121_LP1_4LIV")
artifact_img.save()
Hide code cell output
• path content will be copied to default storage upon `save()` with key `None` ('.lamindb/JTR0hEhRs3Gtrp170000.tiff')
✓ storing artifact 'JTR0hEhRs3Gtrp170000' at '/home/runner/work/lamin-usecases/lamin-usecases/docs/test-spatial/.lamindb/JTR0hEhRs3Gtrp170000.tiff'
Artifact(uid='JTR0hEhRs3Gtrp170000', is_latest=True, description='Suo22 image F121_LP1_4LIV', suffix='.tiff', size=119764004, hash='ZAnyai4Ys01P2fLR_aDIvq', _hash_type='sha1-fl', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-19 07:31:37 UTC)
collection = ln.Collection([artifact_ad, artifact_img], name="Suo22")
collection.save()
Hide code cell output
Collection(uid='vTy2cGwc6KaZ4IXc0000', is_latest=True, name='Suo22', hash='2cD_KhRukcNMArq-dfyyhg', visibility=1, created_by_id=1, transform_id=1, run_id=1, created_at=2024-10-19 07:31:38 UTC)
# clean up test instance
!lamin delete --force test-spatial
!rm -r test-flow
Hide code cell output
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.11.10/x64/bin/lamin", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/rich_click/rich_command.py", line 367, in __call__
    return super().__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/rich_click/rich_command.py", line 152, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/lamin_cli/__main__.py", line 209, in delete
    return delete(instance, force=force)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/lamindb_setup/_delete.py", line 102, in delete
    n_objects = check_storage_is_empty(
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/lamindb_setup/core/upath.py", line 772, in check_storage_is_empty
    raise InstanceNotEmpty(message)
lamindb_setup.core.upath.InstanceNotEmpty: Storage '/home/runner/work/lamin-usecases/lamin-usecases/docs/test-spatial/.lamindb' contains 2 objects - delete them prior to deleting the instance
rm: cannot remove 'test-flow': No such file or directory