/**
  * this is the primary method that sends a query to the CIPRes REST service. It expects to receive
  * an XML file, which it returns in Document if the root tag matc hes xmlRootTag
  */
 public Document cipresQuery(HttpClient httpclient, String URL, String xmlRootTag) {
   if (StringUtil.blank(URL)) return null;
   try {
     HttpGet httpget = new HttpGet(URL);
     httpget.addHeader("cipres-appkey", CIPRESkey);
     try {
       HttpResponse response = httpclient.execute(httpget);
       HttpEntity responseEntity = response.getEntity();
       InputStream instream = responseEntity.getContent();
       BufferedReader br = new BufferedReader(new InputStreamReader(instream));
       String line = "";
       StringBuffer sb = new StringBuffer();
       while ((line = br.readLine()) != null) {
         sb.append(line + StringUtil.lineEnding());
       }
       Document cipresResponseDoc = loadXMLFile(xmlRootTag, sb.toString());
       if (cipresResponseDoc != null && verbose) {
         ownerModule.logln(sb.toString());
       }
       if (cipresResponseDoc == null) {
         Document errorDoc = loadXMLFile(sb.toString());
         if (errorDoc != null) reportError(errorDoc, "Error in communicating with CIPRes", true);
       }
       EntityUtils.consume(response.getEntity());
       return cipresResponseDoc;
     } catch (IOException e) {
       Debugg.printStackTrace(e);
     } catch (Exception e) {
       Debugg.printStackTrace(e);
     }
   } catch (Exception e) {
     Debugg.printStackTrace(e);
   }
   return null;
 }
Exemplo n.º 2
0
  /*................................................................................................*/
  private boolean hireFillers(String directoryPath) {
    String[] files = directory.list();
    String treePath;
    for (int i = 0; i < files.length; i++) {
      if (files[i] != null) {
        String fileLowerCase = files[i].toLowerCase();
        if (fileLowerCase.endsWith(".nex") || fileLowerCase.endsWith(".nexus")) {
          treePath = directoryPath + MesquiteFile.fileSeparator + files[i];
          File treeFile = new File(treePath);
          String treeFileName = treeFile.getName();
          if (StringUtil.blank(treeFileName)) {
            return false;
          }
          TreeBlockFiller newFiller;
          newFiller =
              (TreeBlockFiller)
                  hireNamedEmployee(TreeBlockFiller.class, "#SampleOneTreeFromFile"); // treePath);
          if (newFiller != null) {
            if (((SampleOneTreeFromFile) newFiller).setFilePath(treePath)
                && ((SampleOneTreeFromFile) newFiller).processFile()) {
              fillerTasks.addElement(newFiller);
            } else
              Debugg.println(
                  "Filler "
                      + i
                      + " processFile = "
                      + ((SampleOneTreeFromFile) newFiller).processFile());
          }
        }
      }
    }

    return true;
  }
  /*.................................................................................................................*/
  public void getResults(HttpClient httpclient, String jobURL) {

    String resultsUri = getResultsDirectory(jobURL);
    if (StringUtil.notEmpty(resultsUri)) {
      Document cipresResponseDoc = cipresQuery(httpclient, resultsUri, "results");
      if (cipresResponseDoc != null) {
        CipresJobFile[] cipresJobFiles = processFilesDocument(cipresResponseDoc);
        for (int job = 0; job < cipresJobFiles.length; job++) {
          Debugg.println("fileName: " + cipresJobFiles[job].getFileName());
          Debugg.println("     downloadURL: " + cipresJobFiles[job].getDownloadURL());
          Debugg.println("     downloadTitle: " + cipresJobFiles[job].getDownloadTitle());
          Debugg.println("     length: " + cipresJobFiles[job].getLength());
        }
        previousCipresJobFiles = cipresJobFiles.clone();
      }
    }
  }
 /*.................................................................................................................*/
 public void deleteJob(HttpClient httpclient, String URL) {
   HttpDelete httpdelete = new HttpDelete(URL);
   httpdelete.addHeader("cipres-appkey", CIPRESkey);
   try {
     HttpResponse response = httpclient.execute(httpdelete);
   } catch (IOException e) {
     Debugg.printStackTrace(e);
   }
 }
 /*.................................................................................................................*/
 public void cipresDownload(HttpClient httpclient, String URL, String filePath) {
   HttpGet httpget = new HttpGet(URL);
   httpget.addHeader("cipres-appkey", CIPRESkey);
   try {
     HttpResponse response = httpclient.execute(httpget);
     HttpEntity responseEntity = response.getEntity();
     BufferedInputStream bis = new BufferedInputStream(responseEntity.getContent());
     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
     int inByte;
     while ((inByte = bis.read()) != -1) bos.write(inByte);
     bis.close();
     bos.close();
     EntityUtils.consume(response.getEntity());
   } catch (IOException e) {
     Debugg.printStackTrace(e);
   }
 }
  /** The core method that initiates a job on CIPRes. */
  public boolean postJob(
      HttpClient httpclient, MultipartEntityBuilder builder, MesquiteString jobURL) {
    if (builder == null) return false;
    String URL = baseURL + "/job/" + username;
    HttpPost httppost = new HttpPost(URL);
    httppost.addHeader("cipres-appkey", CIPRESkey);

    // some of this from
    // http://stackoverflow.com/questions/18964288/upload-a-file-through-an-http-form-via-multipartentitybuilder-with-a-progress
    HttpEntity cipresEntity = builder.build();

    httppost.setEntity(cipresEntity);

    try {
      HttpResponse response = httpclient.execute(httppost);

      HttpEntity responseEntity = response.getEntity();
      InputStream instream = responseEntity.getContent();
      BufferedReader br = new BufferedReader(new InputStreamReader(instream));
      StringBuffer sb = new StringBuffer();
      String line = "";
      while ((line = br.readLine()) != null) {
        sb.append(line + StringUtil.lineEnding());
      }

      Document cipresResponseDoc = loadXMLFile("jobstatus", sb.toString()); // let's see how it went
      boolean success = false;
      if (cipresResponseDoc != null) {
        processJobSubmissionResponse(cipresResponseDoc, jobURL);
        if (verbose) ownerModule.logln(sb.toString());
        if (jobURL != null) success = StringUtil.notEmpty(jobURL.getValue());
        else success = true;
      } else {
        cipresResponseDoc = loadXMLFile(sb.toString());
        reportError(cipresResponseDoc, "Error with CIPRes run", false);
      }
      EntityUtils.consume(response.getEntity());
      return success;
    } catch (IOException e) {
      Debugg.printStackTrace(e);
    }
    return false;
  }
