2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.ws.jws2;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Hashtable;
23 import java.util.List;
24 import java.util.StringTokenizer;
26 import compbio.metadata.Argument;
27 import compbio.metadata.Option;
28 import compbio.metadata.Parameter;
29 import compbio.metadata.Preset;
30 import compbio.metadata.PresetManager;
31 import compbio.metadata.RunnerConfig;
33 import jalview.ws.jws2.dm.JabaOption;
34 import jalview.ws.jws2.dm.JabaParameter;
35 import jalview.ws.jws2.dm.JabaWsParamSet;
36 import jalview.ws.jws2.jabaws2.Jws2Instance;
37 import jalview.ws.params.ArgumentI;
38 import jalview.ws.params.ParamDatastoreI;
39 import jalview.ws.params.ParamManager;
40 import jalview.ws.params.WsParamSetI;
42 public class JabaParamStore implements ParamDatastoreI
45 Hashtable<String, JabaWsParamSet> editedParams = new Hashtable<String, JabaWsParamSet>();
47 private Jws2Instance service;
49 private RunnerConfig serviceOptions;
51 private Hashtable<String, JabaPreset> servicePresets;
53 public JabaParamStore(Jws2Instance service)
60 public JabaParamStore(Jws2Instance service, ParamManager manager)
62 this.service = service;
63 serviceOptions = service.getRunnerConfig();
64 this.manager = manager;
65 // discover all the user's locally stored presets for this service and
66 // populate the hash table
69 manager.registerParser(service.getUri(), this);
70 WsParamSetI[] prams = manager.getParameterSet(null, service.getUri(),
74 for (WsParamSetI paramset : prams)
76 if (paramset instanceof JabaWsParamSet)
78 editedParams.put(paramset.getName(), (JabaWsParamSet) paramset);
83 .println("Warning: Ignoring parameter set instance of type "
85 + " : Bound but not applicable for service at "
94 public List<WsParamSetI> getPresets()
96 List<WsParamSetI> prefs = new ArrayList();
97 if (servicePresets == null)
99 servicePresets = new Hashtable<String, JabaPreset>();
101 if ((prman = service.getPresets()) != null)
103 List pset = prman.getPresets();
106 for (Object pr : pset)
108 JabaPreset prset = new JabaPreset(service, (Preset) pr);
109 servicePresets.put(prset.getName(), prset);
114 for (JabaPreset pr : servicePresets.values())
118 for (WsParamSetI wspset : editedParams.values())
126 public WsParamSetI getPreset(String name)
128 for (WsParamSetI pr : getPresets())
130 if (pr.getName().equals(name))
138 public static List<ArgumentI> getJwsArgsfromJaba(List jabargs)
140 List<ArgumentI> rgs = new ArrayList<ArgumentI>();
141 for (Object rg : jabargs)
143 ArgumentI narg = (rg instanceof Parameter) ? new JabaParameter(
144 (Parameter) rg) : (rg instanceof Option) ? new JabaOption(
149 "Implementation Error: Cannot handle Jaba parameter object "
160 public static List getJabafromJwsArgs(List<ArgumentI> jwsargs)
162 List rgs = new ArrayList();
163 for (ArgumentI rg : jwsargs)
165 Argument narg = (rg instanceof JabaOption) ? ((JabaOption) rg)
170 "Implementation Error: Cannot handle Jaba parameter object "
182 public List<ArgumentI> getServiceParameters()
184 return getJwsArgsfromJaba(serviceOptions.getArguments());
188 public boolean presetExists(String name)
190 return (editedParams.containsKey(name) || servicePresets
195 public void deletePreset(String name)
197 if (editedParams.containsKey(name))
199 WsParamSetI parameterSet = editedParams.get(name);
200 editedParams.remove(name);
203 manager.deleteParameterSet(parameterSet);
207 if (servicePresets.containsKey(name))
210 "Implementation error: Attempt to delete a service preset!");
215 public void storePreset(String presetName, String text,
216 List<ArgumentI> jobParams)
218 JabaWsParamSet jps = new JabaWsParamSet(presetName, text, jobParams);
219 jps.setApplicableUrls(new String[]
220 { service.getUri() });
221 editedParams.put(jps.getName(), jps);
224 manager.storeParameterSet(jps);
229 public void updatePreset(String oldName, String presetName, String text,
230 List<ArgumentI> jobParams)
232 JabaWsParamSet jps = (JabaWsParamSet) ((oldName != null) ? getPreset(oldName)
233 : getPreset(presetName));
236 throw new Error("Implementation error: Can't locate either oldname ("
237 + oldName + ") or presetName (" + presetName
238 + "in the datastore!");
240 jps.setName(presetName);
241 jps.setDescription(text);
242 jps.setArguments(jobParams);
243 jps.setApplicableUrls(new String[]
244 { service.getUri() });
245 if (oldName != null && !oldName.equals(jps.getName()))
247 editedParams.remove(oldName);
249 editedParams.put(jps.getName(), jps);
253 manager.storeParameterSet(jps);
258 * create a new, empty parameter set for this service
262 WsParamSetI newWsParamSet()
264 return new JabaWsParamSet();
267 private boolean involves(String[] urls)
269 boolean found = false;
270 for (String url : urls)
272 if (service.getServiceTypeURI().equals(url)
273 || service.getUri().equalsIgnoreCase(url))
283 public WsParamSetI parseServiceParameterFile(String name, String descr,
284 String[] urls, String parameterfile) throws IOException
288 throw new IOException(
289 "Implementation error: Cannot find service url in the given url set!");
292 JabaWsParamSet wsp = new JabaWsParamSet();
294 wsp.setDescription(descr);
295 wsp.setApplicableUrls(urls.clone());
297 List<String> lines = new ArrayList<String>();
298 StringTokenizer st = new StringTokenizer(parameterfile, "\n");
299 while (st.hasMoreTokens())
301 lines.add(st.nextToken());
303 wsp.setjabaArguments(ParameterUtils.processParameters(lines,
304 serviceOptions, " "));
309 public String generateServiceParameterFile(WsParamSetI pset)
312 if (!involves(pset.getApplicableUrls()))
314 throw new IOException(
315 "Implementation error: Cannot find service url in the given url set for this service parameter store ("
316 + service.getUri() + ") !");
319 if (!(pset instanceof JabaWsParamSet))
322 "Implementation error: JabaWsParamSets can only be handled by JabaParamStore");
325 StringBuffer rslt = new StringBuffer();
326 for (String ln : ParameterUtils.writeParameterSet(
327 ((JabaWsParamSet) pset).getjabaArguments(), " "))
333 return rslt.toString();