Jupyter Notebook Binder

Project flow#

LaminDB allows tracking data flow on the entire project level.

Here, we walk through exemplified app uploads, pipelines & notebooks following Schmidt et al., 2022.

A CRISPR screen reading out a phenotypic endpoint on T cells is paired with scRNA-seq to generate insights into IFN-γ production.

These insights get linked back to the original data through the steps taken in the project to provide context for interpretation & future decision making.

More specifically: Why should I care about data flow?

Data flow tracks data sources & transformations to trace biological insights, verify experimental outcomes, meet regulatory standards, increase the robustness of research and optimize the feedback loop of team-wide learning iterations.

While tracking data flow is easier when it’s governed by deterministic pipelines, it becomes hard when it’s governed by interactive human-driven analyses.

LaminDB interfaces workflow mangers for the former and embraces the latter.

Setup#

Init a test instance:

!lamin init --storage ./mydata
Hide code cell output
✅ saved: User(id='DzTjkKse', handle='testuser1', email='testuser1@lamin.ai', name='Test User1', updated_at=2023-09-27 19:02:52)
✅ saved: Storage(id='Uw2znSl2', root='/home/runner/work/lamin-usecases/lamin-usecases/docs/mydata', type='local', updated_at=2023-09-27 19:02:52, created_by_id='DzTjkKse')
💡 loaded instance: testuser1/mydata
💡 did not register local instance on hub (if you want, call `lamin register`)

Import lamindb:

import lamindb as ln
from IPython.display import Image, display
💡 loaded instance: testuser1/mydata (lamindb 0.54.2)

Steps#

In the following, we walk through exemplified steps covering different types of transforms (Transform).

Note

The full notebooks are in this repository.

App upload of phenotypic data #

Register data through app upload from wetlab by testuser1:

ln.setup.login("testuser1")
transform = ln.Transform(name="Upload GWS CRISPRa result", type="app")
ln.track(transform)
output_path = ln.dev.datasets.schmidt22_crispra_gws_IFNG(ln.settings.storage)
output_file = ln.File(output_path, description="Raw data of schmidt22 crispra GWS")
output_file.save()
Hide code cell output
💡 Transform(id='OrYrfe6KoWNH6m', name='Upload GWS CRISPRa result', type='app', updated_at=2023-09-27 19:02:54, created_by_id='DzTjkKse')
💡 Run(id='M5V5GVuLhUZ2nDxFayJR', run_at=2023-09-27 19:02:54, transform_id='OrYrfe6KoWNH6m', created_by_id='DzTjkKse')

Hit identification in notebook #

Access, transform & register data in drylab by testuser2:

ln.setup.login("testuser2")
transform = ln.Transform(name="GWS CRIPSRa analysis", type="notebook")
ln.track(transform)
# access
input_file = ln.File.filter(key="schmidt22-crispra-gws-IFNG.csv").one()
# identify hits
input_df = input_file.load().set_index("id")
output_df = input_df[input_df["pos|fdr"] < 0.01].copy()
# register hits in output file
ln.File(output_df, description="hits from schmidt22 crispra GWS").save()
Hide code cell output
💡 Transform(id='VOzpXfg2Ly1UAx', name='GWS CRIPSRa analysis', type='notebook', updated_at=2023-09-27 19:02:55, created_by_id='bKeW4T6E')
💡 Run(id='8yVjjb5ZpggZ2HiZPXZU', run_at=2023-09-27 19:02:55, transform_id='VOzpXfg2Ly1UAx', created_by_id='bKeW4T6E')

Inspect data flow:

file = ln.File.filter(description="hits from schmidt22 crispra GWS").one()
file.view_flow()
https://d33wubrfki0l68.cloudfront.net/1308d171c75016543f64dd692ba7d55778ee47fd/0c386/_images/ceaf65d7d020a8b11f3ad12fda2e3da2eb5dd5f43c0d6a29585368bff5e755f1.svg

Sequencer upload #

Upload files from sequencer:

