Skip to content

Data Loading API

Load 10x Genomics Cell Ranger VDJ and GEX directories into paired AnnData objects.

loader

Data loading functions for TCRsift.

Handles loading CellRanger VDJ and GEX outputs into unified data structures.

load_cellranger_vdj

load_cellranger_vdj(vdj_dir: str | Path, sample_name: str, annotations_filename: str = 'filtered_contig_annotations.csv', clonotypes_filename: str = 'clonotypes.csv', verbose: bool = True) -> pd.DataFrame

Load CellRanger VDJ output files.

Parameters:

Name Type Description Default
vdj_dir str or Path

Path to CellRanger VDJ output directory (or contig CSV/.csv.gz file)

required
sample_name str

Name to assign to this sample

required
annotations_filename str

Name of the contig annotations CSV file

'filtered_contig_annotations.csv'
clonotypes_filename str

Name of the clonotypes CSV file

'clonotypes.csv'
verbose bool

Print progress information

True

Returns:

Type Description
DataFrame

DataFrame with VDJ annotations for all cells

Source code in tcrsift/loader.py
def load_cellranger_vdj(
    vdj_dir: str | Path,
    sample_name: str,
    annotations_filename: str = "filtered_contig_annotations.csv",
    clonotypes_filename: str = "clonotypes.csv",
    verbose: bool = True,
) -> pd.DataFrame:
    """
    Load CellRanger VDJ output files.

    Parameters
    ----------
    vdj_dir : str or Path
        Path to CellRanger VDJ output directory (or contig CSV/.csv.gz file)
    sample_name : str
        Name to assign to this sample
    annotations_filename : str
        Name of the contig annotations CSV file
    clonotypes_filename : str
        Name of the clonotypes CSV file
    verbose : bool
        Print progress information

    Returns
    -------
    pd.DataFrame
        DataFrame with VDJ annotations for all cells
    """
    vdj_dir = Path(vdj_dir)

    try:
        annotations_path, unfiltered = resolve_cellranger_vdj_file(
            vdj_dir, annotations_filename=annotations_filename
        )
    except TCRsiftValidationError as exc:
        if "Multiple " in str(exc):
            raise
        raise TCRsiftValidationError(
            f"Invalid CellRanger VDJ directory for sample '{sample_name}': {vdj_dir}",
            hint="Make sure this is the 'outs' directory from 'cellranger vdj'. "
            "It should contain 'filtered_contig_annotations.csv(.gz)' or 'all_contig_annotations.csv(.gz)'.",
        ) from exc

    vdj_base_dir = annotations_path.parent
    sample_prefix = _sample_prefix_from_contig_file(annotations_path) if vdj_dir.is_file() else ""
    clonotypes_path = _resolve_clonotypes_path(
        vdj_base_dir, clonotypes_filename, sample_prefix=sample_prefix
    )
    if unfiltered and verbose:
        logger.info(f"  Using unfiltered contig table {annotations_path.name}")

    logger.info(f"Loading VDJ annotations from {annotations_path}")
    df = pd.read_csv(annotations_path, low_memory=False)

    # Validate the VDJ data
    if len(df) == 0:
        raise TCRsiftValidationError(
            f"VDJ annotations file is empty: {annotations_path}",
            hint="Check that CellRanger VDJ ran successfully. "
            "The file should contain contig annotations.",
        )

    required_cols = ["barcode", "chain"]
    missing_cols = [c for c in required_cols if c not in df.columns]
    if missing_cols:
        raise TCRsiftValidationError(
            f"VDJ annotations missing required columns: {missing_cols}",
            hint=f"Available columns: {list(df.columns)[:15]}. "
            "This doesn't look like a CellRanger VDJ output file.",
        )

    # all_contig_annotations.csv also lists contigs from barcodes that were not
    # called as cells and low-confidence contigs. Apply Cell Ranger's own
    # definition of the filtered table so the fallback means the same thing.
    from ._schema import clean_cdr3_series, truthy_flag_series

    if unfiltered:
        keep = pd.Series(True, index=df.index)
        for flag in ("is_cell", "high_confidence"):
            if flag in df.columns:
                keep &= truthy_flag_series(df[flag])
        if verbose and not keep.all():
            logger.info(f"  Kept {int(keep.sum()):,} of {len(df):,} contigs (called cells, high confidence)")
        df = df[keep]
    # A contig with no chain call (read as missing) cannot be assigned to a
    # receptor chain. Left in, it turned the per-chain entry numbers into
    # floats and produced columns such as ``TRA_1.0_cdr3``.
    no_chain = clean_cdr3_series(df, "chain") == ""
    if no_chain.any():
        if verbose:
            logger.info(f"  Dropped {int(no_chain.sum()):,} contigs with no chain call")
        df = df[~no_chain]
    # Cell Ranger's filtered table still lists non-productive contigs (frame
    # shifts, stop codons, no CDR3). Keep explicitly productive contigs, plus
    # 5'-partial contigs where Cell Ranger left ``productive="None"`` because
    # 5' coverage stopped downstream of the leader ATG despite a complete
    # V+J call and a stop-free canonical IMGT C...[FW] CDR3 (common in Cell
    # Ranger 2/3 and targeted TCR libraries such as GSE171037).
    if "productive" in df.columns:
        prod_str = df["productive"].astype(str).str.strip().str.lower()
        productive = truthy_flag_series(df["productive"])
        undetermined = df["productive"].isna() | prod_str.isin({"none", "", "nan", "<na>"})
        if undetermined.any() and {"v_gene", "j_gene", "cdr3"} <= set(df.columns):
            v_ok = clean_cdr3_series(df, "v_gene") != ""
            j_ok = clean_cdr3_series(df, "j_gene") != ""
            cdr3_ok = (
                df["cdr3"]
                .astype("string")
                .str.strip()
                .str.fullmatch(r"C[ACDEFGHIKLMNPQRSTVWY]{3,28}[FW]", na=False)
                .to_numpy(dtype=bool)
            )
            cell_ok = pd.Series(True, index=df.index)
            for flag in ("is_cell", "high_confidence"):
                if flag in df.columns:
                    cell_ok &= truthy_flag_series(df[flag])
            partial_rescue = undetermined & v_ok & j_ok & cdr3_ok & cell_ok
        else:
            partial_rescue = pd.Series(False, index=df.index)
        keep_prod = productive | partial_rescue
        if verbose and not keep_prod.all():
            logger.info(f"  Dropped {int((~keep_prod).sum()):,} non-productive contigs")
        if verbose and partial_rescue.any():
            logger.info(
                f"  Retained {int(partial_rescue.sum()):,} 5'-partial contigs "
                "(productive=None) with complete V+J genes and in-frame CDR3"
            )
        df = df[keep_prod].copy()
        df["_is_full_productive"] = productive.loc[df.index].to_numpy(dtype=bool)
    else:
        df = df.copy()
        df["_is_full_productive"] = True
    if len(df) == 0:
        raise TCRsiftValidationError(
            f"No usable contigs in: {annotations_path}",
            hint="Every contig was from a non-cell barcode, low confidence, non-productive, "
            "or had no chain call.",
        )

    # Log summary statistics
    n_contigs = len(df)
    n_cells = df["barcode"].nunique()
    if "productive" in df.columns:
        n_productive = "all productive"
    else:
        n_productive = "productivity not reported"
    if verbose:
        logger.info(
            f"  Loaded {n_contigs:,} contigs from {n_cells:,} cells ({n_productive})"
        )

    # Validate chain types
    valid_chains = {"TRA", "TRB", "TRD", "TRG", "IGH", "IGK", "IGL", "Multi"}
    invalid_chains = set(df["chain"].unique()) - valid_chains
    if invalid_chains:
        logger.warning(f"  Unexpected chain types found: {invalid_chains}")

    # Add sample information
    df["sample"] = sample_name
    df["vdj_dir"] = str(vdj_base_dir)

    # Load clonotypes if available
    if clonotypes_path is not None and clonotypes_path.exists():
        logger.info(f"Loading clonotypes from {clonotypes_path}")
        df_clonotypes = pd.read_csv(clonotypes_path)
        # Clonotypes contain MAIT/NKT evidence
        if (
            "clonotype_id" in df_clonotypes.columns
            and "raw_clonotype_id" in df.columns
            and (
                "mait_evidence" in df_clonotypes.columns
                or "inkt_evidence" in df_clonotypes.columns
            )
        ):
            # Merge clonotype info
            clonotype_cols = ["clonotype_id"]
            if "mait_evidence" in df_clonotypes.columns:
                clonotype_cols.append("mait_evidence")
            if "inkt_evidence" in df_clonotypes.columns:
                clonotype_cols.append("inkt_evidence")
            df_clonotypes_subset = df_clonotypes[clonotype_cols].copy()
            df_clonotypes_subset = df_clonotypes_subset.rename(
                columns={"clonotype_id": "raw_clonotype_id"}
            )
            # Drop duplicate clonotype ids before the left merge: a malformed /
            # concatenated clonotypes.csv with a repeated id would otherwise
            # fan-out (duplicate) every contig row for that clonotype.
            df_clonotypes_subset = df_clonotypes_subset.drop_duplicates(
                "raw_clonotype_id"
            )
            df = df.merge(df_clonotypes_subset, on="raw_clonotype_id", how="left")

    # Combine VDJ segments into full sequence ONLY on rows where all 7 segments
    # (fwr1..fwr4) are present and stop-free; 5'-partial contigs leave vdj_aa as None
    # rather than emitting an N-terminally truncated fragment.
    if all(col in df.columns for col in VDJ_SEGMENT_COLS):
        seg_complete = pd.Series(True, index=df.index)
        for col in VDJ_SEGMENT_COLS:
            s_val = df[col].astype("string").str.strip()
            seg_complete &= (
                df[col].notna()
                & ~s_val.str.lower().isin({"", "none", "nan", "<na>"})
                & ~s_val.str.contains(r"\*", regex=True, na=True)
            )
        joined_aa = df[VDJ_SEGMENT_COLS].fillna("").agg("".join, axis=1)
        df["vdj_aa"] = joined_aa.where(seg_complete, other=None)
        has_nt_segs = all(col in df.columns for col in VDJ_SEGMENT_NT_COLS)
        if has_nt_segs:
            nt_complete = seg_complete.copy()
            for col in VDJ_SEGMENT_NT_COLS:
                s_nt = df[col].astype("string").str.strip()
                nt_complete &= df[col].notna() & ~s_nt.str.lower().isin({"", "none", "nan", "<na>"})
            joined_nt = df[VDJ_SEGMENT_NT_COLS].fillna("").agg("".join, axis=1)
            df["vdj_nt"] = joined_nt.where(nt_complete, other=None)

        # Retain contiguous stop-free 3' segment suffixes (at least cdr3+fwr4) on
        # 5'-partial contigs so opt-in --germline-vdj-fallback can rescue only the
        # missing N-terminal framework prefix while keeping sequenced donor codons.
        if (~seg_complete).any():
            partial_aa_vals: list[str | None] = [None] * len(df)
            partial_nt_vals: list[str | None] = [None] * len(df)
            has_any_partial = False
            for row_pos, idx in enumerate(df.index):
                if bool(seg_complete.loc[idx]):
                    continue
                row_s = df.loc[idx]
                suffix_cols: list[str] = []
                for col in reversed(VDJ_SEGMENT_COLS):
                    val = row_s[col]
                    sval = str(val).strip() if pd.notna(val) else ""
                    if not sval or sval.lower() in {"none", "nan", "<na>"} or "*" in sval:
                        break
                    suffix_cols.append(col)
                if len(suffix_cols) >= 2:  # at least cdr3 + fwr4
                    suffix_cols.reverse()
                    p_aa = "".join(str(row_s[c]).strip() for c in suffix_cols)
                    partial_aa_vals[row_pos] = p_aa
                    has_any_partial = True
                    if has_nt_segs:
                        nt_cols = [c + "_nt" for c in suffix_cols]
                        nt_parts = [
                            str(row_s[c]).strip() if pd.notna(row_s[c]) else ""
                            for c in nt_cols
                        ]
                        if all(p and p.lower() not in {"none", "nan", "<na>"} for p in nt_parts):
                            partial_nt_vals[row_pos] = "".join(nt_parts).lower()
            if has_any_partial:
                df["partial_vdj_aa"] = partial_aa_vals
                if any(v is not None for v in partial_nt_vals):
                    df["partial_vdj_nt"] = partial_nt_vals
    if "vdj_aa" not in df.columns:
        df = _enrich_vdj_from_json(
            df, vdj_base_dir, sample_prefix=sample_prefix, verbose=verbose
        )

    return df

