triger 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- triger/__init__.py +16 -0
- triger/association.py +170 -0
- triger/greedy_peeling.py +405 -0
- triger/plotting.py +222 -0
- triger-1.0.0.dist-info/METADATA +133 -0
- triger-1.0.0.dist-info/RECORD +8 -0
- triger-1.0.0.dist-info/WHEEL +4 -0
- triger-1.0.0.dist-info/licenses/LICENSE +21 -0
triger/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""TriGer: structured cross-omics interaction discovery with a triple-graph model."""
|
|
2
|
+
from .greedy_peeling import (Block, Community, greedy_peeling_XY_one,
|
|
3
|
+
greedy_peeling_XY_all, greedy_peeling_X_one,
|
|
4
|
+
greedy_peeling_X_all, sort_result,
|
|
5
|
+
sort_result_XandY, greedy_3step)
|
|
6
|
+
from .association import (Association, Module, TrigerResult,
|
|
7
|
+
association_matrices, triger)
|
|
8
|
+
from .plotting import plot3in1_v1, plot_all_result, matlab_jet
|
|
9
|
+
|
|
10
|
+
__version__ = "1.0.0"
|
|
11
|
+
__all__ = ["triger", "association_matrices", "greedy_3step",
|
|
12
|
+
"greedy_peeling_XY_one", "greedy_peeling_XY_all",
|
|
13
|
+
"greedy_peeling_X_one", "greedy_peeling_X_all", "sort_result",
|
|
14
|
+
"sort_result_XandY", "plot3in1_v1", "plot_all_result",
|
|
15
|
+
"matlab_jet", "Block", "Community", "Module", "Association",
|
|
16
|
+
"TrigerResult"]
|
triger/association.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TriGer on data: association matrices for two omics layers and the full
|
|
3
|
+
pipeline. All indices are 0-based.
|
|
4
|
+
"""
|
|
5
|
+
from typing import List, NamedTuple, Optional
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
from scipy import stats
|
|
9
|
+
|
|
10
|
+
from .greedy_peeling import _as_matrix, greedy_3step
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Association(NamedTuple):
|
|
14
|
+
"""Association matrices over the features of two omics layers: signed
|
|
15
|
+
correlations ``R``, -log10 p-values ``P``, feature names, and the
|
|
16
|
+
numbers of X and Y features."""
|
|
17
|
+
R: np.ndarray
|
|
18
|
+
P: np.ndarray
|
|
19
|
+
names: List[str]
|
|
20
|
+
m: int
|
|
21
|
+
n: int
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Module(NamedTuple):
|
|
25
|
+
"""A cross-omics module: X and Y indices, the penalized density of the
|
|
26
|
+
step-1 block, and the X and Y feature names."""
|
|
27
|
+
X: np.ndarray
|
|
28
|
+
Y: np.ndarray
|
|
29
|
+
density: float
|
|
30
|
+
X_names: List[str]
|
|
31
|
+
Y_names: List[str]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class TrigerResult(NamedTuple):
|
|
35
|
+
"""Output of :func:`triger`: the modules and the association matrices."""
|
|
36
|
+
modules: List[Module]
|
|
37
|
+
R: np.ndarray
|
|
38
|
+
P: np.ndarray
|
|
39
|
+
names: List[str]
|
|
40
|
+
m: int
|
|
41
|
+
n: int
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _feature_names(x, prefix):
|
|
45
|
+
cols = getattr(x, "columns", None)
|
|
46
|
+
if cols is not None:
|
|
47
|
+
return [str(c) for c in cols]
|
|
48
|
+
return [f"{prefix}{i}" for i in range(np.shape(x)[1])]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def association_matrices(X, Y, method="pearson") -> Association:
|
|
52
|
+
"""Association matrices for two omics layers.
|
|
53
|
+
|
|
54
|
+
Computes the signed correlation matrix and the matrix of -log10
|
|
55
|
+
two-sided p-values over the features of two omics layers measured on
|
|
56
|
+
the same samples. These are the inputs ``R`` and ``P`` of
|
|
57
|
+
:func:`triger.greedy_3step`.
|
|
58
|
+
|
|
59
|
+
The p-value of a correlation coefficient r estimated from N samples is
|
|
60
|
+
computed from ``t = |r| * sqrt((N - 2) / (1 - r**2))`` with N - 2
|
|
61
|
+
degrees of freedom, as in ``scipy.stats.pearsonr`` (for Spearman
|
|
62
|
+
correlations, the same t approximation). ``r**2`` is capped at
|
|
63
|
+
``1 - 1e-12`` so that perfectly correlated features receive a large
|
|
64
|
+
finite value, and the diagonal of P is set to 0.
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
X : array_like or pandas.DataFrame, shape (N, m)
|
|
69
|
+
First omics layer, samples in rows and features in columns.
|
|
70
|
+
Y : array_like or pandas.DataFrame, shape (N, n)
|
|
71
|
+
Second omics layer, with the same samples in the same order.
|
|
72
|
+
method : {"pearson", "spearman"}
|
|
73
|
+
Correlation coefficient.
|
|
74
|
+
|
|
75
|
+
Returns
|
|
76
|
+
-------
|
|
77
|
+
Association
|
|
78
|
+
``R`` and ``P``, both ``(m + n, m + n)`` with the m features of X
|
|
79
|
+
first; ``names``, the column names of X and Y for DataFrames and
|
|
80
|
+
``X0, X1, ...`` and ``Y0, Y1, ...`` otherwise; ``m`` and ``n``.
|
|
81
|
+
"""
|
|
82
|
+
if method not in ("pearson", "spearman"):
|
|
83
|
+
raise ValueError("`method` must be 'pearson' or 'spearman'")
|
|
84
|
+
names = _feature_names(X, "X") + _feature_names(Y, "Y")
|
|
85
|
+
X = _as_matrix(X, "X")
|
|
86
|
+
Y = _as_matrix(Y, "Y")
|
|
87
|
+
if X.shape[0] != Y.shape[0]:
|
|
88
|
+
raise ValueError("`X` and `Y` must have the same number of samples "
|
|
89
|
+
"(rows)")
|
|
90
|
+
N = X.shape[0]
|
|
91
|
+
if N < 3:
|
|
92
|
+
raise ValueError("at least 3 samples are required")
|
|
93
|
+
XY = np.hstack([X, Y])
|
|
94
|
+
constant = np.all(XY == XY[0], axis=0)
|
|
95
|
+
if constant.any():
|
|
96
|
+
bad = [names[i] for i in np.flatnonzero(constant)[:5]]
|
|
97
|
+
raise ValueError("features with zero variance: " + ", ".join(bad))
|
|
98
|
+
if method == "spearman":
|
|
99
|
+
XY = np.apply_along_axis(stats.rankdata, 0, XY)
|
|
100
|
+
R = np.corrcoef(XY, rowvar=False)
|
|
101
|
+
t = np.abs(R) * np.sqrt((N - 2) / (1 - np.minimum(R ** 2, 1 - 1e-12)))
|
|
102
|
+
log_p = np.log(2) + stats.t.logsf(t, df=N - 2)
|
|
103
|
+
P = -log_p / np.log(10)
|
|
104
|
+
np.fill_diagonal(P, 0.0)
|
|
105
|
+
return Association(R, P, names, X.shape[1], Y.shape[1])
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _match_samples(X, Y):
|
|
109
|
+
ix = getattr(X, "index", None)
|
|
110
|
+
iy = getattr(Y, "index", None)
|
|
111
|
+
if ix is None or iy is None:
|
|
112
|
+
return Y
|
|
113
|
+
if (not ix.is_unique or not iy.is_unique
|
|
114
|
+
or set(ix) != set(iy)):
|
|
115
|
+
raise ValueError("the sample names (index) of `X` and `Y` must be "
|
|
116
|
+
"unique and identical")
|
|
117
|
+
return Y.loc[ix]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def triger(X, Y, lambda1=1.5, lambda2=1.5, lambda3=1.5, X_size=(10, 200),
|
|
121
|
+
Y_size=(10, 200), method="pearson", **kwargs) -> TrigerResult:
|
|
122
|
+
"""Detect cross-omics modules with TriGer.
|
|
123
|
+
|
|
124
|
+
Runs the TriGer pipeline on two omics layers measured on the same
|
|
125
|
+
samples. :func:`association_matrices` computes the cross- and
|
|
126
|
+
within-omics correlations and their -log10 p-values, and
|
|
127
|
+
:func:`triger.greedy_3step` finds blocks of X and Y features that are
|
|
128
|
+
densely associated across the two layers and refines large blocks by
|
|
129
|
+
within-omics peeling.
|
|
130
|
+
|
|
131
|
+
When X and Y are both pandas DataFrames, samples are matched by index
|
|
132
|
+
and Y is reordered to the sample order of X. Otherwise, the samples must
|
|
133
|
+
be in the same order.
|
|
134
|
+
|
|
135
|
+
Parameters
|
|
136
|
+
----------
|
|
137
|
+
X, Y : array_like or pandas.DataFrame
|
|
138
|
+
The two omics layers, samples in rows and features in columns.
|
|
139
|
+
lambda1 : float
|
|
140
|
+
Cross-omics size penalty (step 1). Higher values give smaller,
|
|
141
|
+
denser blocks.
|
|
142
|
+
lambda2, lambda3 : float
|
|
143
|
+
Within-omics size penalties for X and Y (steps 2 and 3).
|
|
144
|
+
X_size, Y_size : (min, max)
|
|
145
|
+
Passed to :func:`triger.greedy_3step`: blocks with ``min`` or fewer
|
|
146
|
+
features on either side are dropped, and sides with more than
|
|
147
|
+
``max`` features are refined.
|
|
148
|
+
method : {"pearson", "spearman"}
|
|
149
|
+
Correlation coefficient.
|
|
150
|
+
**kwargs
|
|
151
|
+
Further arguments to :func:`triger.greedy_3step`:
|
|
152
|
+
``min_community_size``, ``keep_unrefined_if_empty``, ``max_blocks``.
|
|
153
|
+
|
|
154
|
+
Returns
|
|
155
|
+
-------
|
|
156
|
+
TrigerResult
|
|
157
|
+
``modules``, a list of :class:`Module` records with indices into
|
|
158
|
+
the features of X and Y and their names, and the association
|
|
159
|
+
matrices ``R``, ``P``, ``names``, ``m`` and ``n``.
|
|
160
|
+
"""
|
|
161
|
+
Y = _match_samples(X, Y)
|
|
162
|
+
assoc = association_matrices(X, Y, method=method)
|
|
163
|
+
m, n = assoc.m, assoc.n
|
|
164
|
+
blocks = greedy_3step(assoc.P, assoc.R, m, n, lambda1=lambda1,
|
|
165
|
+
lambda2=lambda2, lambda3=lambda3, X_size=X_size,
|
|
166
|
+
Y_size=Y_size, **kwargs)
|
|
167
|
+
x_names, y_names = assoc.names[:m], assoc.names[m:]
|
|
168
|
+
modules = [Module(b.X, b.Y, b.density, [x_names[i] for i in b.X],
|
|
169
|
+
[y_names[j] for j in b.Y]) for b in blocks]
|
|
170
|
+
return TrigerResult(modules, *assoc)
|
triger/greedy_peeling.py
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Greedy peeling for TriGer: cross-omics (X-Y) and within-omics (symmetric)
|
|
3
|
+
peeling, block sorting, and greedy_3step, the full pipeline on precomputed
|
|
4
|
+
association matrices. All indices are 0-based.
|
|
5
|
+
"""
|
|
6
|
+
from typing import List, NamedTuple, Optional, Sequence, Tuple
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Block(NamedTuple):
|
|
12
|
+
"""A cross-omics block: X (row) indices, Y (column) indices and the
|
|
13
|
+
penalized density of the block."""
|
|
14
|
+
X: np.ndarray
|
|
15
|
+
Y: np.ndarray
|
|
16
|
+
density: float
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Community(NamedTuple):
|
|
20
|
+
"""A within-omics community: feature indices and penalized density."""
|
|
21
|
+
X: np.ndarray
|
|
22
|
+
density: float
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# ----------------------------------------------------------------------
|
|
26
|
+
# Input checks
|
|
27
|
+
# ----------------------------------------------------------------------
|
|
28
|
+
def _as_matrix(x, name):
|
|
29
|
+
try:
|
|
30
|
+
x = np.asarray(x, dtype=np.float64)
|
|
31
|
+
except (TypeError, ValueError):
|
|
32
|
+
raise ValueError(f"`{name}` must be a numeric matrix") from None
|
|
33
|
+
if x.ndim != 2 or x.shape[0] == 0 or x.shape[1] == 0:
|
|
34
|
+
raise ValueError(f"`{name}` must be a matrix with at least one row "
|
|
35
|
+
"and one column")
|
|
36
|
+
if not np.all(np.isfinite(x)):
|
|
37
|
+
raise ValueError(f"`{name}` must not contain missing or infinite "
|
|
38
|
+
"values")
|
|
39
|
+
return x
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _check_symmetric(x, name):
|
|
43
|
+
if x.shape[0] != x.shape[1] or not np.allclose(x, x.T, rtol=0,
|
|
44
|
+
atol=1e-8):
|
|
45
|
+
raise ValueError(f"`{name}` must be symmetric")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _check_number(x, name):
|
|
49
|
+
if (isinstance(x, bool) or not isinstance(x, (int, float, np.number))
|
|
50
|
+
or not np.isfinite(x)):
|
|
51
|
+
raise ValueError(f"`{name}` must be a finite number")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _check_count(x, name, minimum=1):
|
|
55
|
+
if (isinstance(x, bool) or not isinstance(x, (int, np.integer))
|
|
56
|
+
or x < minimum):
|
|
57
|
+
raise ValueError(f"`{name}` must be an integer of at least {minimum}")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _check_size(x, name):
|
|
61
|
+
try:
|
|
62
|
+
lo, hi = x
|
|
63
|
+
except (TypeError, ValueError):
|
|
64
|
+
raise ValueError(f"`{name}` must be (min, max)") from None
|
|
65
|
+
if not (np.isfinite(lo) and np.isfinite(hi)) or lo > hi:
|
|
66
|
+
raise ValueError(f"`{name}` must be (min, max) with min <= max")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# ----------------------------------------------------------------------
|
|
70
|
+
# Step 1: cross-omics (X x Y) peeling
|
|
71
|
+
# ----------------------------------------------------------------------
|
|
72
|
+
def greedy_peeling_XY_one(Wp, lam):
|
|
73
|
+
"""Peel one dense block from a non-negative cross-omics matrix.
|
|
74
|
+
|
|
75
|
+
Greedily removes the X feature (row) or Y feature (column) with the
|
|
76
|
+
weakest remaining association and keeps the intermediate submatrix with
|
|
77
|
+
the highest penalized density, ``sum / (|X| * |Y|) ** (lam / 2)``. A row
|
|
78
|
+
is removed when its sum, scaled by the current ratio ``|X| / |Y|``, is
|
|
79
|
+
at most the smallest column sum; otherwise a column is removed.
|
|
80
|
+
|
|
81
|
+
Parameters
|
|
82
|
+
----------
|
|
83
|
+
Wp : array_like, shape (m, n)
|
|
84
|
+
Non-negative cross-omics associations (X features in rows, Y
|
|
85
|
+
features in columns), for example -log10 p-values.
|
|
86
|
+
lam : float
|
|
87
|
+
Size penalty, typically in [1, 2]. Higher values give smaller,
|
|
88
|
+
denser blocks.
|
|
89
|
+
|
|
90
|
+
Returns
|
|
91
|
+
-------
|
|
92
|
+
density : float
|
|
93
|
+
Penalized density of the block.
|
|
94
|
+
X, Y : ndarray
|
|
95
|
+
Row and column indices of the block.
|
|
96
|
+
"""
|
|
97
|
+
Wp = _as_matrix(Wp, "Wp")
|
|
98
|
+
_check_number(lam, "lam")
|
|
99
|
+
m, n = Wp.shape
|
|
100
|
+
C = Wp.sum(axis=0)
|
|
101
|
+
R = Wp.sum(axis=1)
|
|
102
|
+
rem_X = np.zeros(m, dtype=bool)
|
|
103
|
+
rem_Y = np.zeros(n, dtype=bool)
|
|
104
|
+
best_rem_X, best_rem_Y = rem_X.copy(), rem_Y.copy()
|
|
105
|
+
len_X, len_Y = m, n
|
|
106
|
+
W_density = 0.0
|
|
107
|
+
|
|
108
|
+
for _ in range(m + n):
|
|
109
|
+
Cc = np.where(rem_Y, np.inf, C)
|
|
110
|
+
Rr = np.where(rem_X, np.inf, R)
|
|
111
|
+
IndT = int(np.argmin(Cc))
|
|
112
|
+
dT = Cc[IndT]
|
|
113
|
+
IndS = int(np.argmin(Rr))
|
|
114
|
+
dS = Rr[IndS]
|
|
115
|
+
if not (np.isfinite(dT) and np.isfinite(dS)):
|
|
116
|
+
break
|
|
117
|
+
if (len_X / len_Y) * dS <= dT: # remove a row (X feature)
|
|
118
|
+
rem_X[IndS] = True
|
|
119
|
+
C = C - Wp[IndS, :]
|
|
120
|
+
C[rem_Y] = 0.0
|
|
121
|
+
Wp_sum = C.sum()
|
|
122
|
+
len_X -= 1
|
|
123
|
+
else: # remove a column (Y feature)
|
|
124
|
+
rem_Y[IndT] = True
|
|
125
|
+
R = R - Wp[:, IndT]
|
|
126
|
+
R[rem_X] = 0.0
|
|
127
|
+
Wp_sum = R.sum()
|
|
128
|
+
len_Y -= 1
|
|
129
|
+
if len_X <= 0 or len_Y <= 0:
|
|
130
|
+
break
|
|
131
|
+
score = Wp_sum / (len_X * len_Y) ** (lam / 2.0)
|
|
132
|
+
if np.isfinite(score) and score > W_density:
|
|
133
|
+
W_density = float(score)
|
|
134
|
+
best_rem_X, best_rem_Y = rem_X.copy(), rem_Y.copy()
|
|
135
|
+
|
|
136
|
+
return W_density, np.flatnonzero(~best_rem_X), np.flatnonzero(~best_rem_Y)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def greedy_peeling_XY_all(XY, lam=1.5, max_blocks=5) -> List[Block]:
|
|
140
|
+
"""Peel dense blocks from a cross-omics matrix repeatedly.
|
|
141
|
+
|
|
142
|
+
Applies :func:`greedy_peeling_XY_one` repeatedly, removing the rows and
|
|
143
|
+
columns of each accepted block before the next round. A block is
|
|
144
|
+
accepted while its penalized density exceeds that of the full matrix, up
|
|
145
|
+
to ``max_blocks`` blocks.
|
|
146
|
+
|
|
147
|
+
Parameters
|
|
148
|
+
----------
|
|
149
|
+
XY : array_like, shape (m, n)
|
|
150
|
+
Non-negative cross-omics association matrix.
|
|
151
|
+
lam : float
|
|
152
|
+
Size penalty.
|
|
153
|
+
max_blocks : int
|
|
154
|
+
Maximum number of blocks returned.
|
|
155
|
+
|
|
156
|
+
Returns
|
|
157
|
+
-------
|
|
158
|
+
list of Block
|
|
159
|
+
Blocks with indices into the rows (``X``) and columns (``Y``) of XY.
|
|
160
|
+
"""
|
|
161
|
+
XY = _as_matrix(XY, "XY")
|
|
162
|
+
_check_number(lam, "lam")
|
|
163
|
+
_check_count(max_blocks, "max_blocks")
|
|
164
|
+
m, n = XY.shape
|
|
165
|
+
org_density = XY.sum() / (m * n) ** (lam / 2.0)
|
|
166
|
+
remain_X, remain_Y = np.arange(m), np.arange(n)
|
|
167
|
+
result: List[Block] = []
|
|
168
|
+
while len(result) < max_blocks and len(remain_X) and len(remain_Y):
|
|
169
|
+
dens, sX, sY = greedy_peeling_XY_one(XY[np.ix_(remain_X, remain_Y)],
|
|
170
|
+
lam)
|
|
171
|
+
if dens <= org_density or len(sX) == 0 or len(sY) == 0:
|
|
172
|
+
break
|
|
173
|
+
blk = Block(remain_X[sX], remain_Y[sY], dens)
|
|
174
|
+
result.append(blk)
|
|
175
|
+
remain_X = np.setdiff1d(remain_X, blk.X)
|
|
176
|
+
remain_Y = np.setdiff1d(remain_Y, blk.Y)
|
|
177
|
+
return result
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# ----------------------------------------------------------------------
|
|
181
|
+
# Steps 2 and 3: within-omics (symmetric) peeling
|
|
182
|
+
# ----------------------------------------------------------------------
|
|
183
|
+
def greedy_peeling_X_one(Wp, lam):
|
|
184
|
+
"""Peel one dense community from a symmetric within-omics matrix.
|
|
185
|
+
|
|
186
|
+
Greedily removes the feature with the smallest remaining sum and keeps
|
|
187
|
+
the intermediate subset with the highest penalized density,
|
|
188
|
+
``sum / size ** lam``.
|
|
189
|
+
|
|
190
|
+
Parameters
|
|
191
|
+
----------
|
|
192
|
+
Wp : array_like, shape (m, m)
|
|
193
|
+
Symmetric within-omics associations, for example signed
|
|
194
|
+
correlations.
|
|
195
|
+
lam : float
|
|
196
|
+
Size penalty.
|
|
197
|
+
|
|
198
|
+
Returns
|
|
199
|
+
-------
|
|
200
|
+
density : float
|
|
201
|
+
Penalized density of the community.
|
|
202
|
+
X : ndarray
|
|
203
|
+
Indices of the community.
|
|
204
|
+
"""
|
|
205
|
+
Wp = _as_matrix(Wp, "Wp")
|
|
206
|
+
_check_symmetric(Wp, "Wp")
|
|
207
|
+
_check_number(lam, "lam")
|
|
208
|
+
m = Wp.shape[0]
|
|
209
|
+
C = Wp.sum(axis=0)
|
|
210
|
+
rem = np.zeros(m, dtype=bool)
|
|
211
|
+
best_rem = rem.copy()
|
|
212
|
+
W_density = 0.0
|
|
213
|
+
for i in range(1, m + 1):
|
|
214
|
+
Ind = int(np.argmin(np.where(rem, np.inf, C)))
|
|
215
|
+
rem[Ind] = True
|
|
216
|
+
C = C - Wp[Ind, :]
|
|
217
|
+
C[rem] = 0.0
|
|
218
|
+
if i == m:
|
|
219
|
+
break
|
|
220
|
+
score = C.sum() / (m - i) ** lam
|
|
221
|
+
if np.isfinite(score) and score > W_density:
|
|
222
|
+
W_density = float(score)
|
|
223
|
+
best_rem = rem.copy()
|
|
224
|
+
return W_density, np.flatnonzero(~best_rem)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def greedy_peeling_X_all(Wp, lam, min_size=1) -> List[Community]:
|
|
228
|
+
"""Peel dense communities from a within-omics matrix repeatedly.
|
|
229
|
+
|
|
230
|
+
Applies :func:`greedy_peeling_X_one` repeatedly, removing each accepted
|
|
231
|
+
community before the next round, while the community's penalized
|
|
232
|
+
density exceeds that of the full matrix.
|
|
233
|
+
|
|
234
|
+
Parameters
|
|
235
|
+
----------
|
|
236
|
+
Wp : array_like, shape (m, m)
|
|
237
|
+
Symmetric within-omics association matrix.
|
|
238
|
+
lam : float
|
|
239
|
+
Size penalty.
|
|
240
|
+
min_size : int
|
|
241
|
+
Smallest community returned. Communities with fewer features are
|
|
242
|
+
peeled but not returned. The default, 1, returns every community.
|
|
243
|
+
|
|
244
|
+
Returns
|
|
245
|
+
-------
|
|
246
|
+
list of Community
|
|
247
|
+
Communities with indices into the rows of Wp.
|
|
248
|
+
"""
|
|
249
|
+
Wp = _as_matrix(Wp, "Wp")
|
|
250
|
+
_check_symmetric(Wp, "Wp")
|
|
251
|
+
_check_number(lam, "lam")
|
|
252
|
+
_check_count(min_size, "min_size")
|
|
253
|
+
m = Wp.shape[0]
|
|
254
|
+
org_density = Wp.sum() / m ** lam
|
|
255
|
+
remain = np.arange(m)
|
|
256
|
+
result: List[Community] = []
|
|
257
|
+
while len(remain):
|
|
258
|
+
dens, sel = greedy_peeling_X_one(Wp[np.ix_(remain, remain)], lam)
|
|
259
|
+
if dens <= org_density or len(sel) == 0:
|
|
260
|
+
break
|
|
261
|
+
comm = Community(remain[sel], dens)
|
|
262
|
+
result.append(comm)
|
|
263
|
+
remain = np.setdiff1d(remain, comm.X)
|
|
264
|
+
return [c for c in result if len(c.X) >= min_size]
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
# ----------------------------------------------------------------------
|
|
268
|
+
# Ordering features within blocks
|
|
269
|
+
# ----------------------------------------------------------------------
|
|
270
|
+
def sort_result(Wp, blocks, symmetric: Optional[bool] = None):
|
|
271
|
+
"""Order the features of each block by their within-block sums in Wp.
|
|
272
|
+
|
|
273
|
+
For a cross-omics Wp, rows and columns are ordered by the absolute
|
|
274
|
+
values of their sums. For a within-omics (symmetric) Wp, pass
|
|
275
|
+
communities and ``symmetric=True`` (the default for square matrices).
|
|
276
|
+
|
|
277
|
+
Returns
|
|
278
|
+
-------
|
|
279
|
+
list
|
|
280
|
+
``blocks`` with the features of each block reordered; records keep
|
|
281
|
+
their type (Block, Community or tuple).
|
|
282
|
+
"""
|
|
283
|
+
Wp = np.asarray(Wp, dtype=np.float64)
|
|
284
|
+
if symmetric is None:
|
|
285
|
+
symmetric = Wp.shape[0] == Wp.shape[1]
|
|
286
|
+
out = []
|
|
287
|
+
for blk in blocks:
|
|
288
|
+
if symmetric:
|
|
289
|
+
idx = np.asarray(blk[0], dtype=int)
|
|
290
|
+
sums = Wp[np.ix_(idx, idx)].sum(axis=1)
|
|
291
|
+
new = (idx[np.argsort(-sums, kind="stable")],) + tuple(blk[1:])
|
|
292
|
+
else:
|
|
293
|
+
xi = np.asarray(blk[0], dtype=int)
|
|
294
|
+
yi = np.asarray(blk[1], dtype=int)
|
|
295
|
+
sub = Wp[np.ix_(xi, yi)]
|
|
296
|
+
ox = np.argsort(-np.abs(sub.sum(axis=1)), kind="stable")
|
|
297
|
+
oy = np.argsort(-np.abs(sub.sum(axis=0)), kind="stable")
|
|
298
|
+
new = (xi[ox], yi[oy]) + tuple(blk[2:])
|
|
299
|
+
out.append(type(blk)(*new) if hasattr(blk, "_fields") else new)
|
|
300
|
+
return out
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def sort_result_XandY(X, Y, blocks):
|
|
304
|
+
"""Order each block's X features by their sums within ``X[xi, xi]`` and
|
|
305
|
+
its Y features by their sums within ``Y[yi, yi]``."""
|
|
306
|
+
X = np.asarray(X, dtype=np.float64)
|
|
307
|
+
Y = np.asarray(Y, dtype=np.float64)
|
|
308
|
+
out = []
|
|
309
|
+
for blk in blocks:
|
|
310
|
+
xi = np.asarray(blk[0], dtype=int)
|
|
311
|
+
yi = np.asarray(blk[1], dtype=int)
|
|
312
|
+
ox = np.argsort(-X[np.ix_(xi, xi)].sum(axis=0), kind="stable")
|
|
313
|
+
oy = np.argsort(-Y[np.ix_(yi, yi)].sum(axis=0), kind="stable")
|
|
314
|
+
new = (xi[ox], yi[oy]) + tuple(blk[2:])
|
|
315
|
+
out.append(type(blk)(*new) if hasattr(blk, "_fields") else new)
|
|
316
|
+
return out
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
# ----------------------------------------------------------------------
|
|
320
|
+
# The full pipeline on precomputed matrices
|
|
321
|
+
# ----------------------------------------------------------------------
|
|
322
|
+
def greedy_3step(P, R, m, n, lambda1=1.5, lambda2=1.5, lambda3=1.5,
|
|
323
|
+
X_size: Tuple[float, float] = (10, 200),
|
|
324
|
+
Y_size: Tuple[float, float] = (10, 200),
|
|
325
|
+
min_community_size=2, keep_unrefined_if_empty=True,
|
|
326
|
+
max_blocks=5) -> List[Block]:
|
|
327
|
+
"""Detect cross-omics modules from association matrices.
|
|
328
|
+
|
|
329
|
+
Step 1 peels dense blocks from the cross-omics part of P with
|
|
330
|
+
:func:`greedy_peeling_XY_all`. Blocks with ``X_size[0]`` or fewer X
|
|
331
|
+
features, or ``Y_size[0]`` or fewer Y features, are dropped. In steps 2
|
|
332
|
+
and 3, the X and Y sides of blocks with more than ``X_size[1]`` or
|
|
333
|
+
``Y_size[1]`` features are refined by within-omics peeling of the signed
|
|
334
|
+
correlations in R: the side is replaced by the communities with at least
|
|
335
|
+
``min_community_size`` features, each ordered by within-community sums.
|
|
336
|
+
:func:`triger.triger` computes P and R from data and calls this
|
|
337
|
+
function.
|
|
338
|
+
|
|
339
|
+
Parameters
|
|
340
|
+
----------
|
|
341
|
+
P : array_like, shape (m + n, m + n)
|
|
342
|
+
Non-negative association matrix over the X and Y features (for
|
|
343
|
+
example -log10 p-values of their correlations); only ``P[:m, m:]``
|
|
344
|
+
is used.
|
|
345
|
+
R : array_like, shape (m + n, m + n)
|
|
346
|
+
Signed correlation matrix over the X and Y features, used for
|
|
347
|
+
refinement.
|
|
348
|
+
m, n : int
|
|
349
|
+
Numbers of X and Y features.
|
|
350
|
+
lambda1 : float
|
|
351
|
+
Cross-omics size penalty (step 1).
|
|
352
|
+
lambda2, lambda3 : float
|
|
353
|
+
Within-omics size penalties for X and Y (steps 2 and 3).
|
|
354
|
+
X_size, Y_size : (min, max)
|
|
355
|
+
Blocks with ``min`` or fewer features on either side are dropped,
|
|
356
|
+
and sides with more than ``max`` features are refined.
|
|
357
|
+
min_community_size : int
|
|
358
|
+
Smallest within-omics community kept during refinement.
|
|
359
|
+
keep_unrefined_if_empty : bool
|
|
360
|
+
If refinement keeps no community, keep that side of the block
|
|
361
|
+
unrefined (True) or empty it (False).
|
|
362
|
+
max_blocks : int
|
|
363
|
+
Maximum number of blocks found in step 1.
|
|
364
|
+
|
|
365
|
+
Returns
|
|
366
|
+
-------
|
|
367
|
+
list of Block
|
|
368
|
+
Modules with ``X`` in ``0..m-1``, ``Y`` in ``0..n-1``, and
|
|
369
|
+
``density`` the penalized density of the step-1 block before
|
|
370
|
+
refinement.
|
|
371
|
+
"""
|
|
372
|
+
P = _as_matrix(P, "P")
|
|
373
|
+
R = _as_matrix(R, "R")
|
|
374
|
+
_check_count(m, "m")
|
|
375
|
+
_check_count(n, "n")
|
|
376
|
+
if P.shape != (m + n, m + n):
|
|
377
|
+
raise ValueError("`P` must be (m + n) x (m + n)")
|
|
378
|
+
if R.shape != (m + n, m + n):
|
|
379
|
+
raise ValueError("`R` must be (m + n) x (m + n)")
|
|
380
|
+
_check_size(X_size, "X_size")
|
|
381
|
+
_check_size(Y_size, "Y_size")
|
|
382
|
+
_check_count(min_community_size, "min_community_size")
|
|
383
|
+
|
|
384
|
+
blocks = greedy_peeling_XY_all(P[:m, m:], lambda1, max_blocks)
|
|
385
|
+
blocks = [b for b in blocks
|
|
386
|
+
if len(b.X) > X_size[0] and len(b.Y) > Y_size[0]]
|
|
387
|
+
|
|
388
|
+
def refine(idx, offset, lam):
|
|
389
|
+
sub = R[np.ix_(offset + idx, offset + idx)]
|
|
390
|
+
sub = (sub + sub.T) / 2.0
|
|
391
|
+
comm = greedy_peeling_X_all(sub, lam, min_size=min_community_size)
|
|
392
|
+
if not comm:
|
|
393
|
+
return idx if keep_unrefined_if_empty else idx[:0]
|
|
394
|
+
comm = sort_result(sub, comm, symmetric=True)
|
|
395
|
+
return idx[np.concatenate([c.X for c in comm])]
|
|
396
|
+
|
|
397
|
+
out = []
|
|
398
|
+
for b in blocks:
|
|
399
|
+
xi, yi = b.X, b.Y
|
|
400
|
+
if len(xi) > X_size[1]:
|
|
401
|
+
xi = refine(xi, 0, lambda2)
|
|
402
|
+
if len(yi) > Y_size[1]:
|
|
403
|
+
yi = refine(yi, m, lambda3)
|
|
404
|
+
out.append(Block(xi, yi, b.density))
|
|
405
|
+
return out
|
triger/plotting.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Plotting functions for TriGer results (matplotlib).
|
|
3
|
+
|
|
4
|
+
`result` is a list of (X_idx, Y_idx, ...) tuples, as returned by the peeling
|
|
5
|
+
functions; for a symmetric matrix, (idx, ...) tuples.
|
|
6
|
+
Indices are 0-based unless one_based=True.
|
|
7
|
+
"""
|
|
8
|
+
import numpy as np
|
|
9
|
+
import matplotlib as mpl
|
|
10
|
+
import matplotlib.pyplot as plt
|
|
11
|
+
from matplotlib.collections import PolyCollection
|
|
12
|
+
from matplotlib.colors import ListedColormap, Normalize
|
|
13
|
+
from matplotlib.patches import Rectangle
|
|
14
|
+
|
|
15
|
+
MATLAB_GREY = (0.15, 0.15, 0.15)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def matlab_jet(m=256):
|
|
19
|
+
"""Exact port of MATLAB's jet(m) colormap (R2019b+ default length 256)."""
|
|
20
|
+
n = int(np.ceil(m / 4))
|
|
21
|
+
u = np.concatenate([np.arange(1, n + 1) / n, np.ones(n - 1), np.arange(n, 0, -1) / n])
|
|
22
|
+
g = int(np.ceil(n / 2)) - (m % 4 == 1) + np.arange(1, len(u) + 1)
|
|
23
|
+
r = g + n
|
|
24
|
+
b = g - n
|
|
25
|
+
r = r[r <= m]
|
|
26
|
+
g = g[g <= m]
|
|
27
|
+
b = b[b >= 1]
|
|
28
|
+
J = np.zeros((m, 3))
|
|
29
|
+
J[r - 1, 0] = u[:len(r)]
|
|
30
|
+
J[g - 1, 1] = u[:len(g)]
|
|
31
|
+
J[b - 1, 2] = u[len(u) - len(b):]
|
|
32
|
+
return ListedColormap(J, name="matlab_jet")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _unique_keep_last(v):
|
|
36
|
+
"""MATLAB: [~,idx]=unique(flip(v),'stable'); v=flip(v(end-idx+1))
|
|
37
|
+
-> keep the LAST occurrence of each value, preserving order."""
|
|
38
|
+
rev = v[::-1]
|
|
39
|
+
_, first = np.unique(rev, return_index=True)
|
|
40
|
+
return rev[np.sort(first)][::-1]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _as_idx(x):
|
|
44
|
+
return np.atleast_1d(np.asarray(x, dtype=int)).ravel() if x is not None else np.array([], int)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def plot3in1_v1(X, Y, XY, result, line=False, step=None, bar=False,
|
|
48
|
+
one_based=False, figsize=(5.6, 4.2), cbar_pos=(0.15, 0.13, 0.013, 0.45),
|
|
49
|
+
clim_tri=None, clim_mid=None, box_lw=2):
|
|
50
|
+
"""
|
|
51
|
+
X, Y : square matrices (X is m x m, Y is n x n)
|
|
52
|
+
XY : m x n matrix
|
|
53
|
+
result : list of blocks (X_indices, Y_indices, ...), as returned by
|
|
54
|
+
greedy_peeling_XY_all, sort_result or greedy_3step.
|
|
55
|
+
line : draw red rectangles around blocks
|
|
56
|
+
step : (xs, ys) tick step; default (100, 10) as in MATLAB
|
|
57
|
+
bar : add colorbar for the middle panel (MATLAB: nargin == 7)
|
|
58
|
+
one_based : set True for 1-based indices
|
|
59
|
+
cbar_pos : colorbar [left bottom width height] in figure units, or "right" = just right
|
|
60
|
+
of the plot (past the right triangle tip), bottom/height aligned with the middle square
|
|
61
|
+
(MATLAB hard-coded value by default)
|
|
62
|
+
clim_tri : colour limits of the two triangles (MATLAB: [-1 1])
|
|
63
|
+
box_lw : line width of the red block boxes (MATLAB: 2)
|
|
64
|
+
clim_mid : colour limits of the middle panel (MATLAB: [0 10] if max(A)>1 else [-1 1])
|
|
65
|
+
"""
|
|
66
|
+
result = [(_as_idx(r[0]), _as_idx(r[1])) for r in result]
|
|
67
|
+
off = 1 if one_based else 0
|
|
68
|
+
select_X = np.concatenate([r[0] for r in result]) - off
|
|
69
|
+
select_Y = np.concatenate([r[1] for r in result]) - off
|
|
70
|
+
select_X = _unique_keep_last(select_X)
|
|
71
|
+
select_Y = _unique_keep_last(select_Y)
|
|
72
|
+
|
|
73
|
+
X, Y, XY = np.asarray(X), np.asarray(Y), np.asarray(XY)
|
|
74
|
+
C = Y[np.ix_(select_Y, select_Y)]
|
|
75
|
+
B = X[np.ix_(select_X, select_X)]
|
|
76
|
+
A = XY[np.ix_(select_X, select_Y)]
|
|
77
|
+
|
|
78
|
+
m, n = A.shape
|
|
79
|
+
ratio = m / n / 1.2
|
|
80
|
+
|
|
81
|
+
cmap = matlab_jet(256)
|
|
82
|
+
with mpl.rc_context({
|
|
83
|
+
"font.family": "sans-serif",
|
|
84
|
+
"font.sans-serif": ["Helvetica", "Arial", "Liberation Sans", "DejaVu Sans"],
|
|
85
|
+
"font.size": 10,
|
|
86
|
+
"xtick.color": MATLAB_GREY, "ytick.color": MATLAB_GREY,
|
|
87
|
+
"xtick.labelcolor": MATLAB_GREY, "ytick.labelcolor": MATLAB_GREY,
|
|
88
|
+
"xtick.direction": "in", "ytick.direction": "in",
|
|
89
|
+
"axes.linewidth": 0.5, "axes.unicode_minus": False, "xtick.major.width": 0.5, "ytick.major.width": 0.5,
|
|
90
|
+
}):
|
|
91
|
+
fig = plt.figure(figsize=figsize, facecolor="white")
|
|
92
|
+
ax = fig.add_axes([0.13, 0.11, 0.775, 0.815]) # MATLAB default axes Position
|
|
93
|
+
|
|
94
|
+
# ---------- Right: diamonds for Y (lower triangle) ----------
|
|
95
|
+
vx = np.array([0, 1, 0, -1]); vy = np.array([1, 0, -1, 0])
|
|
96
|
+
polys, cols = [], []
|
|
97
|
+
for i in range(1, n + 1):
|
|
98
|
+
for j in range(i, n + 1):
|
|
99
|
+
x0 = i - 1 + m * 2 / ratio
|
|
100
|
+
y0 = 2 * n - j * 2 + i
|
|
101
|
+
polys.append(np.column_stack([x0 + vx, y0 + vy]))
|
|
102
|
+
cols.append(C[j - 1, j - i])
|
|
103
|
+
norm1 = Normalize(*(clim_tri or (-1, 1)), clip=True) # caxis(ax1, [-1 1])
|
|
104
|
+
pc_right = PolyCollection(polys, array=np.array(cols), cmap=cmap, norm=norm1,
|
|
105
|
+
edgecolors="face", linewidths=0.2, zorder=1)
|
|
106
|
+
ax.add_collection(pc_right)
|
|
107
|
+
|
|
108
|
+
# ---------- Top: diamonds for X (lower triangle) ----------
|
|
109
|
+
vx = np.array([0, 1, 0, -1]) / ratio; vy = np.array([1, 0, -1, 0]) / ratio
|
|
110
|
+
polys, cols = [], []
|
|
111
|
+
for i in range(1, m + 1):
|
|
112
|
+
for j in range(i, m + 1):
|
|
113
|
+
x0 = (j * 2 - i) / ratio
|
|
114
|
+
y0 = (i - 1) / ratio + n * 2
|
|
115
|
+
polys.append(np.column_stack([x0 + vx, y0 + vy]))
|
|
116
|
+
cols.append(B[j - 1, j - i])
|
|
117
|
+
pc_top = PolyCollection(polys, array=np.array(cols), cmap=cmap, norm=norm1,
|
|
118
|
+
edgecolors="face", linewidths=0.2, zorder=1)
|
|
119
|
+
ax.add_collection(pc_top)
|
|
120
|
+
|
|
121
|
+
# ---------- Middle square: XY ----------
|
|
122
|
+
vxs = np.array([0, 2 / ratio, 2 / ratio, 0]); vys = np.array([0, 0, 2, 2])
|
|
123
|
+
polys, cols = [], []
|
|
124
|
+
for i in range(1, m + 1):
|
|
125
|
+
for j in range(1, n + 1):
|
|
126
|
+
x0 = (i - 1) * 2 / ratio
|
|
127
|
+
y0 = (j - 1) * 2
|
|
128
|
+
polys.append(np.column_stack([x0 + vxs, y0 + vys]))
|
|
129
|
+
cols.append(A[i - 1, n - j])
|
|
130
|
+
clim2 = clim_mid or ((0, 10) if np.nanmax(A) > 1 else (-1, 1)) # same rule as MATLAB
|
|
131
|
+
pc_mid = PolyCollection(polys, array=np.array(cols), cmap=cmap,
|
|
132
|
+
norm=Normalize(*clim2, clip=True),
|
|
133
|
+
edgecolors="face", linewidths=0.2, zorder=2)
|
|
134
|
+
ax.add_collection(pc_mid)
|
|
135
|
+
|
|
136
|
+
# ---------- axis equal tight + linkaxes (union of both data ranges) ----------
|
|
137
|
+
ax.set_xlim(0, max(2 * m / ratio, n + 2 * m / ratio))
|
|
138
|
+
ax.set_ylim(0, 2 * n + m / ratio)
|
|
139
|
+
ax.set_aspect("equal", adjustable="box", anchor="C")
|
|
140
|
+
for s in ax.spines.values(): # Axle 'none' + box off
|
|
141
|
+
s.set_visible(False)
|
|
142
|
+
ax.set_facecolor("none")
|
|
143
|
+
|
|
144
|
+
# ---------- Clean tick labels ----------
|
|
145
|
+
xs, ys = (100, 10) if step is None else (step[0], step[1])
|
|
146
|
+
clean = lambda maxv, base: np.arange(base, np.floor(maxv / base) * base + 1, base)
|
|
147
|
+
xTickIdx = clean(m, xs).astype(int)
|
|
148
|
+
yTickIdx = clean(n, ys).astype(int)[::-1]
|
|
149
|
+
ax.set_xticks((xTickIdx - 1) * 2 / ratio, [str(v) for v in xTickIdx])
|
|
150
|
+
ax.set_yticks((n - yTickIdx) * 2 + 1, [str(v) for v in yTickIdx])
|
|
151
|
+
ax.tick_params(top=False, right=False)
|
|
152
|
+
|
|
153
|
+
# MATLAB TickLength 0.01 of the longest axes side
|
|
154
|
+
ax.apply_aspect()
|
|
155
|
+
bb = ax.get_position()
|
|
156
|
+
longest_pt = max(bb.width * figsize[0], bb.height * figsize[1]) * 72
|
|
157
|
+
ax.tick_params(length=0.01 * longest_pt)
|
|
158
|
+
|
|
159
|
+
if bar:
|
|
160
|
+
if isinstance(cbar_pos, str) and cbar_pos == "right":
|
|
161
|
+
sq_h = bb.height * (2 * n) / (2 * n + m / ratio) # middle-square height
|
|
162
|
+
cbar_pos = (bb.x1 + 0.02, bb.y0, 0.013, sq_h)
|
|
163
|
+
cax = fig.add_axes(list(cbar_pos))
|
|
164
|
+
cb = fig.colorbar(pc_mid, cax=cax)
|
|
165
|
+
cb.ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(lambda v, p: f"{v:g}"))
|
|
166
|
+
cb.outline.set_linewidth(0.5)
|
|
167
|
+
cb.outline.set_edgecolor(MATLAB_GREY)
|
|
168
|
+
cax.tick_params(direction="in", length=0.01 * longest_pt,
|
|
169
|
+
color=MATLAB_GREY, labelcolor=MATLAB_GREY)
|
|
170
|
+
|
|
171
|
+
# ---------- Lines separating blocks ----------
|
|
172
|
+
if line:
|
|
173
|
+
L = np.array([[len(r[0]), len(r[1])] for r in result], dtype=float)
|
|
174
|
+
L[:, 0] *= 2 / ratio
|
|
175
|
+
L[:, 1] *= 2
|
|
176
|
+
x, y = 0.0, L[:, 1].sum()
|
|
177
|
+
i = 0
|
|
178
|
+
while i < len(L) and L[i, 0] != 0:
|
|
179
|
+
ax.add_patch(Rectangle((x, y - L[i, 1]), L[i, 0], L[i, 1], fill=False,
|
|
180
|
+
edgecolor=(1, 0, 0), linewidth=box_lw, zorder=3))
|
|
181
|
+
y -= L[i, 1]
|
|
182
|
+
x += L[i, 0]
|
|
183
|
+
i += 1
|
|
184
|
+
|
|
185
|
+
return fig, ax
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def plot_all_result(Wp, result, full=False, line=True, crange=None, figsize=(8, 6)):
|
|
189
|
+
"""Heatmap of Wp reordered by the detected communities.
|
|
190
|
+
full=True shows the whole matrix (selected features first) with one box
|
|
191
|
+
around the selected region; full=False shows only the selected region
|
|
192
|
+
with one box per community."""
|
|
193
|
+
Wp = np.asarray(Wp)
|
|
194
|
+
m, n = Wp.shape
|
|
195
|
+
if m == n:
|
|
196
|
+
result = [(r[0], r[0]) for r in result]
|
|
197
|
+
rows = [_as_idx(r[0]) for r in result]
|
|
198
|
+
cols = [_as_idx(r[1]) for r in result]
|
|
199
|
+
sel_X = np.concatenate(rows) if rows else np.array([], int)
|
|
200
|
+
sel_Y = np.concatenate(cols) if cols else np.array([], int)
|
|
201
|
+
if full:
|
|
202
|
+
M = Wp[np.ix_(np.r_[sel_X, np.setdiff1d(np.arange(m), sel_X)],
|
|
203
|
+
np.r_[sel_Y, np.setdiff1d(np.arange(n), sel_Y)])]
|
|
204
|
+
else:
|
|
205
|
+
M = Wp[np.ix_(sel_X, sel_Y)]
|
|
206
|
+
if crange is None:
|
|
207
|
+
crange = (np.nanmin(M), np.nanmax(M))
|
|
208
|
+
fig, ax = plt.subplots(figsize=figsize)
|
|
209
|
+
im = ax.imshow(M, aspect="auto", cmap=matlab_jet(256), vmin=crange[0], vmax=crange[1],
|
|
210
|
+
interpolation="nearest")
|
|
211
|
+
fig.colorbar(im, ax=ax)
|
|
212
|
+
if line:
|
|
213
|
+
if full:
|
|
214
|
+
ax.add_patch(Rectangle((-0.5, -0.5), len(sel_Y), len(sel_X), fill=False, ec="r", lw=2))
|
|
215
|
+
else:
|
|
216
|
+
x = y = -0.5
|
|
217
|
+
for r, c in zip(rows, cols):
|
|
218
|
+
if len(r) == 0:
|
|
219
|
+
break
|
|
220
|
+
ax.add_patch(Rectangle((x, y), len(c), len(r), fill=False, ec="r", lw=2))
|
|
221
|
+
x += len(c); y += len(r)
|
|
222
|
+
return fig, ax
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: triger
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Structured cross-omics interaction discovery with a triple-graph model
|
|
5
|
+
Project-URL: Homepage, https://github.com/MATRIX-Research-Lab/TriGer-PyPI
|
|
6
|
+
Project-URL: Issues, https://github.com/MATRIX-Research-Lab/TriGer-PyPI/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/MATRIX-Research-Lab/TriGer-PyPI/blob/main/CHANGELOG.md
|
|
8
|
+
Project-URL: Paper, https://doi.org/10.64898/2026.09.10.749540
|
|
9
|
+
Project-URL: R package, https://github.com/MATRIX-Research-Lab/TriGer
|
|
10
|
+
Project-URL: MATLAB code, https://github.com/jy2941/TriGer
|
|
11
|
+
Author-email: Jiaao Yu <jy2941@umd.edu>, Huang Lin <huanglinfrederick@gmail.com>
|
|
12
|
+
Maintainer-email: Huang Lin <huanglinfrederick@gmail.com>
|
|
13
|
+
License-Expression: MIT
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Keywords: biclustering,dense subgraph,greedy peeling,integration,multi-omics
|
|
16
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
17
|
+
Classifier: Intended Audience :: Science/Research
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
26
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
27
|
+
Requires-Python: >=3.10
|
|
28
|
+
Requires-Dist: matplotlib>=3.5
|
|
29
|
+
Requires-Dist: numpy>=1.21
|
|
30
|
+
Requires-Dist: scipy>=1.8
|
|
31
|
+
Provides-Extra: test
|
|
32
|
+
Requires-Dist: pandas>=1.4; extra == 'test'
|
|
33
|
+
Requires-Dist: pytest>=7; extra == 'test'
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# TriGer
|
|
37
|
+
|
|
38
|
+
[](https://github.com/MATRIX-Research-Lab/TriGer-PyPI/actions/workflows/tests.yml)
|
|
39
|
+
[](https://pypi.org/project/triger/)
|
|
40
|
+
[](https://doi.org/10.64898/2026.09.10.749540)
|
|
41
|
+
[](https://github.com/MATRIX-Research-Lab/TriGer-PyPI/blob/main/LICENSE)
|
|
42
|
+
|
|
43
|
+
| Platform | Repository | Installation |
|
|
44
|
+
|----------|------------|--------------|
|
|
45
|
+
| R | [MATRIX-Research-Lab/TriGer](https://github.com/MATRIX-Research-Lab/TriGer) | `remotes::install_github("MATRIX-Research-Lab/TriGer")` |
|
|
46
|
+
| Python | [MATRIX-Research-Lab/TriGer-PyPI](https://github.com/MATRIX-Research-Lab/TriGer-PyPI) (this repository) | `pip install triger` |
|
|
47
|
+
| MATLAB | [jy2941/TriGer](https://github.com/jy2941/TriGer) | Clone the repository and add `matlab/methods` and `matlab/visualization` to the MATLAB path |
|
|
48
|
+
|
|
49
|
+
The R and Python packages give identical results on the same input; both are
|
|
50
|
+
tested against shared reference outputs. The MATLAB repository also contains
|
|
51
|
+
the code for the simulations and the real-data analyses of the paper.
|
|
52
|
+
|
|
53
|
+
Python package `triger` for **TriGer**, from the paper *Structured cross-omics
|
|
54
|
+
interaction discovery with a triple-graph model*.
|
|
55
|
+
|
|
56
|
+
TriGer finds modules of features that are densely associated across two omics
|
|
57
|
+
layers (X and Y) and coherent within each layer. It peels dense blocks from the
|
|
58
|
+
cross-omics association matrix, then refines large blocks using the
|
|
59
|
+
within-omics correlation matrices.
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install triger
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The development version:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install git+https://github.com/MATRIX-Research-Lab/TriGer-PyPI.git
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Quick start
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
import triger
|
|
77
|
+
|
|
78
|
+
# X: samples x m features, Y: samples x n features (NumPy arrays or pandas
|
|
79
|
+
# DataFrames; DataFrames are matched by sample index and keep feature names)
|
|
80
|
+
res = triger.triger(X, Y, lambda1=1.1, lambda2=1.8, lambda3=1.8,
|
|
81
|
+
X_size=(10, 15), Y_size=(10, 250))
|
|
82
|
+
|
|
83
|
+
for mod in res.modules:
|
|
84
|
+
print(len(mod.X), "X features,", len(mod.Y), "Y features")
|
|
85
|
+
print(res.modules[0].X_names)
|
|
86
|
+
|
|
87
|
+
# Cross- and within-omics correlations of the module features
|
|
88
|
+
m = res.m
|
|
89
|
+
triger.plot3in1_v1(res.R[:m, :m], res.R[m:, m:], res.R[:m, m:], res.modules,
|
|
90
|
+
line=True)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
The lower-level functions work on precomputed matrices:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
a = triger.association_matrices(X, Y) # signed R and -log10 p P
|
|
97
|
+
blocks = triger.greedy_3step(a.P, a.R, a.m, a.n, lambda1=1.1, lambda2=1.8,
|
|
98
|
+
lambda3=1.8, X_size=(10, 15), Y_size=(10, 250))
|
|
99
|
+
x_idx, y_idx, density = blocks[0] # or blocks[0].X, .Y, .density
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Functions
|
|
103
|
+
|
|
104
|
+
| Function | Description |
|
|
105
|
+
|----------|-------------|
|
|
106
|
+
| `triger` | Full pipeline on two omics layers |
|
|
107
|
+
| `association_matrices` | Signed correlation and -log10 p-value matrices over both layers |
|
|
108
|
+
| `greedy_3step` | Full pipeline on precomputed association matrices |
|
|
109
|
+
| `greedy_peeling_XY_one` / `greedy_peeling_XY_all` | Dense blocks in a cross-omics (X-Y) matrix |
|
|
110
|
+
| `greedy_peeling_X_one` / `greedy_peeling_X_all` | Dense communities in a within-omics (symmetric) matrix |
|
|
111
|
+
| `sort_result` / `sort_result_XandY` | Order features within each block |
|
|
112
|
+
| `plot3in1_v1` | X-Y heatmap with the X-X and Y-Y matrices as triangles |
|
|
113
|
+
| `plot_all_result` | Heatmap of the detected blocks |
|
|
114
|
+
|
|
115
|
+
Blocks are returned as `Block(X, Y, density)` records, communities as
|
|
116
|
+
`Community(X, density)`, and the modules of `triger` as
|
|
117
|
+
`Module(X, Y, density, X_names, Y_names)`. All indices are 0-based.
|
|
118
|
+
|
|
119
|
+
`lambda1` is the cross-omics size penalty and `lambda2` / `lambda3` are the
|
|
120
|
+
within-omics penalties for X and Y (λ₀, λ₁, λ₂ in the paper); higher values give
|
|
121
|
+
smaller, denser blocks. `max_blocks` is the maximum number of blocks found in
|
|
122
|
+
the cross-omics step (default 5). The CRC and IBD analyses of the paper, run
|
|
123
|
+
with the MATLAB code, correspond to `max_blocks=4`.
|
|
124
|
+
|
|
125
|
+
## Citation
|
|
126
|
+
|
|
127
|
+
Yu J, Lin H, Chen S (2026). Structured cross-omics interaction discovery with a
|
|
128
|
+
triple-graph model. *bioRxiv*.
|
|
129
|
+
[doi:10.64898/2026.09.10.749540](https://doi.org/10.64898/2026.09.10.749540)
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
triger/__init__.py,sha256=GgDSfhQ4pwU1AMOugifnTW20m4-IsgEqSoU_5M5nFww,920
|
|
2
|
+
triger/association.py,sha256=NXI-42iOSoPdC3bNfDKJVJ_A5gLgB1hUcy-vksz_P_c,6349
|
|
3
|
+
triger/greedy_peeling.py,sha256=9NFkwMHutuJ76oKqOAcXLkckRZbbGjMtpgIjftOW42s,14735
|
|
4
|
+
triger/plotting.py,sha256=J1fGZfF7SCt4CSYTN2KFHxowyvCsagZuo2UFULzEK8E,9828
|
|
5
|
+
triger-1.0.0.dist-info/METADATA,sha256=COtkwSjHRIUT44IufzmTuQ6UONWaaUzv8VgZwW0e_uo,5976
|
|
6
|
+
triger-1.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
7
|
+
triger-1.0.0.dist-info/licenses/LICENSE,sha256=scf8hOZop2n12LiO8nAu4qGrJJzvC6RG1StUWQxCefE,1079
|
|
8
|
+
triger-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jiaao Yu and Huang Lin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|