2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.jws2;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Hashtable;
27 import java.util.List;
28 import java.util.StringTokenizer;
30 import org.jmol.util.ArrayUtil;
31 import compbio.metadata.Argument;
32 import compbio.metadata.Option;
33 import compbio.metadata.Parameter;
34 import compbio.metadata.Preset;
35 import compbio.metadata.PresetManager;
36 import compbio.metadata.RunnerConfig;
38 import jalview.util.MessageManager;
39 import jalview.ws.jws2.dm.JabaOption;
40 import jalview.ws.jws2.dm.JabaParameter;
41 import jalview.ws.jws2.dm.JabaWsParamSet;
42 import jalview.ws.jws2.jabaws2.Jws2Instance;
43 import jalview.ws.params.ArgumentI;
44 import jalview.ws.params.ParamDatastoreI;
45 import jalview.ws.params.ParamManager;
46 import jalview.ws.params.WsParamSetI;
48 public class JabaParamStore implements ParamDatastoreI
51 Hashtable<String, JabaWsParamSet> editedParams = new Hashtable<String, JabaWsParamSet>();
53 private Jws2Instance service;
55 private RunnerConfig serviceOptions;
57 private Hashtable<String, JabaPreset> servicePresets;
59 public JabaParamStore(Jws2Instance service)
66 public JabaParamStore(Jws2Instance service, ParamManager manager)
68 this.service = service;
69 serviceOptions = service.getRunnerConfig();
70 this.manager = manager;
71 // discover all the user's locally stored presets for this service and
72 // populate the hash table
75 manager.registerParser(service.getUri(), this);
76 WsParamSetI[] prams = manager.getParameterSet(null, service.getUri(),
80 for (WsParamSetI paramset : prams)
82 if (paramset instanceof JabaWsParamSet)
84 editedParams.put(paramset.getName(), (JabaWsParamSet) paramset);
89 .println("Warning: Ignoring parameter set instance of type "
91 + " : Bound but not applicable for service at "
100 public List<WsParamSetI> getPresets()
102 List<WsParamSetI> prefs = new ArrayList();
103 if (servicePresets == null)
105 servicePresets = new Hashtable<String, JabaPreset>();
107 if ((prman = service.getPresets()) != null)
109 List pset = prman.getPresets();
112 for (Object pr : pset)
114 JabaPreset prset = new JabaPreset(service, (Preset) pr);
115 servicePresets.put(prset.getName(), prset);
120 for (JabaPreset pr : servicePresets.values())
124 for (WsParamSetI wspset : editedParams.values())
132 public WsParamSetI getPreset(String name)
134 for (WsParamSetI pr : getPresets())
136 if (pr.getName().equals(name))
144 public static List<ArgumentI> getJwsArgsfromJaba(List jabargs)
146 return getJwsArgsfromJaba(jabargs, true);
149 public static List<ArgumentI> getJwsArgsfromJaba(List jabargs,
152 List<ArgumentI> rgs = new ArrayList<ArgumentI>();
153 List<String> rgnames = new ArrayList<String>();
154 for (Object rg : jabargs)
156 ArgumentI narg = null;
157 String nargstring = null;
158 if (rg instanceof Parameter)
160 narg = new JabaParameter((Parameter) rg);
161 nargstring = narg.getName(); // just sort by name for this
163 else if (rg instanceof Option)
165 narg = new JabaOption((Option) rg);
166 nargstring = (String) ((Option) rg).getOptionNames().get(0);
170 throw new Error(MessageManager.formatMessage("error.implementation_error_cannot_handle_jaba_param", new String[]{rg.getClass().toString()}));
175 rgnames.add(nargstring);
182 ArgumentI[] rgssort = rgs.toArray(new ArgumentI[rgs.size()]);
183 String[] rgssorton = rgnames.toArray(new String[rgs.size()]);
184 jalview.util.QuickSort.sort(rgssorton, rgssort);
187 while (rgssort.length - i > i)
189 tmp1 = rgssort[rgssort.length - i - 1];
190 rgssort[rgssort.length - i - 1] = rgssort[i];
193 return Arrays.asList(rgssort);
196 public static List getJabafromJwsArgs(List<ArgumentI> jwsargs)
198 List rgs = new ArrayList();
199 for (ArgumentI rg : jwsargs)
201 Argument narg = (rg instanceof JabaOption) ? ((JabaOption) rg)
205 throw new Error(MessageManager.formatMessage("error.implementation_error_cannot_handle_jaba_param", new String[]{rg.getClass().toString()}));
216 public List<ArgumentI> getServiceParameters()
218 return getJwsArgsfromJaba(serviceOptions.getArguments());
222 public boolean presetExists(String name)
224 return (editedParams.containsKey(name) || servicePresets
229 public void deletePreset(String name)
231 if (editedParams.containsKey(name))
233 WsParamSetI parameterSet = editedParams.get(name);
234 editedParams.remove(name);
237 manager.deleteParameterSet(parameterSet);
241 if (servicePresets.containsKey(name))
243 throw new Error(MessageManager.getString("error.implementation_error_attempt_to_delete_service_preset"));
248 public void storePreset(String presetName, String text,
249 List<ArgumentI> jobParams)
251 JabaWsParamSet jps = new JabaWsParamSet(presetName, text, jobParams);
252 jps.setApplicableUrls(new String[]
253 { service.getUri() });
254 editedParams.put(jps.getName(), jps);
257 manager.storeParameterSet(jps);
262 public void updatePreset(String oldName, String presetName, String text,
263 List<ArgumentI> jobParams)
265 JabaWsParamSet jps = (JabaWsParamSet) ((oldName != null) ? getPreset(oldName)
266 : getPreset(presetName));
269 throw new Error(MessageManager.formatMessage("error.implementation_error_cannot_locate_oldname_presetname", new String[]{oldName,presetName}));
271 jps.setName(presetName);
272 jps.setDescription(text);
273 jps.setArguments(jobParams);
274 jps.setApplicableUrls(new String[]
275 { service.getUri() });
276 if (oldName != null && !oldName.equals(jps.getName()))
278 editedParams.remove(oldName);
280 editedParams.put(jps.getName(), jps);
284 manager.storeParameterSet(jps);
289 * create a new, empty parameter set for this service
293 WsParamSetI newWsParamSet()
295 return new JabaWsParamSet();
298 private boolean involves(String[] urls)
300 boolean found = false;
301 for (String url : urls)
303 if (service.getServiceTypeURI().equals(url)
304 || service.getUri().equalsIgnoreCase(url))
314 public WsParamSetI parseServiceParameterFile(String name, String descr,
315 String[] urls, String parameterfile) throws IOException
319 throw new IOException(MessageManager.getString("error.implementation_error_cannot_find_service_url_in_given_set"));
322 JabaWsParamSet wsp = new JabaWsParamSet();
324 wsp.setDescription(descr);
325 wsp.setApplicableUrls(urls.clone());
327 List<String> lines = new ArrayList<String>();
328 StringTokenizer st = new StringTokenizer(parameterfile, "\n");
329 while (st.hasMoreTokens())
331 lines.add(st.nextToken());
333 wsp.setjabaArguments(ParameterUtils.processParameters(lines,
334 serviceOptions, " "));
339 public String generateServiceParameterFile(WsParamSetI pset)
342 if (!involves(pset.getApplicableUrls()))
344 throw new IOException(MessageManager.formatMessage("error.implementation_error_cannot_find_service_url_in_given_set_param_store", new String[]{service.getUri()}));
346 if (!(pset instanceof JabaWsParamSet))
348 throw new Error(MessageManager.getString("error.implementation_error_jabaws_param_set_only_handled_by"));
351 StringBuffer rslt = new StringBuffer();
352 for (String ln : ParameterUtils.writeParameterSet(
353 ((JabaWsParamSet) pset).getjabaArguments(), " "))
359 return rslt.toString();