load_cellranger_gex

load_cellranger_gex(gex_dir: str | Path, sample_name: str, min_genes: int = 250, max_genes: int = 15000, min_counts: int = 500, max_counts: int = 100000, max_mito_pct: float = 8.0, min_mito_pct: float = 2.0, verbose: bool = True) -> ad.AnnData

Load CellRanger gene expression output.

Parameters:

Name Type Description Default
gex_dir str or Path

Path to CellRanger count output directory

required
sample_name str

Name to assign to this sample

required
min_genes int

Minimum genes detected per cell

250
max_genes int

Maximum genes detected per cell

15000
min_counts int

Minimum UMI counts per cell

500
max_counts int

Maximum UMI counts per cell

100000
max_mito_pct float

Maximum mitochondrial percentage

8.0
min_mito_pct float

Minimum mitochondrial percentage (a FLOOR: cells below this are dropped, targeting near-zero-mito empty/ambient droplets). Default 2.0; the drop is logged as a warning so it isn't silent (#168). Set 0 to disable the floor.

2.0
verbose bool

Print progress information

True

Returns:

Type Description
AnnData

AnnData object with gene expression data

Source code in tcrsift/loader.py
def load_cellranger_gex(
    gex_dir: str | Path,
    sample_name: str,
    min_genes: int = 250,
    max_genes: int = 15000,
    min_counts: int = 500,
    max_counts: int = 100000,
    max_mito_pct: float = 8.0,
    min_mito_pct: float = 2.0,
    verbose: bool = True,
) -> ad.AnnData:
    """
    Load CellRanger gene expression output.

    Parameters
    ----------
    gex_dir : str or Path
        Path to CellRanger count output directory
    sample_name : str
        Name to assign to this sample
    min_genes : int
        Minimum genes detected per cell
    max_genes : int
        Maximum genes detected per cell
    min_counts : int
        Minimum UMI counts per cell
    max_counts : int
        Maximum UMI counts per cell
    max_mito_pct : float
        Maximum mitochondrial percentage
    min_mito_pct : float
        Minimum mitochondrial percentage (a FLOOR: cells below this are
        dropped, targeting near-zero-mito empty/ambient droplets). Default 2.0;
        the drop is logged as a warning so it isn't silent (#168). Set 0 to
        disable the floor.
    verbose : bool
        Print progress information

    Returns
    -------
    ad.AnnData
        AnnData object with gene expression data
    """
    # Validate numeric parameters
    validate_numeric_param(min_genes, "min_genes", min_value=0)
    validate_numeric_param(max_genes, "max_genes", min_value=1)
    validate_numeric_param(min_counts, "min_counts", min_value=0)
    validate_numeric_param(max_counts, "max_counts", min_value=1)
    validate_numeric_param(min_mito_pct, "min_mito_pct", min_value=0, max_value=100)
    validate_numeric_param(max_mito_pct, "max_mito_pct", min_value=0, max_value=100)

    if min_genes > max_genes:
        raise TCRsiftValidationError(
            f"min_genes ({min_genes}) cannot be greater than max_genes ({max_genes})",
            hint="Check your QC filter parameters.",
        )
    if min_counts > max_counts:
        raise TCRsiftValidationError(
            f"min_counts ({min_counts}) cannot be greater than max_counts ({max_counts})",
            hint="Check your QC filter parameters.",
        )
    if min_mito_pct > max_mito_pct:
        raise TCRsiftValidationError(
            f"min_mito_pct ({min_mito_pct}) cannot be greater than max_mito_pct ({max_mito_pct})",
            hint="Check your QC filter parameters.",
        )

    gex_dir = Path(gex_dir)

    try:
        source_kind, resolved_source = resolve_cellranger_gex_source(gex_dir)
    except TCRsiftValidationError as exc:
        if "Multiple " in str(exc):
            raise
        raise TCRsiftValidationError(
            f"Invalid CellRanger GEX directory for sample '{sample_name}': {gex_dir}",
            hint="Make sure this is the 'outs' directory from 'cellranger count' (or a 10x .h5 file). "
            "It should contain 'filtered_feature_bc_matrix' or 'filtered_feature_bc_matrix.h5'.",
        ) from exc

    if source_kind == "h5":
        logger.info(f"Loading GEX from h5 file: {resolved_source}")
        with warnings.catch_warnings():
            warnings.filterwarnings(
                "ignore",
                message="Variable names are not unique",
                category=UserWarning,
            )
            adata = sc.read_10x_h5(str(resolved_source))
        # Index genes by Ensembl id, as the matrix-directory reader does.
        # Symbols repeat in 10x references, and a repeated name makes
        # every name-based gene lookup fail; an h5 sample combined with a
        # matrix-directory sample also needs the same gene axis.
        if "gene_ids" in adata.var.columns:
            adata.var["gene_symbols"] = adata.var_names.astype(str)
            adata.var_names = adata.var["gene_ids"].astype(str)
            adata.var = adata.var.drop(columns="gene_ids")
            adata.var.index.name = None
    else:
        logger.info(f"Loading GEX from matrix directory: {resolved_source}")
        adata = sc.read_10x_mtx(str(resolved_source), var_names="gene_ids")

    # Validate loaded data
    if adata.n_obs == 0:
        raise TCRsiftValidationError(
            f"Gene expression matrix contains no cells: {gex_dir}",
            hint="Check that CellRanger count ran successfully.",
        )

    if verbose:
        logger.info(f"  Loaded {adata.n_obs:,} cells x {adata.n_vars:,} genes")

    # Add sample information
    adata.obs["sample"] = sample_name
    adata.obs["gex_dir"] = str(resolved_source.parent if gex_dir.is_file() else gex_dir)

    # Calculate QC metrics - detect mitochondrial genes across human (MT-*, ENSG*)
    # and mouse (mt-*, ENSMUSG*) inputs via the shared symbol resolver.
    adata.var["mt"] = adata_mito_mask(adata)
    sc.pp.calculate_qc_metrics(adata, qc_vars=["mt"], percent_top=None, log1p=False, inplace=True)

    # Rename QC columns for consistency
    if "pct_counts_mt" in adata.obs.columns:
        adata.obs["percent_mt"] = adata.obs["pct_counts_mt"]
    if "n_genes_by_counts" in adata.obs.columns:
        adata.obs["n_genes"] = adata.obs["n_genes_by_counts"]
    if "total_counts" in adata.obs.columns:
        adata.obs["n_counts"] = adata.obs["total_counts"]

    # Add QC filter flags
    has_mt_genes = bool(adata.var["mt"].any())
    adata.obs["filter:min_genes"] = adata.obs["n_genes"] >= min_genes
    adata.obs["filter:max_genes"] = adata.obs["n_genes"] <= max_genes
    adata.obs["filter:min_counts"] = adata.obs["n_counts"] >= min_counts
    adata.obs["filter:max_counts"] = adata.obs["n_counts"] <= max_counts
    if has_mt_genes:
        adata.obs["filter:min_mito"] = adata.obs["percent_mt"] >= min_mito_pct
        adata.obs["filter:max_mito"] = adata.obs["percent_mt"] <= max_mito_pct
    else:
        adata.obs["filter:min_mito"] = True
        adata.obs["filter:max_mito"] = True
        if min_mito_pct > 0 or max_mito_pct < 100:
            logger.warning(
                "  No mitochondrial genes found in %s; skipping min_mito_pct/max_mito_pct QC gates.",
                sample_name,
            )

    # The min_mito_pct FLOOR discards *low*-mito cells (near-zero-mito
    # empty/ambient droplets). The default 2.0 floor trims a small tail on most
    # datasets — that's the intended behavior, so report it at info level rather
    # than warning on every run (#168). Only escalate to a warning when the floor
    # culls an unusually large fraction (>10%), which signals it's set too high.
    if has_mt_genes and min_mito_pct > 0:
        n_below_floor = int((~adata.obs["filter:min_mito"]).sum())
        if n_below_floor:
            frac = n_below_floor / adata.n_obs if adata.n_obs else 0.0
            msg = (
                f"  min_mito_pct={min_mito_pct} FLOOR dropped "
                f"{n_below_floor:,}/{adata.n_obs:,} ({frac * 100:.0f}%) low-mito cells"
            )
            if frac > 0.10:
                logger.warning(
                    msg + " — a >10% cull from the mito floor is unusual; "
                    "set min_mito_pct=0 to disable it."
                )
            elif verbose:
                logger.info(msg)

    adata.obs["filter:pass_qc"] = (
        adata.obs["filter:min_genes"]
        & adata.obs["filter:max_genes"]
        & adata.obs["filter:min_counts"]
        & adata.obs["filter:max_counts"]
        & adata.obs["filter:min_mito"]
        & adata.obs["filter:max_mito"]
    )

    # Apply the QC mask. Prior to issue #39 these flags were advisory only —
    # the column was computed but cells failing QC were not dropped, so the
    # user-facing --min-mito/--max-mito/--min-genes/etc. parameters were
    # silently no-ops.
    n_before = adata.n_obs
    adata = adata[adata.obs["filter:pass_qc"]].copy()
    if verbose:
        logger.info(
            f"  QC: {n_before:,} -> {adata.n_obs:,} cells pass "
            f"(dropped {n_before - adata.n_obs:,})"
        )
    # Catch "silently wrong" config mistakes (e.g. --min-mito 1.0 typo for
    # 10) where the filter eats most of the sample and downstream artifacts
    # just look weird rather than failing clearly (#41). 50% is a reasonable
    # line — peripheral blood routinely passes 90%+, tumor sits around 70%,
    # below half is almost always a config or dataset-mismatch issue.
    pass_rate = adata.n_obs / n_before if n_before else 0.0
    if pass_rate < 0.5:
        logger.warning(
            f"  QC dropped {(1 - pass_rate) * 100:.0f}% of cells "
            f"({n_before - adata.n_obs:,}/{n_before:,}) — "
            "check --min-genes/--max-genes/--min-counts/--max-counts/"
            "--min-mito/--max-mito if this is unexpected."
        )

    return adata

