Skip to content

Utilities

Utility functions for scpviz.

This package provides helper and processing functions used throughout scpviz. Import as:

from scpviz import utils as scutils

Submodules (for maintainers): formatting, data, class_filter, id_maps, stats, serialization (.pdata save/load encoding; not star-imported), limma (moderated t-test behind de(method='limma'); not star-imported).

Text / formatting

Functions:

Name Description
format_log_prefix

Return standardized log prefixes for messages.

Data access + transformation

Functions:

Name Description
parse_filename_index

Parse sample metadata from filename columns.

get_samplenames

Resolve sample names for given classes from .obs.

get_classlist

Return unique class values for specified .obs columns.

get_adata_layer

Safely extract a matrix from .X or .layers.

get_adata

Retrieve the .prot or .pep AnnData from a pAnnData object.

get_abundance

Extract abundance data from pAnnData or AnnData.

resolve_accessions

Map gene names or accessions to .var_names.

resolve_peptides

Map gene/accession/peptide names to .pep.var_names.

get_pep_prot_mapping

Determine peptide-to-protein mapping column.

get_peptides_for_accessions

Map accessions or genes to observed peptides via RS.

get_accessions_for_peptides

Map peptide IDs or sequences to protein accessions via RS.

strip_peptide_sequence

Clean PD/DIA-NN peptide strings for Biopython analysis.

compute_peptide_properties

Compute local / Biopython peptide metrics from sequences.

get_peptide_properties

Compute peptide biophysical metrics and annotate .pep.var.

update_layer_provenance

Register a matrix layer in adata.uns['layer_provenance'].

resolve_input_layer

Map layer='X' to uns['current_X_layer'] for provenance.

infer_layer_is_log

Infer log-transformed layers via provenance or name heuristic.

Sample selection / set logic

Functions:

Name Description
format_class_filter

Standardize class/value inputs for filtering.

filter

Legacy sample filtering (prefer pAnnData.filter_sample_values).

resolve_class_filter

Resolve class/value pairs and apply filtering.

get_upset_contents

Build contents for UpSet plots from pAnnData.

get_upset_query

Query features present/absent in UpSet contents.

Identifier mappings (UniProt / STRING)

Functions:

Name Description
get_uniprot_fields_worker

Low-level UniProt REST API query (accessions endpoint, chunked).

get_uniprot_fields

High-level UniProt API wrapper with batching.

standardize_uniprot_columns

Normalize UniProt column names for stable downstream use.

get_string_mappings

Map UniProt accessions to STRING IDs (UniProt + STRING fallback).

convert_identifiers

Convert between accession / gene / STRING / organism_id.

Statistics

Functions:

Name Description
pairwise_log2fc

Compute pairwise median log2 fold change between groups.

de_adata

Differential expression helper over AnnData matrices.

get_pca_importance

Identify most important features for PCA components.

get_protein_clusters

Retrieve hierarchical clusters from stored linkage.

correlation_linkage

Average-linkage clustering with correlation distance (1 − r).

Warning

Many functions here are internal helpers. For common workflows (filtering, plotting, enrichment), prefer the corresponding pAnnData methods when available.

bh_adjust_pvalues

bh_adjust_pvalues(
    pvals: ndarray | list[float],
) -> np.ndarray

Benjamini-Hochberg FDR adjustment for a 1-D array of p-values.

NaN and non-finite entries are left unchanged; only finite values are corrected.

Source code in src/scpviz/utils/stats.py
def bh_adjust_pvalues(pvals: np.ndarray | list[float]) -> np.ndarray:
    """
    Benjamini-Hochberg FDR adjustment for a 1-D array of p-values.

    NaN and non-finite entries are left unchanged; only finite values are corrected.
    """
    pvals_arr = np.asarray(pvals, dtype=float)
    adj_pvals = np.full(pvals_arr.shape, np.nan, dtype=float)
    valid_mask = np.isfinite(pvals_arr)
    n_valid = int(valid_mask.sum())
    if n_valid > 1:
        adj_pvals[valid_mask] = false_discovery_control(
            pvals_arr[valid_mask], method="bh"
        )
    elif n_valid == 1:
        adj_pvals[valid_mask] = pvals_arr[valid_mask]
    return adj_pvals

compute_peptide_properties

compute_peptide_properties(
    sequences: str | Sequence[str] | Series,
    properties: Sequence[str] = DEFAULT_PEPTIDE_PROPERTIES,
    *,
    monoisotopic: bool = False,
    charge_at_pH: float | None = None,
    protein_scale: dict[str, float] | None = None,
    protein_scale_window: int = 9,
    protein_scale_edge: float = 1.0
) -> pd.DataFrame

Compute peptide biophysical properties for one or more amino-acid sequences.

Supports local metrics (e.g. length) and Biopython ProtParam metrics. Does not require a pAnnData object. Raw Proteome Discoverer strings are cleaned automatically; DIA-NN Stripped.Sequence values can be passed directly.

Parameters:

Name Type Description Default
sequences str, list of str, or pandas.Series

Peptide sequence(s). Non-standard input yields pd.NA for that row.

required
properties list of str

Metric names to compute (local and/or Biopython; see Supported properties below). Default: gravy, molecular_weight, isoelectric_point.

DEFAULT_PEPTIDE_PROPERTIES
monoisotopic bool

Use monoisotopic mass for molecular_weight.

False
charge_at_pH float

Required when "charge_at_pH" is in properties.

None
protein_scale dict

Amino-acid scale dict; required when "protein_scale" is requested.

None
protein_scale_window int

Window size for protein_scale only.

9
protein_scale_edge float

Edge weight for protein_scale only.

1.0

Returns:

Type Description
DataFrame

pd.DataFrame: One row per input sequence with columns for each requested metric.

Example

Clean a specified peptide list, then compute features (no pAnnData required):

from scpviz import utils as scutils

raw_peptides = [
    "[K].KPVVDCVVSVPCFYTDAER.[R]",
    "[R].MQHNLEQQIQAR.[N] MOD:1xOxidation [M1]",
]
cleaned = [scutils.strip_peptide_sequence(p) for p in raw_peptides]
props = scutils.compute_peptide_properties(cleaned)
props
#    gravy  molecular_weight  isoelectric_point
# 0  ...              ...                ...
# 1 -1.392         1495.663              6.502

Single already-clean sequence (e.g. DIA-NN Stripped.Sequence):

scutils.compute_peptide_properties("MQHNLEQQIQAR")
#    gravy  molecular_weight  isoelectric_point
# 0 -1.392         1495.663              6.502

Mix local and Biopython metrics:

cleaned = scutils.strip_peptide_sequence("[R].MQHNLEQQIQAR.[N]")
scutils.compute_peptide_properties(
    cleaned,
    properties=["length", "gravy", "aromaticity"],
)

Supported properties

Pass any mix of the names below in properties.

Local (scpviz, not Biopython):

  • length → length — amino-acid count of the cleaned sequence (len(sequence))

Biopython (Bio.SeqUtils.ProtParam.ProteinAnalysis method names). See Bio.SeqUtils.ProtParam:

Scalar (one column each):

  • gravy → gravy
  • molecular_weight → molecular_weight
  • aromaticity → aromaticity
  • instability_index → instability_index
  • isoelectric_point → isoelectric_point
  • charge_at_pH → charge_at_pH_<pH> (requires charge_at_pH kwarg)

Tuple-valued (split into multiple columns):

  • secondary_structure_fraction → ss_helix, ss_turn, ss_sheet
  • molar_extinction_coefficient → extinction_reduced, extinction_oxidized

Dict- or list-valued (object columns):

  • count_amino_acids → aa_counts (dict)
  • get_amino_acids_percent → aa_percent (dict)
  • flexibility → flexibility (list)
  • protein_scale → protein_scale (list; requires protein_scale, protein_scale_window, and protein_scale_edge kwargs)

Default properties: gravy, molecular_weight, isoelectric_point.

Note

Biopython accepts only standard amino-acid letters. Modifications are ignored after stripping PD/DIA-NN notation.

Source code in src/scpviz/utils/peptide_param.py
def compute_peptide_properties(
    sequences: str | Sequence[str] | pd.Series,
    properties: Sequence[str] = DEFAULT_PEPTIDE_PROPERTIES,
    *,
    monoisotopic: bool = False,
    charge_at_pH: float | None = None,
    protein_scale: dict[str, float] | None = None,
    protein_scale_window: int = 9,
    protein_scale_edge: float = 1.0,
) -> pd.DataFrame:
    """
    Compute peptide biophysical properties for one or more amino-acid sequences.

    Supports local metrics (e.g. ``length``) and Biopython ProtParam metrics.
    Does not require a ``pAnnData`` object. Raw Proteome Discoverer strings are
    cleaned automatically; DIA-NN ``Stripped.Sequence`` values can be passed directly.

    Args:
        sequences (str, list of str, or pandas.Series): Peptide sequence(s). Non-standard
            input yields ``pd.NA`` for that row.
        properties (list of str): Metric names to compute (local and/or Biopython; see
            Supported properties below). Default: ``gravy``, ``molecular_weight``,
            ``isoelectric_point``.
        monoisotopic (bool): Use monoisotopic mass for ``molecular_weight``.
        charge_at_pH (float, optional): Required when ``"charge_at_pH"`` is in ``properties``.
        protein_scale (dict, optional): Amino-acid scale dict; required when
            ``"protein_scale"`` is requested.
        protein_scale_window (int): Window size for ``protein_scale`` only.
        protein_scale_edge (float): Edge weight for ``protein_scale`` only.

    Returns:
        pd.DataFrame: One row per input sequence with columns for each requested metric.

    Example:
        Clean a specified peptide list, then compute features (no ``pAnnData`` required):
            ```python
            from scpviz import utils as scutils

            raw_peptides = [
                "[K].KPVVDCVVSVPCFYTDAER.[R]",
                "[R].MQHNLEQQIQAR.[N] MOD:1xOxidation [M1]",
            ]
            cleaned = [scutils.strip_peptide_sequence(p) for p in raw_peptides]
            props = scutils.compute_peptide_properties(cleaned)
            props
            #    gravy  molecular_weight  isoelectric_point
            # 0  ...              ...                ...
            # 1 -1.392         1495.663              6.502
            ```

        Single already-clean sequence (e.g. DIA-NN ``Stripped.Sequence``):
            ```python
            scutils.compute_peptide_properties("MQHNLEQQIQAR")
            #    gravy  molecular_weight  isoelectric_point
            # 0 -1.392         1495.663              6.502
            ```

        Mix local and Biopython metrics:
            ```python
            cleaned = scutils.strip_peptide_sequence("[R].MQHNLEQQIQAR.[N]")
            scutils.compute_peptide_properties(
                cleaned,
                properties=["length", "gravy", "aromaticity"],
            )
            ```

    !!! note "Supported ``properties``"
        Pass any mix of the names below in ``properties``.

        **Local (scpviz, not Biopython):**

        - ``length`` → ``length`` — amino-acid count of the cleaned sequence
          (``len(sequence)``)

        **Biopython** (``Bio.SeqUtils.ProtParam.ProteinAnalysis`` method names). See
        [Bio.SeqUtils.ProtParam](https://biopython.org/docs/latest/api/Bio.SeqUtils.ProtParam.html):

        Scalar (one column each):

        - ``gravy`` → ``gravy``
        - ``molecular_weight`` → ``molecular_weight``
        - ``aromaticity`` → ``aromaticity``
        - ``instability_index`` → ``instability_index``
        - ``isoelectric_point`` → ``isoelectric_point``
        - ``charge_at_pH`` → ``charge_at_pH_<pH>`` (requires ``charge_at_pH`` kwarg)

        Tuple-valued (split into multiple columns):

        - ``secondary_structure_fraction`` → ``ss_helix``, ``ss_turn``, ``ss_sheet``
        - ``molar_extinction_coefficient`` → ``extinction_reduced``, ``extinction_oxidized``

        Dict- or list-valued (object columns):

        - ``count_amino_acids`` → ``aa_counts`` (dict)
        - ``get_amino_acids_percent`` → ``aa_percent`` (dict)
        - ``flexibility`` → ``flexibility`` (list)
        - ``protein_scale`` → ``protein_scale`` (list; requires ``protein_scale``,
          ``protein_scale_window``, and ``protein_scale_edge`` kwargs)

        Default ``properties``: ``gravy``, ``molecular_weight``, ``isoelectric_point``.

    Note:
        Biopython accepts only standard amino-acid letters. Modifications are ignored
        after stripping PD/DIA-NN notation.

    Related Functions:
        - strip_peptide_sequence: Clean individual peptide strings.
        - get_peptide_properties: Compute and annotate peptides in a ``pAnnData`` object.
    """
    props = _validate_properties(properties)
    if "protein_scale" not in props:
        protein_scale = None

    if isinstance(sequences, str):
        seq_list = [sequences]
        index = [0]
    elif isinstance(sequences, pd.Series):
        seq_list = sequences.astype(str).tolist()
        index = sequences.index
    else:
        seq_list = [str(s) for s in sequences]
        index = range(len(seq_list))

    columns = _property_columns(props, charge_at_pH=charge_at_pH)
    rows: list[dict[str, Any]] = []
    for seq in seq_list:
        raw = str(seq).strip().upper()
        clean = raw if _STANDARD_AA.fullmatch(raw) else strip_peptide_sequence(raw)
        if clean is None:
            rows.append({col: pd.NA for col in columns})
            continue
        rows.append(
            _compute_one(
                clean,
                props,
                monoisotopic=monoisotopic,
                charge_at_pH=charge_at_pH,
                protein_scale=protein_scale,
                protein_scale_window=protein_scale_window,
                protein_scale_edge=protein_scale_edge,
            )
        )

    df = pd.DataFrame(rows, index=index)
    for col in columns:
        if col not in df.columns:
            df[col] = pd.NA
        if col not in DICT_OR_LIST_PROPERTIES.values():
            # pd.NA rows leave object dtype; coerce to float/int (NaN), which h5ad can write
            df[col] = pd.to_numeric(df[col], errors="coerce")
    return df[columns]

convert_identifiers

convert_identifiers(
    ids: list[str],
    from_type: str,
    to_type: str | list[str],
    pdata: pAnnData | None = None,
    use_cache: bool = True,
    return_type: str = "dict",
    verbose: bool = True,
) -> (
    dict[str, dict[str, Any]]
    | pd.DataFrame
    | tuple[dict[str, dict[str, Any]], pd.DataFrame]
)

Convert identifiers between UniProt-compatible types.

Supports mapping between protein accessions, gene names, STRING IDs, and organism IDs. Multiple output types may be requested at once.

Parameters:

Name Type Description Default
ids list of str

Input identifiers.

required
from_type str

Source identifier type ("accession" or "gene"). "organism_id" cannot be used as a source.

required
to_type str or list of str

Target identifier type(s). May include any of "gene", "string", or "organism_id".

required
pdata pAnnData

pAnnData object providing cached accession–gene mappings. If provided, use_cache is automatically set to True.

None
use_cache bool

Whether to use cached mappings from pdata. Default is True.

True
return_type str

Output format. "dict" returns a nested dict mapping each input ID to requested fields; "df" returns a DataFrame with columns for from_type and all to_type fields; "both" returns (dict, DataFrame).

'dict'
verbose bool

Whether to print progress messages. Default is True.

True

Returns:

Type Description
dict[str, dict[str, Any]] | DataFrame | tuple[dict[str, dict[str, Any]], DataFrame]

dict, pandas.DataFrame, or tuple: Depends on return_type.

Example

Map one accession to a gene name using cached pdata mappings (dict output):

from scpviz import utils as scutils

scutils.convert_identifiers(
    ["P40925"], "accession", "gene", return_type="dict", pdata=pdata
)

Convert accessions to gene, STRING, and organism ID in one table—gene names come from pdata, STRING and organism ID from UniProt:

scutils.convert_identifiers(
    ["P40925", "P55072", "P04637"],
    "accession",
    ["gene", "string", "organism_id"],
    return_type="df",
    pdata=pdata,
)

Reverse lookup: gene symbols to accession (cached) plus STRING and organism ID (UniProt):

scutils.convert_identifiers(
    ["MDH1", "VCP", "TP53"],
    "gene",
    ["accession", "string", "organism_id"],
    return_type="df",
    pdata=pdata,
)

Same multi-field conversion without pdata—queries UniProt for all fields:

scutils.convert_identifiers(
    ["P40925", "P55072", "P04637"],
    "accession",
    ["gene", "string", "organism_id"],
    return_type="df",
)

Return a nested dict mapping each input ID to all requested fields:

scutils.convert_identifiers(
    ["P40925", "P55072"],
    "accession",
    ["gene", "string", "organism_id"],
    return_type="dict",
)

Source code in src/scpviz/utils/id_maps.py
def convert_identifiers(
    ids: list[str],
    from_type: str,
    to_type: str | list[str],
    pdata: pAnnData | None = None,
    use_cache: bool = True,
    return_type: str = "dict",
    verbose: bool = True,
) -> dict[str, dict[str, Any]] | pd.DataFrame | tuple[dict[str, dict[str, Any]], pd.DataFrame]:
    """
    Convert identifiers between UniProt-compatible types.

    Supports mapping between protein accessions, gene names, STRING IDs,
    and organism IDs. Multiple output types may be requested at once.

    Args:
        ids (list of str): Input identifiers.
        from_type (str): Source identifier type (``"accession"`` or ``"gene"``).
            ``"organism_id"`` cannot be used as a source.
        to_type (str or list of str): Target identifier type(s). May include
            any of ``"gene"``, ``"string"``, or ``"organism_id"``.
        pdata (pAnnData, optional): pAnnData object providing cached
            accession–gene mappings. If provided, ``use_cache`` is
            automatically set to True.
        use_cache (bool): Whether to use cached mappings from ``pdata``.
            Default is True.
        return_type (str): Output format. ``"dict"`` returns a nested dict
            mapping each input ID to requested fields; ``"df"`` returns a
            DataFrame with columns for ``from_type`` and all ``to_type``
            fields; ``"both"`` returns ``(dict, DataFrame)``.
        verbose (bool): Whether to print progress messages. Default is True.

    Returns:
        dict, pandas.DataFrame, or tuple: Depends on ``return_type``.

    Example:
        Map one accession to a gene name using cached ``pdata`` mappings
        (dict output):
            ```python
            from scpviz import utils as scutils

            scutils.convert_identifiers(
                ["P40925"], "accession", "gene", return_type="dict", pdata=pdata
            )
            ```

        Convert accessions to gene, STRING, and organism ID in one table—gene
        names come from ``pdata``, STRING and organism ID from UniProt:
            ```python
            scutils.convert_identifiers(
                ["P40925", "P55072", "P04637"],
                "accession",
                ["gene", "string", "organism_id"],
                return_type="df",
                pdata=pdata,
            )
            ```

        Reverse lookup: gene symbols to accession (cached) plus STRING and
        organism ID (UniProt):
            ```python
            scutils.convert_identifiers(
                ["MDH1", "VCP", "TP53"],
                "gene",
                ["accession", "string", "organism_id"],
                return_type="df",
                pdata=pdata,
            )
            ```

        Same multi-field conversion without ``pdata``—queries UniProt for
        all fields:
            ```python
            scutils.convert_identifiers(
                ["P40925", "P55072", "P04637"],
                "accession",
                ["gene", "string", "organism_id"],
                return_type="df",
            )
            ```

        Return a nested dict mapping each input ID to all requested fields:
            ```python
            scutils.convert_identifiers(
                ["P40925", "P55072"],
                "accession",
                ["gene", "string", "organism_id"],
                return_type="dict",
            )
            ```
    """
    import pandas as pd
    import numpy as np
    import scpviz.utils as _u

    if not ids:
        empty_df = pd.DataFrame(columns=[from_type] + ([to_type] if isinstance(to_type, str) else list(to_type)))
        return {} if return_type != "df" else empty_df

    if pdata is not None:
        use_cache = True

    from_col, to_cols, search_fields = _map_uniprot_field(from_type, to_type)
    if isinstance(to_type, str):
        to_type = [to_type]

    # canonical UniProt field map (consistent with standardize_uniprot_columns)
    _FIELD_MAP = {
        "accession": "accession",
        "gene": "gene_primary",
        "string": "xref_string",
        "organism_id": "organism_id",
    }

    # --- Logging
    if verbose:
        print(f"{format_log_prefix('search', indent=1)} Converting from '{from_type}' to {to_type} for {len(ids)} identifiers...")
        if pdata is not None:
            cacheable_types = {"accession", "gene"}
            api_needed = [t for t in to_type if t not in cacheable_types]
            if set([from_type] + to_type).issubset(cacheable_types):
                print(f"{format_log_prefix('info_only', indent=2)} Using cached mapping from pdata (no UniProt queries).")
            elif api_needed:
                api_list = ", ".join(api_needed)
                print(f"{format_log_prefix('info_only', indent=2)} Using cached mapping for gene/accession; UniProt lookup required for: {api_list}.")
        else:
            print(f"{format_log_prefix('info_only', indent=2)} No pdata provided — querying UniProt for all target fields.")

    # --- Tier 1: cache lookup (only accession <-> gene)
    resolved = {id_: {t: None for t in to_type} for id_ in ids}
    to_query = list(ids)

    if pdata is not None and use_cache and {"accession", "gene"}.issuperset({from_type, *to_type}):
        if from_type == "accession" and "gene" in to_type:
            _, acc_to_gene = pdata.get_identifier_maps(on="protein")
            for acc in ids:
                if acc in acc_to_gene:
                    resolved[acc]["gene"] = acc_to_gene[acc]
        elif from_type == "gene" and "accession" in to_type:
            gene_to_acc, _ = pdata.get_identifier_maps(on="protein")
            for gene in ids:
                if gene in gene_to_acc:
                    resolved[gene]["accession"] = gene_to_acc[gene]

        # Filter unmapped
        to_query = [x for x, v in resolved.items() if not any(vv for vv in v.values())]

    # --- Tier 3: UniProt API
    df = pd.DataFrame()
    if len(to_query) > 0:
        # Hybrid case: gene → STRING / organism_id (not gene → accession, which would recurse infinitely)
        if from_type == "gene" and to_type != ["accession"]:
            gene_to_acc = convert_identifiers(to_query, "gene", "accession", pdata=pdata, use_cache=use_cache, verbose=False)
            accs = [v.get("accession") for v in gene_to_acc.values() if v.get("accession")]
            if accs:
                df = _u.get_uniprot_fields(accs, search_fields=search_fields, standardize=True)
                df = _u.standardize_uniprot_columns(df)
                df = df.drop_duplicates(subset="accession", keep="first")

                # Build per-target maps
                per_target_maps = {}
                for t in to_type:
                    col = _FIELD_MAP[t]
                    if col in df.columns:
                        per_target_maps[t] = dict(zip(df["accession"], df[col]))
                    else:
                        per_target_maps[t] = {}

                # Assign results
                for g, acc_dict in gene_to_acc.items():
                    acc = acc_dict.get("accession")
                    for t in to_type:
                        resolved[g][t] = per_target_maps[t].get(acc) if acc else None
            else:
                for g in to_query:
                    for t in to_type:
                        resolved[g][t] = None

        else:
            # Direct mapping (accession → X)
            df = _u.get_uniprot_fields(to_query, search_fields=search_fields, standardize=True)

            # --- Clean up STRING results if present
            if not df.empty:
                if "xref_string" in df.columns and isinstance(df["xref_string"], pd.Series):
                    df["xref_string"] = (
                        df["xref_string"]
                        .astype(str)
                        .apply(lambda s: s.replace(";", "").strip() if isinstance(s, str) else np.nan)
                        .replace({"nan": np.nan, "None": np.nan, "": np.nan})
                    )
                elif "string" in to_type and verbose:
                    print(f"{format_log_prefix('warn_only', indent=3)} UniProt did not return 'xref_string' field — possible API schema drift.")

            if not df.empty and from_col in df.columns:
                per_target_maps = {}
                for t in to_type:
                    col = _FIELD_MAP[t]
                    if col in df.columns:
                        per_target_maps[t] = dict(zip(df[from_col], df[col]))
                    else:
                        per_target_maps[t] = {}

                for id_ in to_query:
                    for t in to_type:
                        resolved[id_][t] = per_target_maps[t].get(id_)
            else:
                for id_ in to_query:
                    for t in to_type:
                        resolved[id_][t] = None

    # --- Reporting
    resolved_count = sum(
        any(vv is not None and not pd.isna(vv) for vv in v.values()) for v in resolved.values()
    )
    missing = [k for k, v in resolved.items() if all(vv is None or pd.isna(vv) for vv in v.values())]

    if verbose:
        local_resolved = len(ids) - len(to_query)
        api_resolved = resolved_count - local_resolved
        print(f"{format_log_prefix('result_only', indent=2)} {resolved_count}/{len(ids)} identifiers successfully converted "
            f"({local_resolved} local, {api_resolved} via UniProt).")
        if missing:
            print(f"{format_log_prefix('warn_only', indent=2)} {len(missing)} identifiers could not be resolved:")
            print("        " + ", ".join(missing[:10]) + ("..." if len(missing) > 10 else ""))

    # --- Output
    result_df = pd.DataFrame({from_type: list(resolved.keys())})
    for t in to_type:
        result_df[t] = [resolved[i][t] for i in result_df[from_type]]

    if return_type == "dict":
        return resolved
    elif return_type == "df":
        return result_df
    elif return_type == "both":
        return resolved, result_df
    else:
        raise ValueError("Invalid return_type. Choose from {'dict', 'df', 'both'}.")

