3
3
import os
4
4
import tempfile
5
5
6
+ import pandas as pd
6
7
7
8
os .environ ["PY4CYTOSCAPE_DETAIL_LOGGER_DIR" ] = str (tempfile .gettempdir ())
8
9
9
10
from pathlib import Path # noqa: E402
10
- from typing import Any , Union # noqa: E402
11
+ from typing import Any , Union , Optional # noqa: E402
11
12
13
+ import libsbml
12
14
import py4cytoscape as p4c # type: ignore # noqa: E402
13
15
from requests .exceptions import RequestException # noqa: E402
14
16
@@ -30,19 +32,91 @@ def visualize_antimony(source: Union[Path, str], delete_session: bool = False) -
30
32
visualize_sbml (Path (f_tmp .name ), delete_session = delete_session )
31
33
32
34
33
- def visualize_sbml (sbml_path : Path , delete_session : bool = False ) -> None :
34
- """Visualize SBML networks in cytoscape."""
35
+ def visualize_sbml (sbml_path : Path , delete_session : bool = False ) -> Optional [int ]:
36
+ """Visualize SBML networks in cytoscape.
37
+
38
+ Returns dictionary with "networks" and "views".
39
+ """
40
+ if sbml_path .suffix != ".xml" :
41
+ console .error (f"SBML path { sbml_path } does not have .xml extension" )
35
42
36
43
try :
37
44
console .print (p4c .cytoscape_version_info ())
38
45
39
46
if delete_session :
40
47
p4c .session .close_session (save_before_closing = False )
41
48
42
- p4c .networks .import_network_from_file (str (sbml_path ))
49
+ networks_views = p4c .networks .import_network_from_file (str (sbml_path ))
50
+ console .print (f"{ networks_views } " )
51
+ network = networks_views ["networks" ][1 ]
52
+ p4c .set_current_view (network = network ) # set the base network
53
+ return network
54
+
55
+ return networks_views
43
56
44
57
except RequestException :
45
58
logger .error (
46
59
"Could not connect to a running Cytoscape instance. "
47
60
"Start Cytoscape before running the python script."
48
61
)
62
+ return None
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+ def read_layout_xml (sbml_path : Path , xml_path : Path ) -> pd .DataFrame :
71
+ """Read own xml layout information form cytoscape."""
72
+ # read positions
73
+ df : pd .DataFrame = pd .read_xml (xml_path , xpath = "//boundingBox" )
74
+ df = df [['id' , 'xpos' , 'ypos' ]]
75
+ df .rename (columns = {"xpos" : "x" , "ypos" : "y" }, inplace = True )
76
+ df .set_index ("id" , inplace = True )
77
+ return df
78
+
79
+ def apply_layout (network , layout : pd .DataFrame ) -> None :
80
+ """Apply layout information from Cytoscape to SBML networks."""
81
+
82
+ # get SUIDs, sbml_id from node table;
83
+ df_nodes = p4c .get_table_columns (table = "node" , columns = ["sbml id" ], network = network )
84
+ console .print (df_nodes )
85
+ sid2suid = {row ["sbml id" ]: suid for suid , row in df_nodes .iterrows ()}
86
+ console .print (sid2suid )
87
+
88
+ # FIXME: necessary to check that all sids exist
89
+ suids = [sid2suid [sid ] for sid in layout .index .values ]
90
+ x_values = layout ["x" ].values .tolist ()
91
+ y_values = layout ["y" ].values .tolist ()
92
+
93
+ # set positions
94
+ # see: https://github.com/cytoscape/py4cytoscape/issues/144
95
+ p4c .set_node_position_bypass (suids , new_x_locations = x_values , new_y_locations = y_values , network = network )
96
+ # p4c.set_node_property_bypass(suids, new_values=x_values, visual_property='NODE_X_LOCATION', network=network)
97
+ # p4c.set_node_property_bypass(suids, new_values=y_values, visual_property='NODE_Y_LOCATION', network=network)
98
+ p4c .set_current_view (network = network )
99
+
100
+ # remove bypass
101
+ # p4c.clear_node_property_bypass(suids, visual_property='NODE_X_LOCATION', network=network)
102
+ # p4c.clear_node_property_bypass(suids, visual_property='NODE_Y_LOCATION', network=network)
103
+
104
+ # fit content
105
+ p4c .fit_content ()
106
+
107
+
108
+
109
+ if __name__ == "__main__" :
110
+ pass
111
+ # # visual style
112
+ # p4c.set_visual_style('Marquee')
113
+ #
114
+ # # fit the content
115
+ # p4c.fit_content()
116
+
117
+ # p4c.load_table_data
118
+
119
+ # annotations!
120
+
121
+ # network_views.export_image
122
+
0 commit comments