load_sample

load_sample(sample: Sample, min_genes: int = 250, max_genes: int = 15000, min_counts: int = 500, max_counts: int = 100000, max_mito_pct: float = 8.0, min_mito_pct: float = 2.0, species: str | None = None) -> ad.AnnData | None

Load all data for a single sample.

Parameters:

Name Type Description Default
sample Sample

Sample object with paths and metadata

required
min_genes int

QC filter parameters for GEX data

250
max_genes int

QC filter parameters for GEX data

250
min_counts int

QC filter parameters for GEX data

250
max_counts int

QC filter parameters for GEX data

250
max_mito_pct int

QC filter parameters for GEX data

250
min_mito_pct int

QC filter parameters for GEX data

250

Returns:

Type Description
AnnData or None

Combined AnnData with GEX and VDJ data, or None if sample has neither gex_dir nor vdj_dir.

Source code in tcrsift/loader.py
def load_sample(
    sample: Sample,
    min_genes: int = 250,
    max_genes: int = 15000,
    min_counts: int = 500,
    max_counts: int = 100000,
    max_mito_pct: float = 8.0,
    min_mito_pct: float = 2.0,
    species: str | None = None,
) -> ad.AnnData | None:
    """
    Load all data for a single sample.

    Parameters
    ----------
    sample : Sample
        Sample object with paths and metadata
    min_genes, max_genes, min_counts, max_counts, max_mito_pct, min_mito_pct
        QC filter parameters for GEX data

    Returns
    -------
    ad.AnnData or None
        Combined AnnData with GEX and VDJ data, or None if sample has neither
        gex_dir nor vdj_dir.
    """
    adata = None
    vdj_df = None

    # Load GEX if available (from gex_dir or standalone gex_path)
    gex_source = sample.gex_dir or getattr(sample, "gex_path", None)
    if gex_source:
        adata = load_cellranger_gex(
            gex_source,
            sample.sample,
            min_genes=min_genes,
            max_genes=max_genes,
            min_counts=min_counts,
            max_counts=max_counts,
            max_mito_pct=max_mito_pct,
            min_mito_pct=min_mito_pct,
        )

    # Load VDJ if available
    if sample.vdj_dir:
        vdj_df = load_cellranger_vdj(sample.vdj_dir, sample.sample)

    # Combine or create from VDJ only
    if adata is not None and vdj_df is not None:
        adata = combine_gex_and_vdj(adata, vdj_df)
    elif adata is not None and adata.n_vars > 0:
        marker_df = _extract_tcell_markers(adata)
        for col in marker_df.columns:
            adata.obs[col] = marker_df[col].values
        adata.obs["CD3"] = adata.obs["CD3D"] + adata.obs["CD3E"] + adata.obs["CD3G"]
        adata.obs["CD8"] = adata.obs["CD8A"] + adata.obs["CD8B"]
    elif vdj_df is not None:
        # Create minimal AnnData from VDJ data
        vdj_pivoted = _pivot_vdj_by_barcode(vdj_df)
        adata = ad.AnnData(obs=vdj_pivoted)
        adata.obs["sample"] = sample.sample

    # Add sample metadata. Skip fields the user didn't supply: an all-None
    # object column survives concat but breaks anndata.write_h5ad, and
    # downstream readers already guard with `if col in adata.obs.columns`.
    if adata is not None:
        metadata = [
            ("antigen_type", sample.antigen_type),
            ("antigen_description", sample.antigen_description),
            ("antigen_name", sample.antigen_name),
            ("antigen_sequence", sample.antigen_sequence),
            ("epitope_sequence", sample.epitope_sequence),
            ("mhc_allele", sample.mhc_allele),
            ("antigen_names", sample.antigen_names),
            ("antigen_sequences", sample.antigen_sequences),
            ("epitope_sequences", sample.epitope_sequences),
            ("source", sample.source),
            ("control_sample", getattr(sample, "control_sample", None)),
            ("patient_id", sample.patient_id),
            ("enrichment_method", sample.enrichment_method),
            ("timepoint", sample.timepoint),
            ("apc_type", sample.apc_type),
            ("tissue", sample.tissue),
            ("pre_sorted", getattr(sample, "pre_sorted", None)),
            ("expected_tcell_type", sample.get_expected_tcell_type()),
            ("species", getattr(sample, "species", None)),
        ]
        _add_obs_columns(
            adata, {col: val for col, val in metadata if val is not None}
        )
        effective_sp = (
            (species if species and species != "auto" else None)
            or getattr(sample, "species", None)
            or species
        )
        if effective_sp is not None:
            resolved_sp = resolve_species(
                effective_sp,
                adata=adata if adata.n_vars > 0 else None,
                df=vdj_df,
                context=f"sample '{sample.sample}'",
            )
            adata.uns["species"] = resolved_sp
            adata.obs["species"] = resolved_sp

    return adata