ln.setup.login("testuser1")
ln.track(ln.Transform(name="Chromium 10x upload", type="pipeline"))
# register output files of upload
upload_dir = ln.dev.datasets.dir_scrnaseq_cellranger(
    "perturbseq", basedir=ln.settings.storage, output_only=False
)
ln.File(upload_dir.parent / "fastq/perturbseq_R1_001.fastq.gz").save()
ln.File(upload_dir.parent / "fastq/perturbseq_R2_001.fastq.gz").save()
ln.setup.login("testuser2")
Hide code cell output
💡 Transform(id='4NLGotZj2DoWd4', name='Chromium 10x upload', type='pipeline', updated_at=2023-09-27 19:02:56, created_by_id='DzTjkKse')
💡 Run(id='QYrR4v25aVvAFzmsozU4', run_at=2023-09-27 19:02:56, transform_id='4NLGotZj2DoWd4', created_by_id='DzTjkKse')
❗ file has more than one suffix (path.suffixes), inferring: '.fastq.gz'
❗ file has more than one suffix (path.suffixes), inferring: '.fastq.gz'

scRNA-seq bioinformatics pipeline #

Process uploaded files using a script or workflow manager: Pipelines and obtain 3 output files in a directory filtered_feature_bc_matrix/:

transform = ln.Transform(name="Cell Ranger", version="7.2.0", type="pipeline")
ln.track(transform)
# access uploaded files as inputs for the pipeline
input_files = ln.File.filter(key__startswith="fastq/perturbseq").all()
input_paths = [file.stage() for file in input_files]
# register output files
output_files = ln.File.from_dir("./mydata/perturbseq/filtered_feature_bc_matrix/")
ln.save(output_files)
Hide code cell output
💡 Transform(id='Sn8ZgVYvAaTqh6', name='Cell Ranger', version='7.2.0', type='pipeline', updated_at=2023-09-27 19:02:57, created_by_id='bKeW4T6E')
💡 Run(id='Z7yK4xernE3IvTYCe3m3', run_at=2023-09-27 19:02:57, transform_id='Sn8ZgVYvAaTqh6', created_by_id='bKeW4T6E')
❗ file has more than one suffix (path.suffixes), inferring: '.mtx.gz'
❗ file has more than one suffix (path.suffixes), inferring: '.tsv.gz'
❗ file has more than one suffix (path.suffixes), inferring: '.tsv.gz'

Post-process these 3 files:

transform = ln.Transform(name="Postprocess Cell Ranger", version="2.0", type="pipeline")
ln.track(transform)
input_files = [f.stage() for f in output_files]
output_path = ln.dev.datasets.schmidt22_perturbseq(basedir=ln.settings.storage)
output_file = ln.File(output_path, description="perturbseq counts")
output_file.save()
Hide code cell output
❗ record with similar name exist! did you mean to load it?
id __ratio__
name
Cell Ranger Sn8ZgVYvAaTqh6 90.0
💡 Transform(id='r1RRcRsLEBXcjA', name='Postprocess Cell Ranger', version='2.0', type='pipeline', updated_at=2023-09-27 19:02:57, created_by_id='bKeW4T6E')
💡 Run(id='kSY5Y5vb5yIfRp5jkK22', run_at=2023-09-27 19:02:57, transform_id='r1RRcRsLEBXcjA', created_by_id='bKeW4T6E')

Inspect data flow:

output_files[0].view_flow()
https://d33wubrfki0l68.cloudfront.net/347e7c6305bed582a4e05fd01d9ce6207173bcad/1ef73/_images/51965ea0829233007562c4db6e2e56903f67533895defca583502e9ba4858555.svg

Integrate scRNA-seq & phenotypic data #

Integrate data in a notebook:

transform = ln.Transform(
    name="Perform single cell analysis, integrate with CRISPRa screen",
    type="notebook",
)
ln.track(transform)

file_ps = ln.File.filter(description__icontains="perturbseq").one()
adata = file_ps.load()
file_hits = ln.File.filter(description="hits from schmidt22 crispra GWS").one()
screen_hits = file_hits.load()

import scanpy as sc

