Package Galaxy.Visitor.Webservice

Source Code of Galaxy.Visitor.Webservice.GalaxyToLoniWorkflowConverter

package Galaxy.Visitor.Webservice;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import CLInterface.ConverterConfig;
import CLInterface.ConverterConstants;
import Core.Pair;
import Galaxy.Tree.Tool.Input.Input;
import Galaxy.Tree.Tool.Input.Inputs;
import Galaxy.Tree.Tool.Input.Param.Parameter;
import Galaxy.Tree.Tool.Output.Data;
import Galaxy.Tree.Tool.Output.Output;
import Galaxy.Tree.Tool.Output.Outputs;
import Galaxy.Tree.Workflow.Action;
import Galaxy.Tree.Workflow.ExternalInput;
import Galaxy.Tree.Workflow.ExternalOutput;
import Galaxy.Tree.Workflow.InputConnection;
import Galaxy.Tree.Workflow.Position;
import Galaxy.Tree.Workflow.Step;
import Galaxy.Tree.Workflow.Workflow;
import Galaxy.Visitor.DFSWorkflowVisitor;
import Galaxy.Visitor.DFSWorkflowVisitor.StepVisitor;
import Galaxy.Visitor.util.LoniContext;
import Galaxy.Visitor.util.LoniEnvironment;
import LONI.tree.GraphObject.GraphObject;
import LONI.tree.GraphObject.Module;
import LONI.tree.GraphObject.ModuleGroup;
import LONI.tree.module.Value;
import LONI.tree.module.format.FileType;
import LONI.tree.module.format.Format;
import LONI.tree.workflow.Connection;
import LONI.tree.workflow.Pipeline;
import LONI.tree.workflow.Variable;
import Specification.GalaxySpecification;
/***
* Converts a Galaxy Workflow to a Loni Pipeline.
* @author viper
*
*/
public class GalaxyToLoniWorkflowConverter extends DFSWorkflowVisitor
{
 
  /***
   * Names of variables defined in the generated Pipeline file.
   *
   */
  final String API_KEY = "API_KEY";
  final String GALAXY_URL = "GALAXY_URL";
  final String GALAXY_DIR = "GALAXY_ROOT_DIR";
  final String SCRIPT_DIR = "SCRIPT_DIR";
  final String PYTHON_DIR = "PYTHON_DIR";
  final String COOKIE = "COOKIE";
  //Filetype names for dictionary filetype, galaxy input file
  //filetypes.
  /***
   * Generic types used in the converted pipeline. Connections
   * between modules pass around data of type GENERIC_DICT_TYPE
   * and connections going into and out of the modulegroup
   * are of type GENERIC_FILE_TYPE.
   */
  final String GENERIC_DICT_TYPE = "DictStruct";
  final String GENERIC_FILE_TYPE="GalaxyFile";
 
 
 
