16959ffd96838423bc74856996c8163a8993c163
[jalview.git] / src / jalview / ws / jws2 / ParameterUtils.java
1 /*
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
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  *  
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.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws.jws2;
19
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23
24 import compbio.metadata.*;
25
26 public class ParameterUtils
27 {
28   public static List<String> writeParameterSet(List<Option> optSet,
29           String pseparator)
30   {
31     List<String> pset = new ArrayList<String>();
32     for (Option o : optSet)
33     {
34       pset.add(o.toCommand(pseparator));
35     }
36     return pset;
37   }
38
39   /**
40    * Converts options supplied via parameters file into {@code Option} objects
41    * (Refactored from compbio.ws.client.Jws2Client)
42    * 
43    * @param <T>
44    *          web service type
45    * @param params
46    * @param options
47    * @return List of Options of type T
48    * 
49    */
50   /*
51    * @SuppressWarnings(value = { "true" }) public static <T> List<Option<T>>
52    * processParameters(List<String> params, RunnerConfig<T> options, String
53    * pseparator)
54    */
55   public static List<Option> processParameters(List<String> params,
56           RunnerConfig options, String pseparator)
57   {
58     List<Option> chosenOptions = new ArrayList<Option>();
59     for (String param : params)
60     {
61       String oname = null;
62       if (isParameter(param, pseparator))
63       {
64         oname = getParamName(param, pseparator);
65       }
66       else
67       {
68         oname = param;
69       }
70       Option o = options.getArgumentByOptionName(oname);
71       if (o == null)
72       {
73         System.out.println("WARN ignoring unsuppoted parameter: " + oname);
74         continue;
75       }
76       if (o instanceof Parameter)
77       {
78         o = copyParameter((Parameter) o);
79       }
80       else
81       {
82         o = copyOption(o);
83       }
84       {
85         try
86         {
87           o.setDefaultValue(isParameter(param, pseparator) ? getParamValue(
88                   param, pseparator) : param);
89         } catch (WrongParameterException e)
90         {
91           System.out.println("Problem setting value for the parameter: "
92                   + param);
93           e.printStackTrace();
94         }
95       }
96       chosenOptions.add(o);
97     }
98     return chosenOptions;
99   }
100
101   static String getParamName(String fullName, String pseparator)
102   {
103     assert isParameter(fullName, pseparator);
104     return fullName.substring(0, fullName.indexOf(pseparator));
105   }
106
107   static String getParamValue(String fullName, String pseparator)
108   {
109     assert isParameter(fullName, pseparator);
110     return fullName.substring(fullName.indexOf(pseparator) + 1);
111   }
112
113   static boolean isParameter(String param, String pseparator)
114   {
115     return param.contains(pseparator);
116   }
117
118   public static Option copyOption(Option option)
119   {
120     Option copy = new Option(option.getName(), option.getDescription());
121     setOptionFrom(copy, option);
122     return copy;
123   }
124
125   public static void setOptionFrom(Option copy, Option option)
126   {
127     copy.setName(option.getName());
128     copy.setDescription(option.getDescription());
129     copy.setFurtherDetails(option.getFurtherDetails());
130     copy.setRequired(option.isRequired());
131     List<String> names = option.getOptionNames();
132     if (names != null)
133     {
134       if (names.size() == 1)
135       {
136         HashSet<String> st = new HashSet();
137         st.add(names.get(0));
138         copy.setOptionNames(st);
139       }
140       else
141       {
142         copy.addOptionNames(names.toArray(new String[]
143         {}));
144       }
145     }
146     try
147     {
148       if (option.getDefaultValue() != null)
149       {
150         copy.setDefaultValue(option.getDefaultValue());
151       }
152     } catch (Exception ex)
153     {
154       ex.printStackTrace();
155     }
156   }
157
158   public static ValueConstrain copyValueConstrain(ValueConstrain vc)
159   {
160     try
161     {
162       ValueConstrain copy = new ValueConstrain();
163       if (vc.getMax() != null)
164       {
165         copy.setMax(vc.getMax().toString());
166       }
167       if (vc.getMin() != null)
168       {
169         copy.setMin(vc.getMin().toString());
170       }
171       if (vc.getType() != null)
172       {
173         copy.setType(vc.getType());
174       }
175       return copy;
176     } catch (Exception e)
177     {
178       e.printStackTrace();
179       throw new Error(
180               "Implementation error: could not copy ValueConstrain!");
181     }
182   }
183
184   public static Parameter copyParameter(Parameter parameter)
185   {
186     Parameter copy = new Parameter(parameter.getName(),
187             parameter.getDescription());
188     if (parameter.getValidValue() != null)
189     {
190       copy.setValidValue(copyValueConstrain(parameter.getValidValue()));
191     }
192     List<String> pv = parameter.getPossibleValues();
193     if (pv != null)
194     {
195       copy.addPossibleValues(pv.toArray(new String[]
196       {}));
197     }
198     setOptionFrom(copy, parameter);
199     return copy;
200   }
201
202 }