JAL-2890 submit/valid jobs now return the actual job objects
[jalview.git] / src / jalview / ext / cipres / TreeJob.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 TreeJob
15 {
16
17   private final CiClient cipresClient;
18
19   private final CiApplication app = CiApplication.getInstance();
20
21   private Map<String, Collection<String>> vParams = new HashMap<>();
22
23   private Map<String, String> inputParams = new HashMap<>();
24
25   private Map<String, String> metadata = new HashMap<>();
26
27   private boolean paramsValidated = false;
28
29   public TreeJob()
30   {
31     cipresClient = new CiClient(
32             app.getAppKey(), app.getUsername(), app.getPassword(),
33             app.getRestUrl()
34     );
35
36   }
37
38   public TreeJob(String alignmentFilePath)
39   {
40     cipresClient = new CiClient(
41             app.getAppKey(), app.getUsername(), app.getPassword(),
42             app.getRestUrl()
43     );
44     inputParams.put("infile_", alignmentFilePath);
45   }
46
47
48
49   /**
50    * Specifies a parameter about the input data for a CIPRES job with its value,
51    * for example the "infile_" parameter for the MSA file to calculate a tree
52    * from with file path as value. Note that different tools support different
53    * input parameters.
54    * 
55    * @param parameter
56    * @param value
57    */
58   public void addInputParameter(String parameter, String value)
59   {
60     inputParams.put(parameter, value);
61     paramsValidated = false;
62
63   }
64
65   /**
66    * Adds a CIPRES tool parameter that specifies job behaviour, for example
67    * "runtime_" with the maximum runtime (in hours) as value.
68    * 
69    * @param parameter
70    * @param values
71    */
72   public void addToolParameters(String parameter, Collection<String> values)
73   {
74     vParams.put(parameter, values);
75     paramsValidated = false;
76   }
77
78   /**
79    * Adds a metadata tag to the job, for example "clientJobName" with the name
80    * that this job should be referred by as value. CIPRES highly recommends all
81    * jobs are given a clientJobId metadata header.
82    * 
83    * @param metadataHeader
84    * @param value
85    */
86   public void addMetadata(String metadataHeader, String value)
87   {
88     metadata.put(metadataHeader, value);
89     paramsValidated = false;
90   }
91
92   public void clearAllParameters()
93   {
94     vParams.clear();
95     inputParams.clear();
96     metadata.clear();
97   }
98
99   public void clearInputParameters()
100   {
101     inputParams.clear();
102   }
103
104   public void clearToolsParameters()
105   {
106     vParams.clear();
107   }
108
109   public void clearMetadata()
110   {
111     metadata.clear();
112   }
113
114   /**
115    * Sends this job with all given parameters to the given Cipres tool but
116    * doesn't actually run the job. This is a lightweight way to validate that
117    * the given parameters are correct for a specified tool.
118    */
119   public CiJob validateJobParams(String toolName)
120   {
121     CiJob validateJob = null;
122     try
123     {
124       validateJob = cipresClient
125               .validateJob(toolName, vParams, inputParams, metadata);
126       validateJob.show(true); // currently outputs just to console, should be
127                               // graphical
128       paramsValidated = true;
129
130
131     } catch (CiCipresException ce)
132     {
133       ErrorData ed = ce.getErrorData();
134       System.out.println(
135               "Cipres error while trying to validate parameters, code="
136                       + ed.code + ", message="
137                       + ed.displayMessage
138       );
139       if (ed.code == ErrorData.FORM_VALIDATION)
140       {
141         for (ParamError pe : ed.paramError)
142         {
143           System.out.println(pe.param + ": " + pe.error);
144         }
145       }
146     }
147     return validateJob;
148   }
149
150   /**
151    * Sends the job to CIPRES. If the job hasn't been validated beforehand this
152    * method will do so before submitting.
153    * 
154    * @param toolName
155    */
156   public CiJob submitJob(String toolName)
157   {
158     CiJob submittedJob = null;
159     if (!paramsValidated)
160     {
161       // validate before running some expensive job first.
162       CiJob validateJob = validateJobParams(toolName);
163
164       // should be redundant but extra check here anyway.
165       if (!validateJob.isError())
166       {
167         submittedJob = submitJob(toolName);
168       }
169     }
170     else
171     {
172       try
173       {
174         submittedJob = cipresClient
175                 .submitJob(toolName, vParams, inputParams, metadata);
176       } catch (CiCipresException ce)
177       {
178         ErrorData ed = ce.getErrorData();
179         System.out.println(
180                 "Cipres error in submitted job, code="
181                         + ed.code + ", message=" + ed.displayMessage);
182
183                 if (ed.code == ErrorData.FORM_VALIDATION)
184         {
185           for (ParamError pe : ed.paramError)
186           // invalid parameters shouldn't be possible here but just in case.
187           {
188             System.out.println(pe.param + ": " + pe.error);
189           }
190         }
191       }
192     }
193     return submittedJob;
194   }
195
196
197 }