Jupyter Notebook Binder

Integrate scRNA-seq datasets#

scRNA-seq data integration is the process of analyzing data from several scRNA sequencing experiments to uncover common or distinct biological insights and patterns.

Here, weโ€™ll demonstrate how to fetch two scRNA-seq datasets by registered metadata such as cell types to finally integrate them.

Setup#

!lamin load test-scrna
Hide code cell output
๐Ÿ’ก found cached instance metadata: /home/runner/.lamin/instance--testuser1--test-scrna.env
๐Ÿ’ก loaded instance: testuser1/test-scrna

import lamindb as ln
import lnschema_bionty as lb
import anndata as ad
๐Ÿ’ก loaded instance: testuser1/test-scrna (lamindb 0.54.1)
ln.track()
๐Ÿ’ก notebook imports: anndata==0.9.2 lamindb==0.54.1 lnschema_bionty==0.31.2
โ— record with similar name exist! did you mean to load it?
id __ratio__
name
scRNA-seq Nv48yAceNSh8z8 90.0
๐Ÿ’ก Transform(id='agayZTonayqAz8', name='Integrate scRNA-seq datasets', short_name='scrna2', version='0', type=notebook, updated_at=2023-09-26 10:01:42, created_by_id='DzTjkKse')
๐Ÿ’ก Run(id='AMlwDfuF1DMaFdQtWmLT', run_at=2023-09-26 10:01:42, 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
Integrate scRNA-seq datasets agayZTonayqAz8 90.0
scRNA-seq Nv48yAceNSh8z8 90.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
lt6Pz6ZsjpYJT67bFxvH JrMfnWtB None .h5ad AnnData Conde22 None 28049505 WEFcMZxJNmMiUOFrcSTaig md5 Nv48yAceNSh8z8 uQjJfR9uejLQvh4Maob9 None 2023-09-26 10:01:05 DzTjkKse
tqWdzJZblcKxZAWpv9Dw JrMfnWtB None .h5ad AnnData 10x reference pbmc68k None 660792 a2V0IgOjMRHsCeZH169UOQ md5 Nv48yAceNSh8z8 uQjJfR9uejLQvh4Maob9 None 2023-09-26 10:01:36 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
lt6Pz6ZsjpYJT67bFxvH JrMfnWtB None .h5ad AnnData Conde22 None 28049505 WEFcMZxJNmMiUOFrcSTaig md5 Nv48yAceNSh8z8 uQjJfR9uejLQvh4Maob9 None 2023-09-26 10:01:05 DzTjkKse
tqWdzJZblcKxZAWpv9Dw JrMfnWtB None .h5ad AnnData 10x reference pbmc68k None 660792 a2V0IgOjMRHsCeZH169UOQ md5 Nv48yAceNSh8z8 uQjJfR9uejLQvh4Maob9 None 2023-09-26 10:01:36 DzTjkKse

Transform #

Compare gene sets#

Get file objects:

query = ln.File.filter()
file1, file2 = query.list()
file1.describe()
File(id='lt6Pz6ZsjpYJT67bFxvH', suffix='.h5ad', accessor='AnnData', description='Conde22', size=28049505, hash='WEFcMZxJNmMiUOFrcSTaig', hash_type='md5', updated_at=2023-09-26 10:01:05)

Provenance:
  ๐Ÿ—ƒ๏ธ storage: Storage(id='JrMfnWtB', root='/home/runner/work/lamin-usecases/lamin-usecases/docs/test-scrna', type='local', updated_at=2023-09-26 10:00:20, created_by_id='DzTjkKse')
  ๐Ÿ“” transform: Transform(id='Nv48yAceNSh8z8', name='scRNA-seq', short_name='scrna', version='0', type='notebook', updated_at=2023-09-26 10:01:36, created_by_id='DzTjkKse')
  ๐Ÿ‘ฃ run: Run(id='uQjJfR9uejLQvh4Maob9', run_at=2023-09-26 10:00:22, transform_id='Nv48yAceNSh8z8', created_by_id='DzTjkKse')
  ๐Ÿ‘ค created_by: User(id='DzTjkKse', handle='testuser1', email='testuser1@lamin.ai', name='Test User1', updated_at=2023-09-26 10:00:20)
Features:
  var: FeatureSet(id='2xaSVOatJvBhugjyofOg', n=36503, type='number', registry='bionty.Gene', hash='dnRexHCtxtmOU81_EpoJ', updated_at=2023-09-26 10:01:01, modality_id='zSP2x0KE', created_by_id='DzTjkKse')
    'IGHVIII-22-2', 'PLCH1-AS1', 'GJA5', 'RCAN3AS', 'TRAV12-2', 'TOM1L1', 'ISL2', 'CCDC106', 'ATP6AP1L', 'PLEKHM1', ...
  obs: FeatureSet(id='8UrKgOnkO6WC95sqmxwm', n=4, registry='core.Feature', hash='HtHmLeLRsrurQy4M012t', updated_at=2023-09-26 10:01:05, modality_id='YH56Pqcv', created_by_id='DzTjkKse')
    ๐Ÿ”— assay (4, bionty.ExperimentalFactor): 'single-cell RNA sequencing', '10x 5' v2', '10x 5' v1', '10x 3' v3'
    ๐Ÿ”— tissue (17, bionty.Tissue): 'transverse colon', 'lung', 'lamina propria', 'liver', 'jejunal epithelium', 'thoracic lymph node', 'caecum', 'omentum', 'thymus', 'bone marrow', ...
    ๐Ÿ”— cell_type (32, bionty.CellType): 'CD16-positive, CD56-dim natural killer cell, human', 'CD4-positive helper T cell', 'germinal center B cell', 'mast cell', 'memory B cell', 'non-classical monocyte', 'CD16-negative, CD56-bright natural killer cell, human', 'dendritic cell, human', 'plasmablast', 'effector memory CD8-positive, alpha-beta T cell, terminally differentiated', ...
    ๐Ÿ”— donor (12, core.ULabel): 'D503', 'D496', 'A37', '582C', '637C', 'A35', '640C', 'A52', 'A29', 'A36', ...
Labels:
  ๐Ÿท๏ธ species (1, bionty.Species): 'human'
  ๐Ÿท๏ธ tissues (17, bionty.Tissue): 'transverse colon', 'lung', 'lamina propria', 'liver', 'jejunal epithelium', 'thoracic lymph node', 'caecum', 'omentum', 'thymus', 'bone marrow', ...
  ๐Ÿท๏ธ cell_types (32, bionty.CellType): 'CD16-positive, CD56-dim natural killer cell, human', 'CD4-positive helper T cell', 'germinal center B cell', 'mast cell', 'memory B cell', 'non-classical monocyte', 'CD16-negative, CD56-bright natural killer cell, human', 'dendritic cell, human', 'plasmablast', 'effector memory CD8-positive, alpha-beta T cell, terminally differentiated', ...
  ๐Ÿท๏ธ experimental_factors (4, bionty.ExperimentalFactor): 'single-cell RNA sequencing', '10x 5' v2', '10x 5' v1', '10x 3' v3'
  ๐Ÿท๏ธ ulabels (12, core.ULabel): 'D503', 'D496', 'A37', '582C', '637C', 'A35', '640C', 'A52', 'A29', 'A36', ...
file1.view_flow()
https://d33wubrfki0l68.cloudfront.net/b053c6018aba48d2dccb01773fabee225f024afd/c8df3/_images/0051f75b4139e3a6222954ef8616c2eff0eb3780ff8f2ee77aeb538b4045a99f.svg
file2.describe()
File(id='tqWdzJZblcKxZAWpv9Dw', suffix='.h5ad', accessor='AnnData', description='10x reference pbmc68k', size=660792, hash='a2V0IgOjMRHsCeZH169UOQ', hash_type='md5', updated_at=2023-09-26 10:01:36)

Provenance:
  ๐Ÿ—ƒ๏ธ storage: Storage(id='JrMfnWtB', root='/home/runner/work/lamin-usecases/lamin-usecases/docs/test-scrna', type='local', updated_at=2023-09-26 10:00:20, created_by_id='DzTjkKse')
  ๐Ÿ“” transform: Transform(id='Nv48yAceNSh8z8', name='scRNA-seq', short_name='scrna', version='0', type='notebook', updated_at=2023-09-26 10:01:36, created_by_id='DzTjkKse')
  ๐Ÿ‘ฃ run: Run(id='uQjJfR9uejLQvh4Maob9', run_at=2023-09-26 10:00:22, transform_id='Nv48yAceNSh8z8', created_by_id='DzTjkKse')
  ๐Ÿ‘ค created_by: User(id='DzTjkKse', handle='testuser1', email='testuser1@lamin.ai', name='Test User1', updated_at=2023-09-26 10:00:20)
Features:
  var: FeatureSet(id='WLNHMA3KRo1WRjVxCtqM', n=754, type='number', registry='bionty.Gene', hash='WMDxN7253SdzGwmznV5d', updated_at=2023-09-26 10:01:36, modality_id='zSP2x0KE', created_by_id='DzTjkKse')
    'PRR4', 'SERTAD3', 'GNAI2', 'SH2D2A', 'PSMB2', 'TTC39C', 'AKR1C3', 'CREG1', 'LYAR', 'NDUFA1', ...
  obs: FeatureSet(id='ZuJum5OBPLMZ2KhxsBxV', n=1, registry='core.Feature', hash='4alveawsOtPd6XOQrelA', updated_at=2023-09-26 10:01:36, modality_id='YH56Pqcv', created_by_id='DzTjkKse')
    ๐Ÿ”— cell_type (9, bionty.CellType): 'CD16-positive, CD56-dim natural killer cell, human', 'monocyte', 'gamma-delta T cell', 'CD24-positive, CD4 single-positive thymocyte', 'CD8-positive, CD25-positive, alpha-beta regulatory T cell', 'dendritic cell', 'cytotoxic T cell', 'B cell, CD19-positive', 'CD4-positive, alpha-beta T cell'
  external: FeatureSet(id='rfq2FhDLPC0pcoMBdYXK', n=2, registry='core.Feature', hash='z92d4gRZnl8_S4ZxaErt', updated_at=2023-09-26 10:01:37, modality_id='YH56Pqcv', 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): 'CD16-positive, CD56-dim natural killer cell, human', 'monocyte', 'gamma-delta T cell', 'CD24-positive, CD4 single-positive thymocyte', 'CD8-positive, CD25-positive, alpha-beta regulatory T cell', 'dendritic cell', 'cytotoxic T cell', 'B cell, CD19-positive', 'CD4-positive, alpha-beta T cell'
  ๐Ÿท๏ธ experimental_factors (1, bionty.ExperimentalFactor): 'single-cell RNA sequencing'
file2.view_flow()
https://d33wubrfki0l68.cloudfront.net/f51aafd78b445793eea131964441ac60a5dc1dfc/669b2/_images/480197fd2beada2ba6dc00db196f23a5eaaf534970365d9cb0ae0e7d1f381c58.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]
['VPS50',
 'FYB1',
 'TCEAL8',
 'LDHB',
 'CCL5',
 'PSMD7',
 'ANKRD12',
 'MRPS6',
 'GTF3C6',
 'NSRP1']

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 pbmc68k      4
CD16-positive, CD56-dim natural killer cell, human  10x reference pbmc68k      3
dtype: int64
# clean up test instance
!lamin delete --force test-scrna
!rm -r ./test-scrna
Hide code cell output
๐Ÿ’ก deleting instance testuser1/test-scrna
โœ…     deleted instance settings file: /home/runner/.lamin/instance--testuser1--test-scrna.env
โœ…     instance cache deleted
โœ…     deleted '.lndb' sqlite file
โ—     consider manually deleting your stored data: /home/runner/work/lamin-usecases/lamin-usecases/docs/test-scrna