correlation_linkage

correlation_linkage(
    X: ndarray,
    method: str = "pearson",
    linkage_method: str = "average",
    optimal_ordering: bool = False,
) -> tuple[np.ndarray, np.ndarray]

Compute a hierarchical clustering linkage matrix using correlation distance.

Distance is defined as 1 - correlation(x, y) for each pair of rows in X. Rows are typically feature profiles across samples (e.g. z-scored protein abundance). Aggregate to group means before calling if per-cell relationships are not the intent.

Parameters:

Name Type Description Default
X ndarray

Array of shape (n_rows, n_features). Each row is a profile to compare pairwise. Must not contain NaN (impute upstream).

required
method str

Correlation method, either "pearson" or "spearman". Spearman is Pearson correlation on rank-transformed rows (ties via average rank).

'pearson'
linkage_method str

Linkage rule passed to scipy.cluster.hierarchy.linkage. Defaults to "average" (UPGMA).

'average'
optimal_ordering bool

If True, reorders the linkage so adjacent leaves are as similar as possible (display only; does not change cluster membership).

False

Returns:

Type Description
ndarray

A tuple of:

ndarray
  • Z: (n_rows - 1, 4) linkage matrix for dendrogram().
tuple[ndarray, ndarray]
  • condensed_dist: Condensed pairwise distance vector used to build Z.

Raises:

Type Description
ValueError

If method is invalid, or any row of X has zero variance (correlation undefined).

Note

Uses signed 1 - r, not 1 - |r| — anti-correlated profiles are treated as dissimilar.

Example

Compute linkage for group-averaged profiles:

from scpviz.utils import correlation_linkage
from scipy.cluster.hierarchy import dendrogram

Z, dist = correlation_linkage(group_means, method="pearson")
dendrogram(Z, labels=group_labels)

Source code in src/scpviz/utils/stats.py
def correlation_linkage(
    X: np.ndarray,
    method: str = "pearson",
    linkage_method: str = "average",
    optimal_ordering: bool = False,
) -> tuple[np.ndarray, np.ndarray]:
    """
    Compute a hierarchical clustering linkage matrix using correlation distance.

    Distance is defined as ``1 - correlation(x, y)`` for each pair of rows in ``X``.
    Rows are typically feature profiles across samples (e.g. z-scored protein
    abundance). Aggregate to group means before calling if per-cell relationships
    are not the intent.

    Args:
        X: Array of shape ``(n_rows, n_features)``. Each row is a profile to
            compare pairwise. Must not contain NaN (impute upstream).
        method: Correlation method, either ``"pearson"`` or ``"spearman"``.
            Spearman is Pearson correlation on rank-transformed rows (ties via
            average rank).
        linkage_method: Linkage rule passed to ``scipy.cluster.hierarchy.linkage``.
            Defaults to ``"average"`` (UPGMA).
        optimal_ordering: If True, reorders the linkage so adjacent leaves are as
            similar as possible (display only; does not change cluster membership).

    Returns:
        A tuple of:

        - ``Z``: ``(n_rows - 1, 4)`` linkage matrix for ``dendrogram()``.
        - ``condensed_dist``: Condensed pairwise distance vector used to build ``Z``.

    Raises:
        ValueError: If ``method`` is invalid, or any row of ``X`` has zero variance
            (correlation undefined).

    Note:
        Uses signed ``1 - r``, not ``1 - |r|`` — anti-correlated profiles are treated
        as dissimilar.

    Example:
        Compute linkage for group-averaged profiles:
            ```python
            from scpviz.utils import correlation_linkage
            from scipy.cluster.hierarchy import dendrogram

            Z, dist = correlation_linkage(group_means, method="pearson")
            dendrogram(Z, labels=group_labels)
            ```
    """
    from scipy.cluster.hierarchy import linkage
    from scipy.spatial.distance import pdist
    from scipy.stats import rankdata

    if method not in ("pearson", "spearman"):
        raise ValueError(f"method must be 'pearson' or 'spearman', got {method!r}")

    X = np.asarray(X, dtype=float)
    row_var = X.var(axis=1)
    if np.any(row_var == 0):
        bad_rows = np.where(row_var == 0)[0].tolist()
        raise ValueError(
            f"Rows {bad_rows} have zero variance; correlation is undefined"
        )

    if method == "spearman":
        X = np.apply_along_axis(rankdata, axis=1, arr=X)

    # pdist 'correlation' metric computes 1 - Pearson r directly
    condensed_dist = pdist(X, metric="correlation")
    Z = linkage(
        condensed_dist, method=linkage_method, optimal_ordering=optimal_ordering
    )
    return Z, condensed_dist

de_adata

de_adata(
    adata: AnnData,
    values: list[dict[str, Any] | list[str]] | None = None,
    class_type: str | list[str] | None = None,
    method: str = "ttest",
    fold_change_mode: str = "mean",
    layer: str = "X",
    threshold: float = 0.05,
    log2fc: float = 1.0,
    correct_fdr: bool = False,
    equal_var: bool = True,
    pval: float | None = None,
    data_is_log: bool = False,
    log_base: float = 2.0,
    pseudocount: float = 1.0,
    gene_col: str | None = None,
    min_valid_fraction: float = 0.5,
) -> pd.DataFrame

Standalone DE analysis for AnnData. Produces a volcano-ready DataFrame identical to pdata.de().

Supports
  • Legacy-style: class_type="condition", values=["A","B"]
  • Legacy multi-col: class_type=["cellline","treatment"], values=[["HCT116","DMSO"], ["HCT116","Drug"]]
  • Dictionary-style: values=[{"cellline":"HCT116","treatment":"DMSO"}, {...}]

Parameters:

Name Type Description Default
adata AnnData

AnnData object.

required
values list of dict or list of list

Sample group filters to compare.

  • Dictionary-style (recommended): [{'cellline': 'HCT116', 'treatment': 'DMSO'}, {...}]
  • Legacy-style (if class_type is provided): [['HCT116', 'DMSO'], ['HCT116', 'DrugX']]
None
class_type str or list of str

Legacy-style class label(s) to interpret values.

None
method str

'ttest', 'mannwhitneyu', 'wilcoxon', or 'limma' (moderated t-test with empirical Bayes variance shrinkage; see scpviz.utils.limma). 'limma' needs a log-transformed layer (data_is_log=True) and fold_change_mode='mean'; test_statistic is then the moderated t and equal_var is ignored.

'ttest'
fold_change_mode str

'mean' or 'pairwise_median'.

'mean'
layer str

Layer to use. Default is 'X'.

'X'
threshold float

Significance cutoff. Applied to raw p_value when correct_fdr=False, and to adj_p_value when correct_fdr=True.

0.05
pval float

Deprecated alias for threshold.

None
log2fc float

Minimum absolute log2 fold change for significance labeling.

1.0
correct_fdr bool

If True, apply Benjamini-Hochberg FDR correction and label significance using adjusted p-values.

False
equal_var bool

Passed to :func:scipy.stats.ttest_ind when method='ttest'. True (default) uses Student's t-test; False uses Welch's t-test.

True
data_is_log bool

If True, treat layer as log-transformed: with fold_change_mode='mean' the log2FC is the difference of the log-scale group means converted to log2 units (as in pdata.de), and zeros (log_base(pseudocount)) are treated as missing.

False
log_base float

Base of the log used in layer. Default 2.0.

2.0
pseudocount float

If data is log of (x + pseudocount), provide that here (e.g., 1.0 for log2(x+1)).

1.0
gene_col str

Column in adata.var to use for the "Genes" field in the output. Will use: - adata.var['Genes'] by default, - adata.var[<gene_col>] if provided by the user, otherwise - adata.var_names if the above do not exist.

None
min_valid_fraction float

Only used when correct_fdr=True. Features with a fraction of non-missing values below this in either group are excluded from the BH correction (adj_p_value is NaN; raw p_value is kept). Default 0.5. Ignored for method='limma' (filter sparse proteins upstream, e.g. with filter_prot).

0.5

Returns:

Type Description
DataFrame

pandas.DataFrame: DE results with volcano-ready columns.

Note

Zeros are treated as missing and set to NaN before testing and fold change (see :func:mask_zeros_as_nan).

Source code in src/scpviz/utils/stats.py
def de_adata(
    adata: ad.AnnData,
    values: list[dict[str, Any] | list[str]] | None = None,
    class_type: str | list[str] | None = None,
    method: str = "ttest",
    fold_change_mode: str = "mean",
    layer: str = "X",
    threshold: float = 0.05,
    log2fc: float = 1.0,
    correct_fdr: bool = False,
    equal_var: bool = True,
    pval: float | None = None,
    data_is_log: bool = False,
    log_base: float = 2.0,
    pseudocount: float = 1.0,
    gene_col: str | None = None,
    min_valid_fraction: float = 0.5,
) -> pd.DataFrame:
    """
    Standalone DE analysis for AnnData. Produces a volcano-ready DataFrame identical to pdata.de().

    Supports:
        - Legacy-style: class_type="condition", values=["A","B"]
        - Legacy multi-col: class_type=["cellline","treatment"],
                            values=[["HCT116","DMSO"], ["HCT116","Drug"]]
        - Dictionary-style: values=[{"cellline":"HCT116","treatment":"DMSO"}, {...}]    

    Args:
        adata (AnnData): AnnData object.
        values (list of dict or list of list): Sample group filters to compare.

            - Dictionary-style (recommended): [{'cellline': 'HCT116', 'treatment': 'DMSO'}, {...}]
            - Legacy-style (if `class_type` is provided): [['HCT116', 'DMSO'], ['HCT116', 'DrugX']]

        class_type (str or list of str, optional): Legacy-style class label(s) to interpret `values`.
        method (str): 'ttest', 'mannwhitneyu', 'wilcoxon', or 'limma' (moderated t-test with
            empirical Bayes variance shrinkage; see ``scpviz.utils.limma``). 'limma' needs a
            log-transformed layer (``data_is_log=True``) and ``fold_change_mode='mean'``; ``test_statistic``
            is then the moderated t and ``equal_var`` is ignored.
        fold_change_mode (str): 'mean' or 'pairwise_median'.
        layer (str): Layer to use. Default is 'X'.
        threshold (float): Significance cutoff. Applied to raw ``p_value`` when
            ``correct_fdr=False``, and to ``adj_p_value`` when ``correct_fdr=True``.
        pval (float, optional): Deprecated alias for ``threshold``.
        log2fc (float): Minimum absolute log2 fold change for significance labeling.
        correct_fdr (bool): If True, apply Benjamini-Hochberg FDR correction and
            label significance using adjusted p-values.
        equal_var (bool): Passed to :func:`scipy.stats.ttest_ind` when ``method='ttest'``.
            ``True`` (default) uses Student's t-test; ``False`` uses Welch's t-test.
        data_is_log (bool): If True, treat `layer` as log-transformed: with
            ``fold_change_mode='mean'`` the log2FC is the difference of the log-scale group
            means converted to log2 units (as in ``pdata.de``), and zeros (``log_base(pseudocount)``)
            are treated as missing.
        log_base (float): Base of the log used in `layer`. Default 2.0.
        pseudocount (float): If data is log of (x + pseudocount), provide that
            here (e.g., 1.0 for log2(x+1)).
        gene_col (str, optional): Column in `adata.var` to use for the "Genes"
            field in the output. Will use:
            - `adata.var['Genes']` by default,
            - `adata.var[<gene_col>]` if provided by the user, otherwise
            - `adata.var_names` if the above do not exist.
        min_valid_fraction (float): Only used when ``correct_fdr=True``. Features
            with a fraction of non-missing values below this in either group are
            excluded from the BH correction (``adj_p_value`` is NaN; raw
            ``p_value`` is kept). Default 0.5. Ignored for ``method='limma'``
            (filter sparse proteins upstream, e.g. with ``filter_prot``).

    Returns:
        pandas.DataFrame: DE results with volcano-ready columns.

    Note:
        Zeros are treated as missing and set to NaN before testing and fold
        change (see :func:`mask_zeros_as_nan`).
    """
    if pval is not None:
        print(
            f"{format_log_prefix('warn')} `pval` is deprecated in de_adata(); "
            f"use `threshold` instead (applied pval={pval})."
        )
        if pval != threshold:
            print(
                f"{format_log_prefix('warn')} Both `threshold`={threshold} and "
                f"`pval={pval}` were passed to de_adata(); using `pval`."
            )
        threshold = pval

    def to_dict_list(class_type, val):
        """Convert legacy values into a list of dictionary filters."""
        if isinstance(val, dict):
            return [val]

        # if class_type is singular
        if isinstance(class_type, str):
            return [{class_type: val}]

        # if class_type is list (multi-column)
        if isinstance(class_type, list) and isinstance(val, list):
            if len(class_type) != len(val):
                raise ValueError("Length mismatch: class_type and values.")
            return [dict(zip(class_type, val))]

        raise ValueError("Invalid legacy DE input format.")

    # identify sample indices for each group
    def filter_indices(adata, filters):
        """Return sample indices matching a list of dict filters."""
        mask = np.ones(len(adata), dtype=bool)
        for f in filters:
            for col, val in f.items():
                mask &= (adata.obs[col].astype(str) == str(val))
        return np.where(mask)[0]

    if values is None:
        raise ValueError("Please supply `values` (2 groups) for DE.")

    if len(values) != 2:
        raise ValueError("`values` must contain exactly two group definitions.")

    if values[0] == values[1]:
        raise ValueError("Both groups in `values` refer to the same condition. Please provide two distinct groups.")

    # convert values to standardized dict format
    if isinstance(values[0], dict):
        group1_filters = [values[0]]
        group2_filters = [values[1]]
    else:
        if class_type is None:
            raise ValueError("class_type must be provided for legacy DE format.")
        group1_filters = to_dict_list(class_type, values[0])
        group2_filters = to_dict_list(class_type, values[1])


    idx1 = filter_indices(adata, group1_filters)
    idx2 = filter_indices(adata, group2_filters)

    if len(idx1) == 0 or len(idx2) == 0:
        raise ValueError("One of the groups has zero samples.")

    # extract matrices
    if layer == "X":
        X = adata.X
    else:
        if layer not in adata.layers:
            raise KeyError(f"Layer '{layer}' not found in adata.layers.")
        X = adata.layers[layer]

    X = X.toarray() if sparse.issparse(X) else np.asarray(X)
    data1 = mask_zeros_as_nan(X[idx1, :], data_is_log, log_base, pseudocount)
    data2 = mask_zeros_as_nan(X[idx2, :], data_is_log, log_base, pseudocount)

    # log2FC computation

    if fold_change_mode == 'mean':
        with np.errstate(all='ignore'), warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)
            m1 = np.nanmean(data1, axis=0)
            m2 = np.nanmean(data2, axis=0)
            mask_invalid = np.isnan(m1) | np.isnan(m2)
            if data_is_log:  # difference of log-scale means (as pdata.de), converted to log2 units
                log2fc_vals = (m1 - m2) * np.log2(log_base)
            else:
                mask_invalid |= (m1 == 0) | (m2 == 0)
                log2fc_vals = np.log2(m1 / m2)
            log2fc_vals[mask_invalid] = np.nan

    elif fold_change_mode == 'pairwise_median':
        mask_invalid = np.any(np.isnan(data1), axis=0) | np.any(np.isnan(data2), axis=0)
        if not data_is_log:  # on a log layer 0 is a real value; true zeros are already NaN
            mask_invalid |= np.any(data1 == 0, axis=0) | np.any(data2 == 0, axis=0)
        # Compute median pairwise log2FC
        log2fc_vals = pairwise_log2fc(data1, data2, data_is_log, log_base)
        log2fc_vals[mask_invalid] = np.nan # Mark invalid features as NaN
        n_invalid = np.sum(mask_invalid)
        if n_invalid > 0:
            print(f"{format_log_prefix('info',2)} {n_invalid} proteins were not comparable (zero or NaN mean in one group).")

    else:
        raise ValueError(f"{format_log_prefix('error')} Unsupported fold_change_mode '{fold_change_mode}'")

    # statistical test

    if method not in {"ttest", "mannwhitneyu", "wilcoxon", "limma"}:
        raise ValueError(f"{format_log_prefix('error')} Unsupported method '{method}'")

    if method == "limma":
        if not data_is_log:
            raise ValueError(f"{format_log_prefix('error')} method='limma' needs a log-transformed layer (set data_is_log=True).")
        if fold_change_mode != "mean":
            raise ValueError(f"{format_log_prefix('error')} method='limma' requires fold_change_mode='mean' (log2fc is the modeled group difference).")
        lim = _moderated_two_group_test(data1, data2)
        pvals, stats = lim["p_value"], lim["t"]  # log2fc stays the group mean difference (== limma beta)
        _print_limma_summary(lim)
    else:
        results = [run_two_group_test(data1[:, i], data2[:, i], method, equal_var) for i in range(X.shape[1])]
        pvals = [r[0] for r in results]
        stats = [r[1] for r in results]

    pvals = np.array(pvals)

    # mean abundance
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", RuntimeWarning)
        mean1 = np.nanmean(data1, axis=0)
        mean2 = np.nanmean(data2, axis=0)

    group1_label = format_de_group_label(group1_filters)
    group2_label = format_de_group_label(group2_filters)

    # assemble DataFrame (pAnnData-compatible)
    df = pd.DataFrame(index=adata.var_names)

    if gene_col is not None:
        # User-specified or default "Genes"
        if gene_col in adata.var.columns:
            df["Genes"] = adata.var[gene_col].astype(str).values
        else:
            raise KeyError(
                f"Requested gene_col='{gene_col}', but this column is not in adata.var.\n"
                f"Available columns: {list(adata.var.columns)}"
            )
    else:
        # Fallback logic: use adata.var['Genes'] if it exists
        if "Genes" in adata.var.columns:
            df["Genes"] = adata.var["Genes"].astype(str).values
        else:
            df["Genes"] = adata.var_names.astype(str)

    df[group1_label] = mean1
    df[group2_label] = mean2
    df["log2fc"] = log2fc_vals
    df["p_value"] = pvals
    df["test_statistic"] = stats
    df["-log10(p_value)"] = -np.log10(np.where(pvals == 0, np.nan, pvals))
    df["significance_score"] = df["-log10(p_value)"] * df["log2fc"]

    if correct_fdr:
        if method == "limma":  # the prior is fitted on the supplied proteins; filter upstream (filter_prot)
            pvals_bh, n_sparse = pvals, 0
        else:
            pvals_bh, n_sparse = exclude_sparse_pvalues(pvals, data1, data2, min_valid_fraction)
        if n_sparse > 0:
            print(
                f"{format_log_prefix('info', 2)} {n_sparse} proteins had <{min_valid_fraction:.0%} "
                "valid values in at least one group and were excluded from FDR correction "
                "(too sparse for a reliable test); adj_p_value is NaN for these."
            )
        df["adj_p_value"] = bh_adjust_pvalues(pvals_bh)
        df["-log10(adj_p_value)"] = -np.log10(
            df["adj_p_value"].replace(0, np.nan).astype(float)
        )
        p_for_sig = df["adj_p_value"]
    else:
        p_for_sig = df["p_value"]

    # significance classification
    df["significance"] = "not significant"
    df.loc[df["log2fc"].isna(), "significance"] = "not comparable"
    df.loc[(p_for_sig < threshold) & (df["log2fc"] > log2fc), "significance"] = "upregulated"
    df.loc[(p_for_sig < threshold) & (df["log2fc"] < -log2fc), "significance"] = "downregulated"

    df["significance"] = pd.Categorical(
        df["significance"],
        categories=["upregulated", "downregulated", "not significant", "not comparable"],
        ordered=True,
    )

    # group labels for plotting annotation
    df.attrs["group1_label"] = group1_label
    df.attrs["group2_label"] = group2_label
    if method == "limma":
        df.attrs["limma"] = {k: lim[k] for k in ("s2_prior", "df_prior", "estimator")}

    return df

exclude_sparse_pvalues

exclude_sparse_pvalues(
    pvals: ndarray,
    data1: ndarray,
    data2: ndarray,
    min_valid_fraction: float,
) -> tuple[np.ndarray, int]

