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