load_samples

load_samples(sample_sheet_path: str | Path | SampleSheet, min_genes: int = 250, max_genes: int = 15000, min_counts: int = 500, max_counts: int = 100000, max_mito_pct: float = 8.0, min_mito_pct: float = 2.0, verbose: bool = True, show_progress: bool = True, tmpdir: str | Path | None = None, species: str | None = None) -> ad.AnnData

Load all samples from a sample sheet into a single AnnData object.

Parameters:

Name Type Description Default
sample_sheet_path str or Path or SampleSheet

Path to sample sheet (CSV or YAML), or a SampleSheet instance.

required
min_genes int

QC filter parameters.

250
max_genes int

QC filter parameters.

250
min_counts int

QC filter parameters.

250
max_counts int

QC filter parameters.

250
max_mito_pct int

QC filter parameters.

250
min_mito_pct int

QC filter parameters.

250
verbose bool

Print detailed progress information.

True
show_progress bool

Show progress bar.

True
tmpdir str or Path or None

Parent directory for the spill tempdir (see Notes). Defaults to the system temp location ($TMPDIR / /tmp). Pass an explicit disk-backed directory when the system temp is on tmpfs.

None

Returns:

Type Description
AnnData

Combined AnnData with all samples. X is None when every input was VDJ-only, otherwise it carries the outer-joined expression matrix.