sc.tl.score_genes(adata, adata.var_names.intersection(screen_hits.index).tolist())
filesuffix = "_fig1_score-wgs-hits.png"
sc.pl.umap(adata, color="score", show=False, save=filesuffix)
filepath = f"figures/umap{filesuffix}"
file = ln.File(filepath, key=filepath)
file.save()
filesuffix = "fig2_score-wgs-hits-per-cluster.png"
sc.pl.matrixplot(
    adata, groupby="cluster_name", var_names=["score"], show=False, save=filesuffix
)
filepath = f"figures/matrixplot_{filesuffix}"
file = ln.File(filepath, key=filepath)
file.save()
Hide code cell output
💡 Transform(id='RVcqo41p7tTWjE', name='Perform single cell analysis, integrate with CRISPRa screen', type='notebook', updated_at=2023-09-27 19:02:58, created_by_id='bKeW4T6E')
💡 Run(id='jhvRsZjEFYmvGig40uH4', run_at=2023-09-27 19:02:58, transform_id='RVcqo41p7tTWjE', created_by_id='bKeW4T6E')
WARNING: saving figure to file figures/umap_fig1_score-wgs-hits.png
WARNING: saving figure to file figures/matrixplot_fig2_score-wgs-hits-per-cluster.png

Review results#

Let’s load one of the plots:

ln.track()
file = ln.File.filter(key__contains="figures/matrixplot").one()
file.stage()
Hide code cell output
💡 notebook imports: ipython==8.15.0 lamindb==0.54.2 scanpy==1.9.5
💡 Transform(id='1LCd8kco9lZUz8', name='Project flow', short_name='project-flow', version='0', type=notebook, updated_at=2023-09-27 19:03:00, created_by_id='bKeW4T6E')
💡 Run(id='LporIYJ0FMfTd9eq47sn', run_at=2023-09-27 19:03:00, transform_id='1LCd8kco9lZUz8', created_by_id='bKeW4T6E')
PosixUPath('/home/runner/work/lamin-usecases/lamin-usecases/docs/mydata/figures/matrixplot_fig2_score-wgs-hits-per-cluster.png')
display(Image(filename=file.path))
https://d33wubrfki0l68.cloudfront.net/b3b5eb3f53a7759762d1dca2d67bd76974729731/e5dd6/_images/f096e9d4768812e880e81babbd6eeae4f64efc120154dc379ad9c346ea2ebe9d.png

We see that the image file is tracked as an input of the current notebook. The input is highlighted, the notebook follows at the bottom:

file.view_flow()
https://d33wubrfki0l68.cloudfront.net/5cff9b2972faa33ac36763f9324cddd0e65a8447/49303/_images/23643a3c86343cc1ef76f5f417f4ddb8a6045e0f4f764749930c34d585d16105.svg

Alternatively, we can also look at the sequence of transforms:

transform = ln.Transform.search("Bird's eye view", return_queryset=True).first()
transform.parents.df()
name short_name version type reference reference_type initial_version_id updated_at created_by_id
id
VOzpXfg2Ly1UAx GWS CRIPSRa analysis None None notebook None None None 2023-09-27 19:02:55 bKeW4T6E
r1RRcRsLEBXcjA Postprocess Cell Ranger None 2.0 pipeline None None None 2023-09-27 19:02:58 bKeW4T6E
transform.view_parents()
https://d33wubrfki0l68.cloudfront.net/c6b1a8ea905d8126b2a251ba81fb2b92fc292756/cab6f/_images/43ba7f2ba3412af4971431b080e32a08cbd14c6169a81cc42dae740b8dce5fb5.svg

Understand runs#

We tracked pipeline and notebook runs through run_context, which stores a Transform and a Run record as a global context.

File objects are the inputs and outputs of runs.

What if I don’t want a global context?

Sometimes, we don’t want to create a global run context but manually pass a run when creating a file:

run = ln.Run(transform=transform)
ln.File(filepath, run=run)
When does a file appear as a run input?

When accessing a file via stage(), load() or backed(), two things happen:

  1. The current run gets added to file.input_of

  2. The transform of that file gets added as a parent of the current transform

You can then switch off auto-tracking of run inputs if you set ln.settings.track_run_inputs = False: Can I disable tracking run inputs?

You can also track run inputs on a case by case basis via is_run_input=True, e.g., here:

file.load(is_run_input=True)

Query by provenance#

We can query or search for the notebook that created the file:

transform = ln.Transform.search("GWS CRIPSRa analysis", return_queryset=True).first()

