Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/gxformat2/cytoscape.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 """Build standalone visualization for Galaxy workflows.""" | |
2 import json | |
3 import os | |
4 import string | |
5 import sys | |
6 | |
7 import pkg_resources | |
8 | |
9 from gxformat2.converter import ensure_step_position | |
10 from gxformat2.normalize import steps_normalized | |
11 | |
12 CYTOSCAPE_JS_TEMPLATE = pkg_resources.resource_filename(__name__, 'cytoscape.html') | |
13 MAIN_TS_PREFIX = "toolshed.g2.bx.psu.edu/repos/" | |
14 | |
15 | |
16 def to_cytoscape(workflow_path, output_path=None): | |
17 """Produce cytoscape output for supplied workflow path.""" | |
18 if output_path is None: | |
19 output_path, _ = os.path.splitext(workflow_path) | |
20 output_path += ".html" | |
21 | |
22 steps = steps_normalized(workflow_path=workflow_path) | |
23 elements = [] | |
24 for i, step in enumerate(steps): | |
25 step_id = step.get("id") or step.get("label") or str(i) | |
26 step_type = step.get("type") or 'tool' | |
27 classes = ["type_%s" % step_type] | |
28 if step_type in ['tool', 'subworkflow']: | |
29 classes.append("runnable") | |
30 else: | |
31 classes.append("input") | |
32 | |
33 tool_id = step.get("tool_id") | |
34 if tool_id and tool_id.startswith(MAIN_TS_PREFIX): | |
35 tool_id = tool_id[len(MAIN_TS_PREFIX):] | |
36 label = step.get("id") or step.get("label") or ("tool:%s" % tool_id) or str(i) | |
37 ensure_step_position(step, i) | |
38 node_position = dict(x=int(step["position"]["left"]), y=int(step["position"]["top"])) | |
39 repo_link = None | |
40 if "tool_shed_repository" in step: | |
41 repo = step["tool_shed_repository"] | |
42 repo_link = "https://" + repo["tool_shed"] + "/view/" + repo["owner"] + "/" + repo["name"] + "/" + repo["changeset_revision"] | |
43 node_data = { | |
44 "id": step_id, | |
45 "label": label, | |
46 "doc": step.get("doc"), | |
47 "tool_id": step.get("tool_id"), | |
48 "step_type": step_type, | |
49 "repo_link": repo_link | |
50 } | |
51 elements.append({"group": "nodes", "data": node_data, "classes": classes, "position": node_position}) | |
52 for key, value in (step.get("in") or {}).items(): | |
53 # handle lists? | |
54 if isinstance(value, dict) and 'source' in value: | |
55 value = value["source"] | |
56 elif isinstance(value, dict): | |
57 continue | |
58 if "/" in value: | |
59 from_step, output = value.split("/", 1) | |
60 else: | |
61 from_step, output = value, None | |
62 edge_id = "%s__to__%s" % (step_id, from_step) | |
63 edge_data = {"id": edge_id, "source": from_step, "target": step_id, "input": key, "output": output} | |
64 elements.append({"group": "edges", "data": edge_data}) | |
65 | |
66 if output_path.endswith(".html"): | |
67 with open(CYTOSCAPE_JS_TEMPLATE, "r") as f: | |
68 template = f.read() | |
69 viz = string.Template(template).safe_substitute(elements=json.dumps(elements)) | |
70 with open(output_path, "w") as f: | |
71 f.write(viz) | |
72 else: | |
73 with open(output_path, "w") as f: | |
74 json.dump(elements, f) | |
75 | |
76 | |
77 def main(argv=None): | |
78 """Entry point for building Cytoscape visualizations of Galaxy workflows.""" | |
79 if argv is None: | |
80 argv = sys.argv[1:] | |
81 | |
82 workflow_path = argv[0] | |
83 if len(argv) > 1: | |
84 output_path = argv[1] | |
85 | |
86 to_cytoscape(workflow_path, output_path) | |
87 | |
88 | |
89 if __name__ == "__main__": | |
90 main() |