JAL-2890 added job results wrapper
[jalview.git] / src / jalview / ext / cipres / CipresJob.java
1 package jalview.ext.cipres;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.ngbw.directclient.CiApplication;
8 import org.ngbw.directclient.CiCipresException;
9 import org.ngbw.directclient.CiClient;
10 import org.ngbw.directclient.CiJob;
11 import org.ngbw.restdatatypes.ErrorData;
12 import org.ngbw.restdatatypes.ParamError;
13
14 public class CipresJob
15 {
16
17   private final static CiApplication app = CiApplication.getInstance();
18
19   private final static CiClient cipresClient = new CiClient(
20           app.getAppKey(), app.getUsername(), app.getPassword(),
21           app.getRestUrl()
22   );
23
24   private CiJob submittedJob;
25
26   private String tool;
27
28   private Map<String, Collection<String>> vParams = new HashMap<>();
29
30   private Map<String, String> inputParams = new HashMap<>();
31
32   private Map<String, String> metadata = new HashMap<>();
33
34   private boolean paramsValidated = false;
35
36   // Clients for umbrella application, not currently in use.
37   private static CiClient adminClient;
38
39   private static CiClient userClient;
40
41   public CipresJob()
42   {
43   }
44
45   public CipresJob(String alignmentFilePath)
46   {
47     this();
48     inputParams.put("infile_", alignmentFilePath);
49   }
50
51   public CipresJob(String alignmentFilePath, String toolName)
52   {
53     this(alignmentFilePath);
54     tool = toolName;
55   }
56
57
58   /**
59    * Specifies a parameter about the input data for a CIPRES job with its value,
60    * for example the "infile_" parameter for the MSA file to calculate a tree
61    * from with file path as value. Note that different tools support different
62    * input parameters.
63    * 
64    * @param parameter
65    * @param value
66    */
67   public void addInputParameter(String parameter, String value)
68   {
69     inputParams.put(parameter, value);
70     paramsValidated = false;
71
72   }
73
74   /**
75    * Adds a CIPRES tool parameter that specifies job behaviour, for example
76    * "runtime_" with the maximum runtime (in hours) as value.
77    * 
78    * @param parameter
79    * @param values
80    */
81   public void addToolParameters(String parameter, Collection<String> values)
82   {
83     vParams.put(parameter, values);
84     paramsValidated = false;
85   }
86
87   /**
88    * Adds a metadata tag to the job, for example "clientJobName" with the name
89    * that this job should be referred by as value. CIPRES highly recommends all
90    * jobs are given a clientJobId metadata header.
91    * 
92    * @param metadataHeader
93    * @param value
94    */
95   public void addMetadata(String metadataHeader, String value)
96   {
97     metadata.put(metadataHeader, value);
98     paramsValidated = false;
99   }
100
101   public void clearAllParameters()
102   {
103     vParams.clear();
104     inputParams.clear();
105     metadata.clear();
106   }
107
108   public void clearInputParameters()
109   {
110     inputParams.clear();
111   }
112
113   public void clearToolsParameters()
114   {
115     vParams.clear();
116   }
117
118   public void clearMetadata()
119   {
120     metadata.clear();
121   }
122
123   public String getTool()
124   {
125     return tool;
126   }
127
128   public void setTool(String toolName)
129   {
130
131     if (!(this.tool.equals(toolName)))
132     {
133       this.tool = toolName;
134       paramsValidated = false;
135     }
136
137   }
138
139   /**
140    * Sends this job with all given parameters to the given Cipres tool but
141    * doesn't actually run the job. This is a lightweight way to validate that
142    * the given parameters are correct for a specified tool.
143    */
144   public CiJob validateJobParams()
145   {
146     CiJob validateJob = null;
147     try
148     {
149       validateJob = cipresClient
150               .validateJob(tool, vParams, inputParams, metadata);
151       validateJob.show(true); // currently outputs just to console, should be
152                               // graphical
153       paramsValidated = true;
154
155
156     } catch (CiCipresException ce)
157     {
158       ErrorData ed = ce.getErrorData();
159       System.out.println(
160               "Cipres error while trying to validate parameters, code="
161                       + ed.code + ", message="
162                       + ed.displayMessage
163       );
164       if (ed.code == ErrorData.FORM_VALIDATION)
165       {
166         for (ParamError pe : ed.paramError)
167         {
168           System.out.println(pe.param + ": " + pe.error);
169         }
170       }
171     }
172     return validateJob;
173   }
174
175   /**
176    * Sends the job to CIPRES. If the job hasn't been validated beforehand this
177    * method will do so before submitting.
178    * 
179    * 
180    */
181   public CiJob submitJob()
182   {
183
184     if (!paramsValidated)
185     {
186       // validate before running some expensive job first.
187       CiJob validateJob = validateJobParams();
188
189         submittedJob = submitJob();
190
191     }
192     else
193     {
194       try
195       {
196         submittedJob = cipresClient
197                 .submitJob(tool, vParams, inputParams, metadata);
198
199       } catch (CiCipresException ce)
200       {
201         ErrorData ed = ce.getErrorData();
202         System.out.println(
203                 "Cipres error in submitted job, code="
204                         + ed.code + ", message=" + ed.displayMessage);
205
206         // invalid parameters shouldn't be possible here but just in case.
207                 if (ed.code == ErrorData.FORM_VALIDATION)
208         {
209           for (ParamError pe : ed.paramError)
210
211           {
212             System.out.println(pe.param + ": " + pe.error);
213           }
214         }
215       }
216     }
217     return submittedJob;
218   }
219
220   public CiJob getSubmittedJob()
221   {
222     return submittedJob;
223   }
224
225
226   public boolean areParamsValidated()
227   {
228     return paramsValidated;
229   }
230
231   public Map<String, Collection<String>> getvParams()
232   {
233     return vParams;
234   }
235
236   public Map<String, String> getInputParams()
237   {
238     return inputParams;
239   }
240
241   public Map<String, String> getMetadata()
242   {
243     return metadata;
244   }
245
246
247
248 }