|
8 | 8 | class CWLViewer:
|
9 | 9 | """Produce similar images with the https://github.com/common-workflow-language/cwlviewer."""
|
10 | 10 |
|
11 |
| - _queries_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'rdfqueries') |
12 |
| - _get_inner_edges_query_path = os.path.join(_queries_dir, 'get_inner_edges.sparql') |
13 |
| - _get_input_edges_query_path = os.path.join(_queries_dir, 'get_input_edges.sparql') |
14 |
| - _get_output_edges_query_path = os.path.join(_queries_dir, 'get_output_edges.sparql') |
15 |
| - _get_root_query_path = os.path.join(_queries_dir, 'get_root.sparql') |
| 11 | + _queries_dir = os.path.join( |
| 12 | + os.path.abspath(os.path.dirname(__file__)), "rdfqueries" |
| 13 | + ) |
| 14 | + _get_inner_edges_query_path = os.path.join(_queries_dir, "get_inner_edges.sparql") |
| 15 | + _get_input_edges_query_path = os.path.join(_queries_dir, "get_input_edges.sparql") |
| 16 | + _get_output_edges_query_path = os.path.join(_queries_dir, "get_output_edges.sparql") |
| 17 | + _get_root_query_path = os.path.join(_queries_dir, "get_root.sparql") |
16 | 18 |
|
17 | 19 | def __init__(
|
18 |
| - self, |
19 |
| - rdf_description # type: str |
| 20 | + self, rdf_description # type: str |
20 | 21 | ):
|
21 | 22 | """Create a viewer object based on the rdf description of the workflow."""
|
22 | 23 | self._dot_graph = CWLViewer._init_dot_graph() # type: pydot.Graph
|
23 |
| - self._rdf_graph = self._load_cwl_graph(rdf_description) # type: rdflib.graph.Graph |
| 24 | + self._rdf_graph = self._load_cwl_graph( |
| 25 | + rdf_description |
| 26 | + ) # type: rdflib.graph.Graph |
24 | 27 | self._root_graph_uri = self._get_root_graph_uri() # type: str
|
25 | 28 | self._set_inner_edges()
|
26 | 29 | self._set_input_edges()
|
27 | 30 | self._set_output_edges()
|
28 | 31 |
|
29 | 32 | def _load_cwl_graph(
|
30 |
| - self, |
31 |
| - rdf_description # type: str |
| 33 | + self, rdf_description # type: str |
32 | 34 | ) -> rdflib.graph.Graph:
|
33 | 35 | rdf_graph = rdflib.Graph()
|
34 |
| - rdf_graph.parse(data=rdf_description, format='n3') |
| 36 | + rdf_graph.parse(data=rdf_description, format="n3") |
35 | 37 | return rdf_graph
|
36 | 38 |
|
37 | 39 | def _set_inner_edges(self) -> None:
|
38 | 40 | with open(self._get_inner_edges_query_path) as f:
|
39 | 41 | get_inner_edges_query = f.read()
|
40 |
| - inner_edges = self._rdf_graph.query(get_inner_edges_query, initBindings={'root_graph': self._root_graph_uri}) |
| 42 | + inner_edges = self._rdf_graph.query( |
| 43 | + get_inner_edges_query, initBindings={"root_graph": self._root_graph_uri} |
| 44 | + ) |
41 | 45 | for inner_edge_row in inner_edges:
|
42 |
| - source_label = inner_edge_row['source_label'] \ |
43 |
| - if inner_edge_row['source_label'] is not None \ |
44 |
| - else urlparse(inner_edge_row['source_step']).fragment |
| 46 | + source_label = ( |
| 47 | + inner_edge_row["source_label"] |
| 48 | + if inner_edge_row["source_label"] is not None |
| 49 | + else urlparse(inner_edge_row["source_step"]).fragment |
| 50 | + ) |
45 | 51 | n = pydot.Node(
|
46 |
| - '', |
47 |
| - fillcolor='lightgoldenrodyellow', style="filled", |
| 52 | + "", |
| 53 | + fillcolor="lightgoldenrodyellow", |
| 54 | + style="filled", |
48 | 55 | label=source_label,
|
49 |
| - shape='record' |
| 56 | + shape="record", |
50 | 57 | )
|
51 |
| - n.set_name(str(inner_edge_row['source_step'])) |
| 58 | + n.set_name(str(inner_edge_row["source_step"])) |
52 | 59 | self._dot_graph.add_node(n)
|
53 |
| - target_label = inner_edge_row['target_label'] \ |
54 |
| - if inner_edge_row['target_label'] is not None \ |
55 |
| - else urlparse(inner_edge_row['target_step']).fragment |
| 60 | + target_label = ( |
| 61 | + inner_edge_row["target_label"] |
| 62 | + if inner_edge_row["target_label"] is not None |
| 63 | + else urlparse(inner_edge_row["target_step"]).fragment |
| 64 | + ) |
56 | 65 | n = pydot.Node(
|
57 |
| - '', |
58 |
| - fillcolor='lightgoldenrodyellow', style="filled", |
| 66 | + "", |
| 67 | + fillcolor="lightgoldenrodyellow", |
| 68 | + style="filled", |
59 | 69 | label=target_label,
|
60 |
| - shape='record' |
| 70 | + shape="record", |
61 | 71 | )
|
62 |
| - n.set_name(str(inner_edge_row['target_step'])) |
| 72 | + n.set_name(str(inner_edge_row["target_step"])) |
63 | 73 | self._dot_graph.add_node(n)
|
64 |
| - self._dot_graph.add_edge(pydot.Edge(str(inner_edge_row['source_step']), str(inner_edge_row['target_step']))) |
| 74 | + self._dot_graph.add_edge( |
| 75 | + pydot.Edge( |
| 76 | + str(inner_edge_row["source_step"]), |
| 77 | + str(inner_edge_row["target_step"]), |
| 78 | + ) |
| 79 | + ) |
65 | 80 |
|
66 | 81 | def _set_input_edges(self) -> None:
|
67 | 82 | with open(self._get_input_edges_query_path) as f:
|
68 | 83 | get_input_edges_query = f.read()
|
69 | 84 | inputs_subgraph = pydot.Subgraph(graph_name="cluster_inputs")
|
70 | 85 | self._dot_graph.add_subgraph(inputs_subgraph)
|
71 | 86 | inputs_subgraph.set_rank("same")
|
72 |
| - inputs_subgraph.create_attribute_methods(['style']) |
| 87 | + inputs_subgraph.create_attribute_methods(["style"]) |
73 | 88 | inputs_subgraph.set_style("dashed")
|
74 | 89 | inputs_subgraph.set_label("Workflow Inputs")
|
75 | 90 |
|
76 |
| - input_edges = self._rdf_graph.query(get_input_edges_query, initBindings={'root_graph': self._root_graph_uri}) |
| 91 | + input_edges = self._rdf_graph.query( |
| 92 | + get_input_edges_query, initBindings={"root_graph": self._root_graph_uri} |
| 93 | + ) |
77 | 94 | for input_row in input_edges:
|
78 | 95 | n = pydot.Node(
|
79 |
| - '', |
| 96 | + "", |
80 | 97 | fillcolor="#94DDF4",
|
81 | 98 | style="filled",
|
82 |
| - label=urlparse(input_row['input']).fragment, |
83 |
| - shape='record' |
| 99 | + label=urlparse(input_row["input"]).fragment, |
| 100 | + shape="record", |
84 | 101 | )
|
85 |
| - n.set_name(str(input_row['input'])) |
| 102 | + n.set_name(str(input_row["input"])) |
86 | 103 | inputs_subgraph.add_node(n)
|
87 |
| - self._dot_graph.add_edge(pydot.Edge(str(input_row['input']), str(input_row['step']))) |
| 104 | + self._dot_graph.add_edge( |
| 105 | + pydot.Edge(str(input_row["input"]), str(input_row["step"])) |
| 106 | + ) |
88 | 107 |
|
89 | 108 | def _set_output_edges(self) -> None:
|
90 | 109 | with open(self._get_output_edges_query_path) as f:
|
91 | 110 | get_output_edges = f.read()
|
92 | 111 | outputs_graph = pydot.Subgraph(graph_name="cluster_outputs")
|
93 | 112 | self._dot_graph.add_subgraph(outputs_graph)
|
94 | 113 | outputs_graph.set_rank("same")
|
95 |
| - outputs_graph.create_attribute_methods(['style']) |
| 114 | + outputs_graph.create_attribute_methods(["style"]) |
96 | 115 | outputs_graph.set_style("dashed")
|
97 | 116 | outputs_graph.set_label("Workflow Outputs")
|
98 | 117 | outputs_graph.set_labelloc("b")
|
99 |
| - output_edges = self._rdf_graph.query(get_output_edges, initBindings={'root_graph': self._root_graph_uri}) |
| 118 | + output_edges = self._rdf_graph.query( |
| 119 | + get_output_edges, initBindings={"root_graph": self._root_graph_uri} |
| 120 | + ) |
100 | 121 | for output_edge_row in output_edges:
|
101 |
| - n = pydot.Node('', |
102 |
| - fillcolor="#94DDF4", |
103 |
| - style="filled", |
104 |
| - label=urlparse(output_edge_row['output']).fragment, |
105 |
| - shape='record' |
106 |
| - ) |
107 |
| - n.set_name(str(output_edge_row['output'])) |
| 122 | + n = pydot.Node( |
| 123 | + "", |
| 124 | + fillcolor="#94DDF4", |
| 125 | + style="filled", |
| 126 | + label=urlparse(output_edge_row["output"]).fragment, |
| 127 | + shape="record", |
| 128 | + ) |
| 129 | + n.set_name(str(output_edge_row["output"])) |
108 | 130 | outputs_graph.add_node(n)
|
109 |
| - self._dot_graph.add_edge(pydot.Edge(output_edge_row['step'], output_edge_row['output'])) |
| 131 | + self._dot_graph.add_edge( |
| 132 | + pydot.Edge(output_edge_row["step"], output_edge_row["output"]) |
| 133 | + ) |
110 | 134 |
|
111 | 135 | def _get_root_graph_uri(self) -> rdflib.URIRef:
|
112 | 136 | with open(self._get_root_query_path) as f:
|
113 | 137 | get_root_query = f.read()
|
114 |
| - root = list(self._rdf_graph.query(get_root_query, )) |
| 138 | + root = list( |
| 139 | + self._rdf_graph.query( |
| 140 | + get_root_query, |
| 141 | + ) |
| 142 | + ) |
115 | 143 | if len(root) != 1:
|
116 |
| - raise RuntimeError("Cannot identify root workflow! Notice that only Workflows can be visualized") |
| 144 | + raise RuntimeError( |
| 145 | + "Cannot identify root workflow! Notice that only Workflows can be visualized" |
| 146 | + ) |
117 | 147 |
|
118 |
| - workflow = root[0]['workflow'] # type: rdflib.URIRef |
| 148 | + workflow = root[0]["workflow"] # type: rdflib.URIRef |
119 | 149 | return workflow
|
120 | 150 |
|
121 | 151 | @classmethod
|
122 | 152 | def _init_dot_graph(cls) -> pydot.Graph:
|
123 |
| - graph = pydot.Graph(graph_type='digraph', simplify=False) |
| 153 | + graph = pydot.Graph(graph_type="digraph", simplify=False) |
124 | 154 | graph.set_bgcolor("#eeeeee")
|
125 | 155 | graph.set_clusterrank("local")
|
126 | 156 | graph.set_labelloc("bottom")
|
|
0 commit comments