Heuristic Generation Probability (tcrsift.pgen)¶
tcrsift.pgen implements a fast, dependency-free generation-probability
heuristic over CDR3 amino-acid length, V/J gene marginals, and estimated
junctional N-insertions, used by annotate_clonotypes(add_publicness=True) to
flag likely public CDR3s when reviewing database matches.
For new ranking workflows, prefer the fitted k-mer and TCRpeg models in
tcrsift.seqprob (especially observed-repertoire Ppost). Do
not transfer numeric cutoffs between tcrsift.pgen and tcrsift.seqprob, as
their log-probability scales differ.
Rationale and decomposition¶
Exact V(D)J dynamic programming in OLGA (Sethna et al., 2019) requires ~30 MB
of model tables and a GPL runtime dependency. When only approximate rank order
between germline-like public CDR3s and high-insertion private CDR3s is needed,
tcrsift.pgen decomposes base-10 log probability (log10_pgen) as:
log10 Pgen(CDR3, V, J, chain)
≈ log10 P(V) # V-gene marginal
+ log10 P(J) # J-gene marginal
+ log10 P(length) # Chain-specific CDR3 AA length
+ log10 P(n_inserted) # Geometric N-insertion model (1 junction in α, 2 in β)
+ log10 P(CDR3 | length) # Uniform 20-amino-acid baseline
Estimated non-templated nucleotide insertions (n_inserted, inferred from CDR3
length minus typical germline V/D/J contribution) carry the largest weight, as
zero-insertion rearrangements arise convergently across donors. Both human
(species="human") and mouse (species="mouse", using murine TRAV/TRAJ and
TRBV/TRBJ usage priors and case-insensitive gene normalization) are
supported.
Scale and calibration¶
Because of the uniform amino-acid baseline, scores sit roughly 10 log10 units
(orders of magnitude) below exact OLGA log10 Pgen. Default publicness_score
bounds (-30 / -18) are calibrated to this module's log10 scale; pass
-20 / -8 when scoring exact OLGA log10 Pgen outputs, or set
auto_quantile=True for cohort-quantile scaling.
Usage¶
from tcrsift import annotate_clonotypes
from tcrsift.pgen import annotate_publicness
clones = annotate_publicness(clones)
public = clones[clones["publicness"] >= 0.5]
# Or enable directly during database annotation:
annotated = annotate_clonotypes(
clones,
vdjdb_path="vdjdb.tsv",
add_publicness=True,
)
pgen ¶
Lightweight Pgen estimator for TCR β / α CDR3 sequences (#58).
The gold-standard generation-probability tool is OLGA (Sethna et al. 2019), which fits V/D/J/insertion models and computes Pgen by dynamic programming. OLGA gives the right answer but ships ~30 MB of pre-trained model files plus a runtime dependency chain.
Most tcrsift use cases (publicness discounting in DB annotation, ranking convergent vs. private sequences) just need a coarse Pgen proxy — values that rank sequences from "highly generatable" to "clearly private", not necessarily numerically calibrated. This module provides that proxy in <200 lines, with no runtime dependency beyond numpy.
Decomposition:
log Pgen(CDR3, V, J, chain)
≈ log P(V) # gene-usage marginal
+ log P(J) # gene-usage marginal
+ log P(length) # CDR3 AA length, per chain
+ log P(n_inserted) # estimated total non-templated nt
+ log P(CDR3 | length) # length-normalized AA-composition term
For an observed sequence, n_inserted is estimated from the CDR3
nucleotide length minus a typical templated boundary contribution
(see :data:_TEMPLATED_NT_TYPICAL). When the caller doesn't have
nucleotide-level data, we approximate from AA length.
Marginal parameters bundled inline (not as external data files) — they're small (<3 KB total), and inlining keeps the module self-contained and easy to audit. Sources are documented per-table.
For real Pgens use OLGA; for ranking/discounting :func:compute_pgen
is sufficient.
pgen_single ¶
pgen_single(cdr3_aa: str, v_gene: str | None, j_gene: str | None, *, chain: str = 'beta', n_inserted: int | None = None, species: str = 'human') -> float
Return log10 Pgen estimate for one sequence.
Source code in tcrsift/pgen.py
pgen_components ¶
pgen_components(cdr3_aa: str, v_gene: str | None, j_gene: str | None, *, chain: str = 'beta', n_inserted: int | None = None, species: str = 'human') -> dict[str, float]
Decompose log10 Pgen into its five terms.
Useful for debugging: shows which term is driving a sequence toward "public" or "private".
Source code in tcrsift/pgen.py
compute_pgen ¶
compute_pgen(df: DataFrame, *, cdr3_col: str = 'CDR3_beta', v_gene_col: str = 'beta_v_gene', j_gene_col: str = 'beta_j_gene', chain: str = 'beta', n_inserted_col: str | None = None, output_col: str = 'log10_pgen', species: str = 'human') -> pd.Series
Compute log10 Pgen estimate for each row in df.
Source code in tcrsift/pgen.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
publicness_score ¶
publicness_score(log10_pgen: Series | Iterable[float], *, low_pgen_cutoff: float | None = -30.0, high_pgen_cutoff: float | None = -18.0, auto_quantile: bool = False, quantile_low: float = 0.1, quantile_high: float = 0.9) -> pd.Series
Map log10 Pgen → [0, 1] publicness in a monotone-decreasing way.
log10 Pgen ≥ high_pgen_cutoff(very generatable) → 1.0log10 Pgen ≤ low_pgen_cutoff(very private) → 0.0- linear interpolation in between.
A high publicness score means the sequence is likely to be public and DB matches against it should be discounted.
Defaults are calibrated to this module's coarse Pgen estimator, not OLGA. Typical β CDR3s land around log10 ≈ −25; very public short sequences with canonical V/J around log10 ≈ −18. The estimator is uniformly more negative than OLGA because its AA composition term is uniform-over-20 rather than position-specific. Override the cutoffs if you're feeding in real OLGA values (use ~−20 / −8 for OLGA).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log10_pgen
|
Series | Iterable[float]
|
Per-row log10 Pgen estimates. When a Series, its index is preserved in the returned Series. |
required |
low_pgen_cutoff
|
float | None
|
Fixed cutoffs. Ignored when |
-30.0
|
high_pgen_cutoff
|
float | None
|
Fixed cutoffs. Ignored when |
-30.0
|
auto_quantile
|
bool
|
When True, derive cutoffs from the input distribution rather than a fixed pair. Useful when the input's Pgen scale is unknown or when the fixed cutoffs would saturate the score for most rows (e.g. a narrow distribution all within ~5 log units). |
False
|
quantile_low
|
float
|
Quantile cutoffs used when |
0.1
|
quantile_high
|
float
|
Quantile cutoffs used when |
0.1
|
Source code in tcrsift/pgen.py
annotate_publicness ¶
annotate_publicness(df: DataFrame, *, cdr3_col: str = 'CDR3_beta', v_gene_col: str = 'beta_v_gene', j_gene_col: str = 'beta_j_gene', chain: str = 'beta', pgen_col: str = 'log10_pgen', publicness_col: str = 'publicness', species: str = 'human') -> pd.DataFrame
Augment df with log10_pgen + publicness columns.
Convenience wrapper combining :func:compute_pgen and
:func:publicness_score. Does not modify the input frame.