scrna4/6 Jupyter Notebook lamindata

Analyze a dataset in memory#

Here, we’ll analyze the growing dataset by loading it into memory.

This is only possible if it’s not too large. If you deal with particularly large data, please read the guide on iterating over datta batches (to come).

import lamindb as ln
import lnschema_bionty as lb
import anndata as ad
💡 loaded instance: testuser1/test-scrna (lamindb 0.55.2)
ln.track()
💡 notebook imports: anndata==0.9.2 lamindb==0.55.2 lnschema_bionty==0.31.2 scanpy==1.9.5
💡 Transform(id='mfWKm8OtAzp8z8', name='Analyze a dataset in memory', short_name='scrna4', version='0', type=notebook, updated_at=2023-10-10 15:43:51, created_by_id='DzTjkKse')
💡 Run(id='rBpXaKwLgV2rLNLLgYXP', run_at=2023-10-10 15:43:51, transform_id='mfWKm8OtAzp8z8', created_by_id='DzTjkKse')
ln.Dataset.filter().df()
name description version hash reference reference_type transform_id run_id file_id storage_id initial_version_id updated_at created_by_id
id
oYS8NR45oBcEPgCISfCm My versioned scRNA-seq dataset None 1 6Hu1BywwK6bfIU2Dpku2xZ None None Nv48yAceNSh8z8 RNgU2xx7TeUrL3d83b4F oYS8NR45oBcEPgCISfCm None None 2023-10-10 15:42:41 DzTjkKse
oYS8NR45oBcEPgCISfHj My versioned scRNA-seq dataset None 2 JNjc88f22TLVPJHdgo7X None None ManDYgmftZ8Cz8 JRscXJ0ZxufwqGUoGmIJ None None oYS8NR45oBcEPgCISfCm 2023-10-10 15:43:32 DzTjkKse
dataset = ln.Dataset.filter(name="My versioned scRNA-seq dataset", version="2").one()
dataset.files.df()
storage_id key suffix accessor description version size hash hash_type transform_id run_id initial_version_id updated_at created_by_id
id
oYS8NR45oBcEPgCISfCm YpFBBtr4 None .h5ad AnnData Conde22 None 57615999 6Hu1BywwK6bfIU2Dpku2xZ sha1-fl Nv48yAceNSh8z8 RNgU2xx7TeUrL3d83b4F None 2023-10-10 15:42:41 DzTjkKse
wRXv3wXHrtOF3OihYYya YpFBBtr4 None .h5ad AnnData 10x reference adata None 857752 j6o6e27xPdqHQyT7Em_7MQ md5 ManDYgmftZ8Cz8 JRscXJ0ZxufwqGUoGmIJ None 2023-10-10 15:43:24 DzTjkKse

If the dataset doesn’t consist of too many files, 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 dataset, rather than the individual pieces.

adata = dataset.load()

The default is an outer join during concatenation as in pandas:

adata
AnnData object with n_obs × n_vars = 1718 × 36508
    obs: 'donor', 'tissue', 'cell_type', 'assay', 'n_genes', 'percent_mito', 'louvain', 'file_id'
    obsm: 'X_umap', 'X_pca'

The AnnData has the reference to the individual files in the .obs annotations:

adata.obs.file_id.cat.categories
Index(['oYS8NR45oBcEPgCISfCm', 'wRXv3wXHrtOF3OihYYya'], dtype='object')

We can easily obtain ensemble IDs for gene symbols using the look up object:

genes = lb.Gene.lookup(field="symbol")
genes.itm2b.ensembl_gene_id
'ENSG00000136156'

Let us create a plot:

import scanpy as sc

sc.pp.pca(adata, n_comps=2)
2023-10-10 15:43:56,153:INFO - Failed to extract font properties from /usr/share/fonts/truetype/noto/NotoColorEmoji.ttf: In FT2Font: Can not load face (unknown file format; error code 0x2)
2023-10-10 15:43:56,278:INFO - generated new fontManager
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
https://d33wubrfki0l68.cloudfront.net/86fde50191ff1169a082b768b0265dcdd3fa870c/52c2e/_images/affcdf940684a976e5f0a9fc8f8cdc2eb938aa47814f9903eaa6660590d47336.png
file = ln.File("./figures/pca_itm2b.pdf", description="My result on ITM2B")
file.save()
file.view_flow()
https://d33wubrfki0l68.cloudfront.net/e3090bf50e326f4882b9693b47e5a51b327c4654/e921e/_images/c58c52bddb59a75cf0e83db0c6f0bca866a7cf18eb69232dc4f1d9ddf6bd90f8.svg

Given the image is part of the notebook, there isn’t an actual need to save it and you can also rely on the report that you’ll create when saving the notebook via the command line via:

lamin save <notebook_path>