NaN the p-values of features too sparse for a reliable test (for FDR correction).

A feature is excluded when either group has a fraction of non-NaN values below min_valid_fraction.

Parameters:

Name Type Description Default
pvals ndarray

Raw p-values, shape (n_features,).

required
data1 ndarray

Group 1 matrix, (n_samples1, n_features).

required
data2 ndarray

Group 2 matrix, (n_samples2, n_features).

required
min_valid_fraction float

Minimum fraction of valid values per group.

required

Returns:

Name Type Description
tuple ndarray

(pvals_for_bh, n_excluded). Excluded features are NaN in

int

pvals_for_bh; features that were already NaN are not counted.

Source code in src/scpviz/utils/stats.py
def exclude_sparse_pvalues(
    pvals: np.ndarray,
    data1: np.ndarray,
    data2: np.ndarray,
    min_valid_fraction: float,
) -> tuple[np.ndarray, int]:
    """
    NaN the p-values of features too sparse for a reliable test (for FDR correction).

    A feature is excluded when either group has a fraction of non-NaN values
    below ``min_valid_fraction``.

    Args:
        pvals (numpy.ndarray): Raw p-values, shape ``(n_features,)``.
        data1 (numpy.ndarray): Group 1 matrix, ``(n_samples1, n_features)``.
        data2 (numpy.ndarray): Group 2 matrix, ``(n_samples2, n_features)``.
        min_valid_fraction (float): Minimum fraction of valid values per group.

    Returns:
        tuple: ``(pvals_for_bh, n_excluded)``. Excluded features are NaN in
        ``pvals_for_bh``; features that were already NaN are not counted.
    """
    sparse_mask = (
        (np.mean(~np.isnan(data1), axis=0) < min_valid_fraction)
        | (np.mean(~np.isnan(data2), axis=0) < min_valid_fraction)
    )
    pvals_arr = np.asarray(pvals, dtype=float)
    n_excluded = int((sparse_mask & ~np.isnan(pvals_arr)).sum())
    return np.where(sparse_mask, np.nan, pvals_arr), n_excluded

filter

filter(
    pdata: pAnnData | AnnData,
    class_type: str | list[str],
    values: dict[str, Any] | list[Any] | str,
    exact_cases: bool = False,
    debug: bool = False,
) -> pAnnData | ad.AnnData

Legacy-style filtering of samples in pAnnData or AnnData objects.

This function filters samples based on metadata values using the older (class_type, values) interface. For pAnnData objects, it automatically delegates to .filter_sample_values() after converting the input into the recommended dictionary-style format.

Warning

For pAnnData users, prefer .filter_sample_values() with dictionary-style input, as it is more flexible and consistent. The filter() utility is retained primarily for backward compatibility and direct AnnData usage.

Parameters:

Name Type Description Default
pdata pAnnData or AnnData

Input data object to filter.

required
class_type str or list of str

Metadata field(s) in .obs to filter on. Example: "treatment", or ["cell_type", "treatment"].

required
values list, dict, or list of dict

Metadata values to match. - If exact_cases=False: Provide a dictionary or list-of-values per class. - If exact_cases=True: Provide a list of dictionaries specifying exact combinations across fields.

required
exact_cases bool

Whether to interpret values as exact combinations (AND logic). Defaults to False, which applies OR logic within each class type.

False
debug bool

If True, print the query string used for filtering.

False

Returns:

Name Type Description
filtered pAnnData or AnnData

A filtered object of the same type as pdata.

Raises:

Type Description
ValueError

If input types are invalid, if fields are missing in .obs, or if values format does not match exact_cases.

Example

Filter samples by a single metadata field:

samples = utils.filter(pdata, class_type="treatment", values="kd")

Filter by multiple fields with OR logic:

samples = utils.filter(
        adata,
        class_type=["cell_type", "treatment"],
        values=[["wt", "kd"], ["control", "treatment"]]
    ) 
# returns samples where cell_type is either 'wt' or 'kd' and treatment is either 'control' or 'treatment'

Filter by exact case combinations:

samples = utils.filter(
        adata,
        class_type=["cell_type", "treatment"],
        values=[{"cell_type": "wt", "treatment": "control"},
                {"cell_type": "kd", "treatment": "treatment"}],
        exact_cases=True
    )
# returns samples where cell_type is 'wt' and treatment is 'kd', or cell_type is 'control' and treatment is 'treatment'

Source code in src/scpviz/utils/class_filter.py
def filter(
    pdata: pAnnData | ad.AnnData,
    class_type: str | list[str],
    values: dict[str, Any] | list[Any] | str,
    exact_cases: bool = False,
    debug: bool = False,
) -> pAnnData | ad.AnnData:
    """
    Legacy-style filtering of samples in pAnnData or AnnData objects.

    This function filters samples based on metadata values using the older
    `(class_type, values)` interface. For pAnnData objects, it automatically
    delegates to `.filter_sample_values()` after converting the input into the
    recommended dictionary-style format.

    !!! warning

        For pAnnData users, prefer `.filter_sample_values()` with dictionary-style
        input, as it is more flexible and consistent. The `filter()` utility is
        retained primarily for backward compatibility and direct AnnData usage.


    Args:
        pdata (pAnnData or AnnData): Input data object to filter.
        class_type (str or list of str): Metadata field(s) in `.obs` to filter on.
            Example: `"treatment"`, or `["cell_type", "treatment"]`.
        values (list, dict, or list of dict): Metadata values to match.
            - If `exact_cases=False`: Provide a dictionary or list-of-values per class.
            - If `exact_cases=True`: Provide a list of dictionaries specifying
              exact combinations across fields.
        exact_cases (bool): Whether to interpret `values` as exact combinations (AND logic).
            Defaults to False, which applies OR logic within each class type.
        debug (bool): If True, print the query string used for filtering.

    Returns:
        filtered (pAnnData or AnnData): A filtered object of the same type as `pdata`.


    Raises:
        ValueError: If input types are invalid, if fields are missing in `.obs`,
            or if `values` format does not match `exact_cases`.

    Example:
        Filter samples by a single metadata field:
            ```python
            samples = utils.filter(pdata, class_type="treatment", values="kd")
            ```

        Filter by multiple fields with OR logic: 
            ```python
            samples = utils.filter(
                    adata,
                    class_type=["cell_type", "treatment"],
                    values=[["wt", "kd"], ["control", "treatment"]]
                ) 
            # returns samples where cell_type is either 'wt' or 'kd' and treatment is either 'control' or 'treatment'
            ```

        Filter by exact case combinations:
            ```python 
            samples = utils.filter(
                    adata,
                    class_type=["cell_type", "treatment"],
                    values=[{"cell_type": "wt", "treatment": "control"},
                            {"cell_type": "kd", "treatment": "treatment"}],
                    exact_cases=True
                )
            # returns samples where cell_type is 'wt' and treatment is 'kd', or cell_type is 'control' and treatment is 'treatment'
            ```
    """

    if hasattr(pdata, "filter_sample_values"):
        warnings.warn(
            "You passed a pAnnData object to `filter()`. "
            "It is recommended to use `pdata.filter_sample_values()` directly.",
            UserWarning)

        print("UserWarning: It is recommended to use the class method `.filter_sample_values()` with dictionary-style input for cleaner and more consistent filtering.")

    formatted_values = format_class_filter(class_type, values, exact_cases)

    # pAnnData input
    if hasattr(pdata, "filter_sample_values"):
        return pdata.filter_sample_values(
            values=formatted_values,
            exact_cases=exact_cases,
            debug=debug,
            return_copy=True
        )

    # plain AnnData input
    elif isinstance(pdata, ad.AnnData):
        adata = pdata
        obs_keys = adata.obs.columns

        if exact_cases:
            if not isinstance(formatted_values, list) or not all(isinstance(v, dict) for v in formatted_values):
                raise ValueError("When exact_cases=True, `values` must be a list of dictionaries.")

            for case in formatted_values:
                if not case:
                    raise ValueError("Empty dictionary found in values.")
                for key in case:
                    if key not in obs_keys:
                        raise ValueError(f"Field '{key}' not found in adata.obs.")

            query = " | ".join([
                " & ".join([
                    f"(adata.obs['{k}'] == '{v}')" for k, v in case.items()
                ])
                for case in formatted_values
            ])

        else:
            if not isinstance(formatted_values, dict):
                raise ValueError("When exact_cases=False, `values` must be a dictionary.")

            for key in formatted_values:
                if key not in obs_keys:
                    raise ValueError(f"Field '{key}' not found in adata.obs.")

            query_parts = []
            for k, v in formatted_values.items():
                v_list = v if isinstance(v, list) else [v]
                part = " | ".join([f"(adata.obs['{k}'] == '{val}')" for val in v_list])
                query_parts.append(f"({part})")
            query = " & ".join(query_parts)

        if debug:
            print(f"Filter query: {query}")

        return adata[eval(query)]

    else:
        raise ValueError("Input must be a pAnnData or AnnData object.")

format_class_filter

format_class_filter(
    classes: str | list[str],
    class_value: str | list[str] | list[list[str]],
    exact_cases: bool = False,
) -> dict[str, Any] | list[dict[str, Any]]

Convert legacy-style filter input into dictionary-style format.

This function standardizes (classes, class_value) input into the dictionary format expected by pAnnData.filter_sample_values(). It supports both loose OR-style filtering and exact case matching across multiple metadata fields.

Parameters:

Name Type Description Default
classes str or list of str

Metadata field(s) to filter on. Example: "treatment" or ["cellline", "treatment"].

required
class_value str, list of str, or list of list

Values to filter by. - str: May be underscore-joined (e.g. "kd_AS"). - list of str: Multiple values, interpreted as OR (if exact_cases=False) or split into combinations (if exact_cases=True). - list of list: Each inner list defines a full set of values across classes.

required
exact_cases bool

If True, return a list of dictionaries representing exact combinations across fields. If False, return a dictionary with OR logic applied.

False

Returns:

Name Type Description
formatted dict or list of dict

Dictionary-style filter input compatible

dict[str, Any] | list[dict[str, Any]]

with .filter_sample_values().

Raises:

Type Description
ValueError

If input shapes are inconsistent with the number of classes, or if class_value entries are not valid strings/lists.

Example

Single class with OR logic:

format_class_filter("treatment", ["kd", "sc"])
{'treatment': ['kd', 'sc']}

Multiple classes with loose matching:

format_class_filter(["cellline", "treatment"], ["AS", "kd"])
{'cellline': 'AS', 'treatment': 'kd'}

Multiple classes with exact cases (underscore-joined strings):

format_class_filter(
    ["cellline", "treatment"],
    ["AS_kd", "BE_sc"],
    exact_cases=True
)
[{'cellline': 'AS', 'treatment': 'kd'},
 {'cellline': 'BE', 'treatment': 'sc'}]

Multiple classes with exact cases (list of lists):

format_class_filter(
    ["cellline", "treatment"],
    [["AS", "kd"], ["BE", "sc"]],
    exact_cases=True
)
# [{'cellline': 'AS', 'treatment': 'kd'},
 {'cellline': 'BE', 'treatment': 'sc'}]

Note

This function is primarily used internally by utils.filter() and pAnnData.filter_sample_values(). End users should generally call .filter_sample_values() directly on pAnnData objects instead of using this helper.

Source code in src/scpviz/utils/class_filter.py
def format_class_filter(
    classes: str | list[str],
    class_value: str | list[str] | list[list[str]],
    exact_cases: bool = False,
) -> dict[str, Any] | list[dict[str, Any]]:
    """
    Convert legacy-style filter input into dictionary-style format.

    This function standardizes `(classes, class_value)` input into the dictionary
    format expected by `pAnnData.filter_sample_values()`. It supports both loose
    OR-style filtering and exact case matching across multiple metadata fields.

    Args:
        classes (str or list of str): Metadata field(s) to filter on.
            Example: `"treatment"` or `["cellline", "treatment"]`.
        class_value (str, list of str, or list of list): Values to filter by.
            - str: May be underscore-joined (e.g. `"kd_AS"`).
            - list of str: Multiple values, interpreted as OR (if `exact_cases=False`)
              or split into combinations (if `exact_cases=True`).
            - list of list: Each inner list defines a full set of values across classes.
        exact_cases (bool): If True, return a list of dictionaries representing
            exact combinations across fields. If False, return a dictionary with
            OR logic applied.

    Returns:
        formatted (dict or list of dict): Dictionary-style filter input compatible
        with `.filter_sample_values()`.

    Raises:
        ValueError: If input shapes are inconsistent with the number of classes,
            or if `class_value` entries are not valid strings/lists.

    Example:
        Single class with OR logic:
            ```python
            format_class_filter("treatment", ["kd", "sc"])
            ```
            ```
            {'treatment': ['kd', 'sc']}
            ```

        Multiple classes with loose matching:
            ```python
            format_class_filter(["cellline", "treatment"], ["AS", "kd"])
            ```
            ```
            {'cellline': 'AS', 'treatment': 'kd'}
            ```

        Multiple classes with exact cases (underscore-joined strings):
            ```python
            format_class_filter(
                ["cellline", "treatment"],
                ["AS_kd", "BE_sc"],
                exact_cases=True
            )
            ```
            ```
            [{'cellline': 'AS', 'treatment': 'kd'},
             {'cellline': 'BE', 'treatment': 'sc'}]
            ```

        Multiple classes with exact cases (list of lists):
            ```python 
            format_class_filter(
                ["cellline", "treatment"],
                [["AS", "kd"], ["BE", "sc"]],
                exact_cases=True
            )
            ```
            ```
            # [{'cellline': 'AS', 'treatment': 'kd'},
             {'cellline': 'BE', 'treatment': 'sc'}]
            ```

    !!! warning "Note"

        This function is primarily used internally by `utils.filter()` and
        `pAnnData.filter_sample_values()`. End users should generally call
        `.filter_sample_values()` directly on `pAnnData` objects instead of
        using this helper.
    """

    if isinstance(classes, str):
        # Simple case: one class
        if isinstance(class_value, list) and exact_cases:
            return [{classes: val} for val in class_value]
        else:
            return {classes: class_value}

    elif isinstance(classes, list):
        if exact_cases:
            if isinstance(class_value, str):
                class_value = [class_value]

            formatted = []
            for entry in class_value:
                if isinstance(entry, str):
                    values = entry.split('_')
                elif isinstance(entry, list):
                    values = entry
                else:
                    raise ValueError("Each class_value entry must be a string or a list.")

                if len(values) != len(classes):
                    raise ValueError("Each class_value entry must match the number of classes. Check that group/class labels did not contain unintentional underscores ('_').")
                formatted.append({cls: val for cls, val in zip(classes, values)})

            return formatted

        else:
            # loose match — OR within each class
            if isinstance(class_value, str):
                values = class_value.split('_')
            else:
                values = class_value
            if len(values) != len(classes):
                raise ValueError("class_value must align with the number of classes. Check that group/class labels did not contain unintentional underscores ('_').")
            return {cls: val for cls, val in zip(classes, values)}

    else:
        raise ValueError("Invalid input: `classes` should be a string or list of strings.")

get_abundance

get_abundance(
    pdata: pAnnData | AnnData, *args: Any, **kwargs: Any
) -> pd.DataFrame

Wrapper to extract abundance from either pAnnData or AnnData.

This is a convenience wrapper that dispatches to the appropriate method: - If pdata is a pAnnData object, it calls pdata.get_abundance(). - If pdata is an AnnData object, it falls back to the internal helper _get_abundance_from_adata.

Parameters:

Name Type Description Default
pdata pAnnData or AnnData

Input object to extract abundance from.

required
*args Any

Positional arguments forwarded to get_abundance.

()
**kwargs Any

Keyword arguments forwarded to get_abundance.

{}
Note

See pAnnData.get_abundance for full parameter documentation. Briefly,

- namelist (list of str, optional): List of accessions or gene names to extract.
- layer (str): Data layer name (default = "X").
- on (str): "protein" or "peptide".
- classes (str or list of str, optional): Sample-level `.obs` column(s) to include.
- log (bool): If True, applies log2 transform to abundance values.
- x_label (str): Label features by "gene" or "accession".

Returns:

Name Type Description
df DataFrame

Long-form abundance DataFrame, optionally with

DataFrame

sample metadata and protein/peptide annotations.

See Also
  • :func:pAnnData.get_abundance (EditingMixin): Full-featured version with detailed docs.
  • get_adata_layer: Helper to access abundance matrices from AnnData layers.
Source code in src/scpviz/utils/data.py
def get_abundance(pdata: pAnnData | ad.AnnData, *args: Any, **kwargs: Any) -> pd.DataFrame:
    """
    Wrapper to extract abundance from either pAnnData or AnnData.

    This is a convenience wrapper that dispatches to the appropriate method:
    - If `pdata` is a `pAnnData` object, it calls `pdata.get_abundance()`.
    - If `pdata` is an `AnnData` object, it falls back to the internal
      helper `_get_abundance_from_adata`.

    Args:
        pdata (pAnnData or anndata.AnnData): Input object to extract abundance from.
        *args: Positional arguments forwarded to `get_abundance`.
        **kwargs: Keyword arguments forwarded to `get_abundance`.

    Note:
        See `pAnnData.get_abundance` for full parameter documentation. Briefly,

            - namelist (list of str, optional): List of accessions or gene names to extract.
            - layer (str): Data layer name (default = "X").
            - on (str): "protein" or "peptide".
            - classes (str or list of str, optional): Sample-level `.obs` column(s) to include.
            - log (bool): If True, applies log2 transform to abundance values.
            - x_label (str): Label features by "gene" or "accession".

    Returns:
        df (pandas.DataFrame): Long-form abundance DataFrame, optionally with
        sample metadata and protein/peptide annotations.

    See Also:
        - :func:`pAnnData.get_abundance` (EditingMixin): Full-featured version with detailed docs.
        - get_adata_layer: Helper to access abundance matrices from AnnData layers.
    """
    if hasattr(pdata, "get_abundance"):
        return pdata.get_abundance(*args, **kwargs)
    import scpviz.utils as _u

    return _u._get_abundance_from_adata(pdata, *args, **kwargs)

get_accessions_for_peptides

get_accessions_for_peptides(
    pdata: pAnnData,
    peptides: list[str],
    *,
    sequence_from: str = "index"
) -> pd.DataFrame

Map peptides to protein accessions via the RS matrix.

Peptide inputs may be .pep.var_names or amino-acid strings matched against Stripped.Sequence, Modified.Sequence, or Annotated Sequence.

Parameters:

Name Type Description Default
pdata pAnnData

Object with .prot, .pep, and .rs.

required
peptides list of str

Peptide IDs or sequence strings.

required
sequence_from str

Column in .pep.var for the sequence output, or "index" (default) to use .pep.var_names.

'index'

Returns:

Type Description
DataFrame

pd.DataFrame: Columns peptide_id, accession, sequence. One row per peptide–accession pair (shared peptides yield multiple rows). Unmatched inputs are skipped with a warning.

Raises:

Type Description
ValueError

If .prot, .pep, or .rs is missing, or if sequence_from is not "index" and the column is absent.

Example

Map peptide IDs (.pep.var_names) to protein accessions:

from scpviz import utils as scutils

pep_id = pdata.pep.var_names[0]
df = scutils.get_accessions_for_peptides(pdata, [pep_id])
df.head()
#   peptide_id  accession  sequence
# 0  ...         P34932     ...

DIA-NN: look up by stripped amino-acid sequence (may match multiple precursors):

seq = pdata.pep.var["Stripped.Sequence"].iloc[0]
df = scutils.get_accessions_for_peptides(
    pdata,
    [seq],
    sequence_from="Stripped.Sequence",
)

Round-trip from accession → peptide → accession:

acc = pdata.prot.var_names[0]
peps = scutils.get_peptides_for_accessions(pdata, [acc])
prots = scutils.get_accessions_for_peptides(
    pdata, [peps.iloc[0]["peptide_id"]]
)
assert acc in prots["accession"].values

Note

Shared peptides linked to multiple proteins in the RS matrix return one row per accession. A single sequence string can also match multiple precursor IDs when resolved from .pep.var columns.

Source code in src/scpviz/utils/data.py
def get_accessions_for_peptides(
    pdata: pAnnData,
    peptides: list[str],
    *,
    sequence_from: str = "index",
) -> pd.DataFrame:
    """
    Map peptides to protein accessions via the RS matrix.

    Peptide inputs may be ``.pep.var_names`` or amino-acid strings matched
    against ``Stripped.Sequence``, ``Modified.Sequence``, or ``Annotated Sequence``.

    Args:
        pdata (pAnnData): Object with ``.prot``, ``.pep``, and ``.rs``.
        peptides (list of str): Peptide IDs or sequence strings.
        sequence_from (str): Column in ``.pep.var`` for the ``sequence`` output,
            or ``"index"`` (default) to use ``.pep.var_names``.

    Returns:
        pd.DataFrame: Columns ``peptide_id``, ``accession``, ``sequence``.
            One row per peptide–accession pair (shared peptides yield multiple rows).
            Unmatched inputs are skipped with a warning.

    Raises:
        ValueError: If ``.prot``, ``.pep``, or ``.rs`` is missing, or if
            ``sequence_from`` is not ``"index"`` and the column is absent.

    Example:
        Map peptide IDs (``.pep.var_names``) to protein accessions:
            ```python
            from scpviz import utils as scutils

            pep_id = pdata.pep.var_names[0]
            df = scutils.get_accessions_for_peptides(pdata, [pep_id])
            df.head()
            #   peptide_id  accession  sequence
            # 0  ...         P34932     ...
            ```

        DIA-NN: look up by stripped amino-acid sequence (may match multiple precursors):
            ```python
            seq = pdata.pep.var["Stripped.Sequence"].iloc[0]
            df = scutils.get_accessions_for_peptides(
                pdata,
                [seq],
                sequence_from="Stripped.Sequence",
            )
            ```

        Round-trip from accession → peptide → accession:
            ```python
            acc = pdata.prot.var_names[0]
            peps = scutils.get_peptides_for_accessions(pdata, [acc])
            prots = scutils.get_accessions_for_peptides(
                pdata, [peps.iloc[0]["peptide_id"]]
            )
            assert acc in prots["accession"].values
            ```

    Note:
        Shared peptides linked to multiple proteins in the RS matrix return one
        row per accession. A single sequence string can also match multiple
        precursor IDs when resolved from ``.pep.var`` columns.

    Related Functions:
        - get_peptides_for_accessions: Reverse lookup (accession → peptide).
        - get_pep_prot_mapping: Column name for peptide-to-protein metadata in ``.pep.var``.
    """
    columns = ["peptide_id", "accession", "sequence"]
    if not peptides:
        return pd.DataFrame(columns=columns)

    if pdata.prot is None or pdata.pep is None or pdata.rs is None:
        raise ValueError(
            "get_accessions_for_peptides requires protein, peptide, and RS data."
        )

    if sequence_from != "index" and sequence_from not in pdata.pep.var.columns:
        available = ", ".join(map(str, pdata.pep.var.columns))
        raise ValueError(
            f"Column '{sequence_from}' not found in .pep.var. Available: {available}"
        )

    rs = pdata.rs
    pep_var_names = pdata.pep.var_names.astype(str)
    prot_names = np.array(pdata.prot.var_names)

    rows = []
    unmatched = []
    for query in peptides:
        query = str(query)
        matched_ids = []
        if query in pep_var_names.values:
            matched_ids = [query]
        else:
            for col in _PEPTIDE_SEQUENCE_SEARCH_COLS:
                if col not in pdata.pep.var.columns:
                    continue
                hits = pep_var_names[pdata.pep.var[col].astype(str) == query]
                if len(hits):
                    matched_ids = hits.tolist()
                    break

        if not matched_ids:
            unmatched.append(query)
            continue

        for peptide_id in matched_ids:
            pep_idx = pep_var_names.get_loc(peptide_id)
            prot_idx = rs[:, pep_idx].nonzero()[0]
            if sequence_from == "index":
                sequence = peptide_id
            else:
                sequence = pdata.pep.var.loc[peptide_id, sequence_from]
            for i in prot_idx:
                rows.append(
                    {
                        "peptide_id": peptide_id,
                        "accession": prot_names[i],
                        "sequence": sequence,
                    }
                )

    if unmatched:
        print(f"{format_log_prefix('warn')} A match was not found for the following:")
        for u in unmatched:
            print(f"  - {u}")

    if not rows:
        return pd.DataFrame(columns=columns)

    return (
        pd.DataFrame(rows)
        .drop_duplicates()
        .sort_values(["peptide_id", "accession"], kind="mergesort")
        .reset_index(drop=True)
    )

