comparison env/lib/python3.7/site-packages/planemo/training/tool_input.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 """Module contains code for the ToolInput class, dealing with the description of tool in workflow and XML."""
2
3 from planemo import templates
4 from planemo.io import info
5
6
7 INPUT_PARAM = """
8 >{{space}}- *"{{param_label}}"*: `{{param_value}}`
9 """
10
11 INPUT_FILE_TEMPLATE = """
12 >{{space}}- {{ '{%' }} icon {{icon}} {{ '%}' }} *"{{input_name}}"*: {{input_value}}
13 """
14
15 INPUT_SECTION = """
16 >{{space}}- In *"{{section_label}}"*:
17 """
18
19 INPUT_ADD_REPEAT = """
20 >{{space}}- {{ '{%' }} icon param-repeat {{ '%}' }} *"Insert {{repeat_label}}"*
21 """
22
23 SPACE = ' '
24
25
26 class ToolInput(object):
27 """Class to describe a tool input / parameter and its value from a workflow."""
28
29 def __init__(self, tool_inp_desc, wf_param_values, wf_steps, level, should_be_there=False, force_default=False):
30 """Init an instance of ToolInput."""
31 self.name = tool_inp_desc['name']
32 if 'type' not in tool_inp_desc:
33 raise ValueError("No type for the parameter %s" % tool_inp_desc['name'])
34 self.type = tool_inp_desc['type']
35 self.tool_inp_desc = tool_inp_desc
36 self.level = level
37 self.wf_param_values = wf_param_values
38 self.wf_steps = wf_steps
39 self.formatted_desc = ''
40 self.force_default = force_default
41
42 if self.name not in self.wf_param_values:
43 if not should_be_there:
44 info("%s not in workflow" % self.name)
45 else:
46 raise ValueError("%s not in workflow" % (self.name))
47 else:
48 self.wf_param_values = self.wf_param_values[self.name]
49
50 def get_formatted_inputs(self):
51 """Format the inputs of a step."""
52 inputlist = ''
53 inps = []
54 if isinstance(self.wf_param_values, list):
55 # multiple input (not collection)
56 icon = 'param-files'
57 for i in self.wf_param_values:
58 inps.append('`%s` %s' % (
59 i['output_name'],
60 get_input_tool_name(i['id'], self.wf_steps)))
61 else:
62 inp = self.wf_param_values
63 if 'id' in inp:
64 # sinle input or collection
65 inp_type = self.wf_steps[str(inp['id'])]['type']
66 if 'collection' in inp_type:
67 icon = 'param-collection'
68 else:
69 icon = 'param-file'
70 inps = ['`%s` %s' % (
71 inp['output_name'],
72 get_input_tool_name(inp['id'], self.wf_steps))]
73 if len(inps) > 0:
74 inputlist += templates.render(INPUT_FILE_TEMPLATE, **{
75 "icon": icon,
76 "input_name": self.tool_inp_desc['label'],
77 "input_value": ', '.join(inps),
78 "space": SPACE * self.level
79 })
80 return inputlist
81
82 def get_lower_param_desc(self):
83 """Get the formatted description of the paramaters in the 'inputs' of the tool description."""
84 sub_param_desc = ''
85 for inp in self.tool_inp_desc["inputs"]:
86 tool_inp = ToolInput(
87 inp,
88 self.wf_param_values,
89 self.wf_steps,
90 self.level + 1)
91 sub_param_desc += tool_inp.get_formatted_desc()
92 return sub_param_desc
93
94 def get_formatted_section_desc(self):
95 """Format the description (label and value) for parameters in a section."""
96 section_paramlist = ''
97 sub_param_desc = self.get_lower_param_desc()
98 if sub_param_desc != '':
99 section_paramlist += templates.render(INPUT_SECTION, **{
100 'space': SPACE * self.level,
101 'section_label': self.tool_inp_desc['title']})
102 section_paramlist += sub_param_desc
103 return section_paramlist
104
105 def get_formatted_conditional_desc(self):
106 """Format the description (label and value) for parameters in a conditional."""
107 conditional_paramlist = ''
108 # Get conditional parameter
109 inp = ToolInput(
110 self.tool_inp_desc['test_param'],
111 self.wf_param_values,
112 self.wf_steps,
113 self.level,
114 should_be_there=True,
115 force_default=True)
116 conditional_paramlist = inp.get_formatted_desc()
117 cond_param = inp.wf_param_values
118
119 # Get parameters in the when and their values
120 tmp_tool_inp_desc = self.tool_inp_desc
121 for case in tmp_tool_inp_desc['cases']:
122 if case['value'] == cond_param and len(case['inputs']) > 0:
123 self.tool_inp_desc = case
124 conditional_paramlist += self.get_lower_param_desc()
125 self.tool_inp_desc = tmp_tool_inp_desc
126 return conditional_paramlist
127
128 def get_formatted_repeat_desc(self):
129 """Format the description (label and value) for parameters in a repeat."""
130 repeat_paramlist = ''
131 if self.wf_param_values != '[]':
132 tool_inp = {}
133 for inp in self.tool_inp_desc["inputs"]:
134 tool_inp.setdefault(inp['name'], inp)
135 tmp_wf_param_values = self.wf_param_values
136 cur_level = self.level
137 for ind, param in enumerate(tmp_wf_param_values):
138 self.wf_param_values = param
139 self.level = cur_level + 1
140 paramlist_in_repeat = self.get_lower_param_desc()
141 if paramlist_in_repeat != '':
142 # add first click
143 repeat_paramlist += templates.render(INPUT_ADD_REPEAT, **{
144 'space': SPACE * (self.level),
145 'repeat_label': self.tool_inp_desc['title']})
146 repeat_paramlist += paramlist_in_repeat
147 self.level = cur_level
148 self.wf_param_values = tmp_wf_param_values
149
150 repeat_desc = ''
151 if repeat_paramlist != '':
152 repeat_desc += templates.render(INPUT_SECTION, **{
153 'space': SPACE * self.level,
154 'section_label': self.tool_inp_desc['title']}) + repeat_paramlist
155 return repeat_desc
156
157 def get_formatted_other_param_desc(self):
158 """Get value of a 'simple' parameter if different from the default value, None otherwise."""
159 param_value = None
160 if self.tool_inp_desc['value'] == self.wf_param_values and not self.force_default:
161 param_value = None
162 elif self.type == 'boolean':
163 if bool(self.tool_inp_desc['value']) == self.wf_param_values:
164 param_value = None
165 else:
166 param_value = 'Yes' if self.wf_param_values else 'No'
167 elif self.type == 'select':
168 param_values = []
169 for opt in self.tool_inp_desc['options']:
170 if opt[1] == self.wf_param_values:
171 param_values.append(opt[0])
172 param_value = ', '.join(param_values)
173 elif self.type == 'data_column':
174 param_value = "c%s" % self.wf_param_values
175 else:
176 param_value = self.wf_param_values
177
178 param_desc = ''
179 if param_value is not None:
180 param_desc = templates.render(INPUT_PARAM, **{
181 'space': SPACE * self.level,
182 'param_label': self.tool_inp_desc['label'],
183 'param_value': param_value})
184 return param_desc
185
186 def get_formatted_desc(self):
187 """Get the formatted description (ready for hands-on tutorial) of the parameter."""
188 if self.wf_param_values:
189 if self.type == 'data' or self.type == 'data_collection':
190 self.formatted_desc += self.get_formatted_inputs()
191 elif self.type == 'section':
192 self.formatted_desc += self.get_formatted_section_desc()
193 elif self.type == 'conditional':
194 self.formatted_desc += self.get_formatted_conditional_desc()
195 elif self.type == 'repeat':
196 self.formatted_desc += self.get_formatted_repeat_desc()
197 else:
198 self.formatted_desc += self.get_formatted_other_param_desc()
199 return self.formatted_desc
200
201
202 def get_input_tool_name(step_id, steps):
203 """Get the string with the name of the tool that generated an input."""
204 inp_provenance = ''
205 inp_prov_id = str(step_id)
206 if inp_prov_id in steps:
207 name = steps[inp_prov_id]['name']
208 if 'Input dataset' in name:
209 inp_provenance = "(%s)" % name
210 else:
211 inp_provenance = "(output of **%s** {%% icon tool %%})" % name
212 return inp_provenance
213
214
215 def get_empty_input():
216 """Get the string for an empty input."""
217 return templates.render(INPUT_FILE_TEMPLATE, **{
218 'space': 1 * SPACE,
219 'icon': 'param-file',
220 'input_name': 'Input file',
221 'input_value': 'File'
222 })
223
224
225 def get_empty_param():
226 """Get the string for an empty param."""
227 return templates.render(INPUT_PARAM, **{
228 'space': 1 * SPACE,
229 'param_label': 'Parameter',
230 'param_value': 'a value'
231 })