Sequence probability: Pgen and Ppost¶
Sequence-background scores provide a precursor-frequency/publicness axis:
- Pgen asks how probable a CDR3 is under a generated-repertoire background.
- Ppost asks how common it is under an observed healthy-repertoire background.
Both are ranking proxies, not specificity or avidity measurements. The shipped k-mer scores are natural-log probabilities; lower (more negative) values mean a rarer/private sequence.
Backends and shipped models¶
Both backends implement SequenceProbabilityModel (fit, log_prob, save,
and load):
| Backend | Notes |
|---|---|
KmerProbabilityModel (default) |
Fast order-k Markov model over CDR3 amino acids, with length represented by an end symbol and optional V/J marginals |
TCRpegProbabilityModel |
Autoregressive PyTorch model; heavier and useful when trained on a sufficiently large reference |
TCRpeg and PyTorch are core dependencies. No tcrsift[tcrpeg] extra is
required.
Packaged files are role- and chain-specific:
kmer_pgen_alpha.npz,kmer_pgen_beta.npzkmer_ppost_alpha.npz,kmer_ppost_beta.npz
Pgen references were generated offline with OLGA; tcrsift does not import OLGA
at runtime. Ppost references come from pooled observed healthy PBMC
repertoires. See tcrsift/refseqs/PROVENANCE.md in the source distribution for
the exact data and calibration notes.
Usage¶
Add both roles per chain:
from tcrsift import add_pgen_ppost
clones = add_pgen_ppost(clones, backend="kmer")
# pgen_alpha, ppost_alpha, pgen_beta, ppost_beta are ln(probability)
Score only one role:
from tcrsift.seqprob import score_log_prob
clones["log_ppost_beta"] = score_log_prob(
clones,
chain="beta",
role="ppost",
)
CLI:
Use Ppost as a cohort-relative ranking signal or through
select_specificity_candidates; do not copy a threshold calibrated on a
different model/reference without validation. Ppost also partly reflects CDR3
length, so review length and V/J usage alongside it.
seqprob ¶
Data-driven CDR3 sequence-probability models — the publicness axis.
The data-driven, dependency-light publicness axis (it replaced an earlier
OLGA/SONIA runtime path, since removed) for the precursor-frequency /
publicness measure. Instead of a fixed,
allele-masked GPL prior, a background generation/occurrence model is fit
once on an external reference repertoire and reused; log_pgen(seq) is
then a fast, dependency-light, calibrated score for "how generatable /
common is this CDR3" — lower = more private / rarer precursor.
Two interchangeable backends behind one :class:SequenceProbabilityModel
interface:
- :class:
KmerProbabilityModel— an order-kMarkov model over CDR3 amino acids (numpy-only, no GPL, the default). The shipped default models in :mod:tcrsift.refseqsare fit offline on OLGA-generated synthetic repertoires (OLGA used once at build time to produce training sequences — never at runtime, so tcrsift stays Apache-2.0). - :class:
TCRpegProbabilityModel— wraps TCRpeg (Jiang & Li 2023), an autoregressive deep model. TCRpeg and PyTorch are core dependencies.
Both are trained on an external reference (not the experiment's own clones) so the probability is a genuine background, not circular with the selection target.
SequenceProbabilityModel ¶
Bases: ABC
A fittable per-sequence log-probability model over CDR3 strings.
Source code in tcrsift/seqprob.py
KmerProbabilityModel ¶
Bases: SequenceProbabilityModel
Order-k Markov model over CDR3 amino acids (numpy-only).
log P(CDR3) = Σ_i log P(a_i | a_{i-k} … a_{i-1}) with the sequence
padded by order BOS sentinels and terminated by EOS, so both the
composition and the length are captured. Add-alpha (Laplace)
smoothing keeps unseen contexts from giving -inf.
Parameters are a dense (N_SYM**order, N_SYM) log-probability table,
compact enough to ship: the shipped defaults are order 2 (~20-32 KB
float32 per chain); order 3 would be ~1 MB.
Source code in tcrsift/seqprob.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
GeneAwareKmerModel ¶
Bases: SequenceProbabilityModel
Gene-aware Ppost: logP(V) + logP(J) + logP_kmer(CDR3).
All three terms come from one reference repertoire: V and J from
Laplace-smoothed gene marginals (so an unseen gene gets a finite tail
probability, never zero), and the CDR3 from an order-k Markov model
(default order 2 — most data-efficient below ~10⁵ reference seqs).
Gene names are canonicalized via :func:tcrsift.genes.canonicalize_gene
so format variants (alleles, Adaptive, TRAV14/DV4 vs TRAV14DV4)
all resolve.
log_prob scores gene-aware when V/J are supplied and degrades to the
CDR3-only k-mer score when they aren't. NB (the caveat we agreed): don't
benchmark a gene-aware score with TRAV12-2 AUROC — the label is the
V gene; validate against a V-gene-independent publicness label.
Source code in tcrsift/seqprob.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
TCRpegProbabilityModel ¶
Bases: SequenceProbabilityModel
TCRpeg-backed CDR3 probability.
Wraps the autoregressive TCRpeg model (Jiang & Li 2023). Heavier
(PyTorch) but better-calibrated than the k-mer Markov model. Trained on
the same external reference. Lazy import; raises :class:ImportError
with an install hint when the core dependency is missing.
Source code in tcrsift/seqprob.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
load_background_model ¶
load_background_model(chain: str = 'beta', backend: str = 'kmer', role: str = 'ppost') -> SequenceProbabilityModel
Load (and cache) a shipped default background model.
role is "ppost" (default — fit on an observed repertoire, the
post-selection publicness measure) or "pgen" (fit on an
OLGA-generated reference, pre-selection generation probability). Only the
"kmer" backend ships defaults. Role-pure: raises
:class:FileNotFoundError when the requested backend/role/chain model is
not shipped — it never silently returns a Pgen model in place of Ppost.
Callers decide how to degrade.
Source code in tcrsift/seqprob.py
score_log_prob ¶
score_log_prob(df: DataFrame, *, chain: str = 'beta', cdr3_col: str | None = None, v_gene_col: str | None = None, j_gene_col: str | None = None, backend: str = 'kmer', role: str = 'ppost', model: SequenceProbabilityModel | None = None, out_col: str | None = None) -> pd.Series
Per-clone natural-log probability under a background model.
role="ppost" (default) scores against the observed-repertoire model
(post-selection publicness); role="pgen" against the generated model.
Uses model if given, else the shipped default for (chain, backend,
role). cdr3_col defaults to CDR3_<chain>.
When the model is gene-aware (a :class:GeneAwareKmerModel with V/J
marginals) and the V/J columns are present (default <chain>_v_gene /
<chain>_j_gene), the score includes logP(V) + logP(J) — gene names
are canonicalized inside the model. Returns a Series aligned to df.
Source code in tcrsift/seqprob.py
score_log_pgen ¶
score_log_pgen(df: DataFrame, *, chain: str = 'beta', cdr3_col: str | None = None, backend: str = 'kmer', model: SequenceProbabilityModel | None = None, out_col: str = 'log_pgen') -> pd.Series
Per-clone log Pgen (generated-repertoire background). See
:func:score_log_prob.