comparison generate_macros.py @ 0:186565dfa9e5 draft

planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/enasearch/ commit 6eda25f5cccc0cf9be09c38a8b48d37aff56ed87
author iuc
date Tue, 29 Aug 2017 04:12:50 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:186565dfa9e5
1 #!/usr/bin/env python
2
3 import enasearch
4
5 spaces = ' '
6 operator_names = {
7 "=": "equal",
8 "!=": "different",
9 "<": "lower",
10 "<=": "equal or lower",
11 ">": "higher",
12 ">=": "equal or higher",
13 }
14
15
16 def format_name(name, alternative_name):
17 """
18 Format name to remove None name and & in name
19 """
20 if name is None:
21 name = alternative_name
22 name = name.replace("&", "and")
23 return name
24
25
26 def sort_by_name(dict):
27 """
28 Sort a dictionary on the values
29 """
30 return sorted(dict, key=dict.get)
31
32
33 def write_analysis_fields():
34 """
35 Write the analysis fields
36 """
37 s = '%s<xml name="analysis_fields">\n' % (spaces)
38 fields = enasearch.get_returnable_fields(result="analysis", verbose=False)
39 for f in fields:
40 s += '%s<option value="%s">%s</option>\n' % (2 * spaces, f, f)
41 s += '%s</xml>\n' % (spaces)
42 return s
43
44
45 def write_display_options():
46 """
47 Write the display options
48 """
49 s = '%s<xml name="display_options">\n' % (spaces)
50 when_s = '%s<xml name="when_display_options">\n' % (spaces)
51 options = enasearch.get_display_options(verbose=False)
52 for opt in options:
53 s += '%s<option value="%s">%s</option>\n' % (2 * spaces, opt, options[opt]['description'])
54 when_s += '%s<when value="%s">\n' % (2 * spaces, opt)
55 if opt == 'fasta' or opt == 'fastq':
56 when_s += '%s<param name="range_start" argument="--subseq_range" type="integer" optional="true" label="Start integer for subsequences"/>\n' % (3 * spaces)
57 when_s += '%s<param name="range_stop" argument="--subseq_range" type="integer" optional="true" label="Stop integer for subsequences"/>\n' % (3 * spaces)
58 else:
59 when_s += '%s<param argument="--offset" type="integer" optional="true" label="First record to get"/>\n' % (3 * spaces)
60 when_s += '%s<param argument="--length" type="integer" optional="true" label="Number of records to retrieve"/>\n' % (3 * spaces)
61 when_s += '%s</when>\n' % (2 * spaces)
62 s += '%s</xml>\n' % (spaces)
63 when_s += '%s</xml>\n' % (spaces)
64 s += when_s
65 return s
66
67
68 def write_run_fields():
69 """
70 Write the run fields
71 """
72 s = '%s<xml name="run_fields">\n' % (spaces)
73 fields = enasearch.get_returnable_fields(result="read_run", verbose=False)
74 for f in fields:
75 s += '%s<option value="%s">%s</option>\n' % (2 * spaces, f, f)
76 s += '%s</xml>\n' % (spaces)
77 return s
78
79
80 def write_taxonomy_results():
81 """
82 Write the possible taxonomy results
83 """
84 s = '%s<xml name="taxonomy_results">\n' % (spaces)
85 fields = enasearch.get_taxonomy_results(verbose=False)
86 for f in fields:
87 s += '%s<option value="%s">%s</option>\n' % (2 * spaces, f, fields[f]['description'])
88 s += '%s</xml>\n' % (spaces)
89 return s
90
91
92 def write_result_parameters(fts=False):
93 """
94 Write the parameters that are dependant of results
95 """
96 res = enasearch.get_results(verbose=False)
97 options = enasearch.get_display_options(verbose=False)
98 ft = enasearch.get_filter_types(verbose=False)
99 # Format the filter type related parameters
100 ft_parameters = {}
101 for t in ft:
102 s = ''
103 if 'operators' in ft[t]:
104 s = '%s<param name="operation" type="select" label="Operator">\n' % (7 * spaces)
105 for o in ft[t]['operators']:
106 on = o
107 if o in operator_names:
108 on = operator_names[o]
109 s += '%s<option value="%s">%s</option>\n' % (8 * spaces, on, on)
110 s += '%s</param>\n' % (7 * spaces)
111 if 'value' in ft[t]:
112 value_format = 'float' if t == 'Number' else 'text'
113 s += '%s<param name="value" type="%s" value="" label="%s"/>\n' % (7 * spaces, value_format, ft[t]['value'])
114 elif 'values' in ft[t]:
115 s += '%s<param name="value" type="select" label="Value">\n' % (7 * spaces)
116 for v in ft[t]['values']:
117 s += '%s<option value="%s">%s</option>\n' % (8 * spaces, v, v)
118 s += '%s</param>\n' % (7 * spaces)
119 else:
120 s += '%s<conditional name="op">\n' % (7 * spaces)
121 s += '%s<param name="operation" type="select" label="Operation">\n' % (8 * spaces)
122 for op in ft[t]:
123 s += '%s<option value="%s">%s</option>\n' % (9 * spaces, op, ft[t][op]['description'])
124 s += '%s</param>\n' % (8 * spaces)
125 for op in ft[t]:
126 s += '%s<when value="%s">\n' % (8 * spaces, op)
127 s += '%s<param name="values" type="text" value="" label="%s" help="Values separated by simple comma"/>\n' % (9 * spaces, ",".join(ft[t][op]['parameters']))
128 s += '%s</when>\n' % (8 * spaces)
129 s += '%s</conditional>\n' % (7 * spaces)
130 ft_parameters[t] = s
131 # Start adding the conditional
132 s = '%s<conditional name="res">\n' % (2 * spaces)
133 # Add result parameter
134 s += '%s<param argument="--result" type="select" label="Result to return">\n' % (3 * spaces)
135 for r in res:
136 s += '%s<option value="%s">%s</option>\n' % (4 * spaces, r, res[r]['description'])
137 s += '%s</param>\n' % (3 * spaces)
138 for r in res:
139 sf = enasearch.get_sortable_fields(r)
140 ff = res[r]['filter_fields']
141 s += '%s<when value="%s">\n' % (3 * spaces, r)
142 if not fts:
143 s += '%s<repeat name="queries" title="Add a query">\n' % (4 * spaces)
144 # Add combination operator
145 s += '%s<param name="combination_operation" type="select" label="Combination operation">\n' % (5 * spaces)
146 s += '%s<option value="AND">AND</option>\n' % (6 * spaces)
147 s += '%s<option value="OR">OR</option>\n' % (6 * spaces)
148 s += '%s<option value="NOT">NOT</option>\n' % (6 * spaces)
149 s += '%s</param>\n' % (5 * spaces)
150 s += '%s<conditional name="filter_field">\n' % (5 * spaces)
151 s += '%s<param name="field" type="select" label="Field to query">\n' % (6 * spaces)
152 for f in ff:
153 s += '%s<option value="%s">%s</option>\n' % (7 * spaces, f, ff[f]['description'])
154 s += '%s</param>\n' % (6 * spaces)
155 for f in ff:
156 # Add the correct parameter given the type of field
157 typ = ff[f]['type'].capitalize()
158 if typ not in ft_parameters:
159 if f == 'location':
160 typ = 'Geospatial'
161 else:
162 continue
163 s += '%s<when value="%s">\n' % (6 * spaces, f)
164 s += ft_parameters[typ]
165 s += '%s</when>\n' % (6 * spaces)
166 s += '%s</conditional>\n' % (5 * spaces)
167 s += '%s</repeat>\n' % (4 * spaces)
168 # Add display opt
169 s += '%s<conditional name="display_opt">\n' % (4 * spaces)
170 s += '%s<param argument="--display" type="select" label="Display option to specify the display format">\n' % (5 * spaces)
171 s += '%s<expand macro="display_options"/>\n' % (6 * spaces)
172 s += '%s</param>\n' % (5 * spaces)
173 for opt in options:
174 s += '%s<when value="%s"' % (5 * spaces, opt)
175 if opt != 'fasta' and opt != 'fastq':
176 s += '>\n'
177 s += '%s<param argument="--offset" type="integer" optional="true" label="First record to get"/>\n' % (6 * spaces)
178 s += '%s<param argument="--length" type="integer" optional="true" label="Number of records to retrieve"/>\n' % (6 * spaces)
179 if opt == 'report':
180 s += '%s<param argument="--fields" type="select" multiple="true" label="Fields to return">\n' % (6 * spaces)
181 for f in res[r]['returnable_fields']:
182 s += '%s<option value="%s">%s</option>\n' % (7 * spaces, f, f)
183 s += '%s</param>\n' % (6 * spaces)
184 s += '%s<param argument="--sortfields" type="select" optional="true" multiple="true" label="Fields to sort the results">\n' % (6 * spaces)
185 for f in sf:
186 s += '%s<option value="%s">%s</option>\n' % (7 * spaces, f, sf[f]['description'])
187 s += '%s</param>\n' % (6 * spaces)
188 s += '%s</when>\n' % (5 * spaces)
189 else:
190 s += '/>\n'
191 s += '%s</conditional>\n' % (4 * spaces)
192 s += '%s</when>\n' % (3 * spaces)
193 s += '%s</conditional>\n' % (2 * spaces)
194 return s
195
196
197 def write_search_data_parameters():
198 """
199 Write the parameters for search_data
200 """
201 fts = '%s<xml name="free_text_search">\n' % (spaces)
202 fts += write_result_parameters(True)
203 fts += '%s</xml>\n' % (spaces)
204 cts = '%s<xml name="conditional_text_search">\n' % (spaces)
205 cts += write_result_parameters(False)
206 cts += '%s</xml>\n' % (spaces)
207 return fts + cts
208
209
210 def generate_search_macros(filepath):
211 """
212 Generate the content of the macro file
213 """
214 s = '<?xml version="1.0" ?>\n'
215 s += '<macros>\n'
216 s += write_analysis_fields()
217 s += write_display_options()
218 s += write_run_fields()
219 s += write_taxonomy_results()
220 s += write_search_data_parameters()
221 s += '</macros>\n'
222 with open(filepath, "w") as file:
223 file.write(s)
224
225
226 if __name__ == '__main__':
227 generate_search_macros("search_macros.xml")