Skip to content

Display Formatting API

tcrsift.format converts machine-friendly sample and enrichment_method identifiers into compact plot labels with Unicode superscripts:

Raw string Formatted label Rule applied
AIMpos AIM⁺ pos → ⁺
CTYneg CTY⁻ neg → ⁻
AIMpos_CTYneg AIM⁺CTY⁻ Underscore separator removed
CTYneg_tetpos tet⁺CTY⁻ Baseline marker (CTY) moved last
AIMpos_CTYneg-2 AIM⁺CTY⁻ Trailing donor/replicate suffix stripped

Built-in overlap heatmaps, method-recovery bar charts, and per-sample scatter plots apply these helpers automatically; call them directly when labeling custom figures.

Usage

from tcrsift.format import pretty_method, pretty_samples

pretty_method("AIMpos_CTYneg")            # "AIM⁺CTY⁻"
pretty_method("CTYneg_tetpos")            # "tet⁺CTY⁻"
pretty_samples(["AIMpos-1", "tetpos-1"])  # ["AIM⁺", "tet⁺"]

format

Display formatters for sample-sheet method names (#77).

The raw enrichment-method strings in tcrsift sample sheets are pipeline-friendly but ugly in figures: AIMpos, CTYneg, AIMpos_CTYneg, tetpos, IFNpos. Built-in plots route every method/sample axis label through :func:pretty_method so figures show the readable AIM⁺, CTY⁻, AIM⁺CTY⁻ forms.

Rules:

  • Suffix pos → Unicode superscript ⁺.
  • Suffix neg → Unicode superscript ⁻.
  • A name combining several markers (joined by _, or an N-way set combination joined by +) is reordered so any baseline marker (default CTY, an exclusion gate in our cohort) always reads last (CTYneg_tetpos → tet⁺CTY⁻). This gives a consistent label no matter which order the markers happened to appear in the data.

Baseline markers default to :data:BASELINE_MARKERS (("CTY",)) but can be overridden globally via :func:set_baseline_markers or per call via the last= argument. An explicit priority= list pins the leading order of non-baseline markers.

Pass-through behavior: any name that doesn't match the rules is returned unchanged. Non-string input is coerced via str().

pretty_method

pretty_method(name: str, *, priority: Sequence[str] | None = None, last: Sequence[str] | None = None) -> str

Format a tcrsift enrichment-method string for display.

Markers joined by _ are reordered so baseline markers (default CTY) read last, for any number of parts (not just two). Override the baseline set with last= (or globally via :func:set_baseline_markers) and pin leading order with priority=.

Examples: >>> pretty_method("AIMpos") 'AIM⁺' >>> pretty_method("CTYneg") 'CTY⁻' >>> pretty_method("AIMpos_CTYneg") 'AIM⁺CTY⁻' >>> pretty_method("CTYneg_tetpos") 'tet⁺CTY⁻' >>> pretty_method("CTYneg_AIMpos_IFNpos") 'AIM⁺IFN⁺CTY⁻'

Source code in tcrsift/format.py
def pretty_method(
    name: str,
    *,
    priority: Sequence[str] | None = None,
    last: Sequence[str] | None = None,
) -> str:
    """Format a tcrsift enrichment-method string for display.

    Markers joined by ``_`` are reordered so baseline markers (default
    ``CTY``) read last, for any number of parts (not just two). Override the
    baseline set with ``last=`` (or globally via :func:`set_baseline_markers`)
    and pin leading order with ``priority=``.

    Examples:
        >>> pretty_method("AIMpos")
        'AIM⁺'
        >>> pretty_method("CTYneg")
        'CTY⁻'
        >>> pretty_method("AIMpos_CTYneg")
        'AIM⁺CTY⁻'
        >>> pretty_method("CTYneg_tetpos")
        'tet⁺CTY⁻'
        >>> pretty_method("CTYneg_AIMpos_IFNpos")
        'AIM⁺IFN⁺CTY⁻'
    """
    if not isinstance(name, str):
        return str(name)
    parts = name.split("_")
    # Only reorder when at least one token looks like an enrichment marker
    # (``pos``/``neg``) or matches ``priority``/``last``; otherwise preserve
    # input order so arbitrary underscored names aren't scrambled alphabetically.
    effective_last = list(last) if last is not None else list(BASELINE_MARKERS)
    effective_priority = list(priority or ())
    has_marker_or_rule = any(
        p.endswith(("pos", "neg"))
        or _is_baseline(p, effective_last)
        or any(pr and pr in p for pr in effective_priority)
        for p in parts
    )
    ordered = (
        order_conditions(parts, priority=priority, last=last)
        if has_marker_or_rule
        else parts
    )
    return "".join(_format_part(p) for p in ordered)

pretty_methods

pretty_methods(names: Iterable[str]) -> list[str]

Map :func:pretty_method over a list/Series of method names.

Source code in tcrsift/format.py
def pretty_methods(names: Iterable[str]) -> list[str]:
    """Map :func:`pretty_method` over a list/Series of method names."""
    return [pretty_method(str(n)) for n in names]

pretty_sample

pretty_sample(name: str) -> str

Format a sample name like AIMpos_CTYneg-2 as the prettified method (AIM⁺CTY⁻), dropping the trailing -N donor suffix.

Only strips a trailing -<digits> suffix when the stem looks like a marker/method string (contains a pos/neg token), so non-marker hyphenated names like Patient1-TIL, Patient1-PBMC, MART-1, or TRAV12-2 are never collapsed or truncated.

Use in plots that label by sample-of-origin in a single-donor context (the donor is named elsewhere in the figure title).

Source code in tcrsift/format.py
def pretty_sample(name: str) -> str:
    """Format a sample name like ``AIMpos_CTYneg-2`` as the prettified
    method (``AIM⁺CTY⁻``), dropping the trailing ``-N`` donor suffix.

    Only strips a trailing ``-<digits>`` suffix when the stem looks like a
    marker/method string (contains a ``pos``/``neg`` token), so non-marker
    hyphenated names like ``Patient1-TIL``, ``Patient1-PBMC``, ``MART-1``,
    or ``TRAV12-2`` are never collapsed or truncated.

    Use in plots that label by sample-of-origin in a single-donor
    context (the donor is named elsewhere in the figure title).
    """
    if not isinstance(name, str):
        return str(name)
    if "-" in name:
        stem, suffix = name.rsplit("-", 1)
        if (
            stem
            and suffix.isdigit()
            and any(p.endswith(("pos", "neg")) for p in stem.split("_"))
        ):
            return pretty_method(stem)
        return name
    return pretty_method(name)

pretty_samples

pretty_samples(names: Iterable[str]) -> list[str]

Map :func:pretty_sample over a list/Series of sample names.

Source code in tcrsift/format.py
def pretty_samples(names: Iterable[str]) -> list[str]:
    """Map :func:`pretty_sample` over a list/Series of sample names."""
    return [pretty_sample(str(n)) for n in names]