Sequence distance models¶
DDMC supports two interchangeable ways of scoring how well a peptide
sequence matches a cluster's motif, selected via DDMC(..., distance_method=...).
ddmc.binomial¶
ddmc.binomial ¶
Binomial sequence-distance model used by ddmc.clustering.DDMC.
Contains
AAfreq/AAlist: reference amino acid frequencies and the fixed amino acid ordering used throughout the package.- Position weight matrix helpers (
position_weight_matrix,fast_position_weight_matrix,frequencies,GenerateBinarySeqID). - Background phosphosite sequence sampling from PhosphoSitePlus
(
BackgroundSeqs,BackgProportions,CountPsiteTypes, and their cached loaders). - The
Binomialclass: for each cluster, models how enriched each amino acid is at each position (relative to the background) using the binomial-probability approach of Schwartz & Gygi, Nat Biotechnol 2005 (doi:10.1038/nbt1146), and scores every peptide sequence against each cluster's model.
Binomial ¶
Binomial(seqs: ndarray, include_ptms: bool = False)
Binomial sequence-distance model, used by ddmc.clustering.DDMC when
distance_method="Binomial".
For each cluster, scores how enriched each amino acid is at each position of a peptide's sequence relative to a background distribution of phosphosites, following Schwartz & Gygi, Nat Biotechnol 2005 (doi:10.1038/nbt1146).
Attributes:
| Name | Type | Description |
|---|---|---|
background |
Background PWM (amino acid frequency per position) of
shape (len(AAlist), n_pos), built from |
|
n_aa |
Number of amino acids (len(AAlist)). |
|
n_pos |
Number of sequence positions (11). |
|
foreground_flat |
Flattened one-hot encoding of |
|
logWeights |
Log-probability of each sequence under each cluster's
current binomial model, of shape (n_seqs, n_clusters). Set to
the scalar |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seqs
|
ndarray
|
The length-11 peptide sequences being clustered. |
required |
include_ptms
|
bool
|
If |
False
|
Source code in ddmc/binomial.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
compute_log_weights ¶
compute_log_weights() -> None
Update self.logWeights for all sequences from self.probmat_flat.
Source code in ddmc/binomial.py
461 462 463 464 | |
estimate_log_prob_batch ¶
estimate_log_prob_batch(batch_idx: ndarray) -> np.ndarray
Evaluate sequence log-probabilities on a mini-batch of peptides.
Source code in ddmc/binomial.py
456 457 458 459 | |
finalize_ema ¶
finalize_ema() -> None
Recompute probmat_flat from the current EMA sufficient statistics.
Source code in ddmc/binomial.py
452 453 454 | |
from_summaries ¶
from_summaries(weightsIn: ndarray) -> None
Refit each cluster's binomial model from the current soft cluster
assignments, and update self.logWeights with each sequence's
log-probability under its (updated) cluster model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weightsIn
|
ndarray
|
Soft cluster assignments (responsibilities) of shape
(n_seqs, n_clusters), i.e. |
required |
Source code in ddmc/binomial.py
481 482 483 484 485 486 487 488 489 490 491 492 493 | |
init_ema ¶
init_ema(n_clusters: int) -> None
Initialize EMA sufficient-statistic accumulators for mini-batch fitting.
Source code in ddmc/binomial.py
421 422 423 424 425 426 | |
score_sequences ¶
score_sequences(seqs: list[str] | ndarray) -> np.ndarray
Score unseen length-11 peptide sequences against the fitted cluster binomial models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seqs
|
list[str] | ndarray
|
Peptide sequences to score. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log-probabilities of shape |
Source code in ddmc/binomial.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
split_cluster ¶
split_cluster(donor: int, target: int) -> None
Halve donor's EMA sufficient statistics into target and donor.
Source code in ddmc/binomial.py
443 444 445 446 447 448 449 450 | |
update_ema ¶
update_ema(
batch_idx: ndarray,
weights_batch: ndarray,
alpha: float,
scale: float = 1.0,
) -> None
Update EMA sufficient statistics from a mini-batch of responsibilities.
Source code in ddmc/binomial.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
BackgroundSeqs ¶
BackgroundSeqs(forseqs: ndarray) -> list[str]
Build a background data set of length-11 phosphosite motifs sampled from PhosphoSitePlus, matching the proportion of pY, pT, and pS sites found in the foreground set of sequences.
Note this PsP data set contains 51976 pY, 226131 pS, 81321 pT Source: https://www.phosphosite.org/staticDownloads.action - Phosphorylation_site_dataset.gz - Last mod: Wed Dec 04 14:56:35 EST 2019 Cite: Hornbeck PV, Zhang B, Murray B, Kornhauser JM, Latham V, Skrzypek E PhosphoSitePlus, 2014: mutations, PTMs and recalibrations. Nucleic Acids Res. 2015 43:D512-20. PMID: 25514926
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forseqs
|
ndarray
|
The foreground peptide sequences whose pY/pS/pT proportions the background set should match. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Length-11 background peptide sequences sampled from PhosphoSitePlus, |
list[str]
|
with the phosphoacceptor lowercased, in pY/pS/pT order. |
Source code in ddmc/binomial.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
BackgProportions ¶
BackgProportions(
refseqs: list[str], pYn: int, pSn: int, pTn: int
) -> list[str]
Slice length-11 motifs out of the +/-7 AA reference sequences, keeping up to the requested number of pY, pS, and pT sites.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refseqs
|
list[str]
|
Raw +/-7 AA PhosphoSitePlus reference sequences. |
required |
pYn
|
int
|
Maximum number of pY motifs to keep. |
required |
pSn
|
int
|
Maximum number of pS motifs to keep. |
required |
pTn
|
int
|
Maximum number of pT motifs to keep. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The length-11 background motifs (phosphoacceptor lowercased), |
list[str]
|
concatenated in pY, pS, pT order. |
Source code in ddmc/binomial.py
323 324 325 326 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 | |
CountPsiteTypes ¶
CountPsiteTypes(X) -> tuple[int, int, int]
Count the number of different phosphorylation types in an MS data set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
list[str]
|
The list of peptide sequences. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
tuple[int, int, int]: The number of pY, pS, and pT sites. |
Source code in ddmc/binomial.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |
position_weight_matrix ¶
position_weight_matrix(
seqs: list[str],
pseudoC: OrderedDict[str, float] = AAfreq,
) -> Any
Build a position weight matrix (PWM) of a given set of same-length sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seqs
|
list[str]
|
Sequences (all the same length) to build the PWM from. |
required |
pseudoC
|
OrderedDict[str, float]
|
Per-amino-acid pseudocounts to add before normalizing,
keyed by one-letter amino acid code. Defaults to |
AAfreq
|
Returns:
| Type | Description |
|---|---|
Any
|
A Biopython |
Any
|
position, normalized to sum to 1 down each column) of shape |
Any
|
(len(AAlist), sequence length). |
Source code in ddmc/binomial.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
fast_position_weight_matrix ¶
fast_position_weight_matrix(
seqs: list[str], include_ptms: bool = False
) -> np.ndarray
Build a (n_aa, seq_length) PWM of a given set of same-length
sequences, equivalent to position_weight_matrix but without the
overhead of Biopython's general-purpose alignment machinery.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seqs
|
list[str]
|
Sequences, all of the same length, to build the PWM from. |
required |
include_ptms
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
ndarray
|
sequence length) |
ndarray
|
pseudocount-smoothed frequency of each token at each position. |
Source code in ddmc/binomial.py
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 | |
frequencies ¶
frequencies(seqs: list[str]) -> Any
Build a per-position amino acid counts matrix of a given set of same-length sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seqs
|
list[str]
|
Sequences, all of the same length, to count. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A Biopython |
Any
|
amino acid at each position across |
Source code in ddmc/binomial.py
169 170 171 172 173 174 175 176 177 178 179 | |
GenerateBinarySeqID ¶
GenerateBinarySeqID(
seqs: list[str] | ndarray, include_ptms: bool = False
) -> np.ndarray
Build a one-hot encoding of amino acid (or flanking PTM) identity at each position, for every sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seqs
|
list[str] | ndarray
|
Length-11 peptide sequences to encode. |
required |
include_ptms
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Boolean array of shape |
ndarray
|
|
Source code in ddmc/binomial.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
ddmc.pam250¶
ddmc.pam250 ¶
PAM250 sequence-distance model used by ddmc.clustering.DDMC.
Contains the PAM250 class, which scores peptide sequences against each
cluster by their average PAM250 substitution-matrix similarity to the other
sequences currently assigned to that cluster, and get_pam250_scores, which
precomputes the full pairwise PAM250 similarity matrix used to do so.
PAM250 ¶
PAM250(
seqs: list[str] | ndarray, include_ptms: bool = False
)
PAM250 sequence-distance model, used by ddmc.clustering.DDMC when
distance_method="PAM250".
Scores each peptide sequence against a cluster by its (responsibility weighted) average pairwise PAM250 substitution score against every other sequence, using the fixed set of pairwise scores computed once at construction time.
Attributes:
| Name | Type | Description |
|---|---|---|
background |
Pairwise PAM250 similarity matrix between all input sequences, of shape (n_seqs, n_seqs). |
|
logWeights |
Log-probability (average PAM250 score) of each sequence
under each cluster's current model, of shape
(n_seqs, n_clusters). Set to the scalar |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seqs
|
list[str] | ndarray
|
The length-11 peptide sequences being clustered. |
required |
include_ptms
|
bool
|
If |
False
|
Source code in ddmc/pam250.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
compute_log_weights ¶
compute_log_weights() -> None
Update self.logWeights for all sequences from the current EMA.
Source code in ddmc/pam250.py
138 139 140 141 | |
estimate_log_prob_batch ¶
estimate_log_prob_batch(batch_idx: ndarray) -> np.ndarray
Evaluate sequence log-probabilities on a mini-batch of peptides.
Source code in ddmc/pam250.py
133 134 135 136 | |
finalize_ema ¶
finalize_ema() -> None
No-op for PAM250 (cluster ratios are computed on demand).
Source code in ddmc/pam250.py
130 131 | |
from_summaries ¶
from_summaries(weightsIn: ndarray) -> None
Update self.logWeights with each sequence's responsibility
weighted average PAM250 similarity to all sequences, per cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weightsIn
|
ndarray
|
Soft cluster assignments (responsibilities) of shape
(n_seqs, n_clusters), i.e. |
required |
Source code in ddmc/pam250.py
159 160 161 162 163 164 165 166 167 168 169 170 171 | |
init_ema ¶
init_ema(n_clusters: int) -> None
Initialize EMA sufficient-statistic accumulators for mini-batch fitting.
Source code in ddmc/pam250.py
102 103 104 105 | |
score_sequences ¶
score_sequences(seqs: list[str] | ndarray) -> np.ndarray
Score unseen length-11 peptide sequences against the fitted cluster PAM250 similarity profiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seqs
|
list[str] | ndarray
|
Peptide sequences to score. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Average PAM250 similarity scores of shape |
Source code in ddmc/pam250.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
split_cluster ¶
split_cluster(donor: int, target: int) -> None
Halve donor's EMA sufficient statistics into target and donor.
Source code in ddmc/pam250.py
121 122 123 124 125 126 127 128 | |
update_ema ¶
update_ema(
batch_idx: ndarray,
weights_batch: ndarray,
alpha: float,
scale: float = 1.0,
) -> None
Update EMA sufficient statistics from a mini-batch of responsibilities.
Source code in ddmc/pam250.py
107 108 109 110 111 112 113 114 115 116 117 118 119 | |
get_pam250_scores ¶
get_pam250_scores(
seqs: list[str] | ndarray,
seqs_b: list[str] | ndarray | None = None,
include_ptms: bool = False,
) -> np.ndarray
Compute the pairwise PAM250 similarity matrix between sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seqs
|
list[str] | ndarray
|
Sequences (all the same length) to score pairwise. |
required |
seqs_b
|
list[str] | ndarray | None
|
Optional second sequence list. If omitted, computes the
symmetric |
None
|
include_ptms
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
ndarray
|
when |
ndarray
|
substitution score between |
ndarray
|
(aligned position-by-position, no gaps). |
Source code in ddmc/pam250.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |