Analyze a collection in memory¶
Here, we’ll analyze the growing collection by loading it into memory. This is only possible if it’s not too large. If your data is large, you’ll likely want to iterate over the collection to train a model, the topic of the next page ().
import lamindb as ln
import bionty as bt
ln.track("mfWKm8OtAzp80000")
Show code cell output
→ connected lamindb: testuser1/test-scrna
→ created Transform('mfWKm8Ot'), started new Run('Pz9zmEkC') at 2024-12-20 15:05:25 UTC
→ notebook imports: bionty==0.53.2 lamindb==0.77.3 scanpy==1.10.4
ln.Collection.df()
Show code cell output
uid | name | description | hash | reference | reference_type | visibility | transform_id | meta_artifact_id | version | is_latest | run_id | created_at | created_by_id | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
id | ||||||||||||||
2 | vU9vGhm9ozKWE66f0001 | My versioned scRNA-seq collection | None | luH-jPb6eJLsXvc1TWGpUg | None | None | 1 | 2 | None | 2 | True | 2 | 2024-12-20 15:05:15.781635+00:00 | 1 |
1 | vU9vGhm9ozKWE66f0000 | My versioned scRNA-seq collection | None | DuyXxlMxwF92YehyBLbhKg | None | None | 1 | 1 | None | None | False | 1 | 2024-12-20 15:04:59.403847+00:00 | 1 |
collection = ln.Collection.get(name="My versioned scRNA-seq collection", version="2")
collection.artifacts.df()
Show code cell output
uid | key | description | suffix | type | size | hash | n_objects | n_observations | _hash_type | _accessor | visibility | _key_is_virtual | storage_id | transform_id | version | is_latest | run_id | created_at | created_by_id | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
id | ||||||||||||||||||||
1 | NAxy8G7q3xarNdqt0000 | None | Human immune cells from Conde22 | .h5ad | dataset | 57612943 | t_YJQpYrAyAGhs7Ir68zKj | None | 1648 | sha1-fl | AnnData | 1 | True | 1 | 1 | None | True | 1 | 2024-12-20 15:04:55.120402+00:00 | 1 |
3 | Y0PbHhR9YXSu8sGt0001 | None | 10x reference adata, trusted cell type annotation | .h5ad | dataset | 857336 | GK721a-L-fGDI8kXefKMtA | None | 70 | md5 | AnnData | 1 | True | 1 | 2 | None | True | 2 | 2024-12-20 15:05:15.598075+00:00 | 1 |
If the collection isn’t too large, we can now load it into memory.
Under-the-hood, the AnnData
objects are concatenated during loading.
The amount of time this takes depends on a variety of factors.
If it occurs often, one might consider storing a concatenated version of the collection, rather than the individual pieces.
adata = collection.load()
The default is an outer join during concatenation as in pandas:
adata
Show code cell output
AnnData object with n_obs × n_vars = 1718 × 36508
obs: 'donor', 'tissue', 'cell_type', 'assay', 'cell_type_untrusted', 'n_genes', 'percent_mito', 'louvain', 'cell_type_untrusted_original', 'artifact_uid'
obsm: 'X_umap', 'X_pca'
The AnnData
has the reference to the individual artifacts in the .obs
annotations:
adata.obs.artifact_uid.cat.categories
Show code cell output
Index(['NAxy8G7q3xarNdqt0000', 'Y0PbHhR9YXSu8sGt0001'], dtype='object')
We can easily obtain ensemble IDs for gene symbols using the look up object:
genes = bt.Gene.lookup(field="symbol")
genes.itm2b.ensembl_gene_id
Show code cell output
'ENSG00000136156'
Let us create a plot:
import scanpy as sc
sc.pp.pca(adata, n_comps=2)
sc.pl.pca(
adata,
color=genes.itm2b.ensembl_gene_id,
title=(
f"{genes.itm2b.symbol} / {genes.itm2b.ensembl_gene_id} /"
f" {genes.itm2b.description}"
),
save="_itm2b",
)
WARNING: saving figure to file figures/pca_itm2b.pdf
We could save a plot as a pdf and then see it in the flow diagram:
artifact = ln.Artifact("./figures/pca_itm2b.pdf", description="My result on ITM2B")
artifact.save()
artifact.view_lineage()
But given the image is part of the notebook, we can also rely on the report that we create when saving the notebook:
ln.finish()