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