21bf33cd96a5cdcb108406ef9d8bdf476fba2c69
[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
60
61   /**
62    * Specifies a parameter about the input data for a CIPRES job with its value,
63    * for example the "infile_" parameter for the MSA file to calculate a tree
64    * from with file path as value. Note that different tools support different
65    * input parameters.
66    * 
67    * @param parameter
68    * @param value
69    */
70   public void addInputParameter(String parameter, String value)
71   {
72     inputParams.put(parameter, value);
73     paramsValidated = false;
74
75   }
76
77   /**
78    * Adds a CIPRES tool parameter that specifies job behaviour, for example
79    * "runtime_" with the maximum runtime (in hours) as value.
80    * 
81    * @param parameter
82    * @param values
83    */
84   public void addToolParameters(String parameter, Collection<String> values)
85   {
86     vParams.put(parameter, values);
87     paramsValidated = false;
88   }
89
90   /**
91    * Adds a metadata tag to the job, for example "clientJobName" with the name
92    * that this job should be referred by as value. CIPRES highly recommends all
93    * jobs are given a clientJobId metadata header.
94    * 
95    * @param metadataHeader
96    * @param value
97    */
98   public void addMetadata(String metadataHeader, String value)
99   {
100     metadata.put(metadataHeader, value);
101     paramsValidated = false;
102   }
103
104   public void clearAllParameters()
105   {
106     vParams.clear();
107     inputParams.clear();
108     metadata.clear();
109   }
110
111   public void clearInputParameters()
112   {
113     inputParams.clear();
114   }
115
116   public void clearToolsParameters()
117   {
118     vParams.clear();
119   }
120
121   public void clearMetadata()
122   {
123     metadata.clear();
124   }
125
126   public String getTool()
127   {
128     return tool;
129   }
130
131   public void setTool(String toolName)
132   {
133
134     if (!(this.tool.equals(toolName)))
135     {
136       this.tool = toolName;
137       paramsValidated = false;
138     }
139
140   }
141
142   /**
143    * Sends this job with all given parameters to the given Cipres tool but
144    * doesn't actually run the job. This is a lightweight way to validate that
145    * the given parameters are correct for a specified tool.
146    */
147   public CiJob validateJobParams()
148   {
149     CiJob validateJob = null;
150     try
151     {
152       validateJob = cipresClient
153               .validateJob(tool, vParams, inputParams, metadata);
154       validateJob.show(true); // currently outputs just to console, should be
155                               // graphical
156       paramsValidated = true;
157
158
159     } catch (CiCipresException ce)
160     {
161       ErrorData ed = ce.getErrorData();
162       System.out.println(
163               "Cipres error while trying to validate parameters, code="
164                       + ed.code + ", message="
165                       + ed.displayMessage
166       );
167       if (ed.code == ErrorData.FORM_VALIDATION)
168       {
169         for (ParamError pe : ed.paramError)
170         {
171           System.out.println(pe.param + ": " + pe.error);
172         }
173       }
174     }
175     return validateJob;
176   }
177
178   /**
179    * Sends the job to CIPRES. If the job hasn't been validated beforehand this
180    * method will do so before submitting.
181    * 
182    * 
183    */
184   public CiJob submitJob()
185   {
186
187     if (!paramsValidated)
188     {
189       // validate before running some expensive job first.
190       CiJob validateJob = validateJobParams();
191
192       // should be redundant but extra check here anyway.
193       if (!validateJob.isError())
194       {
195         submittedJob = submitJob();
196       }
197     }
198     else
199     {
200       try
201       {
202         submittedJob = cipresClient
203                 .submitJob(tool, vParams, inputParams, metadata);
204
205       } catch (CiCipresException ce)
206       {
207         ErrorData ed = ce.getErrorData();
208         System.out.println(
209                 "Cipres error in submitted job, code="
210                         + ed.code + ", message=" + ed.displayMessage);
211
212         // invalid parameters shouldn't be possible here but just in case.
213                 if (ed.code == ErrorData.FORM_VALIDATION)
214         {
215           for (ParamError pe : ed.paramError)
216
217           {
218             System.out.println(pe.param + ": " + pe.error);
219           }
220         }
221       }
222     }
223     return submittedJob;
224   }
225
226   public CiJob getSubmittedJob()
227   {
228     return submittedJob;
229   }
230
231
232 }