Jupyter Notebook

Query individual files#

Here, weโ€™ll query individual files and inspect their metadata.

This guide can be skipped if you are only interested in how to leverage the overall dataset.

import lamindb as ln
import lnschema_bionty as lb
import anndata as ad
๐Ÿ’ก loaded instance: testuser1/test-scrna (lamindb 0.54.4)
ln.track()
๐Ÿ’ก notebook imports: anndata==0.9.2 lamindb==0.54.4 lnschema_bionty==0.31.2
๐Ÿ’ก Transform(id='agayZTonayqAz8', name='Query individual files', short_name='scrna2', version='0', type=notebook, updated_at=2023-10-02 10:19:49, created_by_id='DzTjkKse')
๐Ÿ’ก Run(id='j9Njg6IoNhlhnpu6Y4am', run_at=2023-10-02 10:19:49, transform_id='agayZTonayqAz8', created_by_id='DzTjkKse')

Access #

Query files by provenance metadata#

users = ln.User.lookup()
ln.Transform.filter(created_by=users.testuser1).search("scrna")
id __ratio__
name
scRNA-seq Nv48yAceNSh8z8 90.0
Append a new batch of data ManDYgmftZ8Cz8 36.0
Query individual files agayZTonayqAz8 36.0
transform = ln.Transform.filter(id="Nv48yAceNSh8z8").one()
ln.File.filter(transform=transform).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
p563WE4VtMIf6FQfXJmN 7ZVN6khD None .h5ad AnnData Conde22 None 28049505 WEFcMZxJNmMiUOFrcSTaig md5 Nv48yAceNSh8z8 zfuVcVfil2w5LqYdHn2K None 2023-10-02 10:18:54 DzTjkKse

Query files based on biological metadata#

assays = lb.ExperimentalFactor.lookup()
species = lb.Species.lookup()
cell_types = lb.CellType.lookup()
query = ln.File.filter(
    experimental_factors=assays.single_cell_rna_sequencing,
    species=species.human,
    cell_types=cell_types.gamma_delta_t_cell,
)
query.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
be3FZ3wa9dpwAGDafjLI 7ZVN6khD None .h5ad AnnData 10x reference adata None 660792 a2V0IgOjMRHsCeZH169UOQ md5 ManDYgmftZ8Cz8 BJevtAUNru8jYr9cO093 None 2023-10-02 10:19:35 DzTjkKse
p563WE4VtMIf6FQfXJmN 7ZVN6khD None .h5ad AnnData Conde22 None 28049505 WEFcMZxJNmMiUOFrcSTaig md5 Nv48yAceNSh8z8 zfuVcVfil2w5LqYdHn2K None 2023-10-02 10:18:54 DzTjkKse

Transform #

Compare gene sets#

Get file objects:

query = ln.File.filter()
file1, file2 = query.list()
file1.describe()
File(id='p563WE4VtMIf6FQfXJmN', suffix='.h5ad', accessor='AnnData', description='Conde22', size=28049505, hash='WEFcMZxJNmMiUOFrcSTaig', hash_type='md5', updated_at=2023-10-02 10:18:54)

Provenance:
  ๐Ÿ—ƒ๏ธ storage: Storage(id='7ZVN6khD', root='/home/runner/work/lamin-usecases/lamin-usecases/docs/test-scrna', type='local', updated_at=2023-10-02 10:18:00, created_by_id='DzTjkKse')
  ๐Ÿ“” transform: Transform(id='Nv48yAceNSh8z8', name='scRNA-seq', short_name='scrna', version='0', type='notebook', updated_at=2023-10-02 10:18:54, created_by_id='DzTjkKse')
  ๐Ÿ‘ฃ run: Run(id='zfuVcVfil2w5LqYdHn2K', run_at=2023-10-02 10:18:06, transform_id='Nv48yAceNSh8z8', created_by_id='DzTjkKse')
  ๐Ÿ‘ค created_by: User(id='DzTjkKse', handle='testuser1', email='testuser1@lamin.ai', name='Test User1', updated_at=2023-10-02 10:18:00)
  โฌ‡๏ธ input_of (core.Run): ['2023-10-02 10:19:02']
