Colab Notebook Starter
Pick an analysis and download a commented starter notebook.
Runs in your browser. Nothing you enter is uploaded.
Options
19 cells, ready to run
- textSingle-cell RNA-seq: QC to clusters A complete first-pass analysis with every threshold visible and justified. Before you run this 1. Open in Google Colab and choose a GPU runtime if the notebook asks for one. 2. Replace every `# EDIT:` line with your own paths and parameters. 3. Run cells in order. Each section ends with a check that tells you whether the step worked. Generated by VeriHelix (verihelix.nexagenlabs.com). Background and interpretation: *AI for Biotech* (Tripathy, 2026), Chapter 12.
- In [1]
!pip -q install scanpy==1.10.* leidenalg scrublet celltypist import scanpy as sc, numpy as np, pandas as pd, matplotlib.pyplot as plt sc.settings.verbosity = 1 sc.set_figure_params(dpi=100, facecolor="white") print("scanpy", sc.__version__) - text1. Load the data Edit `DATA_DIR` to point at your own data.
- In [3]
DATA_DIR = "/content/filtered_feature_bc_matrix" # EDIT: your path adata = sc.read_10x_mtx(DATA_DIR, var_names="gene_symbols", cache=True) adata.var_names_make_unique() print(adata) assert adata.n_obs > 0 and adata.n_vars > 0, "Nothing loaded: check DATA_DIR"
- text2. Quality control Starting thresholds for pbmc: at least 200 genes per cell, at most 5000, and under 10% mitochondrial reads. These are defaults, not rules. Look at the distributions below and move them to fit your data.
- In [5]
adata.var["mt"] = adata.var_names.str.startswith(("MT-", "mt-")) sc.pp.calculate_qc_metrics(adata, qc_vars=["mt"], inplace=True, log1p=False) sc.pl.violin(adata, ["n_genes_by_counts", "total_counts", "pct_counts_mt"], jitter=0.4, multi_panel=True) MIN_GENES, MAX_GENES, MAX_MT = 200, 5000, 10 # EDIT after looking at the plots before = adata.n_obs adata = adata[(adata.obs.n_genes_by_counts >= MIN_GENES) & (adata.obs.n_genes_by_counts <= MAX_GENES) & (adata.obs.pct_counts_mt < MAX_MT)].copy() sc.pp.filter_genes(adata, min_cells=3) print(f"Kept {adata.n_obs} of {before} cells ({adata.n_obs/before:.1%})") assert adata.n_obs > 100, "Fewer than 100 cells left: your thresholds are probably too strict" - text3. Doublets Two cells captured in one droplet look like a hybrid cell type and create fake "intermediate" clusters.
- In [7]
sc.pp.scrublet(adata) print(adata.obs.predicted_doublet.value_counts()) adata = adata[~adata.obs.predicted_doublet].copy() print(f"{adata.n_obs} cells after removing doublets") - text4. Normalise and find variable genes The raw counts are kept in `adata.layers["counts"]` because differential expression needs them later.
- In [9]
adata.layers["counts"] = adata.X.copy() sc.pp.normalize_total(adata, target_sum=1e4) sc.pp.log1p(adata) adata.raw = adata sc.pp.highly_variable_genes(adata, n_top_genes=2000, batch_key=None) sc.pl.highly_variable_genes(adata) adata = adata[:, adata.var.highly_variable].copy() sc.pp.scale(adata, max_value=10)
- text5. Dimensionality reduction Choose the number of PCs from the elbow in the variance plot.
- In [11]
sc.tl.pca(adata, svd_solver="arpack", n_comps=50) sc.pl.pca_variance_ratio(adata, n_pcs=50, log=True) N_PCS = 30 # EDIT: read the elbow off the plot above sc.pp.neighbors(adata, n_neighbors=15, n_pcs=N_PCS) sc.tl.umap(adata)
- text6. Clustering Resolution controls how many clusters you get. There is no correct value: try several and keep the one whose clusters correspond to markers you can defend.
- In [13]
for res in [0.3, 0.5, 1.0]: sc.tl.leiden(adata, resolution=res, key_added=f"leiden_{res}", flavor="igraph", n_iterations=2) print(res, adata.obs[f"leiden_{res}"].nunique(), "clusters") sc.pl.umap(adata, color=["leiden_0.3", "leiden_0.5", "leiden_1.0"], wspace=0.3) adata.obs["leiden"] = adata.obs["leiden_0.5"] # EDIT: pick the resolution you trust - In [14]
sc.tl.rank_genes_groups(adata, "leiden", method="wilcoxon", layer="counts", use_raw=False) sc.pl.rank_genes_groups_dotplot(adata, n_genes=5) pd.DataFrame(adata.uns["rank_genes_groups"]["names"]).head(10)
- text7. Automatic annotation CellTypist gives a first pass. Always check its labels against the marker genes above: automatic annotation is a hypothesis.
- In [16]
import celltypist from celltypist import models models.download_models(model=["Immune_All_Low.pkl"]) # EDIT: pick a model for your tissue pred = celltypist.annotate(adata.raw.to_adata(), model="Immune_All_Low.pkl", majority_voting=True) adata.obs["celltypist"] = pred.predicted_labels.majority_voting.values sc.pl.umap(adata, color=["celltypist"], legend_loc="on data", legend_fontsize=7)
- textSave and record what you did Writing versions and thresholds into the file is what makes the analysis reproducible six months from now.
- In [18]
adata.uns["analysis_params"] = dict(min_genes=MIN_GENES, max_genes=MAX_GENES, max_mt=MAX_MT, n_pcs=N_PCS, scanpy=sc.__version__) adata.write("analysis_result.h5ad") print("Saved. Cells:", adata.n_obs, "| Clusters:", adata.obs.leiden.nunique())
# EDIT.Go deeper: The full workflow this notebook sketches: single-cell omics and foundation models — AI for Biotech, Ch. 12 (Tripathy, 2026) Chapter 12: Single-Cell Omics and Foundation Models
These are starting points with the common mistakes designed out, not finished pipelines. Library APIs change; if a cell errors, check that package’s current documentation. AI for Biotech explains what each step does and how to judge the output.
What makes these different from a tutorial
Each notebook has the decisions written into it: why a threshold is set where it is, what to look at before changing it, and a check after each step that tells you whether it worked. The mistakes that most often spoil these analyses are designed out — fitting preprocessing before splitting, normalising counts twice, using the wrong background for enrichment, trusting a cluster resolution because it was the default.
Every line you need to change is marked # EDIT.