And then find all the files created by that notebook:

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
Elu0g9vef5cla9jK9Z4d Uw2znSl2 None .parquet DataFrame hits from schmidt22 crispra GWS None 18368 O2Owo0_QlM9JBS2zAZD4Lw md5 VOzpXfg2Ly1UAx 8yVjjb5ZpggZ2HiZPXZU None 2023-09-27 19:02:55 bKeW4T6E

Which transform ingested a given file?

file = ln.File.filter().first()
file.transform
Transform(id='OrYrfe6KoWNH6m', name='Upload GWS CRISPRa result', type='app', updated_at=2023-09-27 19:02:54, created_by_id='DzTjkKse')

And which user?

file.created_by
User(id='DzTjkKse', handle='testuser1', email='testuser1@lamin.ai', name='Test User1', updated_at=2023-09-27 19:02:56)

Which transforms were created by a given user?

users = ln.User.lookup()
ln.Transform.filter(created_by=users.testuser2).df()
name short_name version type reference reference_type initial_version_id updated_at created_by_id
id
VOzpXfg2Ly1UAx GWS CRIPSRa analysis None None notebook None None None 2023-09-27 19:02:55 bKeW4T6E
Sn8ZgVYvAaTqh6 Cell Ranger None 7.2.0 pipeline None None None 2023-09-27 19:02:57 bKeW4T6E
r1RRcRsLEBXcjA Postprocess Cell Ranger None 2.0 pipeline None None None 2023-09-27 19:02:58 bKeW4T6E
RVcqo41p7tTWjE Perform single cell analysis, integrate with C... None None notebook None None None 2023-09-27 19:03:00 bKeW4T6E
1LCd8kco9lZUz8 Project flow project-flow 0 notebook None None None 2023-09-27 19:03:00 bKeW4T6E

Which notebooks were created by a given user?

ln.Transform.filter(created_by=users.testuser2, type="notebook").df()
name short_name version type reference reference_type initial_version_id updated_at created_by_id
id
VOzpXfg2Ly1UAx GWS CRIPSRa analysis None None notebook None None None 2023-09-27 19:02:55 bKeW4T6E
RVcqo41p7tTWjE Perform single cell analysis, integrate with C... None None notebook None None None 2023-09-27 19:03:00 bKeW4T6E
1LCd8kco9lZUz8 Project flow project-flow 0 notebook None None None 2023-09-27 19:03:00 bKeW4T6E

We can also view all recent additions to the entire database:

