52ebbc0f6825cbb10205316c31365eda6c2ed958
[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.restdatatypes.ErrorData;
11 import org.ngbw.restdatatypes.ParamError;
12
13 public class TreeJob
14 {
15
16   private final CiClient cipresClient;
17
18   private final CiApplication app = CiApplication.getInstance();
19
20   private Map<String, Collection<String>> vParams = new HashMap<>();
21
22   private Map<String, String> inputParams = new HashMap<>();
23
24   private Map<String, String> metadata = new HashMap<>();
25
26   public TreeJob()
27   {
28     cipresClient = new CiClient(
29             app.getAppKey(), app.getUsername(), app.getPassword(),
30             app.getRestUrl()
31     );
32
33   }
34
35   public TreeJob(String alignmentFilePath)
36   {
37     cipresClient = new CiClient(
38             app.getAppKey(), app.getUsername(), app.getPassword(),
39             app.getRestUrl()
40     );
41     inputParams.put("infile_", alignmentFilePath);
42   }
43
44
45
46   /**
47    * Specifies a parameter about the input data for a CIPRES job with its value,
48    * for example the "infile_" parameter for the MSA file to calculate a tree
49    * from with file path as value. Note that different tools support different
50    * input parameters.
51    * 
52    * @param parameter
53    * @param value
54    */
55   public void addInputParameter(String parameter, String value)
56   {
57     inputParams.put(parameter, value);
58
59   }
60
61   /**
62    * Adds a CIPRES tool parameter that specifies job behaviour, for example
63    * "runtime_" with the maximum runtime (in hours) as value.
64    * 
65    * @param parameter
66    * @param values
67    */
68   public void addToolParameters(String parameter, Collection<String> values)
69   {
70     vParams.put(parameter, values);
71   }
72
73   /**
74    * Adds a metadata tag to the job, for example "clientJobName" with the name
75    * that this job should be referred by as value. CIPRES highly recommends all
76    * jobs are given a clientJobId metadata header.
77    * 
78    * @param metadataHeader
79    * @param value
80    */
81   public void addMetadata(String metadataHeader, String value)
82   {
83     metadata.put(metadataHeader, value);
84   }
85
86   public void clearAllParameters()
87   {
88     vParams.clear();
89     inputParams.clear();
90     metadata.clear();
91   }
92
93   public void clearInputParameters()
94   {
95     inputParams.clear();
96   }
97
98   public void clearToolsParameters()
99   {
100     vParams.clear();
101   }
102
103   public void clearMetadata()
104   {
105     metadata.clear();
106   }
107
108   /**
109    * Sends this job with all given parameters to the given Cipres tool but
110    * doesn't actually run the job. This is a lightweight way to validate that
111    * the given parameters are correct for a specified tool.
112    */
113   public void validateJobParams(String toolName)
114   {
115     try
116     {
117       cipresClient.validateJob(toolName, vParams, inputParams, metadata);
118     } catch (CiCipresException ce)
119     {
120       ErrorData ed = ce.getErrorData();
121       System.out.println(
122               "Cipres error while trying to validate parameters, code="
123                       + ed.code + ", message="
124                       + ed.displayMessage
125       );
126       if (ed.code == ErrorData.FORM_VALIDATION)
127       {
128         for (ParamError pe : ed.paramError)
129         {
130           System.out.println(pe.param + ": " + pe.error);
131         }
132       }
133     }
134
135   }
136
137
138 }