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