ln.view()
Hide code cell output
File
storage_id key suffix accessor description version size hash hash_type transform_id run_id initial_version_id updated_at created_by_id
id
xKAc8AgBmNu4c1NUKNjy Uw2znSl2 figures/matrixplot_fig2_score-wgs-hits-per-clu... .png None None None 28814 H0Pxpa-fZOvigo74eXHZsQ md5 RVcqo41p7tTWjE jhvRsZjEFYmvGig40uH4 None 2023-09-27 19:03:00 bKeW4T6E
8Mkf8am8KZ5Y46xgz7nv Uw2znSl2 figures/umap_fig1_score-wgs-hits.png .png None None None 118999 1-WtAvRL1d_SSjZvMMOMkg md5 RVcqo41p7tTWjE jhvRsZjEFYmvGig40uH4 None 2023-09-27 19:02:59 bKeW4T6E
NtK9QssbubarYHa5mh2b Uw2znSl2 schmidt22_perturbseq.h5ad .h5ad AnnData perturbseq counts None 20659936 la7EvqEUMDlug9-rpw-udA md5 r1RRcRsLEBXcjA kSY5Y5vb5yIfRp5jkK22 None 2023-09-27 19:02:58 bKeW4T6E
wtvFlb2B6FhHi9amEKiB Uw2znSl2 perturbseq/filtered_feature_bc_matrix/features... .tsv.gz None None None 6 6IakdZfC6nF6SAucoiA9nA md5 Sn8ZgVYvAaTqh6 Z7yK4xernE3IvTYCe3m3 None 2023-09-27 19:02:57 bKeW4T6E
xG94gTsR9W1fu1bXvLlf Uw2znSl2 perturbseq/filtered_feature_bc_matrix/barcodes... .tsv.gz None None None 6 m4YNkndNF5ljmH8H93-Ggg md5 Sn8ZgVYvAaTqh6 Z7yK4xernE3IvTYCe3m3 None 2023-09-27 19:02:57 bKeW4T6E
OFs7asNSWDrC0bY9xQpM Uw2znSl2 perturbseq/filtered_feature_bc_matrix/matrix.m... .mtx.gz None None None 6 lxpYuK6cvHatHm8DzH6yAQ md5 Sn8ZgVYvAaTqh6 Z7yK4xernE3IvTYCe3m3 None 2023-09-27 19:02:57 bKeW4T6E
b0JnQyypQoHUwRhMmVwx Uw2znSl2 fastq/perturbseq_R2_001.fastq.gz .fastq.gz None None None 6 VowOx7WD8_KjE5ZapfqsNw md5 4NLGotZj2DoWd4 QYrR4v25aVvAFzmsozU4 None 2023-09-27 19:02:56 DzTjkKse
Run
transform_id run_at created_by_id reference reference_type
id
M5V5GVuLhUZ2nDxFayJR OrYrfe6KoWNH6m 2023-09-27 19:02:54 DzTjkKse None None
8yVjjb5ZpggZ2HiZPXZU VOzpXfg2Ly1UAx 2023-09-27 19:02:55 bKeW4T6E None None
QYrR4v25aVvAFzmsozU4 4NLGotZj2DoWd4 2023-09-27 19:02:56 DzTjkKse None None
Z7yK4xernE3IvTYCe3m3 Sn8ZgVYvAaTqh6 2023-09-27 19:02:57 bKeW4T6E None None
kSY5Y5vb5yIfRp5jkK22 r1RRcRsLEBXcjA 2023-09-27 19:02:57 bKeW4T6E None None
jhvRsZjEFYmvGig40uH4 RVcqo41p7tTWjE 2023-09-27 19:02:58 bKeW4T6E None None
LporIYJ0FMfTd9eq47sn 1LCd8kco9lZUz8 2023-09-27 19:03:00 bKeW4T6E None None
Storage
root type region updated_at created_by_id
id
Uw2znSl2 /home/runner/work/lamin-usecases/lamin-usecase... local None 2023-09-27 19:02:52 DzTjkKse
Transform
name short_name version type reference reference_type initial_version_id updated_at created_by_id
id
1LCd8kco9lZUz8 Project flow project-flow 0 notebook None None None 2023-09-27 19:03:00 bKeW4T6E
RVcqo41p7tTWjE Perform single cell analysis, integrate with C... None None notebook None None None 2023-09-27 19:03:00 bKeW4T6E
r1RRcRsLEBXcjA Postprocess Cell Ranger None 2.0 pipeline None None None 2023-09-27 19:02:58 bKeW4T6E
Sn8ZgVYvAaTqh6 Cell Ranger None 7.2.0 pipeline None None None 2023-09-27 19:02:57 bKeW4T6E
4NLGotZj2DoWd4 Chromium 10x upload None None pipeline None None None 2023-09-27 19:02:56 DzTjkKse
VOzpXfg2Ly1UAx GWS CRIPSRa analysis None None notebook None None None 2023-09-27 19:02:55 bKeW4T6E
OrYrfe6KoWNH6m Upload GWS CRISPRa result None None app None None None 2023-09-27 19:02:54 DzTjkKse
User
handle email name updated_at
id
bKeW4T6E testuser2 testuser2@lamin.ai Test User2 2023-09-27 19:02:57
DzTjkKse testuser1 testuser1@lamin.ai Test User1 2023-09-27 19:02:56
Hide code cell content
!lamin login testuser1
!lamin delete --force mydata
!rm -r ./mydata
✅ logged in with email testuser1@lamin.ai and id DzTjkKse
💡 deleting instance testuser1/mydata
✅     deleted instance settings file: /home/runner/.lamin/instance--testuser1--mydata.env
✅     instance cache deleted
✅     deleted '.lndb' sqlite file
❗     consider manually deleting your stored data: /home/runner/work/lamin-usecases/lamin-usecases/docs/mydata