-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
189 lines (164 loc) · 6.4 KB
/
Copy pathmain.py
File metadata and controls
189 lines (164 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
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
from graph.PPINetwork import PPINetwork
from graph.StringLoader import StringLoader
from visual.ReportGenerator import ReportGenerator
from visual.plots_macros import *
def pipeline():
network_type = "functional" # "physical"
protein_query = ["TP53", "BRCA1"]
# protein_query = ["INS", "ALB", "APP", "SNCA", "CFTR"]
nodes_to_add = len(protein_query)*10
scatter_plot_metrics = [
("degree", "betweenness_centrality"),
("degree", "pagerank"),
("clustering_coefficient", "degree")
]
top_edges_scores = 20
loader = StringLoader(
protein_query=protein_query,
species=9606,
add_nodes=nodes_to_add,
required_score=400,
network_type=network_type
)
print("Fetching data...")
data = (
loader
.retrieve_data()
.standardize_data_format()
.get_data()
)
print("Construction PPI network...")
ppin = PPINetwork(data=data)
print("Computing network metrics...")
ppin.compute_all_global_metrics(weighted=False)
ppin.compute_all_edge_metrics(weighted=False)
ppin.compute_all_node_metrics(weighted=False)
exported_files = ppin.export_for_cytoscape(
output_dir="cytoscape_export",
basename=f"ppi_network_{'-'.join(protein_query)}",
)
print(f"Network exported as:")
for file, path in exported_files.items():
print(f"\t{file}: {path}")
print("Generating plots...")
node_labels = ppin.get_nodes_preferred_names()
scatter_plot_html = "\n".join([
f"""
<div class="plotInGrid">
<img src="{s}" class="imagePlotCard">
</div>
"""
for s in [
create_scatter_plot_encoded_image(
x_values=ppin.get_nodes_metric_value(metric_1)[0],
y_values=ppin.get_nodes_metric_value(metric_2)[0],
x_label=metric_1, y_label=metric_2,
labels=node_labels
)
for metric_1, metric_2 in scatter_plot_metrics
]
])
values_e_net, edges_net = ppin.get_edges_metric_value("confidence")
edges_plot = create_barplot_encoded_image(
edges_net[:top_edges_scores],
values_e_net[:top_edges_scores],
y_label="Edge", x_labebl="Confidence"
)
global_plot = create_graph_degree_distribution_encoded_image(
ppin.get_nodes_metric_value("degree")[0],
bins=20
)
weighted_degree_hubs_df = ppin.get_hub_proteins("weighted_degree", top_n=7)
consensus_hubs_df = ppin.get_consensus_hub_proteins(top_n=7)
bar_plot_hubs = create_barplot_encoded_image(
weighted_degree_hubs_df['preferredName'].tolist(),
weighted_degree_hubs_df['weighted_degree'].tolist(),
title="Hubs by Weighted Degree",
x_labebl='weighted_degree'
)
bar_plot_consensus_hubs = create_barplot_encoded_image(
consensus_hubs_df['preferredName'].tolist(),
consensus_hubs_df['weighted_degree'].tolist(),
title="Consensus Hubs",
x_labebl='weighted_degree'
)
ranked_genes_df, gsea_results_df = ppin.run_gsea(
node_attribute="degree_centrality",
gene_sets="GO_Biological_Process_2023",
outdir="gsea_results"
)
print(ranked_genes_df.head())
print(gsea_results_df.head(20))
plot_gsea = ppin.plot_gsea_dotplot(
gsea_results_df=gsea_results_df,
column="FDR q-val",
title="GO Biological Process enrichment",
cutoff=0.25,
top_term=10,
)
hubs_plots_html = "\n".join([
f"""
<div class="plotInGrid">
<img src="{s}" class="imagePlotCard">
</div>
""" for s in [bar_plot_hubs, bar_plot_consensus_hubs, plot_gsea]
])
print("generating html report...")
report_name = "protein_network_report.html"
(ReportGenerator()
.add_cytoscape_html_report(
ppin,
title="STRING Protein Network",
node_color_attr="katz_centrality", # "betweenness_centrality",
edge_width_attr="escore", # "edge_betweenness_centrality",
cmap_name="plasma",
min_node_size=45,
max_node_size=120,
min_edge_width=1,
max_edge_width=10
)
.add_section(html_to_add=f"""
<h2>StringDB fetched network. Click on the logo for more!</h2>
{loader.get_network_html_div()}
""")
.add_section_from_file(
file="./visual/web/double_plots.html",
replacements={
"__global_plot__": global_plot,
"__edges_plot__": edges_plot
}
)
.add_section_from_file(
file="visual/web/grid_plots.html",
replacements={
"__plots_html__": scatter_plot_html,
"__title_grid__": "Scatter plots",
"__description_grid__": (
"The scatter plots compare pairs of node-level network metrics. Each point"
" represents a protein, and the label identifies the corresponding preferred"
" protein name. These plots help reveal whether different centrality or"
" structural measures capture similar or complementary aspects of the network."
)
}
)
.add_section_from_file(
file="visual/web/grid_plots.html",
replacements={
"__plots_html__": hubs_plots_html,
"__title_grid__": "Hubs plots",
"__description_grid__": (
"These plots summarize the main hub proteins identified in the protein-protein "
"interaction network. The first bar plot ranks proteins by weighted degree, "
"highlighting nodes with many high-confidence interactions. The second plot "
"shows the consensus hub ranking, which combines multiple centrality measures to "
"identify proteins that are consistently important across different network perspectives. "
"The enrichment plot provides a biological interpretation of the ranked genes by showing "
"Gene Ontology biological processes associated with the most central proteins."
)
}
)
.generate_report_file(output_file=report_name)
)
print(f"Report generated: {report_name}")
if __name__ == "__main__":
pipeline()