package jalview.ext.cipres; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.ngbw.directclient.CiApplication; import org.ngbw.directclient.CiCipresException; import org.ngbw.directclient.CiClient; import org.ngbw.directclient.CiJob; import org.ngbw.restdatatypes.ErrorData; import org.ngbw.restdatatypes.ParamError; public class CipresQueryBuilder { private final static CiApplication app = CiApplication.getInstance(); private final static CiClient cipresClient = new CiClient( app.getAppKey(), app.getUsername(), app.getPassword(), app.getRestUrl() ); private CiJob submittedJob; private String tool; private Map> vParams = new HashMap<>(); private Map inputParams = new HashMap<>(); private Map metadata = new HashMap<>(); private boolean paramsValidated = false; // Clients for umbrella application, not currently in use. private static CiClient adminClient; private static CiClient userClient; public CipresQueryBuilder() { } public CipresQueryBuilder(String alignmentFilePath) { this(); inputParams.put("infile_", alignmentFilePath); } public CipresQueryBuilder(String alignmentFilePath, String toolName) { this(alignmentFilePath); tool = toolName; } /** * Specifies a parameter about the input data for a CIPRES job with its value, * for example the "infile_" parameter for the MSA file to calculate a tree * from with file path as value. Note that different tools support different * input parameters. * * @param parameter * @param value */ public void addInputParameter(String parameter, String value) { inputParams.put(parameter, value); paramsValidated = false; } /** * Adds a CIPRES tool parameter that specifies job behaviour, for example * "runtime_" with the maximum runtime (in hours) as value. * * @param parameter * @param values */ public void addToolParameters(String parameter, Collection values) { vParams.put(parameter, values); paramsValidated = false; } /** * Adds a metadata tag to the job, for example "clientJobName" with the name * that this job should be referred by as value. CIPRES highly recommends all * jobs are given a clientJobId metadata header. * * @param metadataHeader * @param value */ public void addMetadata(String metadataHeader, String value) { metadata.put(metadataHeader, value); paramsValidated = false; } public void clearAllParameters() { vParams.clear(); inputParams.clear(); metadata.clear(); } public void clearInputParameters() { inputParams.clear(); } public void clearToolsParameters() { vParams.clear(); } public void clearMetadata() { metadata.clear(); } public String getTool() { return tool; } public void setTool(String toolName) { if (!(this.tool.equals(toolName))) { this.tool = toolName; paramsValidated = false; } } /** * Sends this job with all given parameters to the given Cipres tool but * doesn't actually run the job. This is a lightweight way to validate that * the given parameters are correct for a specified tool. */ public CiJob validateJobParams() { CiJob validateJob = null; try { validateJob = cipresClient .validateJob(tool, vParams, inputParams, metadata); validateJob.show(true); // currently outputs just to console, should be // graphical paramsValidated = true; } catch (CiCipresException ce) { ErrorData ed = ce.getErrorData(); System.out.println( "Cipres error while trying to validate parameters, code=" + ed.code + ", message=" + ed.displayMessage ); if (ed.code == ErrorData.FORM_VALIDATION) { for (ParamError pe : ed.paramError) { System.out.println(pe.param + ": " + pe.error); } } } return validateJob; } /** * Sends the job to CIPRES. If the job hasn't been validated beforehand this * method will do so before submitting. * * */ public CiJob submitJob() { if (!paramsValidated) { // validate before running some expensive job first. CiJob validateJob = validateJobParams(); submitJob(); } else { try { submittedJob = cipresClient .submitJob(tool, vParams, inputParams, metadata); } catch (CiCipresException ce) { ErrorData ed = ce.getErrorData(); System.out.println( "Cipres error while submitting job, code=" + ed.code + ", message=" + ed.displayMessage); // invalid parameters shouldn't be possible here but just in case. if (ed.code == ErrorData.FORM_VALIDATION) { for (ParamError pe : ed.paramError) { System.out.println(pe.param + ": " + pe.error); } } } } return submittedJob; } public CiJob getSubmittedJob() { return submittedJob; } public boolean areParamsValidated() { return paramsValidated; } public Map> getvParams() { return vParams; } public Map getInputParams() { return inputParams; } public Map getMetadata() { return metadata; } }