get_adata

get_adata(
    pdata: pAnnData, on: str = "protein"
) -> ad.AnnData

Retrieve the protein- or peptide-level AnnData object from a pAnnData container.

Parameters:

Name Type Description Default
pdata pAnnData

The parent pAnnData object containing both protein- and peptide-level data.

required
on str

Which data object to return.
- "protein": return pdata.prot
- "peptide": return pdata.pep

'protein'

Returns:

Name Type Description
adata AnnData

The requested AnnData object.

Source code in src/scpviz/utils/data.py
def get_adata(pdata: pAnnData, on: str = "protein") -> ad.AnnData:
    """
    Retrieve the protein- or peptide-level AnnData object from a pAnnData container.

    Args:
        pdata (pAnnData): The parent pAnnData object containing both protein- and peptide-level data.

        on (str): Which data object to return.  
            - `"protein"`: return `pdata.prot`  
            - `"peptide"`: return `pdata.pep`  

    Returns:
        adata (anndata.AnnData): The requested AnnData object.
    """

    if on in ('protein','prot'):
        return pdata.prot
    elif on in ('peptide','pep'):
        return pdata.pep
    else:
        raise ValueError("Invalid value for 'on'. Options are 'protein' or 'peptide'.")

get_adata_layer

get_adata_layer(adata: AnnData, layer: str) -> np.ndarray

Safely extract layer data as dense numpy array.

This helper returns the requested layer as a dense numpy.ndarray, ensuring compatibility for downstream operations. Supports both .X and .layers[...].

Parameters:

Name Type Description Default
adata AnnData

AnnData object containing data matrices.

required
layer str

Layer key.
- "X": return the main data matrix.
- any other str: return the corresponding entry from .layers. E.g. "X_norm"

required

Returns:

Name Type Description
data ndarray

Dense matrix representation of the requested layer.

Source code in src/scpviz/utils/data.py
def get_adata_layer(adata: ad.AnnData, layer: str) -> np.ndarray:
    """
    Safely extract layer data as dense numpy array.

    This helper returns the requested layer as a dense `numpy.ndarray`,
    ensuring compatibility for downstream operations. Supports both
    `.X` and `.layers[...]`.

    Args:
        adata (anndata.AnnData): AnnData object containing data matrices.

        layer (str): Layer key.  
            - `"X"`: return the main data matrix.  
            - any other str: return the corresponding entry from `.layers`. E.g. "X_norm"

    Returns:
        data (numpy.ndarray): Dense matrix representation of the requested layer.
    """
    if layer == "X":
        data = adata.X
    elif layer in adata.layers:
        data = adata.layers[layer]
    else:
        raise ValueError(f"Layer '{layer}' not found in .layers and is not 'X'.")

    return data.toarray() if hasattr(data, 'toarray') else data

get_classlist

get_classlist(
    adata: AnnData,
    classes: str | list[str] | None = None,
    order: list[str] | None = None,
) -> list[str]

Retrieve unique class values for specified metadata columns. Useful for plot legends.

Unlike get_samplenames, which returns one identifier per row/sample, this function extracts the set of unique class values for grouping purposes (e.g., plotting categories). Supports optional reordering.

Parameters:

Name Type Description Default
adata AnnData

AnnData object containing sample metadata.

required
classes str or list of str

Column(s) in .obs to use.

  • None: combine all metadata columns up to the first _quant column.
  • str: return unique values from one column.
  • list of str: return unique combined values across multiple columns.
None
order list of str

Custom order of categories. Must exactly match the unique values; otherwise, a ValueError is raised.

None

Returns:

Name Type Description
class_list list of str

Unique class values in .obs, optionally reordered.

Raises:

Type Description
ValueError

If invalid columns are provided, or if order does not

Example

Get unique values from one metadata column:

classes = get_classlist(adata, classes="cell_type")

Combine two columns and return unique class labels:

classes = get_classlist(adata, classes=["cell_type", "treatment"])

Reorder categories explicitly:

classes = get_classlist(
    adata, classes="cell_type", order=["A", "B", "C"]
    )

Source code in src/scpviz/utils/data.py
def get_classlist(
    adata: ad.AnnData,
    classes: str | list[str] | None = None,
    order: list[str] | None = None,
) -> list[str]:
    """
    Retrieve unique class values for specified metadata columns. Useful 
    for plot legends.

    Unlike `get_samplenames`, which returns one identifier per row/sample,
    this function extracts the set of unique class values for grouping
    purposes (e.g., plotting categories). Supports optional reordering.

    Args:
        adata (anndata.AnnData): AnnData object containing sample metadata.

        classes (str or list of str, optional): Column(s) in `.obs` to use.

            - None: combine all metadata columns up to the first `_quant` column.  
            - str: return unique values from one column.  
            - list of str: return unique combined values across multiple columns.  

        order (list of str, optional): Custom order of categories. Must exactly
            match the unique values; otherwise, a `ValueError` is raised.

    Returns:
        class_list (list of str): Unique class values in `.obs`, optionally reordered.

    Raises:
        ValueError: If invalid columns are provided, or if `order` does not
        match the unique class list.

    Example:
        Get unique values from one metadata column:
            ```python
            classes = get_classlist(adata, classes="cell_type")
            ```

        Combine two columns and return unique class labels:
            ```python
            classes = get_classlist(adata, classes=["cell_type", "treatment"])
            ```

        Reorder categories explicitly:
            ```python
            classes = get_classlist(
                adata, classes="cell_type", order=["A", "B", "C"]
                )
            ```

    Related Functions:
        get_samplenames: Return per-sample names (not unique class values).
    """

    if classes is None:
        # combine all .obs columns per row into one string
        # NOTE: might break, should use better method to filter out file-related columns
        quant_col_index = adata.obs.columns.get_loc(next(col for col in adata.obs.columns if "_quant" in col))
        selected_columns = adata.obs.iloc[:, :quant_col_index]
        classes_list = selected_columns.apply(lambda x: "_".join(x.astype(str)), axis=1).unique()
        classes = selected_columns.columns.tolist()
    elif isinstance(classes, str):
        # check if classes is one of the columns of adata.obs
        if classes not in adata.obs.columns:
            raise ValueError(f"Invalid value for 'classes'. '{classes}' is not a column in adata.obs.")
        classes_list = adata.obs[classes].unique()
    elif isinstance(classes, list):
        # check if list has length 1
        if len(classes) == 1:
            classes_list = adata.obs[classes[0]].unique()
        # check if all classes are columns of adata.obs
        else:
            if not all([c in adata.obs.columns for c in classes]):
                raise ValueError(f"Invalid value for 'classes'. Not all elements in '{classes}' are columns in adata.obs.")
            classes_list = adata.obs[classes].apply(lambda x: "_".join(x.astype(str)), axis=1).unique()
    else:
        raise ValueError("Invalid value for 'classes'. Must be None, a string or a list of strings.")

    if isinstance(classes_list, str):
        classes_list = [classes_list]
    if isinstance(order, str):
        order = [order]

    if order is not None:
        # check if order list matches classes_list
        missing_elements = set(classes_list) - set(order)
        extra_elements = set(order) - set(classes_list)
        # Print missing and extra elements if any
        if missing_elements or extra_elements:
            if missing_elements:
                print(f"Missing elements in 'order': {missing_elements}")
            if extra_elements:
                print(f"Extra elements in 'order': {extra_elements}")
            raise ValueError("The 'order' list does not match 'classes_list'.")
        # if they match, then reorder classes_list to match order
        classes_list = order

    return classes_list

get_log_info

get_log_info(
    adata: Any, layer: str, layer_is_log: bool
) -> tuple[str, float, float]

Read the log base and pseudocount of a log-transformed layer from its provenance.

Walks adata.uns['layer_provenance'] from layer back to the log_transform record. Prints a warning when no record is found (defaults assumed) or when other operations were applied after log_transform (zero detection in :func:mask_zeros_as_nan may then miss values).

Parameters:

Name Type Description Default
adata AnnData

Data holding the layer provenance.

required
layer str

Layer name.

required
layer_is_log bool

Whether the layer is log-transformed.

required

Returns:

Name Type Description
tuple str

(base_label, log_base, pseudocount) where base_label is "2", "10"

float

or "e" ("2" when no record is found), log_base the numeric base and

float

pseudocount the log(x + pseudocount) offset (1.0 by default).

Source code in src/scpviz/utils/stats.py
def get_log_info(adata: Any, layer: str, layer_is_log: bool) -> tuple[str, float, float]:
    """
    Read the log base and pseudocount of a log-transformed layer from its provenance.

    Walks ``adata.uns['layer_provenance']`` from ``layer`` back to the ``log_transform`` record.
    Prints a warning when no record is found (defaults assumed) or when other operations were
    applied after ``log_transform`` (zero detection in :func:`mask_zeros_as_nan` may then miss values).

    Args:
        adata (anndata.AnnData): Data holding the layer provenance.
        layer (str): Layer name.
        layer_is_log (bool): Whether the layer is log-transformed.

    Returns:
        tuple: ``(base_label, log_base, pseudocount)`` where ``base_label`` is ``"2"``, ``"10"``
        or ``"e"`` (``"2"`` when no record is found), ``log_base`` the numeric base and
        ``pseudocount`` the ``log(x + pseudocount)`` offset (``1.0`` by default).
    """
    from scpviz.utils.data import resolve_input_layer

    rec: dict[str, Any] = {}
    ops_after_log: list[str] = []
    if layer_is_log:
        reg = adata.uns.get("layer_provenance", {})
        cur = resolve_input_layer(adata, layer)
        for _ in range(len(reg)):
            if cur not in reg:
                break
            if reg[cur].get("op") == "log_transform":
                rec = reg[cur]
                break
            ops_after_log.append(str(reg[cur].get("op", "?")))
            cur = reg[cur].get("input_layer", "")
        if not rec:
            print(
                f"{format_log_prefix('warn')} No log_transform record found for layer {layer!r}; "
                "assuming log base 2 and pseudocount 1 to detect zeros (missing values)."
            )
        elif ops_after_log:
            print(
                f"{format_log_prefix('warn')} Layer {layer!r} was modified after log_transform "
                f"({' -> '.join(reversed(ops_after_log))}); zeros (missing values) may no longer be "
                "detected exactly, so some missing values can be treated as real values."
            )
    base_label = str(rec.get("base", "2"))
    log_base = np.e if base_label == "e" else float(base_label)
    return base_label, log_base, rec.get("pseudocount", 1.0)

get_pca_importance

get_pca_importance(
    model: dict[str, Any] | PCA,
    initial_feature_names: list[str],
    n: int = 1,
) -> pd.DataFrame

Identify the most important features for each principal component.

This function ranks features by their absolute PCA loading values and extracts the top contributors for each principal component.

Parameters:

Name Type Description Default
model PCA or dict

Either a fitted PCA model from scikit-learn, or a dictionary with key "PCs" (array-like, shape: (n_components, n_features)).

required
initial_feature_names list of str

Names of the features, typically adata.var_names.

required
n int

Number of top features to return per principal component (default = 1).

1

Returns:

Name Type Description
df DataFrame

DataFrame with one row per principal component,

DataFrame

listing the top contributing features.

Example

Retrieve the top 5 features contributing to each PC:

from scpviz import utils as scutils
pdata.pca(n_components=5)
df = scutils.get_pca_importance(
    pdata.prot.uns['pca'],
    pdata.prot.var_names,
    n=5
)

Source code in src/scpviz/utils/stats.py
def get_pca_importance(
    model: dict[str, Any] | PCA,
    initial_feature_names: list[str],
    n: int = 1,
) -> pd.DataFrame:
    """
    Identify the most important features for each principal component.

    This function ranks features by their absolute PCA loading values and
    extracts the top contributors for each principal component.

    Args:
        model (sklearn.decomposition.PCA or dict): Either a fitted PCA model
            from scikit-learn, or a dictionary with key `"PCs"`
            (array-like, shape: `(n_components, n_features)`).
        initial_feature_names (list of str): Names of the features, typically
            `adata.var_names`.
        n (int): Number of top features to return per principal component
            (default = 1).

    Returns:
        df (pandas.DataFrame): DataFrame with one row per principal component,
        listing the top contributing features.

    Example:
        Retrieve the top 5 features contributing to each PC:
            ```python
            from scpviz import utils as scutils
            pdata.pca(n_components=5)
            df = scutils.get_pca_importance(
                pdata.prot.uns['pca'],
                pdata.prot.var_names,
                n=5
            )
            ```
    """

    if isinstance(model, dict):
        pcs = np.asarray(model["PCs"])  # shape: n_components x n_features
    else:
        pcs = np.asarray(model.components_)  # shape: n_components x n_features

    n_pcs = pcs.shape[0]

    most_important = [
        np.abs(pcs[i]).argsort()[-n:][::-1] for i in range(n_pcs)
    ]
    most_important_names = [
        [initial_feature_names[idx] for idx in row] for row in most_important
    ]

    result = {
        f"PC{i + 1}": most_important_names[i] for i in range(n_pcs)
    }
    df = pd.DataFrame(result.items(), columns=["Principal Component", "Top Features"])
    return df

get_pep_prot_mapping

get_pep_prot_mapping(
    pdata: pAnnData, return_series: Literal[False] = False
) -> str
get_pep_prot_mapping(
    pdata: pAnnData, return_series: Literal[True]
) -> pd.Series
get_pep_prot_mapping(
    pdata: pAnnData, return_series: bool = False
) -> str | pd.Series

Retrieve the peptide-to-protein mapping column or mapping values.

This function resolves the appropriate .pep.var column for peptide-to-protein mapping based on the data source recorded in pdata.metadata["source"].

Parameters:

Name Type Description Default
pdata pAnnData

The annotated proteomics object containing .metadata and .pep.

required
return_series bool

If True, return a pandas Series of peptide-to-protein mappings. If False (default), return the column name as a string.

False

Returns:

Name Type Description
col str

Column name in .pep.var containing peptide-to-protein mapping,

str | Series

if return_series=False.

mapping Series

Series mapping peptides to proteins,

str | Series

if return_series=True.

Raises:

Type Description
ValueError

If the data source is unrecognized or no valid mapping column is found.

Note

The mapping column depends on the import source:

  • Proteome Discoverer → "Master Protein Accessions"
  • DIA-NN, Spectronaut → "Protein.Group"
  • MaxQuant → "Leading razor protein"
Source code in src/scpviz/utils/data.py
def get_pep_prot_mapping(
    pdata: pAnnData, return_series: bool = False
) -> str | pd.Series:
    """
    Retrieve the peptide-to-protein mapping column or mapping values.

    This function resolves the appropriate `.pep.var` column for peptide-to-protein
    mapping based on the data source recorded in `pdata.metadata["source"]`.

    Args:
        pdata (pAnnData): The annotated proteomics object containing `.metadata` and `.pep`.
        return_series (bool): If True, return a pandas Series of peptide-to-protein
            mappings. If False (default), return the column name as a string.

    Returns:
        col (str): Column name in `.pep.var` containing peptide-to-protein mapping,
        if `return_series=False`.
        mapping (pandas.Series): Series mapping peptides to proteins,
        if `return_series=True`.

    Raises:
        ValueError: If the data source is unrecognized or no valid mapping column is found.

    Note:
        The mapping column depends on the import source:

        - Proteome Discoverer → `"Master Protein Accessions"`
        - DIA-NN, Spectronaut → `"Protein.Group"`
        - MaxQuant → `"Leading razor protein"`
    """
    source = pdata.metadata.get("source", "").lower()

    if source == "proteomediscoverer":
        col = "Master Protein Accessions"
    elif source in ("diann", "spectronaut"):
        col = "Protein.Group"
    elif source == "maxquant":
        col = "Leading razor protein"
    elif not source:
        raise ValueError(
            f"{format_log_prefix('error')} No import source recorded — cannot determine peptide-to-protein mapping. "
            "Set pdata.metadata['source'] = 'diann' | 'spectronaut' | 'proteomeDiscoverer' | 'maxquant'."
        )
    else:
        raise ValueError(
            f"{format_log_prefix('error')} Unknown data source '{source}' — cannot determine peptide-to-protein mapping. "
            "Set pdata.metadata['source'] = 'diann' | 'spectronaut' | 'proteomeDiscoverer' | 'maxquant'."
        )

    if return_series:
        return pdata.pep.var[col]

    return col

get_peptide_properties

get_peptide_properties(
    pdata: pAnnData,
    properties: Sequence[str] = DEFAULT_PEPTIDE_PROPERTIES,
    *,
    accessions: list[str] | None = None,
    sequence_from: str | None = None,
    force: bool = False,
    return_copy: bool = False,
    monoisotopic: bool = False,
    charge_at_pH: float | None = None,
    protein_scale: dict[str, float] | None = None,
    protein_scale_window: int = 9,
    protein_scale_edge: float = 1.0
) -> pd.DataFrame

Compute peptide biophysical properties for peptides in a pAnnData object.

Supports local metrics (e.g. length) and Biopython ProtParam metrics. By default annotates pdata.pep.var (return_copy=False) and returns a DataFrame. Scalar metrics are stored as columns; dict/list metrics (e.g. aa_counts, flexibility) are stored as object columns.

Parameters:

Name Type Description Default
pdata pAnnData

Object with .pep (and .rs when accessions is set).

required
properties list of str

Metric names to compute (local and/or Biopython; see Supported properties below). Default: gravy, molecular_weight, isoelectric_point.

DEFAULT_PEPTIDE_PROPERTIES
accessions list of str

Protein accessions or gene names. When None, all peptides are considered (requires force=True).

None
sequence_from str

.pep.var column for sequence resolution, or None to auto-detect.

None
force bool

Required when accessions=None to compute all peptides.

False
return_copy bool

If True, return the DataFrame without modifying pdata.pep.var.

False
monoisotopic bool

Use monoisotopic masses for molecular_weight.

False
charge_at_pH float

pH for charge_at_pH (required when requested).

None
protein_scale dict

Scale dict for protein_scale (required when requested).

None
protein_scale_window int

Window size for protein_scale only.

9
protein_scale_edge float

Edge weight for protein_scale only.

1.0

Returns:

Type Description
DataFrame

pd.DataFrame: Columns peptide_id, peptide_sequence, requested property columns, and accession when accessions is provided.

Raises:

Type Description
ValueError

If .pep is missing, or if accessions=None without force=True.

Example

Peptide sequences only (no pAnnData): clean, then compute:

from scpviz import utils as scutils

raw_peptides = [
    "[K].KPVVDCVVSVPCFYTDAER.[R]",
    "[R].MQHNLEQQIQAR.[N] MOD:1xOxidation [M1]",
]
cleaned = [scutils.strip_peptide_sequence(p) for p in raw_peptides]
props = scutils.compute_peptide_properties(cleaned)

Peptides for a selected protein accession list (also writes to .pep.var):

from scpviz import utils as scutils

protein_list = ["P34932", "Q9CZW5"]
df = scutils.get_peptide_properties(pdata, accessions=protein_list)
df.head()
#   accession  peptide_id  peptide_sequence  gravy  molecular_weight  isoelectric_point
# 0   P34932    ...         ...               ...    ...               ...

One accession or gene name:

df = scutils.get_peptide_properties(pdata, accessions=["HSPA4"])

Mix local and Biopython metrics:

df = scutils.get_peptide_properties(
    pdata,
    properties=["length", "gravy", "molecular_weight"],
    accessions=protein_list,
)

Return a DataFrame without annotating .pep.var:

df = scutils.get_peptide_properties(
    pdata, accessions=protein_list, return_copy=True
)

All peptides in the object (slow on large datasets; requires force=True):

df = scutils.get_peptide_properties(pdata, force=True)

Equivalent via the pAnnData method:

df = pdata.get_peptide_properties(accessions=protein_list)

Supported properties

Pass any mix of the names below in properties.

Local (scpviz, not Biopython):

  • length → length — amino-acid count of the cleaned sequence (len(sequence))

Biopython (Bio.SeqUtils.ProtParam.ProteinAnalysis method names). See Bio.SeqUtils.ProtParam:

Scalar (one .pep.var / DataFrame column each):

  • gravy → gravy
  • molecular_weight → molecular_weight
  • aromaticity → aromaticity
  • instability_index → instability_index
  • isoelectric_point → isoelectric_point
  • charge_at_pH → charge_at_pH_<pH> (requires charge_at_pH kwarg)

Tuple-valued (split into multiple columns):

  • secondary_structure_fraction → ss_helix, ss_turn, ss_sheet
  • molar_extinction_coefficient → extinction_reduced, extinction_oxidized

Dict- or list-valued (object columns):

  • count_amino_acids → aa_counts (dict)
  • get_amino_acids_percent → aa_percent (dict)
  • flexibility → flexibility (list)
  • protein_scale → protein_scale (list; requires protein_scale, protein_scale_window, and protein_scale_edge kwargs)

Default properties: gravy, molecular_weight, isoelectric_point.

Note

For peptide lists outside pAnnData, use strip_peptide_sequence then compute_peptide_properties (see first example above). With accessions=None and force=False, a warning is printed and computation is refused.