  /**
   * Converting the Galaxy workflow to a webservice-oriented Loni
   * pipeline generates two extra modules: a setup and run module.
   * These two modules have their own contexts, the setup context,
   * and the run context. The contexts contain connection infomation.
   * SetupModule: Initializes structures that will be passed through connections
   * to other nodes.
   * RunModule: Recieves structures from incoming connections and runs the workflow on
   * the server.
   *
   */
  Module setupModule;
  LoniContext setupContext;
  Module runModule;
  LoniContext runContext;
  /**
   * Cleans up a string so that it can be used as a value for a string
   * parameter without causing issues. Basically replaces all curly braces
   * with a sequence of characters.
   * @param s string to clean up
   * @return cleaned up string.
   */
  public String cleanStringValue(String s){
    String str;
    str= s.replaceAll("[\\{]", "\\^paren\\^");
    str= str.replaceAll("[\\}]", "\\$paren\\$");
    return str;
  }
  /**
   * Converts a workflow into a loni pipeline.
   * @param workflow The workflow to be converted.
   */
  public Object visit(Workflow workflow) {
    Pipeline pipeline = new Pipeline();
    ModuleGroup moduleGroup = new ModuleGroup();
    LoniEnvironment le = new LoniEnvironment();
    /*
     * Create a modulegroup
     */
    moduleGroup.setDescription(workflow.getAnnotation());
    moduleGroup.setPosX(0);
    moduleGroup.setPosY(0);
    moduleGroup.setName(workflow.getName());
    moduleGroup.setRotation(0);
    //set module group as root module group in pipeline.
    pipeline.setPipelineModuleGroup(moduleGroup);
    /*
     * INITIALIZE THE SETUP, RUN MODULES
     *
     */
    /*
     * Create the setupModule.
     *
     */
    setupModule = new Module();
    setupContext = new LoniContext();
    setupModule.setId("Setup");
    setupModule.setRotation(4);
    setupModule.setName("Setup");
    setupModule.setLocation(ConverterConstants.LOCALHOST_PATH + ConverterConfig.PYTHON_BIN);
   
    //create the String parameter containing the script name for the
    //setup module.
    LONI.tree.module.Parameter setupScript = new LONI.tree.module.Parameter();
    setupScript.setEnabled(true);
    setupScript.setFileFormat(new Format());
    setupScript.getFileFormat().setCardinality(1);
    setupScript.getFileFormat().setType("String");
    setupScript.setOrder(0);
    setupScript.setName("setupScript");
    Value scriptValue = new Value();
    scriptValue.setValue("{"+SCRIPT_DIR+"}setup_workflow.py");
    setupScript.getValues().addValue(scriptValue);
    setupModule.getInputs().add(setupScript);
   
    //Create the String Parameter that contains the Galaxy API Key
    //for the setupScript Module.
    LONI.tree.module.Parameter setupAPI = new LONI.tree.module.Parameter();
    setupAPI.setEnabled(true);
    setupAPI.setFileFormat(new Format());
    setupAPI.getFileFormat().setCardinality(1);
    setupAPI.getFileFormat().setType("String");
    setupAPI.setOrder(1);
    setupAPI.setName("Galaxy API Key");
    setupAPI.setPrefix("-api-key");
    setupAPI.setPrefixSpaced(true);
    Value apiValue = new Value();
    apiValue.setValue("{"+API_KEY+"}");
    setupAPI.getValues().addValue(apiValue);
    setupModule.getInputs().add(setupAPI);
   
    /*
     * Initialize and add the String parameter that contains the Galaxy Root Directory
     * to the setup Module. This directory is used to call python scripts inside Galaxy.
     */
    LONI.tree.module.Parameter setupGalRootDir = new LONI.tree.module.Parameter();
    setupGalRootDir.setEnabled(true);
    setupGalRootDir.setFileFormat(new Format());
    setupGalRootDir.getFileFormat().setCardinality(1);
    setupGalRootDir.getFileFormat().setType("String");
    setupGalRootDir.setOrder(1);
    setupGalRootDir.setName("Galaxy Root Dir");
    setupGalRootDir.setPrefix("-galaxy-root-dir");
    setupGalRootDir.setPrefixSpaced(true);
    Value setupGalRootDirValue = new Value();
    setupGalRootDirValue.setValue("{"+GALAXY_DIR+"}");
    setupGalRootDir.getValues().addValue(setupGalRootDirValue);
    setupModule.getInputs().add(setupGalRootDir);
   
    /*
     * Initialize and add String parameter containing Galaxy server url to setup module.
     */
    LONI.tree.module.Parameter setupGalURLDir = new LONI.tree.module.Parameter();
    setupGalURLDir.setEnabled(true);
    setupGalURLDir.setFileFormat(new Format());
    setupGalURLDir.getFileFormat().setCardinality(1);
    setupGalURLDir.getFileFormat().setType("String");
    setupGalURLDir.setOrder(2);
    setupGalURLDir.setPrefixSpaced(true);
    setupGalURLDir.setName("Galaxy URL");
    setupGalURLDir.setPrefix("-galaxy-url");
    Value setupGalaxyURLValue = new Value();
    setupGalaxyURLValue.setValue("{"+GALAXY_URL+"}");
    setupGalURLDir.getValues().addValue(setupGalaxyURLValue);
    setupModule.getInputs().add(setupGalURLDir);
   
    /*
     * Initialize and add String cookie parameter containing cookie for website
     * to setup module.
     */
    LONI.tree.module.Parameter setupCookie = new LONI.tree.module.Parameter();
    setupCookie.setEnabled(true);
    setupCookie.setFileFormat(new Format());
    setupCookie.getFileFormat().setCardinality(1);
    setupCookie.getFileFormat().setType("String");
    setupCookie.setOrder(2);
    setupCookie.setName("Cookie for galaxy site");
    setupCookie.setPrefix("-cookie");
    setupCookie.setPrefixSpaced(true);
    Value setupCookieValue = new Value();
    setupCookieValue.setValue("{"+COOKIE+"}");
    setupCookie.getValues().addValue(setupCookieValue);
    setupModule.getInputs().add(setupCookie);
   
    /*
     * Initialize and fill in the run module.
     *
     */
    runModule = new Module();
    runModule.setRotation(4);
    runContext = new LoniContext();
    runModule.setId("Run");
    runModule.setLocation(ConverterConstants.LOCALHOST_PATH + ConverterConfig.PYTHON_BIN);
   
    //Add the String script parameter containing the name of the script to run.
    LONI.tree.module.Parameter runScript = new LONI.tree.module.Parameter();
    runScript.setEnabled(true);
    runScript.setFileFormat(new Format());
    runScript.getFileFormat().setCardinality(1);
    runScript.getFileFormat().setType("String");
    runScript.setOrder(0);
    runScript.setName("runScript");
    scriptValue = new Value();
    scriptValue.setValue("{"+SCRIPT_DIR+"}run_workflow.py");
    runScript.getValues().addValue(scriptValue);
    runModule.getInputs().add(runScript);
   
    //Create the workflow output to the run module. This output contains
    //a functional galaxy workflow that was generated from the loni pipeline
    //modules.
    LONI.tree.module.Output runWorkflow = new LONI.tree.module.Output();
    runWorkflow.setEnabled(true);
    runWorkflow.setFormat(new Format());
    runWorkflow.getFormat().setCardinality(1);
    runWorkflow.getFormat().setType("File");
    FileType runFileType = new FileType();
    runFileType.setName(GENERIC_DICT_TYPE);
    runFileType.setDescription("");
    runFileType.setExtension("");
    runWorkflow.getFormat().getFileTypes().addFileType(runFileType);
    runWorkflow.setOrder(0);
    runWorkflow.setPrefix("-workflow");
    runWorkflow.setPrefixSpaced(true);
    runWorkflow.setName("Workflow Data");
    runModule.getOutputs().add(runWorkflow);
   
    //Add the setup and run modules to the setup and run contexts respectively.
    setupContext.getPathContext().addContext("Setup");
    runContext.getPathContext().addContext("Run");
   
    /*
     *VISIT THE STEPS IN THE WORKFLOW
     */
     //used to place run, setup modules
    int totalY=0; // running sum of y values.
    int leftX = -1; //furthest left x position
    int rightX = 0; //furthest right y position
    int spacing = 200; //spacing between modules.
   
    // TODO Auto-generated method stub
    for(Step s : workflow.getSteps()){
      //visit each step in the workflow
      Pair<GraphObject,LoniEnvironment> step = stepVisitor.visit(s, new LoniContext());
      //add the returned environment to the pipeline environment.
      le.addEnvironment(step.getElem2());
      //add the returned graphobject to the pipeline modulegroup
      moduleGroup.getModules().add(step.getElem1());
      //add the step's y value to the running sum of y values.
      totalY += s.getPosition().getFromTop();
      //update furthest left position
      if(leftX < 0 || leftX > s.getPosition().getFromLeft())
        leftX =s.getPosition().getFromLeft();
      //update furthest right position
      if(rightX < s.getPosition().getFromLeft())
        rightX=s.getPosition().getFromLeft();
    }
    //calculate the average y value from the total.
    int avgY = totalY;
    if(workflow.getSteps().size() > 0)
      avgY = totalY / workflow.getSteps().size();
   
    //place the setup module futhest to the left, at the
    //average y value.
    setupModule.setPosX(leftX);
    setupModule.setPosY(avgY + spacing);
    //Place the run module furthest to the right, at the
    //average y value.
    runModule.setPosX(rightX+ spacing);
    runModule.setPosY(avgY);
   
    //add the run, setupmodule
    moduleGroup.getModules().add(setupModule);
    moduleGroup.getModules().add(runModule);
   
    /*
     * Connect module outputs that are not connected to anything to the
     * run module.
     */
    int conncount=1;
   
    /*
     * For each module output
     */
    for(String id : le.getOutputAliases().keySet()){
      boolean isConnected = false;
      //check if any connections link the output of that module
      //to the input of some other module.
      for(Connection c : le.getConnections()){
        if(c.getSource().equals(id))
          isConnected = true;
      }
      //If the output is not connected to any other modules.
      if(isConnected == false){
        Connection runConn; //connection to the run module
       
        //Create a new parameter for the runmodule that is connected
        //to the connectionless output.
        LONI.tree.module.Parameter runInput = new LONI.tree.module.Parameter();
        runInput.setId(runContext.getPathContext().getAbsoluteContext("conn"+conncount));
        runInput.setFileFormat(new Format());
        runInput.getFormat().setCardinality(1);
        runInput.getFormat().setType("File");
        runFileType = new FileType();
        runFileType.setName(GENERIC_DICT_TYPE);
        runFileType.setDescription("");
        runFileType.setExtension("");
        runInput.getFormat().getFileTypes().addFileType(runFileType);
        runInput.setOrder(conncount);
        runInput.setPrefix("-input");
        runInput.setPrefixSpaced(true);
        runInput.setEnabled(true);
        runInput.setName("input"+ conncount);
        //add the created input to the runmodule.
        runModule.addInput(runInput);
        //create a new connection from the connectionless output to the new run module input.
        runConn = new Connection(id, runInput.getId());
        le.addConnection(runConn);
        conncount++;
      }
    }
    //add all the connections in the environment being maintained to
    //the pipeline
    pipeline.getConnections().addConnections(le.getConnections());
    //set the moduleGroup to the pipeline moduelGroup.
    pipeline.setPipelineModuleGroup(moduleGroup);
   
    /**
     * Create Pipeline Variables
     */
   
    //location of galaxy web service scripts
    Variable scriptVar = new Variable();
    scriptVar.setName(SCRIPT_DIR);
    scriptVar.setValue(ConverterConfig.GALAXY_SCRIPT_DIR);
    scriptVar.setDescription("Where the galaxy wrapper scripts are");
    moduleGroup.getVariables().addVariable(scriptVar);
   
    //api key for the galaxy server
    Variable apikeyVal = new Variable();
    apikeyVal.setName(API_KEY);
    apikeyVal.setValue(ConverterConfig.GALAXY_API_KEY);
    apikeyVal.setDescription("api key for server");
    moduleGroup.getVariables().addVariable(apikeyVal);
   
    //location of the galaxy root. scripts are used from the galaxy root.
    Variable dirVal = new Variable();
    dirVal.setName(GALAXY_DIR);
    dirVal.setValue(ConverterConfig.GALAXY_INPUT_DIR);
    dirVal.setDescription("Galaxy distribution sirectory Here");
    moduleGroup.getVariables().addVariable(dirVal);
   
    //url of galaxy server.
    Variable urlVal = new Variable();
    urlVal.setName(GALAXY_URL);
    urlVal.setValue(ConverterConfig.GALAXY_URL);
    urlVal.setDescription("server address");
    moduleGroup.getVariables().addVariable(urlVal);
   
    /*Temporary : Until everything migrates to api*/
    //value of cookie from website. This will be phased out
    //when galaxy migrates completely to web api.
    Variable cookieVal = new Variable();
    cookieVal.setName(COOKIE);
    cookieVal.setValue(ConverterConfig.GALAXY_COOKIE);
    cookieVal.setDescription("login cookie for size");
    moduleGroup.getVariables().addVariable(cookieVal);
   
   
    return pipeline;
  }
  /*
   * Visiting steps will return a Pair<List<GraphObject>, List<Connection>
   * of connections and graphObjects to add. for each step.
   */
  /**
   * StepVisitor visits types of Steps. So far, there are tool steps
   * and data steps.
   */
  {
  stepVisitor = new StepVisitor(){
      //prefix for giving module ids. Since Galaxy ids are numbers,
      //are prefixed with STEP_PREFIX to create a valid name.
      final String STEP_PREFIX="Step";
     

public Pair<GraphObject, LoniEnvironment> visit(Step s, LoniContext context){
  if(s.getType().equals("tool"))
      return visitStep(s, context);
  else if(s.getType().equals("data_input"))
      return visitData(s, context);
  return null;
}
/**
* Visit a data_input step. Converts it to a Loni Module that is designed to upload
* the inputted data to the server, then pass the dataset information to the nodes
* connected to its output parameter.      
* @param s step to convert
* @param context context to convert in.
* @return the graphobject and the lonienvironment containing information not related to the graphobject.
*/
public Pair<GraphObject, LoniEnvironment> visitData(Step s, LoniContext context){
        //create a new module, environment, and childcontext.
        Module module = new Module();
        LoniEnvironment stepEnv = new LoniEnvironment();
        LoniContext childContext = context.copy();
       
        //update the path context to contain the module name.
        childContext.getPathContext().addContext(STEP_PREFIX+s.getId());
       
        //set the module description, id, position ect.
        module.setDescription(s.getAnnotation());
        module.setId(STEP_PREFIX+s.getId());
        if(s.getPosition() != null){
          module.setPosX(s.getPosition().getFromLeft());
          module.setPosY(s.getPosition().getFromTop());
        }
        module.setName(s.getName());
        module.setRotation(4);
        module.setLocation(ConverterConstants.LOCALHOST_PATH+ ConverterConfig.PYTHON_BIN);
       
        //initialize four parameters and one output.
        LONI.tree.module.Parameter scriptParam = new LONI.tree.module.Parameter();
        LONI.tree.module.Parameter fileInput = new LONI.tree.module.Parameter();
        LONI.tree.module.Parameter idParam = new LONI.tree.module.Parameter();
        LONI.tree.module.Parameter stateParam = new LONI.tree.module.Parameter();
        LONI.tree.module.Output fileOutput = new LONI.tree.module.Output();
       
        //Init parameter containing dataset script name.
        scriptParam.setId(childContext.getPathContext().getAbsoluteContext("script"));
        scriptParam.setName("Script Name");
        scriptParam.setOrder(0);
        scriptParam.setFileFormat(new Format());
        scriptParam.getFormat().setCardinality(1);
        scriptParam.getFormat().setType("String");
        scriptParam.setEnabled(true);
        Value val = new Value();
        val.setValue("{"+SCRIPT_DIR+"}upload_and_process.py");
        scriptParam.getValues().addValue(val);
       
        //Init+add parameter containing dataset to upload.
        fileInput.setEnabled(true);
        fileInput.setId(childContext.getPathContext().getAbsoluteContext("input"));
        fileInput.setPrefix("-input");
        fileInput.setPrefixSpaced(true);
        fileInput.setOrder(1);
        fileInput.setName("input");
        fileInput.setFileFormat(new Format());
        fileInput.getFileFormat().setCardinality(1);
        fileInput.getFileFormat().setType("File");
        FileType ft = new FileType();
        ft.setName(GENERIC_FILE_TYPE);
        ft.setExtension("");
        ft.setDescription("");
        fileInput.getFileFormat().getFileTypes().addFileType(ft);
       
        //Init file output containing uploaded dataset information.
        fileOutput.setEnabled(true);
        fileOutput.setId(childContext.getPathContext().getAbsoluteContext("output"));
        fileOutput.setName("output");
        fileOutput.setOrder(2);
        fileOutput.setPrefix("-output");
        fileOutput.setPrefixSpaced(true);
        stepEnv.addExternalOutput(fileOutput.getId(), "output");
       
        //Init parameter containing id number of data_input step
        idParam.setId(context.getPathContext().getAbsoluteContext("step"));
        idParam.setName("Step Number");
        idParam.setOrder(1);
        idParam.setFileFormat(new Format());
        idParam.getFormat().setCardinality(1);
        idParam.getFormat().setType("Number");
        idParam.setPrefix("-id");
        idParam.setPrefixSpaced(true);
        idParam.setEnabled(true);
        val = new Value();
        val.setValue(""+s.getId());
        idParam.getValues().addValue(val);
       
        //Init parameter containing sanitized tool_state
        stateParam.setId(context.getPathContext().getAbsoluteContext("state"));
        stateParam.setName("Tool Values");
        stateParam.setOrder(1);
        stateParam.setFileFormat(new Format());
        stateParam.getFormat().setCardinality(1);
        stateParam.getFormat().setType("String");
        stateParam.setPrefix("-state");
        stateParam.setPrefixSpaced(true);
        stateParam.setEnabled(true);
        val = new Value();
        String tool_state_string =cleanStringValue(s.getToolState().toString());
        val.setValue(tool_state_string);
        stateParam.getValues().addValue(val);
       
        //add all the parameters
        module.addInput(fileInput);
        module.addInput(scriptParam);
        module.addInput(stateParam);
        module.addInput(idParam);
        module.addOutput(fileOutput);
       
        //Visit each external output from this step, and add
        //each environment to this module's environment, and each
        //returned parameter as a module input.
        for(ExternalInput e : s.getExternalInputs()){
          Pair<Object, LoniEnvironment> pair = stepImplVisitor.visit(e, childContext);
          module.addInput((LONI.tree.module.Parameter) pair.getElem1());
          stepEnv.addEnvironment(pair.getElem2());
        }
        //return the created module and environment.
        return new Pair<GraphObject, LoniEnvironment>(module, stepEnv);
      }
      /**
       * Visit a tool step.
       * @param s step to visit
       * @param context context to visit step in.
       * @return
       */
      public Pair<GraphObject, LoniEnvironment> visitStep(Step s, LoniContext context){
        //create new module, environment, child context from context.
        Module module = new Module();
        LoniEnvironment stepEnv = new LoniEnvironment();
        LoniContext childContext = context.copy();
       
        //add module name to path context.
        childContext.getPathContext().addContext(STEP_PREFIX+s.getId());
       
        //set module id, position, description ect.
        module.setDescription(s.getAnnotation());
        module.setId(STEP_PREFIX+s.getId());
        if(s.getPosition() != null){
          module.setPosX(s.getPosition().getFromLeft());
          module.setPosY(s.getPosition().getFromTop());
        }
        module.setName(s.getName());
        module.setRotation(4);
        module.setLocation(ConverterConstants.LOCALHOST_PATH + ConverterConfig.PYTHON_BIN);
       
        //visit each externalInput.
        for(ExternalInput input :s.getExternalInputs()){
          stepImplVisitor.visit(input, childContext);
        }
       
        //Visit all the connections
       
        for(String sink : s.getConnectionSinks()){
          Pair<Object,LoniEnvironment> connectionPair;
          LONI.tree.module.Parameter param;
          InputConnection source;
          //get connection source using connection sink.
          source = s.getConnections().get(sink);
          /* Add as parameter */
          //create a new parameter for the connection sink.
          param = new LONI.tree.module.Parameter();
          param.setName(sink);
          param.setId(childContext.getPathContext().getAbsoluteContext(sink));
          param.setEnabled(true);
          param.setOrder(1);
          param.setPrefix("-input-"+sink);
          param.setPrefixSpaced(true);
          module.getInputs().add(param);
          /* Explore Connection */
          //get a connection from the source, sink and context
          connectionPair=stepImplVisitor.visit(source, sink, childContext.copy());
          //add the returned connection, environment.
          stepEnv.getConnections().add((Connection) connectionPair.getElem1());
          stepEnv.addEnvironment(connectionPair.getElem2());
        }
        //visit each external input
        for(ExternalInput e : s.getExternalInputs()){
          Pair<Object, LoniEnvironment> pair = stepImplVisitor.visit(e, childContext);
          //add the returned parameter and environment to the module, module environment.
          module.addInput((LONI.tree.module.Parameter) pair.getElem1());
          stepEnv.addEnvironment(pair.getElem2());
        }
       
        //visit each external output
        for(ExternalOutput output : s.getExternalOutputs()){
          Pair<Object,LoniEnvironment> outputPair;
          outputPair = stepImplVisitor.visit(output, childContext.copy());
         
          //add the returned output and environment, to the module output and environment.
          module.getOutputs().add((LONI.tree.module.Output) outputPair.getElem1());
          stepEnv.addEnvironment(outputPair.getElem2());
        }
       
        //Add five additional parameters
        LONI.tree.module.Parameter idParam = new LONI.tree.module.Parameter();
        LONI.tree.module.Parameter stateParam = new LONI.tree.module.Parameter();
        LONI.tree.module.Parameter scriptParam = new LONI.tree.module.Parameter();
        LONI.tree.module.Parameter toolIdParam = new LONI.tree.module.Parameter();
        LONI.tree.module.Parameter toolVersionParam = new LONI.tree.module.Parameter();
       
        Value val;
        //init parameter containing name of script to run.
        scriptParam.setId(context.getPathContext().getAbsoluteContext("script"));
        scriptParam.setName("Script");
        scriptParam.setOrder(0);
        scriptParam.setFileFormat(new Format());
        scriptParam.getFormat().setCardinality(1);
        scriptParam.getFormat().setType("String");
        scriptParam.setEnabled(true);
        val = new Value();
        val.setValue("{"+SCRIPT_DIR+"}/add_tool_step.py");
        scriptParam.getValues().addValue(val);
       
        //init parameter containing id of step.
        idParam.setId(context.getPathContext().getAbsoluteContext("step"));
        idParam.setName("Step Number");
        idParam.setOrder(2);
        idParam.setFileFormat(new Format());
        idParam.getFormat().setCardinality(1);
        idParam.getFormat().setType("Number");
        idParam.setPrefix("-id");
        idParam.setPrefixSpaced(true);
        idParam.setEnabled(true);
        val = new Value();
        val.setValue(""+s.getId());
        idParam.getValues().addValue(val);
       
        //init parameter containing id of tool
        toolIdParam.setId(context.getPathContext().getAbsoluteContext("toolid"));
        toolIdParam.setName("Tool id");
        toolIdParam.setOrder(2);
        toolIdParam.setFileFormat(new Format());
        toolIdParam.getFormat().setCardinality(1);
        toolIdParam.getFormat().setType("String");
        toolIdParam.setPrefix("-tool-id");
        toolIdParam.setPrefixSpaced(true);
        toolIdParam.setEnabled(true);
        val = new Value();
        val.setValue(s.getToolId());
        toolIdParam.getValues().addValue(val);
       
        //init param containing version of tool
        toolVersionParam.setId(context.getPathContext().getAbsoluteContext("toolid"));
        toolVersionParam.setName("Tool version");
        toolVersionParam.setOrder(2);
        toolVersionParam.setFileFormat(new Format());
        toolVersionParam.getFormat().setCardinality(1);
        toolVersionParam.getFormat().setType("String");
        toolVersionParam.setPrefix("-tool-version");
        toolVersionParam.setPrefixSpaced(true);
        toolVersionParam.setEnabled(true);
        val = new Value();
        val.setValue(s.getToolVersion());
        toolVersionParam.getValues().addValue(val);
       
        //init param containing sanitized version of toolstate.
        stateParam.setId(context.getPathContext().getAbsoluteContext("state"));
        stateParam.setName("Tool Values");
        stateParam.setOrder(3);
        stateParam.setFileFormat(new Format());
        stateParam.getFormat().setCardinality(1);
        stateParam.getFormat().setType("String");
        stateParam.setPrefix("-state");
        stateParam.setPrefixSpaced(true);
        stateParam.setEnabled(true);
        val = new Value();
        String tool_state_string = cleanStringValue(s.getToolState().toString());
        val.setValue(tool_state_string);
        stateParam.getValues().addValue(val);
       
        //for each action
        for(String action : s.getPostJobActions()){
          Action myaction = s.getPostJobAction(action);
          //visit the action.
          Pair<Object, LoniEnvironment> le = stepImplVisitor.visit(myaction, childContext);
          String action_string = (String) le.getElem1(); //returns the string of action dictionary.
         
          //create a parameter for the action.
          LONI.tree.module.Parameter actionParam = new LONI.tree.module.Parameter();
          actionParam.setName(action);
          actionParam.setId(childContext.getPathContext().getAbsoluteContext(action));
          actionParam.setEnabled(true);
          actionParam.setFileFormat(new Format());
          actionParam.getFormat().setCardinality(1);
          actionParam.getFormat().setType("String");
          actionParam.setPrefix("-action-"+action);
          actionParam.setPrefixSpaced(true);
          actionParam.setOrder(1);
          Value actionValue = new Value();
          actionValue.setValue(cleanStringValue(action_string));
          actionParam.getValues().addValue(actionValue);
          //add the created action parameter to the module
          module.addInput(actionParam);
        }
        //add all the other parameters
        module.addInput(stateParam);
        module.addInput(idParam);
        module.addInput(scriptParam);
        module.addInput(toolIdParam);
        module.addInput(toolVersionParam);
        //return the module, module environment
        return new Pair<GraphObject, LoniEnvironment>(module, stepEnv);
      }
      {
      stepImplVisitor = new StepImplVisitor(){
       
        public Pair<Object, LoniEnvironment> visit(ExternalInput input, LoniContext context){
          LoniEnvironment extInputEnv = new LoniEnvironment(); //new loni environment
          Connection setup; //new connection from setup module
          //new output parameter for setup and input parameter for external input
          LONI.tree.module.Output setupOutput = new LONI.tree.module.Output();
          LONI.tree.module.Parameter extParam = new LONI.tree.module.Parameter();
         
          //sanitize id from input name
          String idName = input.getName().replaceAll(" ", "");
         
          //initialize external parameter.
          extParam.setId(context.getPathContext().getAbsoluteContext(idName));
          extParam.setEnabled(true);
          extParam.setName(input.getName());
          extParam.setDescription(input.getDescription());
          extParam.setOrder(1);
          extParam.setFileFormat(new Format());
          extParam.getFormat().setCardinality(1);
          extParam.getFileFormat().setType("File");
          extParam.setPrefix("-dict");
          extParam.setPrefixSpaced(true);
          FileType ft = new FileType();
          ft.setName(GENERIC_DICT_TYPE);
          ft.setExtension("");
          ft.setDescription("");
          extParam.getFileFormat().getFileTypes().addFileType(ft);
         
          //initialize setup output parameter.
          setupOutput.setEnabled(true);
          setupOutput.setId(setupContext.getPathContext().getAbsoluteContext(idName));
          setupOutput.setFormat(new Format());
          setupOutput.setOrder(1);
          setupOutput.setName(input.getName());
          setupOutput.setDescription(input.getDescription());
          setupOutput.getFormat().setCardinality(1);
          setupOutput.getFormat().setType("File");
          setupOutput.setPrefixSpaced(true);
          setupOutput.setPrefix("-output");
          FileType setupFileType = new FileType();
          setupFileType.setName(GENERIC_DICT_TYPE);
          setupFileType.setDescription("");
          setupFileType.setExtension("");
          setupOutput.getFormat().getFileTypes().addFileType(setupFileType);
         
          //add setup output parameter to setupModule.
          setupModule.addOutput(setupOutput);
         
          //add connection from created setup output to created input parameter
          //to the external input environment
          setup = new Connection(setupOutput.getId(), extParam.getId());
          extInputEnv.addConnection(setup);
          //return the created external parameter and the external input environment.
          return new Pair<Object, LoniEnvironment>(extParam, extInputEnv);
       
        }
       
        public  Pair<Object, LoniEnvironment>  visit(ExternalOutput output, LoniContext context){
          //generates output and lonienvironment
          LONI.tree.module.Output loniOutput = new LONI.tree.module.Output();
          LoniEnvironment le = new LoniEnvironment();
         
          //initialize loni output object.
          loniOutput.setName(output.getName());
          loniOutput.setId(context.getPathContext().getAbsoluteContext(output.getName()));
          loniOutput.setEnabled(true);
          loniOutput.setPrefix("-output-"+output.getName()+":"+output.getType());
          loniOutput.setPrefixSpaced(true);
          loniOutput.setOrder(1);
          loniOutput.setFormat(new Format());
          loniOutput.getFormat().setCardinality(1);
          loniOutput.getFormat().setType("File");
          FileType outputFileType = new FileType();
          outputFileType.setName(GENERIC_DICT_TYPE);
          outputFileType.setExtension("");
          outputFileType.setDescription("");
          loniOutput.getFormat().getFileTypes().addFileType(outputFileType);
         
          //add association between galaxy output id and loni output id.
          le.addExternalOutput(loniOutput.getId(), output.getName());
         
          //return loni output environment and loni output
          return new Pair<Object, LoniEnvironment>(loniOutput, le);
       
        }
        public  Pair<Object, LoniEnvironment>  visit(InputConnection step, LoniContext context){
          return null;
         
        }
        public  Pair<Object, LoniEnvironment>  visit(Position position, LoniContext context){
          return null;
        }
        public Pair<Object, LoniEnvironment> visit(InputConnection source, String sink, LoniContext context){
          Connection connection;
          LoniContext otherContext= new LoniContext();
          otherContext.getPathContext().addContext(STEP_PREFIX + source.getSourceId());
          //create connection from to the source and the sink.
          connection = new Connection(
              otherContext.getPathContext().getAbsoluteContext(source.getSourceName()),
              context.getPathContext().getAbsoluteContext(sink));
       
          //return the connection
          return new Pair<Object, LoniEnvironment>(connection, new LoniEnvironment());
       
        }
        public Pair<Object, LoniEnvironment> visit(Action action, LoniContext context){
         
          //dump action object to a json dictionary
          String jsonStr=  GalaxySpecification.getGenericJSONGenerator().generate(action);
         
          //return dumped action object and new loni environment
          return new Pair<Object,LoniEnvironment>(jsonStr, new LoniEnvironment());
        }
      };
      }
      /***
       * Visits all the different parts of a tool.
       */
     
  };
  }
 

TOP

Related Classes of Galaxy.Visitor.Webservice.GalaxyToLoniWorkflowConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.