Features:
  var: FeatureSet(id='bDDl0r5yF0f3NAADRbV1', n=36503, type='number', registry='bionty.Gene', hash='dnRexHCtxtmOU81_EpoJ', updated_at=2023-10-02 10:18:44, modality_id='Z4a7WYsI', created_by_id='DzTjkKse')
    'CAPN2', 'RAD1', 'None', 'ENOX2', 'None', 'None', 'None', 'UQCR11', 'None', 'None', 'LINC02719', 'ABCD4', 'GASK1A', 'None', 'IGHV3OR16-8', 'None', 'None', 'None', 'NUP133', 'RIN1', ...
  obs: FeatureSet(id='rDCwyTehbqggk8V1Jxey', n=4, registry='core.Feature', hash='s4COhCi1Xv9Ks3BjuSO9', updated_at=2023-10-02 10:18:48, modality_id='9FuQHsY3', created_by_id='DzTjkKse')
    ๐Ÿ”— donor (12, core.ULabel): 'A37', 'A31', 'A36', 'A29', '637C', 'D503', '640C', 'D496', '621B', '582C', ...
    ๐Ÿ”— cell_type (32, bionty.CellType): 'animal cell', 'mast cell', 'group 3 innate lymphoid cell', 'plasma cell', 'plasmablast', 'memory B cell', 'naive thymus-derived CD4-positive, alpha-beta T cell', 'megakaryocyte', 'macrophage', 'regulatory T cell', ...
    ๐Ÿ”— assay (4, bionty.ExperimentalFactor): 'single-cell RNA sequencing', '10x 5' v1', '10x 3' v3', '10x 5' v2'
    ๐Ÿ”— tissue (17, bionty.Tissue): 'thymus', 'ileum', 'duodenum', 'spleen', 'transverse colon', 'jejunal epithelium', 'lamina propria', 'mesenteric lymph node', 'bone marrow', 'thoracic lymph node', ...
Labels:
  ๐Ÿท๏ธ species (1, bionty.Species): 'human'
  ๐Ÿท๏ธ tissues (17, bionty.Tissue): 'thymus', 'ileum', 'duodenum', 'spleen', 'transverse colon', 'jejunal epithelium', 'lamina propria', 'mesenteric lymph node', 'bone marrow', 'thoracic lymph node', ...
  ๐Ÿท๏ธ cell_types (32, bionty.CellType): 'animal cell', 'mast cell', 'group 3 innate lymphoid cell', 'plasma cell', 'plasmablast', 'memory B cell', 'naive thymus-derived CD4-positive, alpha-beta T cell', 'megakaryocyte', 'macrophage', 'regulatory T cell', ...
  ๐Ÿท๏ธ experimental_factors (4, bionty.ExperimentalFactor): 'single-cell RNA sequencing', '10x 5' v1', '10x 3' v3', '10x 5' v2'
  ๐Ÿท๏ธ ulabels (12, core.ULabel): 'A37', 'A31', 'A36', 'A29', '637C', 'D503', '640C', 'D496', '621B', '582C', ...
file1.view_flow()
https://d33wubrfki0l68.cloudfront.net/a11fc5dcced928e933fb59412f28894c6d609e94/1105d/_images/388b63b73aba4addfbda9d226704f986b3a4a38869f388d799a1433a79c2a709.svg
file2.describe()
File(id='be3FZ3wa9dpwAGDafjLI', suffix='.h5ad', accessor='AnnData', description='10x reference adata', size=660792, hash='a2V0IgOjMRHsCeZH169UOQ', hash_type='md5', updated_at=2023-10-02 10:19:35)

Provenance:
  ๐Ÿ—ƒ๏ธ storage: Storage(id='7ZVN6khD', root='/home/runner/work/lamin-usecases/lamin-usecases/docs/test-scrna', type='local', updated_at=2023-10-02 10:18:00, created_by_id='DzTjkKse')
  ๐Ÿ“” transform: Transform(id='ManDYgmftZ8Cz8', name='Append a new batch of data', short_name='scrna1', version='0', type='notebook', updated_at=2023-10-02 10:19:37, created_by_id='DzTjkKse')
  ๐Ÿ‘ฃ run: Run(id='BJevtAUNru8jYr9cO093', run_at=2023-10-02 10:19:02, transform_id='ManDYgmftZ8Cz8', created_by_id='DzTjkKse')
  ๐Ÿ‘ค created_by: User(id='DzTjkKse', handle='testuser1', email='testuser1@lamin.ai', name='Test User1', updated_at=2023-10-02 10:18:00)