Notes

Memory: each per-sample AnnData is spilled to a tempfile h5ad after load and the in-memory copy is freed, then merged with anndata.experimental.concat_on_disk, which streams inputs and output in bounded chunks (~400 MB default). The merged file is read back into memory once. Peak memory ≈ max(one sample, output), versus the prior in-memory ad.concat peak of ~2 × Σ(samples).

Temp disk: spilled per-sample h5ads plus the merged output, so ~2× the total sparse dataset size. Redirect with tmpdir= when $TMPDIR is on tmpfs.

VDJ-only samples: see _ensure_x — a zero-column placeholder is synthesized to keep the merge path uniform, then stripped after the merge so combined.X is None for VDJ-only loads.

Source code in tcrsift/loader.py
def load_samples(
    sample_sheet_path: str | Path | SampleSheet,
    min_genes: int = 250,
    max_genes: int = 15000,
    min_counts: int = 500,
    max_counts: int = 100000,
    max_mito_pct: float = 8.0,
    min_mito_pct: float = 2.0,
    verbose: bool = True,
    show_progress: bool = True,
    tmpdir: str | Path | None = None,
    species: str | None = None,
) -> ad.AnnData:
    """
    Load all samples from a sample sheet into a single AnnData object.

    Parameters
    ----------
    sample_sheet_path : str or Path or SampleSheet
        Path to sample sheet (CSV or YAML), or a SampleSheet instance.
    min_genes, max_genes, min_counts, max_counts, max_mito_pct, min_mito_pct
        QC filter parameters.
    verbose : bool
        Print detailed progress information.
    show_progress : bool
        Show progress bar.
    tmpdir : str or Path or None
        Parent directory for the spill tempdir (see Notes). Defaults to the
        system temp location ($TMPDIR / /tmp). Pass an explicit disk-backed
        directory when the system temp is on tmpfs.

    Returns
    -------
    ad.AnnData
        Combined AnnData with all samples. `X` is None when every input was
        VDJ-only, otherwise it carries the outer-joined expression matrix.

    Notes
    -----
    Memory: each per-sample AnnData is spilled to a tempfile h5ad after
    load and the in-memory copy is freed, then merged with
    `anndata.experimental.concat_on_disk`, which streams inputs and output
    in bounded chunks (~400 MB default). The merged file is read back into
    memory once. Peak memory ≈ max(one sample, output), versus the prior
    in-memory `ad.concat` peak of ~2 × Σ(samples).

    Temp disk: spilled per-sample h5ads plus the merged output, so ~2× the
    total sparse dataset size. Redirect with `tmpdir=` when $TMPDIR is on
    tmpfs.

    VDJ-only samples: see `_ensure_x` — a zero-column placeholder is
    synthesized to keep the merge path uniform, then stripped after the
    merge so `combined.X` is None for VDJ-only loads.
    """
    # Load sample sheet (path or object)
    if isinstance(sample_sheet_path, SampleSheet):
        sample_sheet = sample_sheet_path
        sample_sheet_label = "<SampleSheet>"
    else:
        sample_sheet_path = validate_file_exists(sample_sheet_path, "sample sheet")
        sample_sheet = load_sample_sheet(sample_sheet_path)
        sample_sheet_label = str(sample_sheet_path)

    if len(sample_sheet) == 0:
        raise TCRsiftValidationError(
            f"Sample sheet is empty: {sample_sheet_label}",
            hint="Add sample entries to the sample sheet.",
        )

    logger.info(f"Loading {len(sample_sheet)} samples from {sample_sheet_label}")

    # Pre-validate all sample paths to fail fast
    validation_errors = []
    for sample in sample_sheet:
        if sample.vdj_dir and not Path(sample.vdj_dir).exists():
            validation_errors.append(
                f"Sample '{sample.sample}': VDJ directory not found: {sample.vdj_dir}"
            )
        if sample.gex_dir and not Path(sample.gex_dir).exists():
            validation_errors.append(
                f"Sample '{sample.sample}': GEX directory not found: {sample.gex_dir}"
            )
        if getattr(sample, "gex_path", None) and not Path(sample.gex_path).exists():
            validation_errors.append(
                f"Sample '{sample.sample}': GEX file not found: {sample.gex_path}"
            )

    if validation_errors:
        raise TCRsiftValidationError(
            f"Sample sheet validation failed with {len(validation_errors)} error(s):\n"
            + "\n".join(f"  - {e}" for e in validation_errors[:5]),
            hint="Check that all paths in the sample sheet are correct and accessible.",
        )

    sample_keys: list[str] = []
    gene_symbols: dict[str, str] = {}
    spill_paths: list[Path] = []
    total_cells = 0

    with tempfile.TemporaryDirectory(prefix="tcrsift_load_", dir=tmpdir) as spill_dir_str:
        spill_dir = Path(spill_dir_str)
        sample_iter = tqdm(
            sample_sheet,
            desc="Loading samples",
            unit="sample",
            disable=not show_progress,
        )

        for sample in sample_iter:
            sample_iter.set_postfix(sample=sample.sample[:20])

            if verbose:
                logger.info(f"Loading sample: {sample.sample}")

            try:
                adata = load_sample(
                    sample,
                    min_genes=min_genes,
                    max_genes=max_genes,
                    min_counts=min_counts,
                    max_counts=max_counts,
                    max_mito_pct=max_mito_pct,
                    min_mito_pct=min_mito_pct,
                    species=species or getattr(sample_sheet, "species", None),
                )
            except TCRsiftValidationError:
                raise
            except Exception as e:
                raise TCRsiftValidationError(
                    f"Failed to load sample '{sample.sample}': {e}",
                    hint="Check that the CellRanger output directories are valid and complete. "
                    f"VDJ: {sample.vdj_dir}, GEX: {sample.gex_dir or sample.gex_path}",
                ) from e

            if adata is None:
                continue
            if adata.n_obs == 0:
                logger.warning(
                    f"  Sample {sample.sample}: 0 cells remain after loading/QC; skipping"
                )
                continue

            _ensure_x(adata)
            rehydrate_obs(adata)
            sample_keys.append(str(sample.sample))
            if "gene_symbols" in adata.var.columns:
                gene_symbols.update(zip(adata.var_names, adata.var["gene_symbols"].astype(str)))
            total_cells += adata.n_obs

            # Number files by the load order of successful samples (skipped
            # samples don't leave gaps), so the spill dir is easy to inspect.
            path = spill_dir / f"sample_{len(spill_paths):04d}.h5ad"
            adata.write_h5ad(path)
            spill_paths.append(path)

            if verbose:
                logger.info(f"  Sample {sample.sample}: {adata.n_obs:,} cells")
            del adata  # release the in-memory copy now that it's on disk

        if not sample_keys:
            raise TCRsiftValidationError(
                "No samples loaded successfully",
                hint="Check that at least one sample has valid VDJ or GEX data.",
            )

        logger.info(f"Concatenating {len(sample_keys)} samples ({total_cells:,} total cells)")
        out_path = spill_dir / "combined.h5ad"
        with tqdm(
            total=1, desc="Concatenating samples", unit="step", disable=not show_progress
        ) as pbar:
            concat_on_disk(
                spill_paths, out_path,
                join="outer", label="sample", keys=sample_keys,
            )
            # Non-unique obs_names are EXPECTED and intentional here: 10x reuses
            # one barcode whitelist across samples, and we deliberately do not
            # uniquify (downstream code indexes positionally with a sample-column
            # mask — see qc.py `gex_vdj_overlap`). So anndata's "Observation names
            # are not unique" notice on read-back is noise, not a problem to fix.
            with warnings.catch_warnings():
                warnings.filterwarnings(
                    "ignore", message="Observation names are not unique",
                    category=UserWarning,
                )
                combined = ad.read_h5ad(out_path)
            pbar.update(1)

        # The concatenation drops per-gene annotations. Matrix-directory inputs
        # index genes by Ensembl id and carry the reference's own symbols in
        # ``var["gene_symbols"]``; restore them so symbol lookups use the
        # sample's reference rather than a different Ensembl release (which
        # renames or lacks some genes and made multi-sample signature scoring
        # fail on symbols the matrices do contain).
        if gene_symbols:
            combined.var["gene_symbols"] = [gene_symbols.get(name, name) for name in combined.var_names]

    rehydrate_obs(combined)

    # Tempdir is cleaned up here; `combined` is fully in-memory.
    # Inverse of `_ensure_x`: an all-VDJ-only sheet produces n_vars == 0,
    # in which case the synthesized empty placeholders should look like the
    # original no-X state to downstream callers.
    if combined.n_vars == 0:
        combined.X = None

    # Store sample sheet as uns as a JSON string. The previous form,
    # `to_dataframe().to_dict()`, produced `{col: {row_idx: val}}` whose
    # int row-index keys break h5ad serialization. Other structured forms
    # (DataFrame directly, `to_dict(orient="list")`) hit anndata/h5py
    # mixed-None-object-column limitations. JSON is unambiguous about
    # None and stores as a single string. Read back with
    # `pd.read_json(io.StringIO(adata.uns["sample_sheet"]), orient="records")`.
    combined.uns["sample_sheet"] = sample_sheet.to_dataframe().to_json(
        orient="records"
    )
    sheet_species = species or getattr(sample_sheet, "species", None)
    combined.uns["species"] = resolve_species(
        sheet_species or "auto",
        adata=combined if combined.n_vars > 0 else None,
        df=combined.obs,
        warn_mismatch=bool(sheet_species and sheet_species != "auto"),
        context="load_samples",
    )

    logger.info(f"Successfully loaded {combined.n_obs:,} cells from {len(sample_keys)} samples")

    return combined