2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.BufferedReader;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.io.Writer;
24 import java.util.ArrayList;
25 import java.util.Hashtable;
26 import java.util.List;
27 import java.util.StringTokenizer;
29 import compbio.metadata.Argument;
30 import compbio.metadata.Option;
31 import compbio.metadata.Parameter;
32 import compbio.metadata.Preset;
33 import compbio.metadata.PresetManager;
34 import compbio.metadata.RunnerConfig;
36 import jalview.ws.jws2.dm.JabaOption;
37 import jalview.ws.jws2.dm.JabaParameter;
38 import jalview.ws.jws2.dm.JabaWsParamSet;
39 import jalview.ws.jws2.jabaws2.Jws2Instance;
40 import jalview.ws.params.ArgumentI;
41 import jalview.ws.params.ParamDatastoreI;
42 import jalview.ws.params.ParamManager;
43 import jalview.ws.params.WsParamSetI;
45 public class JabaParamStore implements ParamDatastoreI
48 Hashtable<String, JabaWsParamSet> editedParams = new Hashtable<String, JabaWsParamSet>();
50 private Jws2Instance service;
52 private RunnerConfig serviceOptions;
54 private Hashtable<String, JabaPreset> servicePresets;
56 public JabaParamStore(Jws2Instance service)
63 public JabaParamStore(Jws2Instance service, ParamManager manager)
65 this.service = service;
66 serviceOptions = service.getRunnerConfig();
67 this.manager = manager;
68 // discover all the user's locally stored presets for this service and
69 // populate the hash table
72 manager.registerParser(service.getUri(), this);
73 WsParamSetI[] prams = manager.getParameterSet(null, service.getUri(),
77 for (WsParamSetI paramset : prams)
79 if (paramset instanceof JabaWsParamSet)
81 editedParams.put(paramset.getName(), (JabaWsParamSet) paramset);
86 .println("Warning: Ignoring parameter set instance of type "
88 + " : Bound but not applicable for service at "
97 public List<WsParamSetI> getPresets()
99 List<WsParamSetI> prefs = new ArrayList();
100 if (servicePresets == null)
102 servicePresets = new Hashtable<String, JabaPreset>();
104 if ((prman = service.getPresets()) != null)
106 List pset = prman.getPresets();
109 for (Object pr : pset)
111 JabaPreset prset = new JabaPreset(service, (Preset) pr);
112 servicePresets.put(prset.getName(), prset);
117 for (JabaPreset pr : servicePresets.values())
121 for (WsParamSetI wspset : editedParams.values())
129 public WsParamSetI getPreset(String name)
131 for (WsParamSetI pr : getPresets())
133 if (pr.getName().equals(name))
141 public static List<ArgumentI> getJwsArgsfromJaba(List jabargs)
143 List<ArgumentI> rgs = new ArrayList<ArgumentI>();
144 for (Object rg : jabargs)
146 ArgumentI narg = (rg instanceof Parameter) ? new JabaParameter(
147 (Parameter) rg) : (rg instanceof Option) ? new JabaOption(
152 "Implementation Error: Cannot handle Jaba parameter object "
163 public static List getJabafromJwsArgs(List<ArgumentI> jwsargs)
165 List rgs = new ArrayList();
166 for (ArgumentI rg : jwsargs)
168 Argument narg = (rg instanceof JabaOption) ? ((JabaOption) rg)
173 "Implementation Error: Cannot handle Jaba parameter object "
185 public List<ArgumentI> getServiceParameters()
187 return getJwsArgsfromJaba(serviceOptions.getArguments());
191 public boolean presetExists(String name)
193 return (editedParams.containsKey(name) || servicePresets
198 public void deletePreset(String name)
200 if (editedParams.containsKey(name))
202 WsParamSetI parameterSet = editedParams.get(name);
203 editedParams.remove(name);
206 manager.deleteParameterSet(parameterSet);
210 if (servicePresets.containsKey(name))
213 "Implementation error: Attempt to delete a service preset!");
218 public void storePreset(String presetName, String text,
219 List<ArgumentI> jobParams)
221 JabaWsParamSet jps = new JabaWsParamSet(presetName, text, jobParams);
222 jps.setApplicableUrls(new String[]
223 { service.getUri() });
224 editedParams.put(jps.getName(), jps);
227 manager.storeParameterSet(jps);
232 public void updatePreset(String oldName, String presetName, String text,
233 List<ArgumentI> jobParams)
235 JabaWsParamSet jps = (JabaWsParamSet) ((oldName != null) ? getPreset(oldName)
236 : getPreset(presetName));
239 throw new Error("Implementation error: Can't locate either oldname ("
240 + oldName + ") or presetName (" + presetName
241 + "in the datastore!");
243 jps.setName(presetName);
244 jps.setDescription(text);
245 jps.setArguments(jobParams);
246 jps.setApplicableUrls(new String[]
247 { service.getUri() });
248 if (oldName != null && !oldName.equals(jps.getName()))
250 editedParams.remove(oldName);
252 editedParams.put(jps.getName(), jps);
256 manager.storeParameterSet(jps);
261 * create a new, empty parameter set for this service
265 WsParamSetI newWsParamSet()
267 return new JabaWsParamSet();
270 private boolean involves(String[] urls)
272 boolean found = false;
273 for (String url : urls)
275 if (service.getUri().equalsIgnoreCase(url))
285 public WsParamSetI parseServiceParameterFile(String name, String descr,
286 String[] urls, String parameterfile) throws IOException
290 throw new IOException(
291 "Implementation error: Cannot find service url in the given url set!");
294 JabaWsParamSet wsp = new JabaWsParamSet();
296 wsp.setDescription(descr);
297 wsp.setApplicableUrls(urls.clone());
299 List<String> lines = new ArrayList<String>();
300 StringTokenizer st = new StringTokenizer(parameterfile, "\n");
301 while (st.hasMoreTokens())
303 lines.add(st.nextToken());
305 wsp.setjabaArguments(ParameterUtils.processParameters(lines,
306 serviceOptions, " "));
311 public String generateServiceParameterFile(WsParamSetI pset)
314 if (!involves(pset.getApplicableUrls()))
316 throw new IOException(
317 "Implementation error: Cannot find service url in the given url set for this service parameter store ("
318 + service.getUri() + ") !");
321 if (!(pset instanceof JabaWsParamSet))
324 "Implementation error: JabaWsParamSets can only be handled by JabaParamStore");
327 StringBuffer rslt = new StringBuffer();
328 for (String ln : ParameterUtils.writeParameterSet(
329 ((JabaWsParamSet) pset).getjabaArguments(), " "))
335 return rslt.toString();