Features:
  var: FeatureSet(id='JH4lHm0bKOjJwld8gvuS', n=754, type='number', registry='bionty.Gene', hash='WMDxN7253SdzGwmznV5d', updated_at=2023-10-02 10:19:35, modality_id='Z4a7WYsI', created_by_id='DzTjkKse')
    'PTPRC', 'COX17', 'HIGD2A', 'LINC01857', 'MRPS6', 'GZMH', 'TPD52', 'TMEM256', 'SNX2', 'CCT8', 'TBC1D10C', 'FCN1', 'MATK', 'PRDX6', 'APEX1', 'TINF2', 'HSD17B8', 'DEK', 'NCL', 'GZMK', ...
  obs: FeatureSet(id='iJO8vTcbKF2WmUE5abJn', n=1, registry='core.Feature', hash='N2S5inpQSO0LupVR9d63', updated_at=2023-10-02 10:19:35, modality_id='9FuQHsY3', created_by_id='DzTjkKse')
    ๐Ÿ”— cell_type (9, bionty.CellType): 'dendritic cell', 'B cell, CD19-positive', 'cytotoxic T cell', 'CD8-positive, CD25-positive, alpha-beta regulatory T cell', 'monocyte', 'CD4-positive, alpha-beta T cell', 'CD16-positive, CD56-dim natural killer cell, human', 'gamma-delta T cell', 'CD24-positive, CD4 single-positive thymocyte'
  external: FeatureSet(id='Tb1kUtpJdIBsnA90OVZ7', n=2, registry='core.Feature', hash='K0Hptsob4fESZu-GTvkj', updated_at=2023-10-02 10:19:35, modality_id='9FuQHsY3', created_by_id='DzTjkKse')
    ๐Ÿ”— assay (1, bionty.ExperimentalFactor): 'single-cell RNA sequencing'
    ๐Ÿ”— species (1, bionty.Species): 'human'
Labels:
  ๐Ÿท๏ธ species (1, bionty.Species): 'human'
  ๐Ÿท๏ธ cell_types (9, bionty.CellType): 'dendritic cell', 'B cell, CD19-positive', 'cytotoxic T cell', 'CD8-positive, CD25-positive, alpha-beta regulatory T cell', 'monocyte', 'CD4-positive, alpha-beta T cell', 'CD16-positive, CD56-dim natural killer cell, human', 'gamma-delta T cell', 'CD24-positive, CD4 single-positive thymocyte'
  ๐Ÿท๏ธ experimental_factors (1, bionty.ExperimentalFactor): 'single-cell RNA sequencing'
file2.view_flow()
https://d33wubrfki0l68.cloudfront.net/879b51380fd5d4178521cfa78611f5cb4cddc418/58716/_images/4d757bea63cc7c18b5cb7022ed477ad3cf8c3028ee987af84d1b985040b76c10.svg

Load files into memory:

file1_adata = file1.load()
file2_adata = file2.load()

Here we compute shared genes without loading files:

file1_genes = file1.features["var"]
file2_genes = file2.features["var"]

shared_genes = file1_genes & file2_genes
len(shared_genes)
749
shared_genes.list("symbol")[:10]
['CAPN2',
 'NDUFAF3',
 'PRKCQ-AS1',
 'PNISR',
 'CFD',
 'CWC25',
 'LCK',
 'LIMS1',
 'NSA2',
 'EBPL']

Compare cell types#

file1_celltypes = file1.cell_types.all()
file2_celltypes = file2.cell_types.all()

shared_celltypes = file1_celltypes & file2_celltypes
shared_celltypes_names = shared_celltypes.list("name")
shared_celltypes_names
['CD16-positive, CD56-dim natural killer cell, human', 'gamma-delta T cell']

We can now subset the two datasets by shared cell types:

file1_adata_subset = file1_adata[
    file1_adata.obs["cell_type"].isin(shared_celltypes_names)
]

file2_adata_subset = file2_adata[
    file2_adata.obs["cell_type"].isin(shared_celltypes_names)
]

Concatenate subsetted datasets:

adata_concat = ad.concat(
    [file1_adata_subset, file2_adata_subset],
    label="file",
    keys=[file1.description, file2.description],
)
adata_concat
AnnData object with n_obs ร— n_vars = 187 ร— 749
    obs: 'cell_type', 'file'
    obsm: 'X_umap'
adata_concat.obs.value_counts()
cell_type                                           file               
CD16-positive, CD56-dim natural killer cell, human  Conde22                114
gamma-delta T cell                                  Conde22                 66
                                                    10x reference adata      4
CD16-positive, CD56-dim natural killer cell, human  10x reference adata      3
dtype: int64