8bdc9c2f594d34b50fa68f50a9124775f7e9789a
[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 void validateJobParams(String toolName)
120   {
121     try
122     {
123       CiJob validateJob = cipresClient
124               .validateJob(toolName, vParams, inputParams, metadata);
125       validateJob.show(true); // currently outputs just to console, should be
126                               // graphical
127       paramsValidated = true;
128
129     } catch (CiCipresException ce)
130     {
131       paramsValidated = false; // parameters gave an error.
132       ErrorData ed = ce.getErrorData();
133       System.out.println(
134               "Cipres error while trying to validate parameters, code="
135                       + ed.code + ", message="
136                       + ed.displayMessage
137       );
138       if (ed.code == ErrorData.FORM_VALIDATION)
139       {
140         for (ParamError pe : ed.paramError)
141         {
142           System.out.println(pe.param + ": " + pe.error);
143         }
144       }
145     }
146
147   }
148
149   public void submitJob(String toolName)
150   {
151     if (!paramsValidated)
152     {
153       validateJobParams(toolName); // validate before running some expensive job
154                                    // first
155     }
156
157   }
158
159
160 }