|
| 1 | +"""Visualize a CWL workflow.""" |
| 2 | +import rdflib |
| 3 | +from urllib.parse import urlparse |
| 4 | +import os |
| 5 | +import pydot # type: ignore |
| 6 | + |
| 7 | + |
| 8 | +class CWLViewer: |
| 9 | + """Produce similar images with the https://github.com/common-workflow-language/cwlviewer.""" |
| 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') |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + rdf_description # type: str |
| 20 | + ): |
| 21 | + """Create a viewer object based on the rdf description of the workflow.""" |
| 22 | + 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._root_graph_uri = self._get_root_graph_uri() # type: str |
| 25 | + self._set_inner_edges() |
| 26 | + self._set_input_edges() |
| 27 | + self._set_output_edges() |
| 28 | + |
| 29 | + def _load_cwl_graph( |
| 30 | + self, |
| 31 | + rdf_description # type: str |
| 32 | + ) -> rdflib.graph.Graph: |
| 33 | + rdf_graph = rdflib.Graph() |
| 34 | + rdf_graph.parse(data=rdf_description, format='n3') |
| 35 | + return rdf_graph |
| 36 | + |
| 37 | + def _set_inner_edges(self) -> None: |
| 38 | + with open(self._get_inner_edges_query_path) as f: |
| 39 | + get_inner_edges_query = f.read() |
| 40 | + inner_edges = self._rdf_graph.query(get_inner_edges_query, initBindings={'root_graph': self._root_graph_uri}) |
| 41 | + 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 |
| 45 | + n = pydot.Node( |
| 46 | + '', |
| 47 | + fillcolor='lightgoldenrodyellow', style="filled", |
| 48 | + label=source_label, |
| 49 | + shape='record' |
| 50 | + ) |
| 51 | + n.set_name(str(inner_edge_row['source_step'])) |
| 52 | + 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 |
| 56 | + n = pydot.Node( |
| 57 | + '', |
| 58 | + fillcolor='lightgoldenrodyellow', style="filled", |
| 59 | + label=target_label, |
| 60 | + shape='record' |
| 61 | + ) |
| 62 | + n.set_name(str(inner_edge_row['target_step'])) |
| 63 | + 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']))) |
| 65 | + |
| 66 | + def _set_input_edges(self) -> None: |
| 67 | + with open(self._get_input_edges_query_path) as f: |
| 68 | + get_input_edges_query = f.read() |
| 69 | + inputs_subgraph = pydot.Subgraph(graph_name="cluster_inputs") |
| 70 | + self._dot_graph.add_subgraph(inputs_subgraph) |
| 71 | + inputs_subgraph.set_rank("same") |
| 72 | + inputs_subgraph.create_attribute_methods(['style']) |
| 73 | + inputs_subgraph.set_style("dashed") |
| 74 | + inputs_subgraph.set_label("Workflow Inputs") |
| 75 | + |
| 76 | + input_edges = self._rdf_graph.query(get_input_edges_query, initBindings={'root_graph': self._root_graph_uri}) |
| 77 | + for input_row in input_edges: |
| 78 | + n = pydot.Node( |
| 79 | + '', |
| 80 | + fillcolor="#94DDF4", |
| 81 | + style="filled", |
| 82 | + label=urlparse(input_row['input']).fragment, |
| 83 | + shape='record' |
| 84 | + ) |
| 85 | + n.set_name(str(input_row['input'])) |
| 86 | + inputs_subgraph.add_node(n) |
| 87 | + self._dot_graph.add_edge(pydot.Edge(str(input_row['input']), str(input_row['step']))) |
| 88 | + |
| 89 | + def _set_output_edges(self) -> None: |
| 90 | + with open(self._get_output_edges_query_path) as f: |
| 91 | + get_output_edges = f.read() |
| 92 | + outputs_graph = pydot.Subgraph(graph_name="cluster_outputs") |
| 93 | + self._dot_graph.add_subgraph(outputs_graph) |
| 94 | + outputs_graph.set_rank("same") |
| 95 | + outputs_graph.create_attribute_methods(['style']) |
| 96 | + outputs_graph.set_style("dashed") |
| 97 | + outputs_graph.set_label("Workflow Outputs") |
| 98 | + outputs_graph.set_labelloc("b") |
| 99 | + output_edges = self._rdf_graph.query(get_output_edges, initBindings={'root_graph': self._root_graph_uri}) |
| 100 | + 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'])) |
| 108 | + outputs_graph.add_node(n) |
| 109 | + self._dot_graph.add_edge(pydot.Edge(output_edge_row['step'], output_edge_row['output'])) |
| 110 | + |
| 111 | + def _get_root_graph_uri(self) -> rdflib.URIRef: |
| 112 | + with open(self._get_root_query_path) as f: |
| 113 | + get_root_query = f.read() |
| 114 | + root = list(self._rdf_graph.query(get_root_query, )) |
| 115 | + if len(root) != 1: |
| 116 | + raise RuntimeError("Cannot identify root workflow! Notice that only Workflows can be visualized") |
| 117 | + |
| 118 | + workflow = root[0]['workflow'] # type: rdflib.URIRef |
| 119 | + return workflow |
| 120 | + |
| 121 | + @classmethod |
| 122 | + def _init_dot_graph(cls) -> pydot.Graph: |
| 123 | + graph = pydot.Graph(graph_type='digraph', simplify=False) |
| 124 | + graph.set_bgcolor("#eeeeee") |
| 125 | + graph.set_clusterrank("local") |
| 126 | + graph.set_labelloc("bottom") |
| 127 | + graph.set_labelloc("bottom") |
| 128 | + graph.set_labeljust("right") |
| 129 | + |
| 130 | + return graph |
| 131 | + |
| 132 | + def get_dot_graph(self) -> pydot.Graph: |
| 133 | + """Get the dot graph object.""" |
| 134 | + return self._dot_graph |
| 135 | + |
| 136 | + def dot(self) -> str: |
| 137 | + """Get the graph as graphviz.""" |
| 138 | + return str(self._dot_graph.to_string()) |
0 commit comments