@@ -47,42 +47,54 @@ def create_graph(path: pathlib.Path) -> nx.DiGraph:
4747 path = pathlib .Path (path )
4848 with path .open () as fp :
4949 content = json .load (fp )
50+ return create_graph_from_json_object (content )
5051
51- graph = nx .DiGraph ()
5252
53- # Add src/dst nodes
54- graph . add_node ( "SRC" , label = "SRC" , type = "SRC" , id = "SRC" )
55- graph . add_node ( "DST" , label = "DST" , type = "DST" , id = "DST" )
53+ def create_graph_from_json_object ( workflow_instance : dict ) -> nx . DiGraph :
54+ """
55+ Creates a networkX DiGraph from a JSON file in the WfFormat.
5656
57- id_count = 0
57+ :param workflow_instance: A workflow instance as a JSON object.
58+ :type workflow_instance: json
5859
59- for task in content ["workflow" ]["specification" ]["tasks" ]:
60+ :return: graph.
61+ :rtype: networkX DiGraph.
62+ """
63+ graph = nx .DiGraph ()
6064
61- # specific for epigenomics -- have to think about how to do it in general
62- if "genome-dax" in content ["name" ]:
63- _type , * _ = task ["name" ].split ("_" )
64- graph .add_node (task ["name" ], label = _type , type = _type , id = str (id_count ))
65- id_count += 1
66- else :
67- try :
68- _type , _id = task ["name" ].split ("_ID" )
69- except ValueError :
70- _type , _id = task ["name" ].split ("_0" )
71- graph .add_node (task ["name" ], label = _type , type = _type , id = _id )
65+ # Add src/dst nodes
66+ graph .add_node ("SRC" , label = "SRC" , type = "SRC" , id = "SRC" )
67+ graph .add_node ("DST" , label = "DST" , type = "DST" , id = "DST" )
7268
73- for parent in task ["parents" ]:
74- graph .add_edge (parent , task ["name" ])
69+ id_count = 0
7570
76- for node in graph . nodes :
71+ for task in workflow_instance [ "workflow" ][ "specification" ][ "tasks" ] :
7772
78- if node in ["SRC" , "DST" ]:
79- continue
80- if graph .in_degree (node ) <= 0 :
81- graph .add_edge ("SRC" , node )
82- if graph .out_degree (node ) <= 0 :
83- graph .add_edge (node , "DST" )
73+ # specific for epigenomics -- have to think about how to do it in general
74+ if "genome-dax" in workflow_instance ["name" ]:
75+ _type , * _ = task ["name" ].split ("_" )
76+ graph .add_node (task ["name" ], label = _type , type = _type , id = str (id_count ))
77+ id_count += 1
78+ else :
79+ try :
80+ _type , _id = task ["id" ].split ("_ID" )
81+ except ValueError :
82+ _type , _id = task ["id" ].split ("_0" )
83+ graph .add_node (task ["name" ], label = _type , type = _type , id = _id )
8484
85- return graph
85+ for parent in task ["parents" ]:
86+ graph .add_edge (parent , task ["name" ])
87+
88+ for node in graph .nodes :
89+
90+ if node in ["SRC" , "DST" ]:
91+ continue
92+ if graph .in_degree (node ) <= 0 :
93+ graph .add_edge ("SRC" , node )
94+ if graph .out_degree (node ) <= 0 :
95+ graph .add_edge (node , "DST" )
96+
97+ return graph
8698
8799
88100def annotate (g : nx .DiGraph ) -> None :
@@ -176,8 +188,6 @@ def draw(g: nx.DiGraph,
176188 :param subgraph: nodes that were added by replication and will be colored green.
177189 :type subgraph: Set[str].
178190
179-
180-
181191 :return: the figure and the axis used.
182192 :rtype: Tuple[plt.Figure, plt.Axes].
183193 """
0 commit comments