ddmc.clustering¶
The core DDMC model and its supporting functions.
ddmc.clustering ¶
Dual data and motif clustering (DDMC).
Contains the DDMC model itself — a sklearn.mixture.GaussianMixture
subclass that jointly clusters peptides on their phosphorylation signal and
their sequence motif — and get_pspl_pssm_distances, the helper it uses to
compare cluster motifs against kinase specificity profiles.
DDMC ¶
DDMC(
n_components: int,
seq_weight: float,
distance_method: Literal[
"PAM250", "Binomial"
] = "Binomial",
random_state: int | RandomState | None = None,
max_iter: int = 200,
tol: float = 0.0001,
n_epochs: int = 10,
include_ptms: bool = True,
)
Bases: GaussianMixture
Cluster peptides by both sequence similarity and condition-wise phosphorylation following an expectation-maximization algorithm.
DDMC subclasses sklearn.mixture.GaussianMixture and reuses its EM
loop, but scores each peptide against each cluster using both the usual
Gaussian mixture log-probability over its phosphorylation signal and a
sequence-motif term (weighted by seq_weight), and refits both the
Gaussian mixture parameters and the per-cluster sequence motif at every
M step. See ddmc.binomial.Binomial and ddmc.pam250.PAM250 for the
two available motif models.
Attributes set by fit:
p_signal: The p_signal DataFrame passed to fit.
sequences: p_signal.index, as an upper-cased numpy array.
seq_dist: The fitted Binomial or PAM250 sequence-distance model.
scores_: Per-peptide, per-cluster responsibilities (soft cluster
assignments) of shape (n_peptides, n_components).
seq_scores_: Per-peptide, per-cluster weighted sequence
log-probabilities (seq_weight * seq_dist.logWeights) from the
last E step, of shape (n_peptides, n_components).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
The number of clusters to fit. |
required |
seq_weight
|
float
|
Weight applied to the sequence-motif log-probability
relative to the Gaussian mixture log-probability when
scoring each peptide against each cluster. |
required |
distance_method
|
Literal['PAM250', 'Binomial']
|
Which sequence-distance model to use for the
motif term: |
'Binomial'
|
random_state
|
int | RandomState | None
|
Seed or |
None
|
max_iter
|
int
|
Maximum number of EM iterations (kept for API compatibility). |
200
|
tol
|
float
|
Convergence threshold (kept for API compatibility). |
0.0001
|
n_epochs
|
int
|
Number of mini-batch EMA epochs to run before the final exact full-batch EM pass. |
10
|
Source code in ddmc/clustering.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
fit ¶
fit(p_signal: DataFrame) -> DDMC
Compute EM clustering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p_signal
|
DataFrame
|
Dataframe of shape (number of peptides, number of samples)
containing the phosphorylation signal. |
required |
Returns:
| Type | Description |
|---|---|
DDMC
|
self, fit to |
Source code in ddmc/clustering.py
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | |
fold_in ¶
fold_in(
p_signal_test: DataFrame,
cols: ndarray | Sequence[int] | None = None,
) -> np.ndarray
Compute out-of-sample posterior cluster responsibilities for unseen
peptides using only the specified sample columns cols and the
fitted sequence-motif model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p_signal_test
|
DataFrame
|
DataFrame of unseen peptides with length-11 sequences
as index and the same sample columns as |
required |
cols
|
ndarray | Sequence[int] | None
|
Integer column indices to condition on. If |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Posterior cluster responsibilities of shape |
ndarray
|
|
Source code in ddmc/clustering.py
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 | |
get_nonempty_clusters ¶
get_nonempty_clusters() -> np.ndarray
List the clusters that at least one peptide is assigned to.
Returns:
| Type | Description |
|---|---|
ndarray
|
Sorted array of the distinct cluster indices present in |
ndarray
|
|
ndarray
|
are empty. |
Source code in ddmc/clustering.py
862 863 864 865 866 867 868 869 870 | |
get_pssms ¶
get_pssms(
PsP_background: bool = False, clusters: None = None
) -> tuple[np.ndarray, np.ndarray]
get_pssms(
PsP_background: bool = False, *, clusters: list[int]
) -> np.ndarray
get_pssms(
PsP_background: bool = False,
clusters: list[int] | None = None,
) -> tuple[np.ndarray, np.ndarray] | np.ndarray
Compute position-specific scoring matrix of each cluster. Note, to normalize by amino acid frequency this uses either all the sequences in the data set or a collection of random MS phosphosites in PhosphoSitePlus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
PsP_background
|
bool
|
Whether or not PhosphoSitePlus should be used for background frequency. |
False
|
clusters
|
list[int] | None
|
cluster indices to get pssms for |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | ndarray
|
If the clusters argument is used, an array of shape (len(clusters), 20, 11), |
tuple[ndarray, ndarray] | ndarray
|
else two arrays, where the first (of shape (n_pssms,)) |
tuple[ndarray, ndarray] | ndarray
|
contains the clusters of the pssms in the second |
tuple[ndarray, ndarray] | ndarray
|
(of shape (n_pssms, 20, 11)). |
Source code in ddmc/clustering.py
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 | |
has_empty_clusters ¶
has_empty_clusters() -> bool
Checks whether the most recent call to fit() resulted in empty clusters.
Returns:
| Type | Description |
|---|---|
bool
|
True if any of the |
bool
|
assigned to it. |
Source code in ddmc/clustering.py
872 873 874 875 876 877 878 879 880 881 | |
impute ¶
impute(
mode: Literal["soft", "hard"] = "soft",
return_var: Literal[False] = False,
) -> pd.DataFrame
impute(
mode: Literal["soft", "hard"] = "soft",
*,
return_var: Literal[True],
) -> tuple[pd.DataFrame, pd.DataFrame]
impute(
mode: Literal["soft", "hard"] = "soft",
return_var: bool = False,
) -> pd.DataFrame | tuple[pd.DataFrame, pd.DataFrame]
Imputes missing values in the dataset passed in fit() and returns the imputed dataset (and optionally posterior predictive variances).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
Literal['soft', 'hard']
|
Imputation strategy for missing entries:
- |
'soft'
|
return_var
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame | tuple[DataFrame, DataFrame]
|
The imputed |
DataFrame | tuple[DataFrame, DataFrame]
|
when |
Source code in ddmc/clustering.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
labels ¶
labels() -> np.ndarray
Find cluster assignment with highest likelihood for each peptide.
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (n_peptides,) giving each peptide's cluster |
ndarray
|
index. Equivalent to |
Source code in ddmc/clustering.py
990 991 992 993 994 995 996 997 | |
predict ¶
predict() -> np.ndarray
Provided the current model parameters, predict the cluster each peptide belongs to.
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (n_peptides,) giving the index of the |
ndarray
|
highest-likelihood cluster for each peptide in |
Source code in ddmc/clustering.py
980 981 982 983 984 985 986 987 988 | |
predict_upstream_kinases ¶
predict_upstream_kinases(
PsP_background: bool = True,
metric: Literal[
"frobenius", "js", "kl", "cosine", "zscore"
] = "frobenius",
) -> pd.DataFrame
Compute matrix-matrix similarity between kinase specificity profiles and cluster PSSMs to identify upstream kinases regulating clusters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
PsP_background
|
bool
|
Whether or not PhosphoSitePlus should be used
for the background amino acid frequency when building each
cluster's PSSM (see |
True
|
metric
|
Literal['frobenius', 'js', 'kl', 'cosine', 'zscore']
|
Distance metric passed to |
'frobenius'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame of shape (n_kinases, n_nonempty_clusters) with the |
DataFrame
|
distance between each kinase's specificity profile and each |
DataFrame
|
cluster's PSSM; smaller values indicate a better match. |
Source code in ddmc/clustering.py
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 | |
score ¶
score() -> float
Generate score of the fitting.
Returns:
| Type | Description |
|---|---|
float
|
The lower bound on the log-likelihood of the fitted model |
float
|
( |
Source code in ddmc/clustering.py
999 1000 1001 1002 1003 1004 1005 1006 1007 | |
score_held_out ¶
score_held_out(
p_signal_test: DataFrame,
train_cols: ndarray | Sequence[int],
test_cols: ndarray | Sequence[int],
) -> tuple[float, float, int, float, float]
Evaluate bi-cross-validation metrics on held-out entries
(I_test, J_test) after folding in p_signal_test using train_cols.
Returns:
| Type | Description |
|---|---|
float
|
Tuple |
float
|
all observed entries in |
Source code in ddmc/clustering.py
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 | |
transform ¶
transform(as_df: Literal[False] = False) -> np.ndarray
transform(as_df: Literal[True]) -> pd.DataFrame
transform(as_df: bool = False) -> np.ndarray | pd.DataFrame
Return cluster centers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
as_df
|
bool
|
Whether or not the result should be wrapped in a dataframe with labeled axes. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | DataFrame
|
The cluster centers, either a np array or pd df of shape (n_samples, n_components). |
Source code in ddmc/clustering.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | |
get_pspl_pssm_distances ¶
get_pspl_pssm_distances(
pspls: ndarray,
pssms: ndarray,
as_df: Literal[False] = False,
pssm_names: Sequence | ndarray | None = None,
kinases: Sequence | ndarray | None = None,
metric: Literal[
"frobenius", "js", "kl", "cosine", "zscore"
] = "frobenius",
) -> np.ndarray
get_pspl_pssm_distances(
pspls: ndarray,
pssms: ndarray,
as_df: Literal[True],
pssm_names: Sequence | ndarray | None = None,
kinases: Sequence | ndarray | None = None,
metric: Literal[
"frobenius", "js", "kl", "cosine", "zscore"
] = "frobenius",
) -> pd.DataFrame
get_pspl_pssm_distances(
pspls: ndarray,
pssms: ndarray,
as_df: bool = False,
pssm_names: Sequence | ndarray | None = None,
kinases: Sequence | ndarray | None = None,
metric: Literal[
"frobenius", "js", "kl", "cosine", "zscore"
] = "frobenius",
) -> np.ndarray | pd.DataFrame
Computes a distance matrix between PSPLs and PSSMs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pspls
|
ndarray
|
kinase specificity profiles of shape (n_kinase, 20, 9) |
required |
pssms
|
ndarray
|
position-specific scoring matrices of shape (n_pssms, 20, 11) |
required |
as_df
|
bool
|
Whether or not the returned matrix should be returned as a dataframe. Requires pssm_names and kinases. |
False
|
pssm_names
|
Sequence | ndarray | None
|
list of names for the pssms of shape (n_pssms,) |
None
|
kinases
|
Sequence | ndarray | None
|
list of names for the pspls of shape (n_kinase,) |
None
|
metric
|
Literal['frobenius', 'js', 'kl', 'cosine', 'zscore']
|
Distance metric ( |
'frobenius'
|
Returns:
| Type | Description |
|---|---|
ndarray | DataFrame
|
Distance matrix of shape (n_kinase, n_pssms). |
Source code in ddmc/clustering.py
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 | |