Jupyter Notebook

Integrate scRNA-seq datasets#

scRNA-seq data integration is the process of combining and 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 pandas as pd
import anndata as ad
βœ… loaded instance: testuser1/test-scrna (lamindb 0.51.0)
ln.track()
πŸ’‘ notebook imports: anndata==0.9.2 lamindb==0.51.0 lnschema_bionty==0.30.0 pandas==1.5.3
βœ… saved: Transform(id='agayZTonayqAz8', name='Integrate scRNA-seq datasets', short_name='scrna2', version='0', type=notebook, updated_at=2023-08-28 17:16:04, created_by_id='DzTjkKse')
βœ… saved: Run(id='WT8mTT9zT0UYmQZ6CKJ8', run_at=2023-08-28 17:16:04, transform_id='agayZTonayqAz8', created_by_id='DzTjkKse')

Query files based on metadata#

assays = lb.ExperimentalFactor.lookup()
species = lb.Species.lookup()
query = ln.File.filter(
    experimental_factors=assays.single_cell_rna_sequencing,  # scRNA-seq
    species=species.human,  # human
    cell_types__name__contains="monocyte",  # monocyte
).distinct()
query.df()
storage_id key suffix accessor description version initial_version_id size hash hash_type transform_id run_id updated_at created_by_id
id
JAEIQvXk3kQzN49qyaZ1 1cLCSSMz None .h5ad AnnData 10x reference pbmc68k None None 589484 eKVXV5okt5YRYjySMTKGEw md5 Nv48yAceNSh8z8 TCWhch5Bg3elg5AbiosQ 2023-08-28 17:15:56 DzTjkKse
VZkiaYQKBvvUpf3n3su6 1cLCSSMz None .h5ad AnnData Conde22 None None 28049505 WEFcMZxJNmMiUOFrcSTaig md5 Nv48yAceNSh8z8 TCWhch5Bg3elg5AbiosQ 2023-08-28 17:15:37 DzTjkKse

Intersect measured genes between two datasets#

# get file objects
file1, file2 = query.list()
file1.describe()
πŸ’‘ File(id='JAEIQvXk3kQzN49qyaZ1', key=None, suffix='.h5ad', accessor='AnnData', description='10x reference pbmc68k', version=None, size=589484, hash='eKVXV5okt5YRYjySMTKGEw', hash_type='md5', created_at=2023-08-28 17:15:56, updated_at=2023-08-28 17:15:56)

Provenance:
    πŸ—ƒοΈ storage: Storage(id='1cLCSSMz', root='/home/runner/work/lamin-usecases/lamin-usecases/docs/test-scrna', type='local', updated_at=2023-08-28 17:16:02, created_by_id='DzTjkKse')
    πŸ“” transform: Transform(id='Nv48yAceNSh8z8', name='Validate & register scRNA-seq datasets', short_name='scrna', version='0', type='notebook', updated_at=2023-08-28 17:15:56, created_by_id='DzTjkKse')
    πŸ‘£ run: Run(id='TCWhch5Bg3elg5AbiosQ', run_at=2023-08-28 17:14:50, transform_id='Nv48yAceNSh8z8', created_by_id='DzTjkKse')
    πŸ‘€ created_by: User(id='DzTjkKse', handle='testuser1', email='testuser1@lamin.ai', name='Test User1', updated_at=2023-08-28 17:16:02)
Features:
  var (X):
    πŸ”— index (695, bionty.Gene.id): ['a3QmOo0IvnYe', 'c5hmPGeRki1o', 'o2QhTjfss9tH', 'ptpxec1TDoY2', 'Glq61Ynh7mUc'...]
  external:
    πŸ”— assay (1, bionty.ExperimentalFactor): ['single-cell RNA sequencing']
    πŸ”— species (1, bionty.Species): ['human']
  obs (metadata):
    πŸ”— cell_type (9, bionty.CellType): ['cytotoxic T cell', 'CD16-positive, CD56-dim natural killer cell, human', 'B cell, CD19-positive', 'CD14-positive, CD16-negative classical monocyte', 'dendritic cell']
file1.view_lineage()
https://d33wubrfki0l68.cloudfront.net/c19f709ca80a49980ce1bf03cb5d528af96b452d/b3909/_images/3bb4466b4bd31c823ec2962fe904860d8da8558dfce3f905979773ae3e4e3f86.svg
file2.describe()
πŸ’‘ File(id='VZkiaYQKBvvUpf3n3su6', key=None, suffix='.h5ad', accessor='AnnData', description='Conde22', version=None, size=28049505, hash='WEFcMZxJNmMiUOFrcSTaig', hash_type='md5', created_at=2023-08-28 17:15:37, updated_at=2023-08-28 17:15:37)

Provenance:
    πŸ—ƒοΈ storage: Storage(id='1cLCSSMz', root='/home/runner/work/lamin-usecases/lamin-usecases/docs/test-scrna', type='local', updated_at=2023-08-28 17:16:02, created_by_id='DzTjkKse')
    πŸ“” transform: Transform(id='Nv48yAceNSh8z8', name='Validate & register scRNA-seq datasets', short_name='scrna', version='0', type='notebook', updated_at=2023-08-28 17:15:56, created_by_id='DzTjkKse')
    πŸ‘£ run: Run(id='TCWhch5Bg3elg5AbiosQ', run_at=2023-08-28 17:14:50, transform_id='Nv48yAceNSh8z8', created_by_id='DzTjkKse')
    πŸ‘€ created_by: User(id='DzTjkKse', handle='testuser1', email='testuser1@lamin.ai', name='Test User1', updated_at=2023-08-28 17:16:02)
Features:
  var (X):
    πŸ”— index (36503, bionty.Gene.id): ['0lN2hkvVbaC7', '5FZLIl9c0fbk', '5Gb7vNmjnyMe', '264EsVtODOKG', 'fuWx1qdWpSrz'...]
  obs (metadata):
    πŸ”— cell_type (32, bionty.CellType): ['classical monocyte', 'CD16-positive, CD56-dim natural killer cell, human', 'alpha-beta T cell', 'mucosal invariant T cell', 'germinal center B cell']
    πŸ”— assay (4, bionty.ExperimentalFactor): ["10x 3' v3", "10x 5' v2", "10x 5' v1", 'single-cell RNA sequencing']
    πŸ”— tissue (17, bionty.Tissue): ['liver', 'omentum', 'thymus', 'transverse colon', 'duodenum']
    πŸ”— donor (12, core.Label): ['A31', '640C', 'D496', '582C', '637C']
file2.view_lineage()
https://d33wubrfki0l68.cloudfront.net/a5d47d1b8c01080c9980fac4731588644ca5bfe3/13248/_images/156db72fad40204d9083021ffa2843df9aed62ac56ba753c4c8940be003c3d1c.svg

Load files into memory:

file1_adata = file1.load()
file2_adata = file2.load()
πŸ’‘ adding file JAEIQvXk3kQzN49qyaZ1 as input for run WT8mTT9zT0UYmQZ6CKJ8, adding parent transform Nv48yAceNSh8z8
πŸ’‘ adding file VZkiaYQKBvvUpf3n3su6 as input for run WT8mTT9zT0UYmQZ6CKJ8, adding parent transform Nv48yAceNSh8z8

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)
695
shared_genes.list("symbol")[:10]
['UBE2J1',
 'ANAPC16',
 'EIF2AK1',
 'IGBP1',
 'HCST',
 'IRF7',
 'S1PR5',
 'MRPS25',
 'IRF1',
 'PTPRCAP']

We also need to convert the ensembl_gene_id to symbol for file2 so that they can be concatenated:

mapper = pd.DataFrame(shared_genes.values_list("ensembl_gene_id", "symbol")).set_index(
    0
)[1]
mapper.head()
0
ENSG00000198833     UBE2J1
ENSG00000166295    ANAPC16
ENSG00000086232    EIF2AK1
ENSG00000089289      IGBP1
ENSG00000126264       HCST
Name: 1, dtype: object
file2_adata.var.rename(index=mapper, inplace=True)

Intersect 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',
 'conventional dendritic 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 subseted 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 = 126 Γ— 695
    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
conventional dendritic cell                         Conde22                    7
CD16-positive, CD56-dim natural killer cell, human  10x reference pbmc68k      3
conventional dendritic cell                         10x reference pbmc68k      2
dtype: int64
Hide code cell content
# clean up test instance
!lamin delete --force test-scrna
!rm -r ./test-scrna
πŸ’‘ 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