Source code in src/scpviz/utils/peptide_param.py
def get_peptide_properties(
    pdata: pAnnData,
    properties: Sequence[str] = DEFAULT_PEPTIDE_PROPERTIES,
    *,
    accessions: list[str] | None = None,
    sequence_from: str | None = None,
    force: bool = False,
    return_copy: bool = False,
    monoisotopic: bool = False,
    charge_at_pH: float | None = None,
    protein_scale: dict[str, float] | None = None,
    protein_scale_window: int = 9,
    protein_scale_edge: float = 1.0,
) -> pd.DataFrame:
    """
    Compute peptide biophysical properties for peptides in a ``pAnnData`` object.

    Supports local metrics (e.g. ``length``) and Biopython ProtParam metrics.
    By default annotates ``pdata.pep.var`` (``return_copy=False``) and returns a
    DataFrame. Scalar metrics are stored as columns; dict/list metrics (e.g.
    ``aa_counts``, ``flexibility``) are stored as object columns.

    Args:
        pdata (pAnnData): Object with ``.pep`` (and ``.rs`` when ``accessions`` is set).
        properties (list of str): Metric names to compute (local and/or Biopython; see
            Supported properties below). Default: ``gravy``, ``molecular_weight``,
            ``isoelectric_point``.
        accessions (list of str, optional): Protein accessions or gene names. When
            ``None``, all peptides are considered (requires ``force=True``).
        sequence_from (str, optional): ``.pep.var`` column for sequence resolution,
            or ``None`` to auto-detect.
        force (bool): Required when ``accessions=None`` to compute all peptides.
        return_copy (bool): If ``True``, return the DataFrame without modifying
            ``pdata.pep.var``.
        monoisotopic (bool): Use monoisotopic masses for ``molecular_weight``.
        charge_at_pH (float, optional): pH for ``charge_at_pH`` (required when requested).
        protein_scale (dict, optional): Scale dict for ``protein_scale`` (required when requested).
        protein_scale_window (int): Window size for ``protein_scale`` only.
        protein_scale_edge (float): Edge weight for ``protein_scale`` only.

    Returns:
        pd.DataFrame: Columns ``peptide_id``, ``peptide_sequence``, requested property
            columns, and ``accession`` when ``accessions`` is provided.

    Raises:
        ValueError: If ``.pep`` is missing, or if ``accessions=None`` without ``force=True``.

    Example:
        Peptide sequences only (no ``pAnnData``): clean, then compute:
            ```python
            from scpviz import utils as scutils

            raw_peptides = [
                "[K].KPVVDCVVSVPCFYTDAER.[R]",
                "[R].MQHNLEQQIQAR.[N] MOD:1xOxidation [M1]",
            ]
            cleaned = [scutils.strip_peptide_sequence(p) for p in raw_peptides]
            props = scutils.compute_peptide_properties(cleaned)
            ```

        Peptides for a selected protein accession list (also writes to ``.pep.var``):
            ```python
            from scpviz import utils as scutils

            protein_list = ["P34932", "Q9CZW5"]
            df = scutils.get_peptide_properties(pdata, accessions=protein_list)
            df.head()
            #   accession  peptide_id  peptide_sequence  gravy  molecular_weight  isoelectric_point
            # 0   P34932    ...         ...               ...    ...               ...
            ```

        One accession or gene name:
            ```python
            df = scutils.get_peptide_properties(pdata, accessions=["HSPA4"])
            ```

        Mix local and Biopython metrics:
            ```python
            df = scutils.get_peptide_properties(
                pdata,
                properties=["length", "gravy", "molecular_weight"],
                accessions=protein_list,
            )
            ```

        Return a DataFrame without annotating ``.pep.var``:
            ```python
            df = scutils.get_peptide_properties(
                pdata, accessions=protein_list, return_copy=True
            )
            ```

        All peptides in the object (slow on large datasets; requires ``force=True``):
            ```python
            df = scutils.get_peptide_properties(pdata, force=True)
            ```

        Equivalent via the ``pAnnData`` method:
            ```python
            df = pdata.get_peptide_properties(accessions=protein_list)
            ```

    !!! note "Supported ``properties``"
        Pass any mix of the names below in ``properties``.

        **Local (scpviz, not Biopython):**

        - ``length`` → ``length`` — amino-acid count of the cleaned sequence
          (``len(sequence)``)

        **Biopython** (``Bio.SeqUtils.ProtParam.ProteinAnalysis`` method names). See
        [Bio.SeqUtils.ProtParam](https://biopython.org/docs/latest/api/Bio.SeqUtils.ProtParam.html):

        Scalar (one ``.pep.var`` / DataFrame column each):

        - ``gravy`` → ``gravy``
        - ``molecular_weight`` → ``molecular_weight``
        - ``aromaticity`` → ``aromaticity``
        - ``instability_index`` → ``instability_index``
        - ``isoelectric_point`` → ``isoelectric_point``
        - ``charge_at_pH`` → ``charge_at_pH_<pH>`` (requires ``charge_at_pH`` kwarg)

        Tuple-valued (split into multiple columns):

        - ``secondary_structure_fraction`` → ``ss_helix``, ``ss_turn``, ``ss_sheet``
        - ``molar_extinction_coefficient`` → ``extinction_reduced``, ``extinction_oxidized``

        Dict- or list-valued (object columns):

        - ``count_amino_acids`` → ``aa_counts`` (dict)
        - ``get_amino_acids_percent`` → ``aa_percent`` (dict)
        - ``flexibility`` → ``flexibility`` (list)
        - ``protein_scale`` → ``protein_scale`` (list; requires ``protein_scale``,
          ``protein_scale_window``, and ``protein_scale_edge`` kwargs)

        Default ``properties``: ``gravy``, ``molecular_weight``, ``isoelectric_point``.

    Note:
        For peptide lists outside ``pAnnData``, use ``strip_peptide_sequence`` then
        ``compute_peptide_properties`` (see first example above). With
        ``accessions=None`` and ``force=False``, a warning is printed and computation
        is refused.

    Related Functions:
        - strip_peptide_sequence: Clean peptide strings without ``pdata``.
        - compute_peptide_properties: Sequence-only property computation.
        - get_peptides_for_accessions: Map accessions to observed peptide IDs.
        - pAnnData.get_peptide_properties: Same workflow as a method on ``pdata``.
    """
    if pdata.pep is None:
        raise ValueError("get_peptide_properties requires peptide data (.pep).")

    props = _validate_properties(properties)
    if "protein_scale" not in props:
        protein_scale = None

    n_peptides = pdata.pep.n_vars
    if accessions is None and not force:
        print(
            f"{format_log_prefix('warn')} get_peptide_properties: {n_peptides:,} peptides "
            "would be computed for the full dataset, which may take a long time. "
            "Pass accessions= [...] to restrict the calculation, or set force=True "
            "to compute properties for all peptides."
        )
        raise ValueError(
            f"Refusing to compute peptide properties for all {n_peptides:,} peptides "
            "without force=True."
        )

    base_rows = _build_peptide_rows(
        pdata, accessions=accessions, sequence_from=sequence_from
    )
    if base_rows.empty:
        return base_rows

    unique = (
        base_rows.drop_duplicates(subset=["peptide_id"])
        .set_index("peptide_id", drop=False)
        .sort_index(kind="mergesort")
    )
    prop_cols = _property_columns(props, charge_at_pH=charge_at_pH)

    computed = compute_peptide_properties(
        unique["peptide_sequence"].fillna("").tolist(),
        props,
        monoisotopic=monoisotopic,
        charge_at_pH=charge_at_pH,
        protein_scale=protein_scale,
        protein_scale_window=protein_scale_window,
        protein_scale_edge=protein_scale_edge,
    )
    computed.index = unique.index
    prop_by_peptide = computed

    out = base_rows.merge(
        prop_by_peptide,
        left_on="peptide_id",
        right_index=True,
        how="left",
    )
    col_order = ["peptide_id", "peptide_sequence"]
    if "accession" in out.columns:
        col_order = ["accession", *col_order]
    out = out[col_order + [c for c in prop_cols if c in out.columns]]

    if not return_copy:
        if pdata.pep.is_view:
            pdata.pep = pdata.pep.copy()
        pep_index = pdata.pep.var_names.astype(str)
        aligned = prop_by_peptide.reindex(pep_index)
        for col in aligned.columns:
            pdata.pep.var[col] = aligned[col].values

    return out.reset_index(drop=True)

get_protein_clusters

get_protein_clusters(
    pdata: pAnnData,
    on: str = "prot",
    layer: str = "X",
    t: int = 5,
    criterion: str = "maxclust",
) -> dict[Any, list[str]] | None

Retrieve hierarchical clusters of proteins from stored linkage.

This function uses linkage information stored in pdata.stats to partition proteins into clusters.

Parameters:

Name Type Description Default
pdata pAnnData

Input object containing .stats with clustering results.

required
on str

Data level to use, "prot" (default) or "pep".

'prot'
layer str

Data layer name used when the linkage was computed (default = "X").

'X'
t int or float

Number of clusters (if criterion="maxclust") or distance threshold for clustering.

5
criterion str

Clustering criterion passed to scipy.cluster.hierarchy.fcluster, e.g. "maxclust" or "distance".

'maxclust'

Returns:

Name Type Description
clusters dict

Mapping of cluster_id → list of proteins.

None dict[Any, list[str]] | None

If no linkage is found in pdata.stats.

Note

Requires that a clustermap has been previously computed and linkage stored under pdata.stats[f"{on}_{layer}_clustermap"].

Source code in src/scpviz/utils/stats.py
def get_protein_clusters(
    pdata: pAnnData,
    on: str = "prot",
    layer: str = "X",
    t: int = 5,
    criterion: str = "maxclust",
) -> dict[Any, list[str]] | None:
    """
    Retrieve hierarchical clusters of proteins from stored linkage.

    This function uses linkage information stored in `pdata.stats` to
    partition proteins into clusters.

    Args:
        pdata (pAnnData): Input object containing `.stats` with clustering results.
        on (str): Data level to use, `"prot"` (default) or `"pep"`.
        layer (str): Data layer name used when the linkage was computed (default = `"X"`).
        t (int or float): Number of clusters (if `criterion="maxclust"`) or distance
            threshold for clustering.
        criterion (str): Clustering criterion passed to `scipy.cluster.hierarchy.fcluster`,
            e.g. `"maxclust"` or `"distance"`.

    Returns:
        clusters (dict): Mapping of `cluster_id → list of proteins`.
        None: If no linkage is found in `pdata.stats`.

    Note:
        Requires that a clustermap has been previously computed and linkage
        stored under `pdata.stats[f"{on}_{layer}_clustermap"]`.

    Related Functions:
        - plot_clustermap: Generates clustered heatmaps and stores linkage.
    """
    from scipy.cluster.hierarchy import fcluster

    key = f"{on}_{layer}_clustermap"
    stats = pdata.stats.get(key)
    if not stats or "row_linkage" not in stats:
        print(f"No linkage found for {key} in pdata.stats.")
        return None

    linkage = stats["row_linkage"]
    labels = fcluster(linkage, t=t, criterion=criterion)
    order = stats["row_order"]

    from collections import defaultdict
    clusters = defaultdict(list)
    for label, prot in zip(labels, order):
        clusters[label].append(prot)

    return dict(clusters)

get_samplenames

get_samplenames(
    adata: AnnData, classes: str | list[str] | None
) -> list[str] | None

Retrieve sample names for specified class values.

This function resolves .obs metadata into sample-level identifiers (one name per row). It is typically used for plotting functions where sample names are required for labeling or grouping.

Parameters:

Name Type Description Default
adata AnnData

AnnData object containing sample metadata.

required
classes str or list of str

Column(s) in .obs used to build sample names.

  • str: return vlaues from a single column.
  • list of str: combine multiple columns per row with ", ".
required

Returns:

Name Type Description
sample_names list of str

Sample names dervied from .obs.

Raises:

Type Description
KeyError

If any requested classes column is not in adata.obs.

ValueError

If classes is not None, a string, or a list of strings.

Example

Get sample names from a single metadata column:

samples = get_samplenames(adata, "cell_type")

Combine multiple columns into sample identifiers:

samples = get_samplenames(adata, ["cell_type", "treatment"])

Source code in src/scpviz/utils/data.py
def get_samplenames(
    adata: ad.AnnData, classes: str | list[str] | None
) -> list[str] | None:
    """
    Retrieve sample names for specified class values.

    This function resolves `.obs` metadata into sample-level identifiers
    (one name per row). It is typically used for plotting functions where
    sample names are required for labeling or grouping.

    Args:
        adata (anndata.AnnData): AnnData object containing sample metadata.

        classes (str or list of str): Column(s) in `.obs` used to build sample names.

            - str: return vlaues from a single column.
            - list of str: combine multiple columns per row with `", "`.

    Returns:
        sample_names (list of str): Sample names dervied from `.obs`.

    Raises:
        KeyError: If any requested `classes` column is not in `adata.obs`.
        ValueError: If `classes` is not None, a string, or a list of strings.

    Example:
        Get sample names from a single metadata column:
            ```python
            samples = get_samplenames(adata, "cell_type")
            ```

        Combine multiple columns into sample identifiers:
            ```python
            samples = get_samplenames(adata, ["cell_type", "treatment"])
            ```

    Related Functions:
        get_classlist: Return unique class values (not per-sample names).
    """
    if classes is None:
        return None
    cols = [classes] if isinstance(classes, str) else classes
    if isinstance(cols, list):
        missing = [c for c in cols if c not in adata.obs.columns]
        if missing:
            raise KeyError(
                f"{format_log_prefix('error', indent=1)} classes column(s) {missing} not found in .obs. "
                f"Available columns: {list(adata.obs.columns)}"
            )
    if isinstance(classes, str):
        return adata.obs[classes].values.tolist()
    elif isinstance(classes, list):
        return adata.obs[classes].apply(lambda row: ', '.join(row.values.astype(str)), axis=1).values.tolist()
    else:
        raise ValueError("Invalid input for 'classes'. It should be None, a string, or a list of strings.")

get_string_mappings

get_string_mappings(
    identifiers: list[str],
    use_uniprot: bool = True,
    use_string: bool = True,
    caller_identity: str = "scpviz",
    batch_size: int = 100,
    debug: bool = False,
) -> pd.DataFrame

Resolve STRING identifiers for a list of UniProt accessions.

This function maps UniProt protein accessions to STRING IDs using a two-step strategy:

  1. UniProt lookup – retrieves STRING cross-references (xref_string) and organism IDs via the UniProt API (fast).
  2. STRING API lookup – queries the STRING get_string_ids endpoint for any identifiers not resolved via UniProt.

Parameters:

Name Type Description Default
identifiers list of str

List of UniProt accession IDs to map.

required
use_uniprot bool

If True (default), attempt mapping via UniProt xref_string and organism_id fields.

True
use_string bool

If True (default), query the STRING API for any identifiers still unresolved after the UniProt step.

True
caller_identity str

Identifier passed to the STRING API (default: "scpviz").

'scpviz'
batch_size int

Number of identifiers per batch when querying external APIs (default=100).

100
debug bool

If True, print progress and debug information.

False

Returns:

Type Description
DataFrame

pandas.DataFrame: Mapping table with one row per input identifier and

DataFrame

the following columns:

DataFrame
  • input_identifier: UniProt accession provided as input
DataFrame
  • string_identifier: Corresponding STRING ID (if resolved)
DataFrame
  • ncbi_taxon_id: NCBI taxonomy ID inferred from UniProt or STRING
Example

Map a small set of UniProt accessions to STRING IDs:

proteins = ["P40925", "P40926"]
df = get_string_mappings(proteins)
df

Disable the UniProt shortcut and query STRING directly (takes longer than UniProt):

df = get_string_mappings(proteins, use_uniprot=False)

Source code in src/scpviz/utils/id_maps.py
def get_string_mappings(
    identifiers: list[str],
    use_uniprot: bool = True,
    use_string: bool = True,
    caller_identity: str = "scpviz",
    batch_size: int = 100,
    debug: bool = False,
) -> pd.DataFrame:
    """
    Resolve STRING identifiers for a list of UniProt accessions.

    This function maps UniProt protein accessions to STRING IDs using a
    two-step strategy:

    1. **UniProt lookup** – retrieves STRING cross-references (`xref_string`)
       and organism IDs via the UniProt API (fast).
    2. **STRING API lookup** – queries the STRING `get_string_ids` endpoint
       for any identifiers not resolved via UniProt.

    Args:
        identifiers (list of str): List of UniProt accession IDs to map.
        use_uniprot (bool): If True (default), attempt mapping via UniProt
            `xref_string` and `organism_id` fields.
        use_string (bool): If True (default), query the STRING API for any
            identifiers still unresolved after the UniProt step.
        caller_identity (str): Identifier passed to the STRING API
            (default: "scpviz").
        batch_size (int): Number of identifiers per batch when querying
            external APIs (default=100).
        debug (bool): If True, print progress and debug information.

    Returns:
        pandas.DataFrame: Mapping table with one row per input identifier and
        the following columns:

        - `input_identifier`: UniProt accession provided as input  
        - `string_identifier`: Corresponding STRING ID (if resolved)  
        - `ncbi_taxon_id`: NCBI taxonomy ID inferred from UniProt or STRING  

    Example:
        Map a small set of UniProt accessions to STRING IDs:
            ```python
            proteins = ["P40925", "P40926"]
            df = get_string_mappings(proteins)
            df
            ```

        Disable the UniProt shortcut and query STRING directly (takes longer than UniProt):
            ```python
            df = get_string_mappings(proteins, use_uniprot=False)
            ```

    Related Functions:
        - get_uniprot_fields: Retrieve UniProt metadata, including STRING cross-references.
        - pAnnData.EnrichmentMixin (enrichment_functional(), enrichment_ppi())
    """
    import scpviz.utils as _u

    ids = [str(x).strip() for x in identifiers if x is not None and str(x).strip()]
    if not ids:
        return pd.DataFrame(columns=["input_identifier", "string_identifier", "ncbi_taxon_id"])

    found: Dict[str, str] = {}
    species_map: Dict[str, object] = {}

    # Step 1: UniProt xref_string
    uni_df = pd.DataFrame(columns=["input_identifier", "string_identifier"])
    if use_uniprot:
        try:
            uni_df, uni_species = _u._uniprot_get_string_ids(
                ids, batch_size=batch_size, standardize=True, debug=debug
            )
            if not uni_df.empty:
                found.update(dict(zip(uni_df["input_identifier"], uni_df["string_identifier"])))
            species_map.update(uni_species)

            print(f"{format_log_prefix('api',2)} UniProt mapped: {len(uni_df)} / {len(ids)}")
        except Exception as e:
            print(f"{format_log_prefix('error')} UniProt stream step failed: {e}") 

    # Missing after UniProt
    missing = [i for i in ids if i not in found]

    # Step 2: STRING get_string_ids
    string_df = pd.DataFrame(columns=["input_identifier", "string_identifier", "ncbi_taxon_id"])
    if use_string and missing:
        try:
            string_df = _u._string_get_string_ids(
                missing, batch_size=batch_size, caller_identity=caller_identity, debug=debug
            )
            if not string_df.empty:
                found.update(dict(zip(string_df["input_identifier"], string_df["string_identifier"])))

            print(f"{format_log_prefix('api',2)} STRING mapped: {len(string_df)} / {len(missing)} (missing after UniProt)")
        except Exception as e:
            print(f"{format_log_prefix('error')} STRING stream step failed: {e}") 

    # Build output table
    out_df = pd.DataFrame({"input_identifier": ids})
    out_df["string_identifier"] = out_df["input_identifier"].map(found)

    # Taxon: prefer UniProt organism_id, then STRING ncbi_taxon_id
    tax_from_uniprot = out_df["input_identifier"].map(lambda a: species_map.get(a, pd.NA))
    tax_from_uniprot = tax_from_uniprot.apply(scalarize_taxon)

    if not string_df.empty and "ncbi_taxon_id" in string_df.columns:
        string_tax_map = dict(zip(string_df["input_identifier"], string_df["ncbi_taxon_id"]))
        tax_from_string = out_df["input_identifier"].map(lambda a: string_tax_map.get(a, pd.NA))
        tax_from_string = tax_from_string.apply(scalarize_taxon)
    else:
        tax_from_string = pd.Series([pd.NA] * len(out_df), index=out_df.index)

    out_df["ncbi_taxon_id"] = tax_from_uniprot.combine_first(tax_from_string)

    return out_df

get_uniprot_fields

get_uniprot_fields(
    prot_list: list[str],
    search_fields: list[str] | None = None,
    batch_size: int = 100,
    verbose: bool = True,
    standardize: bool = True,
    worker_verbose: bool = False,
) -> pd.DataFrame

Retrieve UniProt metadata for a list of protein accessions.

This function wraps get_uniprot_fields_worker to handle batching of protein IDs, returning results as a single DataFrame.

Parameters:

Name Type Description Default
prot_list list of str

Protein accessions and/or UniProt entry names.

required
search_fields list of str

UniProt fields to return. Defaults include accession, gene names, GO terms, and STRING IDs.

None
batch_size int

Number of accessions per batch (default=100, the UniProt /uniprotkb/accessions GET limit).

100
verbose bool

If True, print progress messages.

True
standardize bool

If True (default), normalize UniProt column names to canonical lowercase keys (e.g., "gene_primary", "organism_id", "xref_string") for consistent downstream processing.

True

Returns:

Name Type Description
df DataFrame

DataFrame containing UniProt metadata for the input proteins.

Example

Query UniProt for a small set of proteins:

proteins = ["P40925", "P40926"]
df = get_uniprot_fields(proteins)
df[["Entry", "Gene Names", "Organism Id"]].head()

Retrieve raw UniProt field names without renaming: >>> df_raw = get_uniprot_fields(proteins, standardize=False)