Exemplo n.º 7
0
  /*.................................................................................................................*/
  public Object doCommand(String commandName, String arguments, CommandChecker checker) {
    if (checker.compare(
        this.getClass(), "Returns the matrix source", null, commandName, "getMatrixSource")) {
      return matrixSourceTask;
    } else if (checker.compare(
        this.getClass(), "Copies the data for selected taxon", null, commandName, "copyData")) {
      if (observedStates == null) return null;
      CharacterData data = observedStates.getParentData();
      if (data == null) return null;
      int it = MesquiteInteger.fromString(parser.getFirstToken(arguments));
      if (MesquiteInteger.isCombinable(it)) {
        StringBuffer sb = new StringBuffer();
        data.copyDataFromRowIntoBuffer(it, sb);
        if (StringUtil.notEmpty(sb.toString())) {
          localCopyDataClipboard = sb.toString();
          localCopyData = data;
          localCopyDataTaxon = data.getTaxa().getTaxonName(it);
        } else {
          localCopyDataClipboard = null;
          localCopyData = null;
          localCopyDataTaxon = null;
        }
      }
      return null;
    } else if (checker.compare(
        this.getClass(), "Pastes the data for selected taxon", null, commandName, "pasteData")) {
      if (observedStates == null) return null;
      CharacterData data = observedStates.getParentData();
      if (data == null) return null;
      int it = MesquiteInteger.fromString(parser.getFirstToken(arguments));
      if (MesquiteInteger.isCombinable(it) && StringUtil.notEmpty(localCopyDataClipboard)) {
        data.pasteDataFromStringIntoTaxon(it, localCopyDataClipboard);
      }
      return null;
    } else if (checker.compare(
        this.getClass(),
        "Pastes the data for selected taxon",
        null,
        commandName,
        "deleteDataTouched")) {
      if (observedStates == null) return null;
      CharacterData data = observedStates.getParentData();
      if (data == null) return null;
      int it = MesquiteInteger.fromString(parser.getFirstToken(arguments));
      Debugg.println("prepare to delete row: " + it);
      if (MesquiteInteger.isCombinable(it)) {
        if (!AlertDialog.query(
            containerOfModule(),
            "Delete Data?",
            "Are you sure you want to delete the data for taxon "
                + data.getTaxa().getTaxonName(it)
                + " in the matrix \""
                + data.getName()
                + "\"",
            "No",
            "Yes")) {
          zapData(data, it);
        }
      }
      return null;
    } else if (checker.compare(
        this.getClass(), "Deletes the data for selected taxa", null, commandName, "deleteData")) {
      if (observedStates == null) return null;
      captureCharacterDataFromObservedStates();
      if (data == null) return null;
      if (!AlertDialog.query(
          containerOfModule(),
          "Delete Data?",
          "Are you sure you want to delete the data for these taxa in the matrix \""
              + data.getName()
              + "\"",
          "No",
          "Yes")) zapData(data);
      return null;
    } else if (checker.compare(
        this.getClass(),
        "deleteds () and anything between",
        null,
        commandName,
        "deletePrepended")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }

      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if ((!anySelected || selected(taxa, it, myColumn))) {
          String note = getNote(it);
          while (!StringUtil.blank(note) && note.indexOf("(") >= 0) {
            int start = note.indexOf("(");
            int end = note.indexOf(")");
            String firstBit = "";
            if (start > 0) firstBit = note.substring(0, start);
            note = firstBit + note.substring(end + 1, note.length());
          }
          setNote(it, note);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else if (checker.compare(this.getClass(), "deletes *", null, commandName, "deleteStar")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }
      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if ((!anySelected || selected(taxa, it, myColumn))) {
          String note = getNote(it);
          while (!StringUtil.blank(note) && note.indexOf("*") >= 0) {
            int start = note.indexOf("*");
            String firstBit = "";
            if (start > 0) firstBit = note.substring(0, start);
            note = firstBit + note.substring(start + 1, note.length());
          }
          setNote(it, note);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else if (checker.compare(
        this.getClass(),
        "Prepends to the note the sequence length (including N\'s and ?\'s) for the selected taxa",
        null,
        commandName,
        "prependLength")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }
      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if (hasData(it) && (!anySelected || selected(taxa, it, myColumn))) {
          String note = getNote(it);
          if (StringUtil.blank(note)) note = "(" + sequenceLength(it) + ")";
          else note = "(" + sequenceLength(it) + ") " + note;
          setNote(it, note);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else if (checker.compare(
        this.getClass(),
        "Prepends to the note the number of non-missing sites (not including N\'s and ?\'s) for the selected taxa",
        null,
        commandName,
        "prependNumSites")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }
      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if (hasData(it) && (!anySelected || selected(taxa, it, myColumn))) {
          String note = getNote(it);
          if (StringUtil.blank(note)) note = "(" + numSites(it) + ")";
          else note = "(" + numSites(it) + ") " + note;
          setNote(it, note);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else if (checker.compare(
        this.getClass(),
        "Deletes the notes for the selected taxa",
        null,
        commandName,
        "deleteAnnotation")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }
      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if (hasData(it) && (!anySelected || selected(taxa, it, myColumn))) {
          setNote(it, null);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else return super.doCommand(commandName, arguments, checker);
  }
  /*.................................................................................................................*/
  void getArguments(
      MultipartEntityBuilder builder,
      StringBuffer sb,
      String fileName,
      String LOCproteinModel,
      String LOCdnaModel,
      String LOCotherOptions,
      int LOCbootstrapreps,
      int LOCbootstrapSeed,
      int LOCnumRuns,
      String LOCoutgroupTaxSetString,
      String LOCMultipleModelFile,
      boolean preflight) {
    if (builder == null) return;
    /*
    if (preflight)
    	arguments += " -n preflight.out ";
    else
    	arguments += " -s " + fileName + " -n file.out ";
    */

    if (isProtein) {
      if (StringUtil.blank(LOCproteinModel))
        addArgument(builder, sb, "vparam.protein_opts_", "PROTGAMMAJTT");
      else addArgument(builder, sb, "vparam.protein_opts_", LOCproteinModel);
    } else if (StringUtil.blank(LOCdnaModel))
      addArgument(builder, sb, "vparam.dna_gtrcat_", "GTRGAMMA");
    else addArgument(builder, sb, "vparam.dna_gtrcat_", "GTRGAMMA");
    //	builder.addTextBody("vparam.dna_gtrcat_",LOCdnaModel);

    /*
    		if (StringUtil.notEmpty(LOCMultipleModelFile))
    			arguments += " -q " + ShellScriptUtil.protectForShellScript(LOCMultipleModelFile);

    		if (!StringUtil.blank(LOCotherOptions))
    			arguments += " " + LOCotherOptions;

    */

    addArgument(builder, sb, "vparam.provide_parsimony_seed_", "1");
    addArgument(builder, sb, "vparam.parsimony_seed_val_", "" + randomIntSeed);

    if (bootstrapOrJackknife()) {
      if (LOCbootstrapreps > 0) {
        addArgument(builder, sb, "vparam.choose_bootstrap_", "b");
        addArgument(builder, sb, "vparam.bootstrap_value_", "" + LOCbootstrapreps);
        addArgument(builder, sb, "vparam.seed_value_", "" + LOCbootstrapSeed);
        //	addArgument(builder, sb, "vparam.bootstrap_",""+LOCbootstrapreps);
        //	addArgument(builder, sb, "vparam.mulparambootstrap_seed_",""+LOCbootstrapSeed);
      } else Debugg.println("TOO FEW BOOTSTRAP REPS");

    } else {
      addArgument(builder, sb, "vparam.specify_runs_", "1");
      addArgument(builder, sb, "vparam.altrun_number_", "" + LOCnumRuns);
      //	if (RAxML814orLater)
      // addArgument(builder, sb, "vparam.mesquite_output_","1");
    }

    TaxaSelectionSet outgroupSet = null;
    if (!StringUtil.blank(LOCoutgroupTaxSetString)) {
      outgroupSet =
          (TaxaSelectionSet) taxa.getSpecsSet(LOCoutgroupTaxSetString, TaxaSelectionSet.class);
      if (outgroupSet != null)
        addArgument(builder, sb, "vparam.outgroup_", outgroupSet.getStringList(",", true));
      arguments += " -o " + outgroupSet.getStringList(",", true);
    }
  }