comparison env/lib/python3.9/site-packages/ephemeris/generate_tool_list_from_ga_workflow_files.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 #!/usr/bin/env python
2 '''Tool to generate tools from workflows'''
3 import json
4 from argparse import ArgumentParser, RawDescriptionHelpFormatter
5
6 import yaml
7
8 from .shed_tools_methods import format_tool_shed_url
9
10 INSTALL_TOOL_DEPENDENCIES = 'install_tool_dependencies: True'
11 INSTALL_REPOSITORY_DEPENDENCIES = 'install_repository_dependencies: True'
12 INSTALL_RESOLVER_DEPENDENCIES = 'install_resolver_dependencies: True'
13
14
15 def _parse_cli_options():
16 """
17 Parse command line options, returning `parse_args` from `ArgumentParser`.
18 """
19 parser = _parser()
20 return parser.parse_args()
21
22
23 def _parser():
24 parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter,
25 usage="%(prog)s <options>",
26 epilog="Workflow files must have been exported from Galaxy release 16.04 or newer.\n\n"
27 "example:\n"
28 "python %(prog)s -w workflow1 workflow2 -o mytool_list.yml -l my_panel_label\n"
29 "Christophe Antoniewski <drosofff@gmail.com>\n"
30 "https://github.com/ARTbio/ansible-artimed/tree/master/extra-files/generate_tool_list_from_ga_workflow_files.py")
31 parser.add_argument('-w', '--workflow',
32 dest="workflow_files",
33 required=True,
34 nargs='+',
35 help='A space separated list of galaxy workflow description files in json format', )
36 parser.add_argument('-o', '--output-file',
37 required=True,
38 dest='output_file',
39 help='The output file with a yml tool list')
40 parser.add_argument('-l', '--panel_label',
41 dest='panel_label',
42 default='Tools from workflows',
43 help='The name of the panel where the tools will show up in Galaxy.'
44 'If not specified: "Tools from workflows"')
45 return parser
46
47
48 def get_workflow_dictionary(json_file):
49 with open(json_file, "r") as File:
50 mydict = json.load(File)[u'steps']
51 return mydict
52
53
54 def translate_workflow_dictionary_to_tool_list(tool_dictionary, panel_label):
55 starting_tool_list = []
56 for step in tool_dictionary.values():
57 tsr = step.get("tool_shed_repository")
58 if tsr:
59 starting_tool_list.append(tsr)
60 tool_list = []
61 for tool in starting_tool_list:
62 sub_dic = {
63 'name': tool['name'],
64 'owner': tool['owner'],
65 'revisions': [tool['changeset_revision']],
66 'tool_panel_section_label': panel_label,
67 'tool_shed_url': format_tool_shed_url(tool['tool_shed'])}
68 tool_list.append(sub_dic)
69 return tool_list
70
71
72 def print_yaml_tool_list(tool_dictionary, output_file):
73 with open(output_file, 'w') as F:
74 F.write("\n".join([INSTALL_TOOL_DEPENDENCIES, INSTALL_REPOSITORY_DEPENDENCIES, INSTALL_RESOLVER_DEPENDENCIES, "", ""]))
75 F.write(yaml.safe_dump(tool_dictionary, default_flow_style=False))
76 return
77
78
79 def reduce_tool_list(tool_list):
80 for current_tool in tool_list:
81 for tool in tool_list:
82 if current_tool is tool:
83 continue
84 if (tool["name"] == current_tool['name'] and
85 tool['owner'] == current_tool['owner'] and
86 tool['tool_panel_section_label'] == current_tool['tool_panel_section_label'] and
87 tool['tool_shed_url'] == current_tool['tool_shed_url']):
88 current_tool["revisions"].extend(tool["revisions"])
89 tool_list.remove(tool)
90 current_tool['revisions'] = list(set(current_tool['revisions']))
91 return tool_list
92
93
94 def generate_tool_list_from_workflow(workflow_files, panel_label, output_file):
95 """
96
97 :rtype: object
98 """
99 intermediate_tool_list = []
100 for workflow in workflow_files:
101 workflow_dictionary = get_workflow_dictionary(workflow)
102 intermediate_tool_list += translate_workflow_dictionary_to_tool_list(workflow_dictionary, panel_label)
103 reduced_tool_list = reduce_tool_list(intermediate_tool_list)
104 convert_dic = {}
105 convert_dic['tools'] = reduced_tool_list
106 print_yaml_tool_list(convert_dic, output_file)
107
108
109 def main():
110 options = _parse_cli_options()
111 generate_tool_list_from_workflow(options.workflow_files, options.panel_label, options.output_file)
112
113
114 if __name__ == "__main__":
115 main()