risk-network 0.0.13b4__py3-none-any.whl → 0.0.14__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.
- risk/__init__.py +3 -2
- risk/_annotation/__init__.py +10 -0
- risk/{annotation/annotation.py → _annotation/_annotation.py} +18 -11
- risk/{annotation/io.py → _annotation/_io.py} +22 -14
- risk/{annotation/nltk_setup.py → _annotation/_nltk_setup.py} +7 -5
- risk/_log/__init__.py +11 -0
- risk/{log/console.py → _log/_console.py} +22 -12
- risk/{log/parameters.py → _log/_parameters.py} +25 -14
- risk/_neighborhoods/__init__.py +8 -0
- risk/{neighborhoods/api.py → _neighborhoods/_api.py} +23 -17
- risk/{neighborhoods/community.py → _neighborhoods/_community.py} +19 -11
- risk/{neighborhoods/domains.py → _neighborhoods/_domains.py} +92 -35
- risk/{neighborhoods/neighborhoods.py → _neighborhoods/_neighborhoods.py} +69 -58
- risk/_neighborhoods/_stats/__init__.py +13 -0
- risk/_neighborhoods/_stats/_permutation/__init__.py +6 -0
- risk/{neighborhoods/stats/permutation/permutation.py → _neighborhoods/_stats/_permutation/_permutation.py} +9 -6
- risk/{neighborhoods/stats/permutation/test_functions.py → _neighborhoods/_stats/_permutation/_test_functions.py} +6 -4
- risk/{neighborhoods/stats/tests.py → _neighborhoods/_stats/_tests.py} +12 -7
- risk/_network/__init__.py +8 -0
- risk/_network/_graph/__init__.py +7 -0
- risk/{network/graph/api.py → _network/_graph/_api.py} +13 -13
- risk/{network/graph/graph.py → _network/_graph/_graph.py} +24 -13
- risk/{network/graph/stats.py → _network/_graph/_stats.py} +8 -5
- risk/{network/graph/summary.py → _network/_graph/_summary.py} +39 -32
- risk/{network/io.py → _network/_io.py} +166 -148
- risk/_network/_plotter/__init__.py +6 -0
- risk/{network/plotter/api.py → _network/_plotter/_api.py} +9 -10
- risk/{network/plotter/canvas.py → _network/_plotter/_canvas.py} +14 -10
- risk/{network/plotter/contour.py → _network/_plotter/_contour.py} +17 -11
- risk/{network/plotter/labels.py → _network/_plotter/_labels.py} +38 -23
- risk/{network/plotter/network.py → _network/_plotter/_network.py} +17 -11
- risk/{network/plotter/plotter.py → _network/_plotter/_plotter.py} +19 -15
- risk/_network/_plotter/_utils/__init__.py +7 -0
- risk/{network/plotter/utils/colors.py → _network/_plotter/_utils/_colors.py} +19 -11
- risk/{network/plotter/utils/layout.py → _network/_plotter/_utils/_layout.py} +8 -5
- risk/{risk.py → _risk.py} +11 -11
- risk_network-0.0.14.dist-info/METADATA +115 -0
- risk_network-0.0.14.dist-info/RECORD +41 -0
- {risk_network-0.0.13b4.dist-info → risk_network-0.0.14.dist-info}/WHEEL +1 -1
- risk/annotation/__init__.py +0 -10
- risk/log/__init__.py +0 -11
- risk/neighborhoods/__init__.py +0 -7
- risk/neighborhoods/stats/__init__.py +0 -13
- risk/neighborhoods/stats/permutation/__init__.py +0 -6
- risk/network/__init__.py +0 -4
- risk/network/graph/__init__.py +0 -4
- risk/network/plotter/__init__.py +0 -4
- risk_network-0.0.13b4.dist-info/METADATA +0 -125
- risk_network-0.0.13b4.dist-info/RECORD +0 -40
- {risk_network-0.0.13b4.dist-info → risk_network-0.0.14.dist-info}/licenses/LICENSE +0 -0
- {risk_network-0.0.13b4.dist-info → risk_network-0.0.14.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_network/_graph/_api
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
import copy
|
@@ -9,26 +9,24 @@ from typing import Any, Dict, Union
|
|
9
9
|
import networkx as nx
|
10
10
|
import pandas as pd
|
11
11
|
|
12
|
-
from
|
13
|
-
from
|
14
|
-
from
|
12
|
+
from ..._annotation import define_top_annotation
|
13
|
+
from ..._log import log_header, logger, params
|
14
|
+
from ..._neighborhoods import (
|
15
15
|
define_domains,
|
16
16
|
process_neighborhoods,
|
17
17
|
trim_domains,
|
18
18
|
)
|
19
|
-
from
|
20
|
-
from
|
19
|
+
from ._graph import Graph
|
20
|
+
from ._stats import calculate_significance_matrices
|
21
21
|
|
22
22
|
|
23
23
|
class GraphAPI:
|
24
|
-
"""
|
24
|
+
"""
|
25
|
+
Handles the loading of network graphs and associated data.
|
25
26
|
|
26
27
|
The GraphAPI class provides methods to load and process network graphs, annotations, and neighborhoods.
|
27
28
|
"""
|
28
29
|
|
29
|
-
def __init__(self) -> None:
|
30
|
-
pass
|
31
|
-
|
32
30
|
def load_graph(
|
33
31
|
self,
|
34
32
|
network: nx.Graph,
|
@@ -46,7 +44,8 @@ class GraphAPI:
|
|
46
44
|
min_cluster_size: int = 5,
|
47
45
|
max_cluster_size: int = 1000,
|
48
46
|
) -> Graph:
|
49
|
-
"""
|
47
|
+
"""
|
48
|
+
Load and process the network graph, defining top annotations and domains.
|
50
49
|
|
51
50
|
Args:
|
52
51
|
network (nx.Graph): The network graph.
|
@@ -169,7 +168,8 @@ class GraphAPI:
|
|
169
168
|
min_cluster_size: int = 5,
|
170
169
|
max_cluster_size: int = 1000,
|
171
170
|
) -> pd.DataFrame:
|
172
|
-
"""
|
171
|
+
"""
|
172
|
+
Define top annotations for the network.
|
173
173
|
|
174
174
|
Args:
|
175
175
|
network (nx.Graph): The network graph.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_network/_graph/_graph
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
from collections import defaultdict
|
@@ -10,11 +10,12 @@ import networkx as nx
|
|
10
10
|
import numpy as np
|
11
11
|
import pandas as pd
|
12
12
|
|
13
|
-
from
|
13
|
+
from ._summary import Summary
|
14
14
|
|
15
15
|
|
16
16
|
class Graph:
|
17
|
-
"""
|
17
|
+
"""
|
18
|
+
A class to represent a network graph and process its nodes and edges.
|
18
19
|
|
19
20
|
The Graph class provides functionality to handle and manipulate a network graph,
|
20
21
|
including managing domains, annotation, and node significance data. It also includes methods
|
@@ -32,7 +33,9 @@ class Graph:
|
|
32
33
|
node_label_to_node_id_map: Dict[str, Any],
|
33
34
|
node_significance_sums: np.ndarray,
|
34
35
|
):
|
35
|
-
"""
|
36
|
+
"""
|
37
|
+
|
38
|
+
Initialize the Graph object.
|
36
39
|
|
37
40
|
Args:
|
38
41
|
network (nx.Graph): The network graph.
|
@@ -72,7 +75,8 @@ class Graph:
|
|
72
75
|
self.summary = Summary(annotation, neighborhoods, self)
|
73
76
|
|
74
77
|
def pop(self, domain_id: int) -> List[str]:
|
75
|
-
"""
|
78
|
+
"""
|
79
|
+
Remove a domain ID from the graph and return the corresponding node labels.
|
76
80
|
|
77
81
|
Args:
|
78
82
|
key (int): The domain ID key to be removed from each mapping.
|
@@ -104,7 +108,8 @@ class Graph:
|
|
104
108
|
return node_labels
|
105
109
|
|
106
110
|
def _create_domain_id_to_node_ids_map(self, domains: pd.DataFrame) -> Dict[int, Any]:
|
107
|
-
"""
|
111
|
+
"""
|
112
|
+
Create a mapping from domains to the list of node IDs belonging to each domain.
|
108
113
|
|
109
114
|
Args:
|
110
115
|
domains (pd.DataFrame): DataFrame containing domain information, including the 'primary domain' for each node.
|
@@ -123,7 +128,8 @@ class Graph:
|
|
123
128
|
def _create_domain_id_to_domain_terms_map(
|
124
129
|
self, trimmed_domains: pd.DataFrame
|
125
130
|
) -> Dict[int, Any]:
|
126
|
-
"""
|
131
|
+
"""
|
132
|
+
Create a mapping from domain IDs to their corresponding terms.
|
127
133
|
|
128
134
|
Args:
|
129
135
|
trimmed_domains (pd.DataFrame): DataFrame containing domain IDs and their corresponding labels.
|
@@ -142,7 +148,8 @@ class Graph:
|
|
142
148
|
self,
|
143
149
|
trimmed_domains: pd.DataFrame,
|
144
150
|
) -> Dict[int, Dict[str, Any]]:
|
145
|
-
"""
|
151
|
+
"""
|
152
|
+
Create a mapping from domain IDs to their corresponding full description and significance score,
|
146
153
|
with scores sorted in descending order.
|
147
154
|
|
148
155
|
Args:
|
@@ -178,7 +185,8 @@ class Graph:
|
|
178
185
|
def _create_node_id_to_domain_ids_and_significances(
|
179
186
|
self, domains: pd.DataFrame
|
180
187
|
) -> Dict[int, Dict]:
|
181
|
-
"""
|
188
|
+
"""
|
189
|
+
Creates a dictionary mapping each node ID to its corresponding domain IDs and significance values.
|
182
190
|
|
183
191
|
Args:
|
184
192
|
domains (pd.DataFrame): A DataFrame containing domain information for each node. Assumes the last
|
@@ -210,7 +218,8 @@ class Graph:
|
|
210
218
|
return node_id_to_domain_ids_and_significances
|
211
219
|
|
212
220
|
def _create_domain_id_to_node_labels_map(self) -> Dict[int, List[str]]:
|
213
|
-
"""
|
221
|
+
"""
|
222
|
+
Create a map from domain IDs to node labels.
|
214
223
|
|
215
224
|
Returns:
|
216
225
|
Dict[int, List[str]]: A dictionary mapping domain IDs to the corresponding node labels.
|
@@ -224,7 +233,8 @@ class Graph:
|
|
224
233
|
return domain_id_to_label_map
|
225
234
|
|
226
235
|
def _unfold_sphere_to_plane(self, G: nx.Graph) -> nx.Graph:
|
227
|
-
"""
|
236
|
+
"""
|
237
|
+
Convert 3D coordinates to 2D by unfolding a sphere to a plane.
|
228
238
|
|
229
239
|
Args:
|
230
240
|
G (nx.Graph): A network graph with 3D coordinates. Each node should have 'x', 'y', and 'z' attributes.
|
@@ -254,7 +264,8 @@ class Graph:
|
|
254
264
|
return G
|
255
265
|
|
256
266
|
def _extract_node_coordinates(self, G: nx.Graph) -> np.ndarray:
|
257
|
-
"""
|
267
|
+
"""
|
268
|
+
Extract 2D coordinates of nodes from the graph.
|
258
269
|
|
259
270
|
Args:
|
260
271
|
G (nx.Graph): The network graph with node coordinates.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_network/_graph/_stats
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
from typing import Any, Dict, Union
|
@@ -16,7 +16,8 @@ def calculate_significance_matrices(
|
|
16
16
|
pval_cutoff: float = 0.05,
|
17
17
|
fdr_cutoff: float = 0.05,
|
18
18
|
) -> Dict[str, Any]:
|
19
|
-
"""
|
19
|
+
"""
|
20
|
+
Calculate significance matrices based on p-values and specified tail.
|
20
21
|
|
21
22
|
Args:
|
22
23
|
depletion_pvals (np.ndarray): Matrix of depletion p-values.
|
@@ -89,7 +90,8 @@ def _select_significance_matrices(
|
|
89
90
|
log_enrichment_matrix: np.ndarray,
|
90
91
|
enrichment_alpha_threshold_matrix: np.ndarray,
|
91
92
|
) -> tuple:
|
92
|
-
"""
|
93
|
+
"""
|
94
|
+
Select significance matrices based on the specified tail type.
|
93
95
|
|
94
96
|
Args:
|
95
97
|
tail (str): The tail type for significance selection. Options are 'left', 'right', or 'both'.
|
@@ -143,7 +145,8 @@ def _compute_threshold_matrix(
|
|
143
145
|
pval_cutoff: float = 0.05,
|
144
146
|
fdr_cutoff: float = 0.05,
|
145
147
|
) -> np.ndarray:
|
146
|
-
"""
|
148
|
+
"""
|
149
|
+
Compute a threshold matrix indicating significance based on p-value and FDR cutoffs.
|
147
150
|
|
148
151
|
Args:
|
149
152
|
pvals (np.ndarray): Array of p-values for statistical tests.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_network/_graph/_summary
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
from typing import Any, Dict, Tuple, Union
|
@@ -9,11 +9,12 @@ import numpy as np
|
|
9
9
|
import pandas as pd
|
10
10
|
from statsmodels.stats.multitest import fdrcorrection
|
11
11
|
|
12
|
-
from
|
12
|
+
from ..._log import log_header, logger
|
13
13
|
|
14
14
|
|
15
15
|
class Summary:
|
16
|
-
"""
|
16
|
+
"""
|
17
|
+
Handles the processing, storage, and export of network analysis results.
|
17
18
|
|
18
19
|
The Results class provides methods to process significance and depletion data, compute
|
19
20
|
FDR-corrected q-values, and structure information on domains and annotations into a
|
@@ -27,7 +28,8 @@ class Summary:
|
|
27
28
|
neighborhoods: Dict[str, Any],
|
28
29
|
graph, # Avoid type hinting Graph to prevent circular imports
|
29
30
|
):
|
30
|
-
"""
|
31
|
+
"""
|
32
|
+
Initialize the Results object with analysis components.
|
31
33
|
|
32
34
|
Args:
|
33
35
|
annotation (Dict[str, Any]): Annotation data, including ordered annotations and matrix of associations.
|
@@ -39,7 +41,8 @@ class Summary:
|
|
39
41
|
self.graph = graph
|
40
42
|
|
41
43
|
def to_csv(self, filepath: str) -> None:
|
42
|
-
"""
|
44
|
+
"""
|
45
|
+
Export significance results to a CSV file.
|
43
46
|
|
44
47
|
Args:
|
45
48
|
filepath (str): The path where the CSV file will be saved.
|
@@ -50,7 +53,8 @@ class Summary:
|
|
50
53
|
logger.info(f"Analysis summary exported to CSV file: {filepath}")
|
51
54
|
|
52
55
|
def to_json(self, filepath: str) -> None:
|
53
|
-
"""
|
56
|
+
"""
|
57
|
+
Export significance results to a JSON file.
|
54
58
|
|
55
59
|
Args:
|
56
60
|
filepath (str): The path where the JSON file will be saved.
|
@@ -61,7 +65,8 @@ class Summary:
|
|
61
65
|
logger.info(f"Analysis summary exported to JSON file: {filepath}")
|
62
66
|
|
63
67
|
def to_txt(self, filepath: str) -> None:
|
64
|
-
"""
|
68
|
+
"""
|
69
|
+
Export significance results to a text file.
|
65
70
|
|
66
71
|
Args:
|
67
72
|
filepath (str): The path where the text file will be saved.
|
@@ -74,11 +79,12 @@ class Summary:
|
|
74
79
|
logger.info(f"Analysis summary exported to text file: {filepath}")
|
75
80
|
|
76
81
|
def load(self) -> pd.DataFrame:
|
77
|
-
"""
|
82
|
+
"""
|
83
|
+
Load and process domain and annotation data into a DataFrame with significance metrics.
|
78
84
|
|
79
85
|
Returns:
|
80
86
|
pd.DataFrame: Processed DataFrame containing significance scores, p-values, q-values,
|
81
|
-
and annotation
|
87
|
+
and matched annotation members information.
|
82
88
|
"""
|
83
89
|
log_header("Loading analysis summary")
|
84
90
|
# Calculate significance and depletion q-values from p-value matrices in annotation
|
@@ -103,9 +109,9 @@ class Summary:
|
|
103
109
|
# Add minimum p-values and q-values to DataFrame
|
104
110
|
results[
|
105
111
|
[
|
106
|
-
"Enrichment P-
|
112
|
+
"Enrichment P-value",
|
107
113
|
"Enrichment Q-value",
|
108
|
-
"Depletion P-
|
114
|
+
"Depletion P-value",
|
109
115
|
"Depletion Q-value",
|
110
116
|
]
|
111
117
|
] = results.apply(
|
@@ -120,13 +126,13 @@ class Summary:
|
|
120
126
|
axis=1,
|
121
127
|
result_type="expand",
|
122
128
|
)
|
123
|
-
# Add annotation members and their counts
|
124
|
-
results["
|
129
|
+
# Add matched annotation members and their counts
|
130
|
+
results["Matched Members"] = results["Annotation"].apply(
|
125
131
|
lambda desc: self._get_annotation_members(desc)
|
126
132
|
)
|
127
|
-
results["
|
128
|
-
"
|
129
|
-
|
133
|
+
results["Matched Count"] = results["Matched Members"].apply(
|
134
|
+
lambda x: len(x.split(";")) if x else 0
|
135
|
+
)
|
130
136
|
|
131
137
|
# Reorder columns and drop rows with NaN values
|
132
138
|
results = (
|
@@ -134,12 +140,12 @@ class Summary:
|
|
134
140
|
[
|
135
141
|
"Domain ID",
|
136
142
|
"Annotation",
|
137
|
-
"
|
138
|
-
"
|
143
|
+
"Matched Members",
|
144
|
+
"Matched Count",
|
139
145
|
"Summed Significance Score",
|
140
|
-
"Enrichment P-
|
146
|
+
"Enrichment P-value",
|
141
147
|
"Enrichment Q-value",
|
142
|
-
"Depletion P-
|
148
|
+
"Depletion P-value",
|
143
149
|
"Depletion Q-value",
|
144
150
|
]
|
145
151
|
]
|
@@ -153,25 +159,24 @@ class Summary:
|
|
153
159
|
results = pd.merge(ordered_annotation, results, on="Annotation", how="left").fillna(
|
154
160
|
{
|
155
161
|
"Domain ID": -1,
|
156
|
-
"
|
157
|
-
"
|
162
|
+
"Matched Members": "",
|
163
|
+
"Matched Count": 0,
|
158
164
|
"Summed Significance Score": 0.0,
|
159
|
-
"Enrichment P-
|
165
|
+
"Enrichment P-value": 1.0,
|
160
166
|
"Enrichment Q-value": 1.0,
|
161
|
-
"Depletion P-
|
167
|
+
"Depletion P-value": 1.0,
|
162
168
|
"Depletion Q-value": 1.0,
|
163
169
|
}
|
164
170
|
)
|
165
|
-
# Convert "Domain ID" and "
|
171
|
+
# Convert "Domain ID" and "Matched Count" to integers
|
166
172
|
results["Domain ID"] = results["Domain ID"].astype(int)
|
167
|
-
results["
|
168
|
-
"Annotation Members in Network Count"
|
169
|
-
].astype(int)
|
173
|
+
results["Matched Count"] = results["Matched Count"].astype(int)
|
170
174
|
|
171
175
|
return results
|
172
176
|
|
173
177
|
def _calculate_qvalues(self, pvals: np.ndarray) -> np.ndarray:
|
174
|
-
"""
|
178
|
+
"""
|
179
|
+
Calculate q-values (FDR) for each row of a p-value matrix.
|
175
180
|
|
176
181
|
Args:
|
177
182
|
pvals (np.ndarray): 2D array of p-values.
|
@@ -190,7 +195,8 @@ class Summary:
|
|
190
195
|
enrichment_qvals: np.ndarray,
|
191
196
|
depletion_qvals: np.ndarray,
|
192
197
|
) -> Tuple[Union[float, None], Union[float, None], Union[float, None], Union[float, None]]:
|
193
|
-
"""
|
198
|
+
"""
|
199
|
+
Retrieve the most significant p-values and q-values (FDR) for a given annotation.
|
194
200
|
|
195
201
|
Args:
|
196
202
|
domain_id (int): The domain ID associated with the annotation.
|
@@ -226,7 +232,8 @@ class Summary:
|
|
226
232
|
)
|
227
233
|
|
228
234
|
def _get_annotation_members(self, description: str) -> str:
|
229
|
-
"""
|
235
|
+
"""
|
236
|
+
Retrieve node labels associated with a given annotation description.
|
230
237
|
|
231
238
|
Args:
|
232
239
|
description (str): The annotation description.
|