Source code in src/scpviz/utils/id_maps.py
def get_uniprot_fields(
    prot_list: list[str],
    search_fields: list[str] | None = None,
    batch_size: int = 100,
    verbose: bool = True,
    standardize: bool = True,
    worker_verbose: bool = False,
) -> pd.DataFrame:
    """
    Retrieve UniProt metadata for a list of protein accessions.

    This function wraps `get_uniprot_fields_worker` to handle batching of
    protein IDs, returning results as a single DataFrame.

    Args:
        prot_list (list of str): Protein accessions and/or UniProt entry names.
        search_fields (list of str): UniProt fields to return.
            Defaults include accession, gene names, GO terms, and STRING IDs.
        batch_size (int): Number of accessions per batch (default=100, the
            UniProt ``/uniprotkb/accessions`` GET limit).
        verbose (bool): If True, print progress messages.
        standardize (bool): If True (default), normalize UniProt column names
            to canonical lowercase keys (e.g., "gene_primary", "organism_id",
            "xref_string") for consistent downstream processing.

    Returns:
        df (pandas.DataFrame): DataFrame containing UniProt metadata for the input proteins.

    Example:
        Query UniProt for a small set of proteins:
            ```python
            proteins = ["P40925", "P40926"]
            df = get_uniprot_fields(proteins)
            df[["Entry", "Gene Names", "Organism Id"]].head()
            ```

        Retrieve raw UniProt field names without renaming:
            >>> df_raw = get_uniprot_fields(proteins, standardize=False)

    Related Functions:
        - get_uniprot_fields_worker: Worker function that handles low-level UniProt API queries.
        - standardize_uniprot_columns: Helper used internally for column normalization.
    """
    import scpviz.utils as _u

    if search_fields is None:
        search_fields = [
            "accession",
            "id",
            "protein_name",
            "gene_primary",
            "gene_names",
            "organism_id",
            "go",
            "go_f",
            "go_c",
            "go_p",
            "cc_interaction",
            "xref_string",
        ]

    # --- Ensure 'accession' field comes first (UniProt requirement)
    search_fields = ["accession"] + [f for f in search_fields if f != "accession"]

    # --- Split IDs into batches
    batches = [prot_list[i:i + batch_size] for i in range(0, len(prot_list), batch_size)]
    all_results = []
    n_batches = len(batches)

    def _fetch_batch(batch, label: str):
        if verbose:
            print(
                f"{format_log_prefix('api', indent=2)} Querying UniProt for batch {label}/{n_batches} "
                f"({len(batch)} proteins) [fields: {', '.join(search_fields)}]"
            )
            if n_batches > 1:
                print(f"{format_log_prefix('info_only', indent=3)} Processing batch {label}/{n_batches}...")

        try:
            batch_df = get_uniprot_fields_worker(batch, search_fields, verbose=worker_verbose)
            if standardize:
                batch_df = _u.standardize_uniprot_columns(batch_df)
            all_results.append(batch_df)
        except requests.exceptions.RequestException as e:
            if len(batch) > 1:
                mid = max(1, len(batch) // 2)
                print(
                    f"{format_log_prefix('warn')} Failed batch {label} ({len(batch)} proteins): {e}. "
                    f"Retrying as two smaller batches."
                )
                _fetch_batch(batch[:mid], f"{label}a")
                _fetch_batch(batch[mid:], f"{label}b")
                return
            print(f"{format_log_prefix('warn')} Failed batch {label}: {e}")
        except Exception as e:
            print(f"{format_log_prefix('warn')} Failed batch {label}: {e}")

    for i, batch in enumerate(batches, start=1):
        _fetch_batch(batch, str(i))

    if not all_results:
        if verbose:
            print(f"{format_log_prefix('warn')} No results retrieved from UniProt.")
        return pd.DataFrame()

    full_method_df = pd.concat(all_results, ignore_index=True)
    if verbose:
        print(f"{format_log_prefix('result_only', 2)} Retrieved UniProt metadata for {len(full_method_df)} entries.")

    return full_method_df

get_uniprot_fields_worker

get_uniprot_fields_worker(
    prot_list: list[str],
    search_fields: list[str] | None = None,
    verbose: bool = False,
) -> pd.DataFrame

Query UniProt for a batch of protein accessions.

Accession-shaped IDs use /uniprotkb/accessions (max 100 per request, chunked internally). UniProt entry names (e.g. MDHC_HUMAN) and other non-accession IDs use /uniprotkb/stream with id: queries. Isoform accessions, missing-ID fallbacks, and UniProt ID redirects are handled automatically.

Parameters:

Name Type Description Default
prot_list list of str

Protein accessions and/or UniProt entry names.

required
search_fields list of str

UniProt return fields. See: https://www.uniprot.org/help/return_fields

None
verbose bool

If True, print progress messages and missing accessions.

False

Returns:

Name Type Description
df DataFrame

DataFrame containing UniProt metadata for the input proteins.

Raises:

Type Description
ValueError

If query_type is unknown or the data source cannot be resolved.

Info

  • This function is intended as a worker and is usually called by get_uniprot_fields.
  • It automatically resolves canonical vs. isoform accessions and will attempt UniProt ID mapping if some accessions cannot be found.
Source code in src/scpviz/utils/id_maps.py
def get_uniprot_fields_worker(
    prot_list: list[str],
    search_fields: list[str] | None = None,
    verbose: bool = False,
) -> pd.DataFrame:
    """
    Query UniProt for a batch of protein accessions.

    Accession-shaped IDs use ``/uniprotkb/accessions`` (max 100 per request,
    chunked internally). UniProt entry names (e.g. ``MDHC_HUMAN``) and other
    non-accession IDs use ``/uniprotkb/stream`` with ``id:`` queries. Isoform
    accessions, missing-ID fallbacks, and UniProt ID redirects are handled
    automatically.

    Args:
        prot_list (list of str): Protein accessions and/or UniProt entry names.
        search_fields (list of str): UniProt return fields.
            See: https://www.uniprot.org/help/return_fields
        verbose (bool): If True, print progress messages and missing accessions.

    Returns:
        df (pandas.DataFrame): DataFrame containing UniProt metadata for the input proteins.

    Raises:
        ValueError: If `query_type` is unknown or the data source cannot be resolved.

    !!! info
        - This function is intended as a **worker** and is usually called by
          `get_uniprot_fields`.
        - It automatically resolves canonical vs. isoform accessions and will
          attempt UniProt ID mapping if some accessions cannot be found.

    Related Functions:
        - get_uniprot_fields: High-level batch UniProt query wrapper.
    """

    if search_fields is None:
        raise ValueError("search_fields is required for UniProt queries.")
    fields_param = ",".join(search_fields)
    format_type = "tsv"

    def _get_tsv(url: str, params: dict, n_ids: int) -> pd.DataFrame:
        results = requests.get(url, params=params, timeout=_UNIPROT_TIMEOUT)
        results.raise_for_status()
        if not results.text.strip():
            if verbose:
                print(
                    f"{format_log_prefix('warn_only', 2)} "
                    f"UniProt returned empty response for {n_ids} proteins."
                )
            return pd.DataFrame()
        return pd.read_csv(io.StringIO(results.text), sep="\t")

    def _concat_frames(frames: list[pd.DataFrame]) -> pd.DataFrame:
        frames = [f for f in frames if f is not None and not f.empty]
        if not frames:
            return pd.DataFrame()
        return pd.concat(frames, ignore_index=True)

    def query_uniprot_batch(ids, query_type="accession"):
        if not ids:
            return pd.DataFrame()

        if query_type == "accession":
            valid = [i for i in ids if _UNIPROT_ACC_RE.match(str(i).strip())]
            frames = []
            for start in range(0, len(valid), _UNIPROT_ACCESSIONS_MAX):
                chunk = valid[start:start + _UNIPROT_ACCESSIONS_MAX]
                if verbose:
                    print(f"Querying UniProt (accessions, TSV mode) for {len(chunk)} proteins")
                frames.append(_get_tsv(
                    _UNIPROT_ACCESSIONS_URL,
                    {
                        "accessions": ",".join(chunk),
                        "format": format_type,
                        "fields": fields_param,
                    },
                    len(chunk),
                ))
            return _concat_frames(frames)

        if query_type == "id":
            frames = []
            for start in range(0, len(ids), _UNIPROT_STREAM_QUERY_MAX):
                chunk = ids[start:start + _UNIPROT_STREAM_QUERY_MAX]
                query = "(" + " OR ".join(f"(id:{x})" for x in chunk) + ")"
                if verbose:
                    print(f"Querying UniProt (id, TSV mode) for {len(chunk)} proteins")
                frames.append(_get_tsv(
                    _UNIPROT_STREAM_URL,
                    {
                        "query": query,
                        "format": format_type,
                        "fields": fields_param,
                    },
                    len(chunk),
                ))
            return _concat_frames(frames)

        raise ValueError(f"Unknown query_type: {query_type}")

    if verbose:
        print(f"{format_log_prefix('API', 1)} Querying UniProt for {len(prot_list)} total proteins [TSV mode].")

    def resolve_uniprot_redirects(accessions, from_db='UniProtKB_AC-ID', to_db='UniProtKB'):
        data = {'from': from_db, 'to': to_db, 'ids': ','.join(accessions)}

        res = requests.post(_UNIPROT_IDMAPPING_RUN_URL, data=data, timeout=_UNIPROT_TIMEOUT)
        res.raise_for_status()
        job_id = res.json()['jobId']

        # Poll until job is complete
        while True:
            status = requests.get(
                f"https://rest.uniprot.org/idmapping/status/{job_id}",
                timeout=_UNIPROT_TIMEOUT,
            ).json()
            if status.get("jobStatus") == "RUNNING":
                time.sleep(1)
            else:
                break

        # Get results
        results = requests.get(
            f"https://rest.uniprot.org/idmapping/uniprotkb/results/{job_id}",
            timeout=_UNIPROT_TIMEOUT,
        ).json()
        mapping = {item['from']: item['to']['primaryAccession'] for item in results.get('results', [])}
        return mapping

    # Accession-shaped IDs go to /accessions; everything else is an entry-name lookup.
    acc_like = [i for i in prot_list if _UNIPROT_ACC_RE.match(str(i).strip())]
    other_ids = [i for i in prot_list if i not in acc_like]
    isoform_ids = [acc for acc in acc_like if "-" in acc]
    canonical_ids = [acc for acc in acc_like if "-" not in acc]

    df_canonical = query_uniprot_batch(canonical_ids, query_type="accession")
    df_isoform = query_uniprot_batch(isoform_ids, query_type="accession")

    found_isoform_ids = set(df_isoform["Entry"]) if not df_isoform.empty and "Entry" in df_isoform.columns else set()
    missing_isoforms = [acc for acc in isoform_ids if acc not in found_isoform_ids]
    if missing_isoforms and verbose:
        print(f"{format_log_prefix('info_only', 3)} Attempting fallback query for {len(missing_isoforms)} isoform base IDs")
    fallback_ids = list({acc.split("-")[0] for acc in missing_isoforms})
    df_fallback = query_uniprot_batch(fallback_ids, query_type="accession")

    df_entry_names = query_uniprot_batch(other_ids, query_type="id")
    if not df_entry_names.empty and "Entry Name" in df_entry_names.columns:
        df_entry_names = df_entry_names.copy()
        queried_names = {str(x) for x in other_ids}
        mask = df_entry_names["Entry Name"].astype(str).isin(queried_names)
        df_entry_names.loc[mask, "Entry"] = df_entry_names.loc[mask, "Entry Name"]

    df = _concat_frames([df_canonical, df_isoform, df_fallback, df_entry_names])

    found_entries = set(df["Entry"].dropna().astype(str)) if "Entry" in df.columns else set()
    still_missing = set(prot_list) - found_entries

    if still_missing:
        if verbose:
            print(f"{format_log_prefix('info_only', 3)} Attempting UniProt ID redirect for {len(still_missing)} unresolved accessions.")
        redirect_map = resolve_uniprot_redirects(list(still_missing))
        if redirect_map:
            redirected_ids = list(redirect_map.values())
            df_redirected = query_uniprot_batch(redirected_ids, query_type="accession")

            # Remap back to original accession
            inv_map = {v: k for k, v in redirect_map.items()}
            if 'Entry' in df_redirected.columns:
                df_redirected['Entry'] = df_redirected['Entry'].apply(lambda x: inv_map.get(x, x))

            df = _concat_frames([df, df_redirected])

            resolved = set(redirect_map.keys())
            still_missing -= resolved

    # Step 5: Fill in placeholders for totally missing accessions
    if still_missing:
        print(f"{format_log_prefix('warn_only', 3)} Proteins not found in UniProt: {list(still_missing)[:5]}") if verbose else None
        missing_df = pd.DataFrame({"Entry": list(still_missing)})
        for col in df.columns:
            if col != "Entry":
                missing_df[col] = np.nan
        df = _concat_frames([df, missing_df])

    if 'STRING' in df.columns:
        # keep first STRING ID (or join all if you prefer)
        df['xref_string'] = df['STRING'].apply(
            lambda s: str(s).split(';')[0].strip() if pd.notna(s) and str(s).strip() else np.nan
        )
        df.drop(columns=['STRING'], inplace=True)

    return df

get_upset_contents

get_upset_contents(
    pdata: pAnnData,
    classes: str | list[str],
    on: str = "protein",
    upsetForm: bool = True,
    debug: bool = False,
) -> pd.DataFrame | dict[str, list[str]]

Construct contents for an UpSet plot from a pAnnData object.

This function extracts feature sets (proteins or peptides) present in specified sample classes and returns them either as a dictionary or in an upsetplot-compatible format.

Parameters:

Name Type Description Default
pdata pAnnData

The pAnnData object containing .prot and .pep.

required
classes str or list of str

Metadata column(s) in .obs to define sample groups. Example: "cell_type", or ["cell_type", "treatment"].

required
on str

Data level to use. Options are "protein" (default) or "peptide".

'protein'
upsetForm bool

If True, return an UpSet-compatible DataFrame via upsetplot.from_contents. If False, return a raw dictionary.

True
debug bool

If True, print filtering steps and class resolution details.

False

Returns:

Name Type Description
upset_data DataFrame

Binary presence/absence DataFrame for use with upsetplot.UpSet, if upsetForm=True.

upset_dict dict

Mapping of class → list of present features, if upsetForm=False.

Raises:

Type Description
ValueError

If on is not "protein" or "peptide".

Example

Get contents for an UpSet plot of sample classes:

upset_data = get_upset_contents(pdata, classes="treatment")
from upsetplot import UpSet
UpSet(upset_data, subset_size="count").plot()

Retrieve raw dictionary of sets instead:

upset_dict = get_upset_contents(pdata, classes="treatment", upsetForm=False)

Query proteins from a set and highlight them in a plot:

upset_data = scutils.get_upset_contents(pdata, classes="condition")
prot_df = scutils.get_upset_query(
    upset_data, present=["treated"], absent=["control"], fetch_uniprot=False, pdata=pdata
)
scplt.plot_rankquant(ax, pdata, classes="condition", cmap=cmaps, color=colors)
scplt.mark_rankquant(ax, pdata, mark_df=prot_df, class_values=["treated"], color="black")

Source code in src/scpviz/utils/class_filter.py
def get_upset_contents(
    pdata: pAnnData,
    classes: str | list[str],
    on: str = "protein",
    upsetForm: bool = True,
    debug: bool = False,
) -> pd.DataFrame | dict[str, list[str]]:
    """
    Construct contents for an UpSet plot from a pAnnData object.

    This function extracts feature sets (proteins or peptides) present in
    specified sample classes and returns them either as a dictionary or
    in an `upsetplot`-compatible format.

    Args:
        pdata (pAnnData): The pAnnData object containing `.prot` and `.pep`.
        classes (str or list of str): Metadata column(s) in `.obs` to define sample groups.
            Example: `"cell_type"`, or `["cell_type", "treatment"]`.
        on (str): Data level to use. Options are `"protein"` (default) or `"peptide"`.
        upsetForm (bool): If True, return an `UpSet`-compatible DataFrame via
            `upsetplot.from_contents`. If False, return a raw dictionary.
        debug (bool): If True, print filtering steps and class resolution details.

    Returns:
        upset_data (pandas.DataFrame): Binary presence/absence DataFrame for use with
            `upsetplot.UpSet`, if `upsetForm=True`.
        upset_dict (dict): Mapping of class → list of present features,
            if `upsetForm=False`.

    Raises:
        ValueError: If `on` is not `"protein"` or `"peptide"`.

    Example:
        Get contents for an UpSet plot of sample classes:
            ```python
            upset_data = get_upset_contents(pdata, classes="treatment")
            from upsetplot import UpSet
            UpSet(upset_data, subset_size="count").plot()
            ```

        Retrieve raw dictionary of sets instead:
            ```python
            upset_dict = get_upset_contents(pdata, classes="treatment", upsetForm=False)
            ```

        Query proteins from a set and highlight them in a plot:
            ```python
            upset_data = scutils.get_upset_contents(pdata, classes="condition")
            prot_df = scutils.get_upset_query(
                upset_data, present=["treated"], absent=["control"], fetch_uniprot=False, pdata=pdata
            )
            scplt.plot_rankquant(ax, pdata, classes="condition", cmap=cmaps, color=colors)
            scplt.mark_rankquant(ax, pdata, mark_df=prot_df, class_values=["treated"], color="black")
            ```

    Related Functions:
        - plot_upset: Plot UpSet diagrams directly.
        - plot_venn: Plot Venn diagrams for up to 3 sets.
        - get_upset_query: Query an intersection and build a mark_df for highlighting.
    """
    import scpviz.utils as _u

    if on == 'protein':
        adata = pdata.prot
    elif on == 'peptide':
        adata = pdata.pep
    else:
        raise ValueError("Invalid value for 'on'. Options are 'protein' or 'peptide'.")

    # Common error: if classes is a list with only one element, unpack it
    if isinstance(classes, list) and len(classes) == 1:
        classes = classes[0]

    classes_list = _u.get_classlist(adata, classes)
    upset_dict = {}

    for j, class_value in enumerate(classes_list):
        data_filtered = _u.resolve_class_filter(adata, classes, class_value, debug=debug)

        # get proteins that are present in the filtered data (at least one value is not NaN, not 0)
        X = data_filtered.X.toarray()
        mask_present = (~np.isnan(X)) & (X != 0)
        prot_present = data_filtered.var_names[mask_present.sum(axis=0) > 0]
        upset_dict[class_value] = prot_present.tolist()

    if upsetForm:
        upset_data = _u.upsetplot.from_contents(upset_dict)
        return upset_data

    else:
        return upset_dict

get_upset_query

get_upset_query(
    upset_content: DataFrame,
    present: list[str],
    absent: list[str],
    *,
    fetch_uniprot: bool,
    pdata: pAnnData | None = None,
    on: str = "protein",
    verbose: bool = True
) -> pd.DataFrame

Query features from UpSet contents given inclusion and exclusion criteria.

This function extracts the set of features (proteins or peptides) that are present in all specified groups and absent in others. You decide whether to query UniProt metadata for the resulting accessions via fetch_uniprot (set False for large intersections to avoid slow API calls). The result is returned as a mark_df-compatible DataFrame for use with mark_rankquant or mark_raincloud.

Parameters:

Name Type Description Default
upset_content DataFrame

Output from get_upset_contents with upsetForm=True (the default).

required
present list of str

List of groups in which the features must be present.

required
absent list of str

List of groups in which the features must be absent.

required
fetch_uniprot bool

If True, query UniProt for full metadata via get_uniprot_fields. If False, build a lightweight DataFrame from pdata using accessions and any available gene names in .var (recommended for large intersections, e.g. 1000+ proteins).

required
pdata pAnnData

Required when fetch_uniprot=False so gene names can be read from .var. Ignored when fetch_uniprot=True.

None
on str

Data level for gene lookup when fetch_uniprot=False. Options are "protein" (default) or "peptide".

'protein'
verbose bool

If True (default), print a progress header and branch messages. Passed through to get_uniprot_fields when fetch_uniprot=True.

True

Returns:

Name Type Description
prot_query_df DataFrame

Features matching the query. When

DataFrame

fetch_uniprot=True, includes UniProt metadata. When

DataFrame

fetch_uniprot=False, includes at least an accession column and,

DataFrame

when available, gene_primary.

Example

Small intersection - Query proteins unique to one group and highlight them in a plot, also fetches UniProt metadata for gene labels:

upset_data = scutils.get_upset_contents(pdata, classes="condition")
prot_df = scutils.get_upset_query(
    upset_data, present=["treated"], absent=["control"], fetch_uniprot=True
)
scplt.plot_rankquant(ax, pdata, classes="condition", cmap=cmaps, color=colors)
scplt.mark_rankquant(ax, pdata, mark_df=prot_df, class_values=["treated"], color="black")

Large intersection - skip UniProt and use local gene names:

upset_data = scutils.get_upset_contents(pdata, classes="condition")
prot_df = scutils.get_upset_query(
    upset_data, present=["treated"], absent=["control"],
    fetch_uniprot=False, pdata=pdata,
)

Source code in src/scpviz/utils/class_filter.py
def get_upset_query(
    upset_content: pd.DataFrame,
    present: list[str],
    absent: list[str],
    *,
    fetch_uniprot: bool,
    pdata: pAnnData | None = None,
    on: str = "protein",
    verbose: bool = True,
) -> pd.DataFrame:
    """
    Query features from UpSet contents given inclusion and exclusion criteria.

    This function extracts the set of features (proteins or peptides) that are
    present in all specified groups and absent in others. You decide whether to
    query UniProt metadata for the resulting accessions via ``fetch_uniprot``
    (set ``False`` for large intersections to avoid slow API calls). The result
    is returned as a ``mark_df``-compatible DataFrame for use with
    ``mark_rankquant`` or ``mark_raincloud``.

    Args:
        upset_content (pandas.DataFrame): Output from `get_upset_contents` with
            ``upsetForm=True`` (the default).
        present (list of str): List of groups in which the features must be present.
        absent (list of str): List of groups in which the features must be absent.
        fetch_uniprot (bool): If True, query UniProt for full metadata via
            `get_uniprot_fields`. If False, build a lightweight DataFrame from
            ``pdata`` using accessions and any available gene names in ``.var``
            (recommended for large intersections, e.g. 1000+ proteins).
        pdata (pAnnData, optional): Required when ``fetch_uniprot=False`` so gene
            names can be read from ``.var``. Ignored when ``fetch_uniprot=True``.
        on (str): Data level for gene lookup when ``fetch_uniprot=False``.
            Options are ``"protein"`` (default) or ``"peptide"``.
        verbose (bool): If True (default), print a progress header and branch
            messages. Passed through to `get_uniprot_fields` when
            ``fetch_uniprot=True``.

    Returns:
        prot_query_df (pandas.DataFrame): Features matching the query. When
        ``fetch_uniprot=True``, includes UniProt metadata. When
        ``fetch_uniprot=False``, includes at least an ``accession`` column and,
        when available, ``gene_primary``.

    Example:
        Small intersection - Query proteins unique to one group and highlight them in a plot, also fetches UniProt metadata for gene labels:
            ```python
            upset_data = scutils.get_upset_contents(pdata, classes="condition")
            prot_df = scutils.get_upset_query(
                upset_data, present=["treated"], absent=["control"], fetch_uniprot=True
            )
            scplt.plot_rankquant(ax, pdata, classes="condition", cmap=cmaps, color=colors)
            scplt.mark_rankquant(ax, pdata, mark_df=prot_df, class_values=["treated"], color="black")
            ```

        Large intersection - skip UniProt and use local gene names:
            ```python
            upset_data = scutils.get_upset_contents(pdata, classes="condition")
            prot_df = scutils.get_upset_query(
                upset_data, present=["treated"], absent=["control"],
                fetch_uniprot=False, pdata=pdata,
            )
            ```

    Related Functions:
        - get_upset_contents: Generate presence/absence sets for UpSet analysis.
        - plot_upset: Plot UpSet diagrams from class-based sets.
        - plot_venn: Plot Venn diagrams for 2 to 3 sets.
    """
    import scpviz.utils as _u
    from scpviz.utils.formatting import format_log_prefix

    prot_query = (
        _u.upsetplot.query(upset_content, present=present, absent=absent)
        .data["id"]
        .tolist()
    )

    if verbose:
        print(
            f"{format_log_prefix('user')} Getting UpSet query "
            f"[{len(prot_query)} features]: present={present}, absent={absent}"
        )

    if not prot_query:
        if verbose:
            print(f"{format_log_prefix('warn')} No features matched the UpSet query.")
        return pd.DataFrame()

    if fetch_uniprot:
        return _u.get_uniprot_fields(prot_query, verbose=verbose)

    if pdata is None:
        raise ValueError(
            "pdata is required when fetch_uniprot=False so gene names can be "
            "read from .var. Pass pdata=pdata, or set fetch_uniprot=True to "
            "query UniProt instead."
        )

    if verbose:
        print(
            f"{format_log_prefix('info_only', indent=2)} Building mark_df from "
            f".{on}.var (no UniProt query)."
        )

    adata = _u.get_adata(pdata, on)
    result = pd.DataFrame({"accession": prot_query})

    gene_var_col = next((c for c in _GENE_VAR_PRECEDENCE if c in adata.var.columns), None)
    if gene_var_col is None:
        warnings.warn(
            f"{format_log_prefix('warn')} No gene name column found in .{on}.var "
            f"(tried: {', '.join(_GENE_VAR_PRECEDENCE)}). Returning accessions only.",
            stacklevel=2,
        )
        if verbose:
            print(
                f"{format_log_prefix('result_only', indent=2)} Built query "
                f"DataFrame: {len(prot_query)} features (accessions only)."
            )
        return result

    genes = pd.Series(
        [
            adata.var.at[acc, gene_var_col] if acc in adata.var_names else pd.NA
            for acc in prot_query
        ],
        index=prot_query,
        dtype=object,
    )
    result["gene_primary"] = genes.values

    missing_mask = genes.isna() | (genes.astype(str).str.strip() == "")
    n_missing = int(missing_mask.sum())
    if n_missing:
        warnings.warn(
            f"{format_log_prefix('warn')} {n_missing}/{len(prot_query)} feature(s) "
            f"have missing gene names in .{on}.var['{gene_var_col}'].",
            stacklevel=2,
        )

    not_in_var = [acc for acc in prot_query if acc not in adata.var_names]
    if not_in_var:
        warnings.warn(
            f"{format_log_prefix('warn')} {len(not_in_var)}/{len(prot_query)} "
            f"feature(s) from the UpSet query are not in .{on}.var_names.",
            stacklevel=2,
        )

    if verbose:
        print(
            f"{format_log_prefix('result_only', indent=2)} Built query DataFrame: "
            f"{len(prot_query)} features (gene names from .{on}.var['{gene_var_col}'])."
        )

    return result

infer_layer_is_log

infer_layer_is_log(
    layer: str, adata: Optional[AnnData] = None
) -> bool

Infer whether a layer contains log-transformed values.

  1. Resolve "X" (if adata is given): map to adata.uns['current_X_layer'] via :func:resolve_input_layer so the active .X matrix is checked under its registered provenance name.
  2. Registry (if adata is given and adata.uns['layer_provenance'] exists): walk ancestors via input_layer (cycle-safe). If any step has op == "log_transform", return True. If the resolved layer is registered and no log_transform appears, return False.
  3. Name fallback: "log" in layer.lower() (on the resolved name when adata was provided).

Standalone AnnData objects (e.g. passed into low-level utils helpers) often have no layer_provenance and no pAnnData .history; only the name heuristic applies unless you populate uns['layer_provenance'] yourself.

Parameters:

Name Type Description Default
layer str

Layer name to inspect.

required
adata Optional[AnnData]

Optional AnnData carrying layer_provenance.

None

Returns:

Type Description
bool

True if the layer is treated as log-transformed.

Source code in src/scpviz/utils/data.py
def infer_layer_is_log(layer: str, adata: Optional[ad.AnnData] = None) -> bool:
    """
    Infer whether a layer contains log-transformed values.

    1. **Resolve ``\"X\"``** (if ``adata`` is given): map to
       ``adata.uns['current_X_layer']`` via :func:`resolve_input_layer` so the
       active ``.X`` matrix is checked under its registered provenance name.
    2. **Registry** (if ``adata`` is given and ``adata.uns['layer_provenance']`` exists):
       walk ancestors via ``input_layer`` (cycle-safe). If any step has
       ``op == \"log_transform\"``, return True. If the resolved layer is registered
       and no ``log_transform`` appears, return False.
    3. **Name fallback**: ``\"log\" in layer.lower()`` (on the resolved name when
       ``adata`` was provided).

    Standalone ``AnnData`` objects (e.g. passed into low-level ``utils`` helpers)
    often have no ``layer_provenance`` and no pAnnData ``.history``; only the
    name heuristic applies unless you populate ``uns['layer_provenance']`` yourself.

    Args:
        layer: Layer name to inspect.
        adata: Optional AnnData carrying ``layer_provenance``.

    Returns:
        True if the layer is treated as log-transformed.
    """
    lookup = layer
    if adata is not None:
        lookup = resolve_input_layer(adata, layer)
        registry = adata.uns.get("layer_provenance", {})
        visited: set[str] = set()
        current: str = lookup
        while current in registry and current not in visited:
            visited.add(current)
            record = registry[current]
            if record.get("op") == "log_transform":
                return True
            nxt = record.get("input_layer", "")
            if not nxt:
                break
            current = nxt
        if lookup in registry:
            return False

    return "log" in lookup.lower()

mask_zeros_as_nan

mask_zeros_as_nan(
    data: ndarray,
    data_is_log: bool = False,
    log_base: float = 2.0,
    pseudocount: float = 1.0,
) -> np.ndarray

Return a copy of data with "zero" (missing-value) entries set to NaN.

On a linear layer a zero is 0. On a log layer produced by log_transform (log(x + pseudocount)) an original zero maps to log_base(pseudocount) (0 for the default pseudocount of 1).

Parameters:

Name Type Description Default
data ndarray

Abundance matrix.

required
data_is_log bool

Whether data is log-transformed.

False
log_base float

Log base of data when data_is_log.

2.0
pseudocount float

Pseudocount used in log(x + pseudocount).

1.0

Returns:

Type Description
ndarray

numpy.ndarray: Float copy of data with zero entries replaced by NaN.

Note

Detection is exact for values straight out of log_transform. If the layer was modified after logging (e.g. normalized again), the zero value shifts and is no longer detected.

Source code in src/scpviz/utils/stats.py
def mask_zeros_as_nan(
    data: np.ndarray,
    data_is_log: bool = False,
    log_base: float = 2.0,
    pseudocount: float = 1.0,
) -> np.ndarray:
    """
    Return a copy of ``data`` with "zero" (missing-value) entries set to NaN.

    On a linear layer a zero is ``0``. On a log layer produced by
    ``log_transform`` (``log(x + pseudocount)``) an original zero maps to
    ``log_base(pseudocount)`` (``0`` for the default pseudocount of 1).

    Args:
        data (numpy.ndarray): Abundance matrix.
        data_is_log (bool): Whether ``data`` is log-transformed.
        log_base (float): Log base of ``data`` when ``data_is_log``.
        pseudocount (float): Pseudocount used in ``log(x + pseudocount)``.

    Returns:
        numpy.ndarray: Float copy of ``data`` with zero entries replaced by NaN.

    Note:
        Detection is exact for values straight out of ``log_transform``. If the
        layer was modified after logging (e.g. normalized again), the zero
        value shifts and is no longer detected.
    """
    out = np.array(data, dtype=float)
    zero_val = np.log(pseudocount) / np.log(log_base) if data_is_log else 0.0
    out[np.isclose(out, zero_val, rtol=0, atol=1e-12)] = np.nan
    return out

pairwise_log2fc

pairwise_log2fc(
    data1: ndarray,
    data2: ndarray,
    data_is_log: bool = False,
    log_base: float = 2.0,
) -> np.ndarray

Compute pairwise median log2 fold change (log2FC) between two groups.

This function calculates all pairwise log2 ratios between features in two groups of samples and returns the median value per feature. It is primarily used as a helper for fold-change strategies in pAnnData.de().

Parameters:

Name Type Description Default
data1 ndarray

Array of shape (n_samples_group1, n_features) containing abundance values for group 1.

required
data2 ndarray

Array of shape (n_samples_group2, n_features) containing abundance values for group 2.

required
data_is_log bool

If True, the data are already log-transformed, so each pairwise fold change is the difference of the two values, converted to log2 units (a ratio of log values is not a fold change). Default False (linear data).

False
log_base float

Log base of the data when data_is_log. Default 2.0.

2.0

Returns:

Name Type Description
median_log2fc ndarray

Array of shape (n_features,) containing

ndarray

the median pairwise log2 fold change for each feature.

Note

This is an internal helper for differential expression calculations. End users should call pAnnData.de() instead of using this function directly.

Source code in src/scpviz/utils/stats.py
def pairwise_log2fc(
    data1: np.ndarray,
    data2: np.ndarray,
    data_is_log: bool = False,
    log_base: float = 2.0,
) -> np.ndarray:
    """
    Compute pairwise median log2 fold change (log2FC) between two groups.

    This function calculates all pairwise log2 ratios between features in
    two groups of samples and returns the median value per feature. It is
    primarily used as a helper for fold-change strategies in `pAnnData.de()`.

    Args:
        data1 (numpy.ndarray): Array of shape `(n_samples_group1, n_features)`
            containing abundance values for group 1.
        data2 (numpy.ndarray): Array of shape `(n_samples_group2, n_features)`
            containing abundance values for group 2.
        data_is_log (bool): If True, the data are already log-transformed, so each pairwise
            fold change is the difference of the two values, converted to log2 units
            (a ratio of log values is not a fold change). Default False (linear data).
        log_base (float): Log base of the data when ``data_is_log``. Default 2.0.

    Returns:
        median_log2fc (numpy.ndarray): Array of shape `(n_features,)` containing
        the median pairwise log2 fold change for each feature.

    Note:
        This is an internal helper for differential expression calculations.
        End users should call `pAnnData.de()` instead of using this function directly.

    Related Functions:
        - pAnnData.de: Differential expression analysis with multiple fold change strategies.
    """
    # data1[:, None, :] has shape (n1, 1, n_features)
    # data2[None, :, :] has shape (1, n2, n_features)
    # The result is an array of shape (n1, n2, n_features)
    with np.errstate(divide='ignore', invalid='ignore'):
        if data_is_log:
            pairwise_ratios = (data1[:, None, :] - data2[None, :, :]) * np.log2(log_base)  # (n1, n2, features)
        else:
            pairwise_ratios = np.log2(data1[:, None, :] / data2[None, :, :])  # (n1, n2, features)
        pairwise_flat = pairwise_ratios.reshape(-1, data1.shape[1])

    # Identify columns that are entirely NaN
    mask_all_nan = np.all(np.isnan(pairwise_flat), axis=0)
    median_fc = np.full(data1.shape[1], np.nan, dtype=float)

    # Compute only on valid columns
    if not np.all(mask_all_nan):
        valid_cols = ~mask_all_nan
        median_fc[valid_cols] = np.nanmedian(pairwise_flat[:, valid_cols], axis=0)

    # # Reshape to (n1*n2, n_features) and compute the median along the first axis.
    # median_fc = np.nanmedian(pairwise_ratios.reshape(-1, data1.shape[1]), axis=0)
    return median_fc

parse_filename_index

parse_filename_index(
    df: DataFrame,
    obs_columns: list[str],
    delimiter: str = "_",
    condition: str | None = None,
) -> pd.DataFrame

Parse DataFrame index (filenames) into metadata columns based on a list of obs_columns. Can label a subset based on condition.

Parameters:

Name Type Description Default
df DataFrame

DataFrame whose index contains delimited filenames.

required
obs_columns list of str

Names of the metadata columns to extract from the filename.

required
delimiter str

Character used to split the filename. Default is "_".

'_'
condition str or None

Optional boolean expression (evaluated with df.eval) that selects a subset of rows for parsing. If None, parse all rows. For example, condition="parsingType == '5-tokens'"

None

Returns:

Type Description
DataFrame

pd.DataFrame: Copy of df with added metadata columns.

Source code in src/scpviz/utils/data.py
def parse_filename_index(
    df: pd.DataFrame,
    obs_columns: list[str],
    delimiter: str = "_",
    condition: str | None = None,
) -> pd.DataFrame:
    """
    Parse DataFrame index (filenames) into metadata columns based on a list of obs_columns. Can label a subset based on condition.

    Args:
        df (pd.DataFrame):
            DataFrame whose index contains delimited filenames.
        obs_columns (list of str):
            Names of the metadata columns to extract from the filename.
        delimiter (str):
            Character used to split the filename. Default is "_".
        condition (str or None):
            Optional boolean expression (evaluated with df.eval) that selects a
            subset of rows for parsing. If None, parse all rows. For example, `condition="parsingType == '5-tokens'"`

    Returns:
        pd.DataFrame:
            Copy of df with added metadata columns.
    """

    df_parsed = df.copy()

    if condition is None:
        mask = pd.Series(True, index=df.index)
    else:
        try:
            mask = df.eval(condition)
        except Exception as e:
            raise ValueError(f"Invalid condition '{condition}': {e}")

        if mask.dtype != bool:
            raise ValueError(f"Condition '{condition}' did not evaluate to a boolean mask.")

    # Nothing to parse
    if not mask.any():
        raise ValueError(
            f"Condition '{condition}' selected 0 rows. "
            f"Check that the column names and values in the condition are correct."
        )

    # Subset of filenames to parse
    idx_to_parse = df.index[mask]

    # Split index by delimiter
    parts = idx_to_parse.to_series().str.split(delimiter, expand=True)

    # Validate number of parts
    expected = len(obs_columns)
    actual = parts.shape[1]
    if actual != expected:
        raise ValueError(
            f"Expected {expected} parts after splitting index by '{delimiter}', "
            f"but got {actual}. Index example: '{idx_to_parse[0]}'"
        )

    # Assign parsed components
    for i, col in enumerate(obs_columns):
        # Create column if missing
        if col not in df_parsed.columns:
            df_parsed[col] = pd.NA
        # Fill only selected rows
        df_parsed.loc[mask, col] = parts.iloc[:, i].values

    return df_parsed

resolve_accessions

resolve_accessions(
    adata: AnnData | pAnnData,
    namelist: list[str],
    gene_col: str = "Genes",
    gene_map: dict[str, str] | None = None,
    *,
    all_matches: bool = False,
    on_empty: str = "raise",
    quiet: bool = False
) -> list[str] | None

Resolve gene or accession names to accession IDs from .var_names.

This function maps user-specified identifiers (gene names or accession IDs) to the canonical accession IDs in an AnnData or pAnnData object. It first checks .var_names for exact matches, then optionally resolves gene names via a specified column (default "Genes"). Unmatched names are reported.

Parameters:

Name Type Description Default
adata AnnData or pAnnData

AnnData-like object containing .var.

required
namelist list of str

Input identifiers to resolve (genes or accessions).

required
gene_col str

Column in .var containing gene names (default: "Genes").

'Genes'
gene_map dict

Precomputed mapping of gene → accession. If None, a mapping is constructed from gene_col. Used only when all_matches=False.

None
all_matches bool

If False (default), each gene name resolves to a single accession (last duplicate wins in gene_map). If True, returns every accession whose gene_col value equals the name; logs info when a name expands to more than one accession.

False
on_empty str

"raise" (default) raises ValueError when nothing resolves; "return" returns an empty list.

'raise'
quiet bool

If True, suppress info / warn print messages (for callers that embed notes in their own log output).

False

Returns:

Name Type Description
resolved list of str

List of accession IDs corresponding to the input names.

Raises:

Type Description
ValueError

If none of the provided names can be resolved and on_empty="raise".

Example

Resolve gene symbols to accession IDs:

accs = resolve_accessions(adata, namelist=["UBE4B", "GAPDH"])

Resolve all isoforms for a gene:

accs = resolve_accessions(adata, namelist=["GAPDH"], all_matches=True)

Resolve accessions directly:

accs = resolve_accessions(adata, namelist=["P12345", "Q67890"])

Source code in src/scpviz/utils/data.py
def resolve_accessions(
    adata: ad.AnnData | pAnnData,
    namelist: list[str],
    gene_col: str = "Genes",
    gene_map: dict[str, str] | None = None,
    *,
    all_matches: bool = False,
    on_empty: str = "raise",
    quiet: bool = False,
) -> list[str] | None:
    """
    Resolve gene or accession names to accession IDs from `.var_names`.

    This function maps user-specified identifiers (gene names or accession IDs)
    to the canonical accession IDs in an AnnData or pAnnData object. It first
    checks `.var_names` for exact matches, then optionally resolves gene names
    via a specified column (default `"Genes"`). Unmatched names are reported.

    Args:
        adata (AnnData or pAnnData): AnnData-like object containing `.var`.
        namelist (list of str): Input identifiers to resolve (genes or accessions).
        gene_col (str): Column in `.var` containing gene names (default: `"Genes"`).
        gene_map (dict, optional): Precomputed mapping of gene → accession. If None,
            a mapping is constructed from `gene_col`. Used only when ``all_matches=False``.
        all_matches (bool): If False (default), each gene name resolves to a single
            accession (last duplicate wins in ``gene_map``). If True, returns every
            accession whose ``gene_col`` value equals the name; logs ``info`` when
            a name expands to more than one accession.
        on_empty (str): ``"raise"`` (default) raises ``ValueError`` when nothing resolves;
            ``"return"`` returns an empty list.
        quiet (bool): If True, suppress ``info`` / ``warn`` print messages (for callers that
            embed notes in their own log output).

    Returns:
        resolved (list of str): List of accession IDs corresponding to the input names.

    Raises:
        ValueError: If none of the provided names can be resolved and ``on_empty="raise"``.

    Example:
        Resolve gene symbols to accession IDs:
            ```python
            accs = resolve_accessions(adata, namelist=["UBE4B", "GAPDH"])
            ```

        Resolve all isoforms for a gene:
            ```python
            accs = resolve_accessions(adata, namelist=["GAPDH"], all_matches=True)
            ```

        Resolve accessions directly:
            ```python
            accs = resolve_accessions(adata, namelist=["P12345", "Q67890"])
            ```

    Related Functions:
        - get_gene_maps: Build full accession → gene mapping dictionaries.
        - get_abundance: Extract abundance values by gene or accession.
    """
    import pandas as pd

    if not namelist:
        return None

    if on_empty not in ("raise", "return"):
        raise ValueError(f"on_empty must be 'raise' or 'return', got {on_empty!r}")

    var_names = adata.var_names.astype(str)

    # Use passed-in gene_map or build one (one-to-one mode only)
    if gene_map is None and not all_matches:
        gene_map = {}
        if gene_col in adata.var.columns:
            for acc, gene in zip(var_names, adata.var[gene_col]):
                if pd.notna(gene):
                    gene_map[str(gene)] = acc

    resolved, unmatched = [], []
    for name in namelist:
        name = str(name)
        if name in var_names:
            resolved.append(name)
        elif all_matches and gene_col in adata.var.columns:
            matches = [
                acc for acc, gene in zip(var_names, adata.var[gene_col])
                if pd.notna(gene) and str(gene) == name
            ]
            if matches:
                resolved.extend(matches)
                if len(matches) > 1 and not quiet:
                    print(
                        f"{format_log_prefix('info')} "
                        f"'{name}' resolved to multiple accessions: {matches}"
                    )
            else:
                unmatched.append(name)
        elif gene_map is not None and name in gene_map:
            resolved.append(gene_map[name])
        else:
            unmatched.append(name)

    if not resolved:
        if on_empty == "return":
            return []
        raise ValueError(
            f"No valid names found in `namelist`: {namelist}.\n"
            f"Check against .var_names or '{gene_col}' column."
        )

    if unmatched and not quiet:
        print(f"{format_log_prefix('warn')} A match was not found for the following:")
        for u in unmatched:
            print(f"  - {u}")

    return resolved

resolve_class_filter

resolve_class_filter(
    adata: pAnnData | AnnData,
    classes: str | list[str],
    class_value: str | list[str],
    debug: bool = False,
    *,
    filter_func: (
        Callable[..., pAnnData | AnnData] | None
    ) = None
) -> pAnnData | ad.AnnData

Resolve (classes, class_value) inputs and apply filtering.

This helper standardizes class/value pairs into dictionary-style filters and applies them to an AnnData or pAnnData object. It is primarily used internally by plotting and analysis functions.

Parameters:

Name Type Description Default
adata AnnData or pAnnData

Input data object to filter.

required
classes str or list of str

Metadata field(s) used for filtering.

required
class_value str or list of str

Values corresponding to classes.

required
debug bool

If True, print resolved class/value pairs.

False
filter_func callable

Filtering function to apply. Defaults to :func:filter.

None

Returns:

Name Type Description
filtered AnnData or pAnnData

Subset of the input object, same type as adata.

Warning

This is an internal helper for use inside functions such as plot_rankquant and plot_raincloud. End users should call pAnnData.filter_sample_values() instead.

Source code in src/scpviz/utils/class_filter.py
def resolve_class_filter(
    adata: pAnnData | ad.AnnData,
    classes: str | list[str],
    class_value: str | list[str],
    debug: bool = False,
    *,
    filter_func: Callable[..., pAnnData | ad.AnnData] | None = None,
) -> pAnnData | ad.AnnData:
    """
    Resolve `(classes, class_value)` inputs and apply filtering.

    This helper standardizes class/value pairs into dictionary-style filters
    and applies them to an AnnData or pAnnData object. It is primarily used
    internally by plotting and analysis functions.

    Args:
        adata (AnnData or pAnnData): Input data object to filter.
        classes (str or list of str): Metadata field(s) used for filtering.
        class_value (str or list of str): Values corresponding to `classes`.
        debug (bool): If True, print resolved class/value pairs.
        filter_func (callable, optional): Filtering function to apply.
            Defaults to :func:`filter`.

    Returns:
        filtered (AnnData or pAnnData): Subset of the input object, same type as `adata`.

    !!! warning
        This is an internal helper for use inside functions such as
        `plot_rankquant` and `plot_raincloud`. End users should call
        `pAnnData.filter_sample_values()` instead.

    Related Functions:
        - filter: Legacy utility for sample filtering.
        - format_class_filter: Standardizes filter inputs.
        - pAnnData.filter_sample_values: Recommended user-facing filter method.
    """

    if isinstance(classes, str):
        values = class_value
    else:
        values = class_value.split('_')

    if debug:
        print(f"Classes: {classes}, Values: {values}")

    if filter_func is None:
        filter_func = filter

    return filter_func(adata, classes, values, debug=debug)

resolve_input_layer

resolve_input_layer(adata: AnnData, layer: str) -> str

Resolve the source layer name for provenance when the user passes layer='X'.

The active matrix .X tracks its logical source in adata.uns['current_X_layer'] (maintained by set_X() and set at import). For any other layer string, return it unchanged.

If current_X_layer is missing (legacy objects), falls back to "X_raw".

Source code in src/scpviz/utils/data.py
def resolve_input_layer(adata: ad.AnnData, layer: str) -> str:
    """
    Resolve the source layer name for provenance when the user passes ``layer='X'``.

    The active matrix ``.X`` tracks its logical source in ``adata.uns['current_X_layer']``
    (maintained by ``set_X()`` and set at import). For any other ``layer`` string,
    return it unchanged.

    If ``current_X_layer`` is missing (legacy objects), falls back to ``\"X_raw\"``.
    """
    if layer == "X":
        return adata.uns.get("current_X_layer", "X_raw")
    return layer

resolve_peptide_sequence

resolve_peptide_sequence(
    pdata: pAnnData,
    peptide_id: str,
    *,
    sequence_from: str | None = None
) -> str | None

Resolve a clean amino-acid sequence for one peptide in pdata.pep.

Auto-detects Stripped.Sequence (DIA-NN), then Annotated Sequence (PD), otherwise parses peptide_id / .pep.var_names.

Parameters:

Name Type Description Default
pdata pAnnData

Object with .pep.

required
peptide_id str

Peptide ID (.pep.var_names entry).

required
sequence_from str

Column in .pep.var for sequence resolution, "index" to parse peptide_id only, or None to auto-detect.

None

Returns:

Type Description
str | None

str or None: Clean amino-acid sequence, or None if parsing fails.

Example

Resolve one peptide from a Proteome Discoverer import:

from scpviz import utils as scutils

pep_id = pdata.pep.var_names[0]
scutils.resolve_peptide_sequence(pdata, pep_id)
# 'KPVVDCVVSVPCFYTDAER'

DIA-NN: uses Stripped.Sequence automatically:

pep_id = pdata.pep.var_names[0]
scutils.resolve_peptide_sequence(pdata, pep_id)

Source code in src/scpviz/utils/peptide_param.py
def resolve_peptide_sequence(
    pdata: pAnnData,
    peptide_id: str,
    *,
    sequence_from: str | None = None,
) -> str | None:
    """
    Resolve a clean amino-acid sequence for one peptide in ``pdata.pep``.

    Auto-detects ``Stripped.Sequence`` (DIA-NN), then ``Annotated Sequence`` (PD),
    otherwise parses ``peptide_id`` / ``.pep.var_names``.

    Args:
        pdata (pAnnData): Object with ``.pep``.
        peptide_id (str): Peptide ID (``.pep.var_names`` entry).
        sequence_from (str, optional): Column in ``.pep.var`` for sequence resolution,
            ``"index"`` to parse ``peptide_id`` only, or ``None`` to auto-detect.

    Returns:
        str or None: Clean amino-acid sequence, or ``None`` if parsing fails.

    Example:
        Resolve one peptide from a Proteome Discoverer import:
            ```python
            from scpviz import utils as scutils

            pep_id = pdata.pep.var_names[0]
            scutils.resolve_peptide_sequence(pdata, pep_id)
            # 'KPVVDCVVSVPCFYTDAER'
            ```

        DIA-NN: uses ``Stripped.Sequence`` automatically:
            ```python
            pep_id = pdata.pep.var_names[0]
            scutils.resolve_peptide_sequence(pdata, pep_id)
            ```

    Related Functions:
        - strip_peptide_sequence: Low-level string cleaning.
        - get_peptide_properties: Batch property computation for ``pdata``.
    """
    pep_var = pdata.pep.var
    peptide_id = str(peptide_id)

    if sequence_from is not None:
        if sequence_from == "index":
            return strip_peptide_sequence(peptide_id)
        if sequence_from not in pep_var.columns:
            available = ", ".join(map(str, pep_var.columns))
            raise ValueError(
                f"Column '{sequence_from}' not found in .pep.var. Available: {available}"
            )
        return strip_peptide_sequence(
            peptide_id, sequence=str(pep_var.loc[peptide_id, sequence_from])
        )

    if "Stripped.Sequence" in pep_var.columns:
        return strip_peptide_sequence(
            peptide_id, sequence=str(pep_var.loc[peptide_id, "Stripped.Sequence"])
        )
    if "Annotated Sequence" in pep_var.columns:
        return strip_peptide_sequence(
            peptide_id, sequence=str(pep_var.loc[peptide_id, "Annotated Sequence"])
        )
    return strip_peptide_sequence(peptide_id)

resolve_peptides

resolve_peptides(
    pdata: pAnnData,
    namelist: list[str],
    *,
    all_matches: bool = True,
    on_empty: str = "return",
    quiet: bool = False
) -> list[str] | None

Resolve peptide IDs from .pep.var_names, or expand gene/accession names to peptides.

Direct peptide IDs in namelist are returned as-is. Otherwise each name is resolved to protein accessions (resolve_accessions with all_matches) and linked peptides are collected via the RS matrix when available, or via the peptide-to-protein mapping column otherwise.

Parameters:

Name Type Description Default
pdata pAnnData

Object with .pep; protein expansion requires .prot.

required
namelist list of str

Peptide IDs, gene names, or protein accessions.

required
all_matches bool

When expanding gene names to proteins, return every isoform (default True).

True
on_empty str

"raise" or "return" (empty list) when nothing resolves.

'return'
quiet bool

If True, suppress info print messages.

False

Returns:

Type Description
list[str] | None

list of str | None: Unique .pep.var_names entries, or None for empty input.

Source code in src/scpviz/utils/data.py
def resolve_peptides(
    pdata: pAnnData,
    namelist: list[str],
    *,
    all_matches: bool = True,
    on_empty: str = "return",
    quiet: bool = False,
) -> list[str] | None:
    """
    Resolve peptide IDs from ``.pep.var_names``, or expand gene/accession names to peptides.

    Direct peptide IDs in ``namelist`` are returned as-is. Otherwise each name is resolved
    to protein accessions (``resolve_accessions`` with ``all_matches``) and linked peptides
    are collected via the RS matrix when available, or via the peptide-to-protein mapping
    column otherwise.

    Args:
        pdata (pAnnData): Object with ``.pep``; protein expansion requires ``.prot``.
        namelist (list of str): Peptide IDs, gene names, or protein accessions.
        all_matches (bool): When expanding gene names to proteins, return every isoform
            (default True).
        on_empty (str): ``"raise"`` or ``"return"`` (empty list) when nothing resolves.
        quiet (bool): If True, suppress ``info`` print messages.

    Returns:
        list of str | None: Unique ``.pep.var_names`` entries, or ``None`` for empty input.
    """
    if not namelist:
        return None

    if on_empty not in ("raise", "return"):
        raise ValueError(f"on_empty must be 'raise' or 'return', got {on_empty!r}")

    if pdata.pep is None:
        raise ValueError("Peptide data (.pep) is not available.")

    pep_names = set(pdata.pep.var_names.astype(str))
    resolved: list[str] = []

    for name in namelist:
        name = str(name)
        if name in pep_names:
            resolved.append(name)
            continue

        if pdata.prot is None:
            continue

        accs = resolve_accessions(
            pdata.prot, [name], all_matches=all_matches, on_empty="return", quiet=quiet
        )
        if not accs:
            continue

        if pdata.rs is not None:
            pep_df = get_peptides_for_accessions(pdata, accs)
            matches = pep_df["peptide_id"].astype(str).tolist()
        else:
            prot_col = get_pep_prot_mapping(pdata)
            acc_set = set(accs)
            matches = []
            for pep_id, prot_val in zip(pdata.pep.var_names, pdata.pep.var[prot_col]):
                if pd.isna(prot_val):
                    continue
                linked = {p.strip() for p in str(prot_val).split(";")}
                if linked & acc_set:
                    matches.append(str(pep_id))

        if matches:
            resolved.extend(matches)
            if len(matches) > 1 and not quiet:
                print(
                    f"{format_log_prefix('info')} "
                    f"'{name}' resolved to multiple peptides: {matches}"
                )

    resolved = list(dict.fromkeys(resolved))

    if not resolved:
        if on_empty == "return":
            return []
        raise ValueError(
            f"No valid peptides found in `namelist`: {namelist}.\n"
            "Check against .pep.var_names or provide a gene/accession with linked peptides."
        )

    return resolved

run_two_group_test

run_two_group_test(
    x1: ndarray,
    x2: ndarray,
    method: str,
    equal_var: bool = True,
) -> tuple[float, float]

Run one two-group test on a single feature, ignoring NaN (missing) values.

'ttest' omits NaNs per group. 'mannwhitneyu' drops NaNs per group before testing. 'wilcoxon' is paired, so a pair is dropped when either value is NaN; unequal group sizes cannot be paired and give NaN.

Parameters:

Name Type Description Default
x1 ndarray

Values of one feature in group 1, shape (n1,).

required
x2 ndarray

Values of one feature in group 2, shape (n2,).

required
method str

'ttest', 'mannwhitneyu' or 'wilcoxon'.

required
equal_var bool

Passed to :func:scipy.stats.ttest_ind for 'ttest'.

True

Returns:

Name Type Description
tuple float

(p_value, statistic); (nan, nan) when the test cannot be run

float

(all-NaN or too-small groups).

Source code in src/scpviz/utils/stats.py
def run_two_group_test(
    x1: np.ndarray, x2: np.ndarray, method: str, equal_var: bool = True
) -> tuple[float, float]:
    """
    Run one two-group test on a single feature, ignoring NaN (missing) values.

    ``'ttest'`` omits NaNs per group. ``'mannwhitneyu'`` drops NaNs per group before testing.
    ``'wilcoxon'`` is paired, so a pair is dropped when either value is NaN; unequal group
    sizes cannot be paired and give NaN.

    Args:
        x1 (numpy.ndarray): Values of one feature in group 1, shape ``(n1,)``.
        x2 (numpy.ndarray): Values of one feature in group 2, shape ``(n2,)``.
        method (str): ``'ttest'``, ``'mannwhitneyu'`` or ``'wilcoxon'``.
        equal_var (bool): Passed to :func:`scipy.stats.ttest_ind` for ``'ttest'``.

    Returns:
        tuple: ``(p_value, statistic)``; ``(nan, nan)`` when the test cannot be run
        (all-NaN or too-small groups).
    """
    try:
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")  # all-NaN / tiny groups give NaN p-values
            if method == 'ttest':
                res = ttest_ind(x1, x2, equal_var=equal_var, nan_policy='omit')
            elif method == 'mannwhitneyu':
                res = mannwhitneyu(x1[~np.isnan(x1)], x2[~np.isnan(x2)], alternative='two-sided')
            else:  # wilcoxon
                ok = ~np.isnan(x1) & ~np.isnan(x2) if len(x1) == len(x2) else np.zeros(len(x1), bool)
                res = wilcoxon(x1[ok], x2[ok])
        return res.pvalue, res.statistic
    except Exception:
        return np.nan, np.nan

scalarize_taxon

scalarize_taxon(x: object) -> object

Normalize taxon-id values so they never contain lists or arrays.

Returns:

Type Description
object

Scalar string-like taxon id, or pd.NA.

Source code in src/scpviz/utils/id_maps.py
def scalarize_taxon(x: object) -> object:
    """
    Normalize taxon-id values so they never contain lists or arrays.

    Returns:
        Scalar string-like taxon id, or pd.NA.
    """
    # Handle pandas missing scalar explicitly first
    if x is pd.NA:
        return pd.NA

    # Handle standard missing
    if x is None:
        return pd.NA
    if isinstance(x, float) and np.isnan(x):
        return pd.NA

    # Empty string
    if isinstance(x, str):
        s = x.strip()
        return pd.NA if s == "" else s

    # Empty container / container → first element
    if isinstance(x, (list, tuple, np.ndarray)):
        if len(x) == 0:
            return pd.NA
        return scalarize_taxon(x[0])

    # Everything else → string
    return str(x)

standardize_uniprot_columns

standardize_uniprot_columns(
    df: DataFrame | None,
) -> pd.DataFrame | None

Normalize UniProt DataFrame column names to a consistent lowercase, snake_case schema.

This ensures stability across UniProt REST API version changes while keeping the user informed only when critical fields are affected.

Parameters:

Name Type Description Default
df DataFrame

Raw UniProt metadata table.

required

Returns:

Type Description
DataFrame | None

pd.DataFrame: Copy of the DataFrame with standardized column names.

Source code in src/scpviz/utils/id_maps.py
def standardize_uniprot_columns(df: pd.DataFrame | None) -> pd.DataFrame | None:
    """
    Normalize UniProt DataFrame column names to a consistent lowercase, snake_case schema.

    This ensures stability across UniProt REST API version changes while keeping
    the user informed only when critical fields are affected.

    Args:
        df (pd.DataFrame): Raw UniProt metadata table.

    Returns:
        pd.DataFrame: Copy of the DataFrame with standardized column names.
    """
    if df is None or not isinstance(df, pd.DataFrame) or df.shape[1] == 0:
        return df

    rename_map = {}
    aliases = {
        # identifiers
        "entry": "accession",
        "entry_name": "id",
        "accession": "accession",
        "primaryaccession": "accession",

        # gene fields
        "gene_names_primary": "gene_primary",
        "gene_name_primary": "gene_primary",
        "gene_primary_name": "gene_primary",
        "gene_primary": "gene_primary",
        "gene_primaryname": "gene_primary",
        "gene_primary_name_": "gene_primary",
        "gene_primaryname_": "gene_primary",

        # organism fields
        "organism_id": "organism_id",
        "organism_identifier": "organism_id",
        "organismid": "organism_id",

        # STRING / cross-reference
        "cross_reference_string": "xref_string",
        "xref_string_id": "xref_string",
        "crossreference_string": "xref_string",
        "string": "xref_string",
        "string_id": "xref_string",
        "xref_string": "xref_string",
    }

    # critical canonical fields we care about if changed or missing
    critical_fields = {"accession", "gene_primary", "organism_id", "xref_string"}

    # known benign patterns — don't warn if these change
    benign_patterns = {
        "gene_ontology",
        "go",
        "gene_names",      # non-primary gene list
        "protein_name",    # descriptive only
        "cc_interaction",  # crossref metadata
    }

    for col in df.columns:
        norm = (
            re.sub(r"[^a-z0-9]+", "_", col.lower())
            .strip("_")
            .replace("__", "_")
        )

        mapped = aliases.get(norm, None)

        if mapped:
            rename_map[col] = mapped
        else:
            # warn only if this looks like a drifted critical column
            if (
                any(k in norm for k in ["accession", "gene", "organism", "string"])
                and not any(p in norm for p in benign_patterns)
            ):
                warnings.warn(
                    f"[standardize_uniprot_columns] ⚠️ Unrecognized UniProt column '{col}' "
                    f"(normalized='{norm}') — may affect critical mapping.",
                    RuntimeWarning,
                    stacklevel=2,
                )
            rename_map[col] = norm  # keep normalized fallback name

    df = df.rename(columns=rename_map)
    # verify that all critical fields exist at least once
    missing_critical = [c for c in critical_fields if c not in df.columns]
    if missing_critical:
        if _setup.GLOBAL_DEBUG:
            warnings.warn(
                f"[standardize_uniprot_columns] Missing expected UniProt columns: {', '.join(missing_critical)}",
                RuntimeWarning,
                stacklevel=2,
            )

    return df.rename(columns=rename_map)

strip_peptide_sequence

strip_peptide_sequence(
    peptide_id: str, *, sequence: str | None = None
) -> str | None

Extract a standard amino-acid string suitable for Biopython ProteinAnalysis.

Strips Proteome Discoverer notation (terminal [X]. / .[X], MOD: suffix, trailing digits). When sequence is provided (e.g. DIA-NN Stripped.Sequence), that value is cleaned instead of peptide_id.

Parameters:

Name Type Description Default
peptide_id str

Raw peptide ID or annotated sequence string.

required
sequence str

Pre-parsed sequence column value. When set, peptide_id is ignored except for context in downstream tables.

None

Returns:

Type Description
str | None

str or None: Uppercase sequence of standard IUPAC letters, or None if invalid.

Example

Clean a Proteome Discoverer annotated string:

from scpviz import utils as scutils

raw = "[R].MQHNLEQQIQAR.[N] MOD:1xOxidation [M1]"
scutils.strip_peptide_sequence(raw)
# 'MQHNLEQQIQAR'

Clean a specified peptide list, then compute features (no pAnnData required):

from scpviz import utils as scutils

raw_peptides = [
    "[K].KPVVDCVVSVPCFYTDAER.[R]",
    "[R].MQHNLEQQIQAR.[N] MOD:1xOxidation [M1]",
]
cleaned = [scutils.strip_peptide_sequence(p) for p in raw_peptides]
props = scutils.compute_peptide_properties(cleaned)
props
#    gravy  molecular_weight  isoelectric_point
# 0  ...              ...                ...
# 1 -1.392         1495.663              6.502

Source code in src/scpviz/utils/peptide_param.py
def strip_peptide_sequence(peptide_id: str, *, sequence: str | None = None) -> str | None:
    """
    Extract a standard amino-acid string suitable for Biopython ``ProteinAnalysis``.

    Strips Proteome Discoverer notation (terminal ``[X].`` / ``.[X]``, ``MOD:`` suffix,
    trailing digits). When ``sequence`` is provided (e.g. DIA-NN ``Stripped.Sequence``),
    that value is cleaned instead of ``peptide_id``.

    Args:
        peptide_id (str): Raw peptide ID or annotated sequence string.
        sequence (str, optional): Pre-parsed sequence column value. When set, ``peptide_id``
            is ignored except for context in downstream tables.

    Returns:
        str or None: Uppercase sequence of standard IUPAC letters, or ``None`` if invalid.

    Example:
        Clean a Proteome Discoverer annotated string:
            ```python
            from scpviz import utils as scutils

            raw = "[R].MQHNLEQQIQAR.[N] MOD:1xOxidation [M1]"
            scutils.strip_peptide_sequence(raw)
            # 'MQHNLEQQIQAR'
            ```

        Clean a specified peptide list, then compute features (no ``pAnnData`` required):
            ```python
            from scpviz import utils as scutils

            raw_peptides = [
                "[K].KPVVDCVVSVPCFYTDAER.[R]",
                "[R].MQHNLEQQIQAR.[N] MOD:1xOxidation [M1]",
            ]
            cleaned = [scutils.strip_peptide_sequence(p) for p in raw_peptides]
            props = scutils.compute_peptide_properties(cleaned)
            props
            #    gravy  molecular_weight  isoelectric_point
            # 0  ...              ...                ...
            # 1 -1.392         1495.663              6.502
            ```

    Related Functions:
        - compute_peptide_properties: Compute ProtParam metrics from cleaned sequences.
        - resolve_peptide_sequence: Resolve sequences from ``pdata.pep`` metadata.
    """
    raw = str(sequence if sequence is not None else peptide_id)
    raw = raw.split(" MOD:")[0]
    raw = _PD_PREFIX.sub("", raw)
    raw = _PD_SUFFIX.sub("", raw)
    raw = _TRAILING_DIGITS.sub("", raw)
    raw = raw.strip().upper()
    if not raw or _STANDARD_AA.fullmatch(raw) is None:
        return None
    return raw

update_layer_provenance

update_layer_provenance(
    adata: AnnData,
    layer_name: str,
    op: str,
    input_layer: str,
    **kwargs: Any
) -> str

Register a layer in the provenance registry stored in adata.uns.

Preprocessing methods (normalize, impute, log_transform) call this before assigning adata.layers[...]. Chains are reconstructable by following input_layer pointers.

If layer_name already exists with a different op or input_layer, a warning is printed and the record is stored under layer_name_1, layer_name_2, …

Parameters:

Name Type Description Default
adata AnnData

AnnData to update (must not rely on pAnnData .history; registry lives only in adata.uns).

required
layer_name str

Intended output layer key.

required
op str

One of "normalize", "impute", "log_transform".

required
input_layer str

Source layer name, or "X" if read from adata.X.

required
**kwargs Any

Extra metadata (e.g. method=..., base=...).

{}

Returns:

Type Description
str

Actual layer key to use in adata.layers (may be suffixed on collision).

Source code in src/scpviz/utils/data.py
def update_layer_provenance(
    adata: ad.AnnData,
    layer_name: str,
    op: str,
    input_layer: str,
    **kwargs: Any,
) -> str:
    """
    Register a layer in the provenance registry stored in ``adata.uns``.

    Preprocessing methods (``normalize``, ``impute``, ``log_transform``) call this
    before assigning ``adata.layers[...]``. Chains are reconstructable by following
    ``input_layer`` pointers.

    If ``layer_name`` already exists with a different ``op`` or ``input_layer``,
    a warning is printed and the record is stored under ``layer_name_1``, ``layer_name_2``, …

    Args:
        adata: AnnData to update (must not rely on pAnnData ``.history``; registry
            lives only in ``adata.uns``).
        layer_name: Intended output layer key.
        op: One of ``\"normalize\"``, ``\"impute\"``, ``\"log_transform\"``.
        input_layer: Source layer name, or ``\"X\"`` if read from ``adata.X``.
        **kwargs: Extra metadata (e.g. ``method=...``, ``base=...``).

    Returns:
        Actual layer key to use in ``adata.layers`` (may be suffixed on collision).
    """
    if "layer_provenance" not in adata.uns:
        adata.uns["layer_provenance"] = {}

    registry = adata.uns["layer_provenance"]
    new_record = {"op": op, "input_layer": input_layer, **kwargs}

    if layer_name in registry:
        existing = registry[layer_name]
        collision = (
            existing.get("input_layer") != input_layer or existing.get("op") != op
        )
        if collision:
            suffix_n = 1
            candidate = f"{layer_name}_{suffix_n}"
            while candidate in registry:
                suffix_n += 1
                candidate = f"{layer_name}_{suffix_n}"

            print(
                f"{format_log_prefix('warn')} Layer '{layer_name}' already exists "
                f"in the provenance registry with a different origin:\n"
                f"       existing: {existing}\n"
                f"       new:      {new_record}\n"
                f"     Storing new layer as '{candidate}' to avoid collision.\n"
                f"     Use pdata.show_layer_provenance('{layer_name}') to inspect "
                "the existing chain."
            )
            layer_name = candidate

    registry[layer_name] = new_record
    return layer_name