/*.................................................................................................................*/
  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equalsIgnoreCase("setToDefaults")) {
      setDefaultTNTCommandsSearchOptions();
      searchField.setText(searchArguments);
      bootstrapSearchField.setText(bootstrapSearchArguments);
      harvestOnlyStrictConsensusBox.setState(harvestOnlyStrictConsensus);
      resamplingAllConsensusTreesBox.setState(!resamplingAllConsensusTrees);
      bootStrapRepsField.setValue(bootstrapreps);

    } else if (e.getActionCommand().equalsIgnoreCase("setToDefaultsOtherOptions")) {
      setDefaultTNTCommandsOtherOptions();
      otherOptionsField.setText(otherOptions);
      convertGapsBox.setState(convertGapsToMissing);
    } else if (e.getActionCommand().equalsIgnoreCase("browseSearchScript")
        && searchScriptPathField != null) {
      MesquiteString directoryName = new MesquiteString();
      MesquiteString fileName = new MesquiteString();
      String path =
          MesquiteFile.openFileDialog("Choose Search Script File", directoryName, fileName);
      if (StringUtil.notEmpty(path)) searchScriptPathField.setText(path);
    } else if (e.getActionCommand().equalsIgnoreCase("browseBootSearchScript")
        && bootSearchScriptPathField != null) {

      MesquiteString directoryName = new MesquiteString();
      MesquiteString fileName = new MesquiteString();
      String path =
          MesquiteFile.openFileDialog(
              "Choose Resampling Search Script File", directoryName, fileName);
      if (StringUtil.notEmpty(path)) bootSearchScriptPathField.setText(path);
    }
  }
 public void processCompletedOutputFiles(String[] outputFilePaths) {
   if (outputFilePaths.length > 1 && !StringUtil.blank(outputFilePaths[1])) {
     ZephyrUtil.copyLogFile(this, "TNT", outputFilePaths[1]);
   }
   if (outputFilePaths.length > 2 && !StringUtil.blank(outputFilePaths[2])) {
     ZephyrUtil.copyOutputText(this, outputFilePaths[2], commands);
   }
 }
  /*.................................................................................................................*/
  public String preparePreferencesForXML() {
    StringBuffer buffer = new StringBuffer(200);
    StringUtil.appendXMLTag(buffer, 2, "mxram", mxram);
    StringUtil.appendXMLTag(buffer, 2, "bootStrapReps", bootstrapreps);
    StringUtil.appendXMLTag(buffer, 2, "convertGapsToMissing", convertGapsToMissing);
    StringUtil.appendXMLTag(buffer, 2, "resamplingAllConsensusTrees", resamplingAllConsensusTrees);
    StringUtil.appendXMLTag(buffer, 2, "harvestOnlyStrictConsensus", harvestOnlyStrictConsensus);
    StringUtil.appendXMLTag(buffer, 2, "searchArguments", searchArguments);
    StringUtil.appendXMLTag(buffer, 2, "bootstrapSearchArguments", bootstrapSearchArguments);
    StringUtil.appendXMLTag(buffer, 2, "searchStyle", searchStyle);
    StringUtil.appendXMLTag(buffer, 2, "otherOptions", otherOptions);

    preferencesSet = true;
    return buffer.toString();
  }
  /*.................................................................................................................*/
  public void processSingleXMLPreference(String tag, String content) {
    if ("bootStrapReps".equalsIgnoreCase(tag)) bootstrapreps = MesquiteInteger.fromString(content);
    if ("mxram".equalsIgnoreCase(tag)) mxram = MesquiteInteger.fromString(content);
    if ("convertGapsToMissing".equalsIgnoreCase(tag))
      convertGapsToMissing = MesquiteBoolean.fromTrueFalseString(content);
    if ("resamplingAllConsensusTrees".equalsIgnoreCase(tag))
      resamplingAllConsensusTrees = MesquiteBoolean.fromTrueFalseString(content);
    if ("harvestOnlyStrictConsensus".equalsIgnoreCase(tag))
      harvestOnlyStrictConsensus = MesquiteBoolean.fromTrueFalseString(content);
    if ("searchStyle".equalsIgnoreCase(tag)) searchStyle = MesquiteInteger.fromString(content);
    if ("searchArguments".equalsIgnoreCase(tag))
      searchArguments = StringUtil.cleanXMLEscapeCharacters(content);
    if ("bootstrapSearchArguments".equalsIgnoreCase(tag))
      bootstrapSearchArguments = StringUtil.cleanXMLEscapeCharacters(content);
    if ("otherOptions".equalsIgnoreCase(tag))
      otherOptions = StringUtil.cleanXMLEscapeCharacters(content);

    // parallel, etc.
    preferencesSet = true;
  }
 /*.................................................................................................................*/
 public void appendAdditionalSearchDetails() {
   appendToSearchDetails("CIPRes Analysis, " + StringUtil.getDateTime() + "\n");
   appendToSearchDetails("Search details: \n");
   if (bootstrapOrJackknife()) {
     appendToSearchDetails("   Bootstrap analysis\n");
     appendToSearchDetails("   " + bootstrapreps + " bootstrap replicates");
   } else {
     appendToSearchDetails("   Search for maximum-likelihood tree\n");
     appendToSearchDetails("   " + numRuns + " search replicate");
     if (numRuns > 1) appendToSearchDetails("s");
   }
 }
 /*.................................................................................................................*/
 public String preparePreferencesForXML() {
   StringBuffer buffer = new StringBuffer(200);
   StringUtil.appendXMLTag(buffer, 2, "availMemory", availMemory);
   buffer.append(super.preparePreferencesForXML());
   return buffer.toString();
 }
  public void runFilesAvailable(int fileNum) {
    String[] logFileNames = getLogFileNames();
    if ((progIndicator != null && progIndicator.isAborted()) || logFileNames == null) return;
    String[] outputFilePaths = new String[logFileNames.length];
    outputFilePaths[fileNum] = externalProcRunner.getOutputFilePath(logFileNames[fileNum]);
    String filePath = outputFilePaths[fileNum];

    if (fileNum == 0
        && outputFilePaths.length > 0
        && !StringUtil.blank(outputFilePaths[0])
        && !bootstrapOrJackknife()) { // tree file
      if (ownerModule instanceof NewTreeProcessor) {
        String treeFilePath = filePath;
        if (taxa != null) {
          TaxaSelectionSet outgroupSet =
              (TaxaSelectionSet) taxa.getSpecsSet(outgroupTaxSetString, TaxaSelectionSet.class);
          ((NewTreeProcessor) ownerModule).newTreeAvailable(treeFilePath, outgroupSet);

        } else ((NewTreeProcessor) ownerModule).newTreeAvailable(treeFilePath, null);
      }
    } else if (fileNum == 1
        && outputFilePaths.length > 1
        && !StringUtil.blank(outputFilePaths[1])
        && !bootstrapOrJackknife()) { // log file
      if (MesquiteFile.fileExists(filePath)) {
        String s = MesquiteFile.getFileLastContents(filePath);
        if (!StringUtil.blank(s))
          if (progIndicator != null) {
            parser.setString(s);
            String rep = parser.getFirstToken(); // generation number
            logln("");
            if (MesquiteInteger.isNumber(rep)) {
              int numReps = MesquiteInteger.fromString(rep) + 1;
              progIndicator.setText(
                  "Replicate: " + numReps); // + ", ln L = " + parser.getNextToken());
              if (bootstrapOrJackknife()) {
                logln("Replicate " + numReps + " of " + bootstrapreps);
              }
              logln("Replicate " + numReps + " of " + totalNumHits);

              progIndicator.spin();
              double timePerRep = 0;
              if (MesquiteInteger.isCombinable(numReps) && numReps > 0) {
                timePerRep = timer.timeSinceVeryStartInSeconds() / numReps; // this is time per rep
              }
              int timeLeft = 0;
              if (bootstrapOrJackknife()) {
                timeLeft = (int) ((bootstrapreps - numReps) * timePerRep);
              } else {
                String token = parser.getNextToken(); // algorithm
                token = parser.getNextToken(); // Tree
                token = parser.getNextToken(); // Score
                String best = parser.getNextToken(); // Best
                logln("  Score " + token + "; best found so far " + best);
                timeLeft = (int) ((totalNumHits - numReps) * timePerRep);
              }

              logln(
                  "  Running time so far "
                      + StringUtil.secondsToHHMMSS((int) timer.timeSinceVeryStartInSeconds())
                      + ", approximate time remaining "
                      + StringUtil.secondsToHHMMSS(timeLeft));
            }
          }
        count++;
      } else if (MesquiteTrunk.debugMode) logln("*** File does not exist (" + filePath + ") ***");
    }
  }
 /*.................................................................................................................*/
 String getComDelim() {
   if (externalProcRunner.isWindows()) return ";" + StringUtil.lineEnding();
   else return ";" + StringUtil.lineEnding();
 }
  /*.................................................................................................................*/
  void formCommandFile(String dataFileName, int firstOutgroup) {
    if (parallel) {
      commands = "";
    }
    commands += getTNTCommand("mxram " + mxram);

    commands += getTNTCommand("report+0/1/0");
    commands += getTNTCommand("log " + logFileName);
    commands += getTNTCommand("p " + dataFileName);
    commands += getTNTCommand("vversion");
    if (MesquiteInteger.isCombinable(firstOutgroup) && firstOutgroup >= 0)
      commands += getTNTCommand("outgroup " + firstOutgroup);
    if (bootstrapOrJackknife()) {
      if (parallel) {
        commands += indentTNTCommand("ptnt begin parallelRun " + numSlaves + "/ram x 2 = ");
      }
      if (StringUtil.notEmpty(bootSearchScriptPath)) {
        String script = MesquiteFile.getFileContentsAsString(bootSearchScriptPath);
        if (StringUtil.notEmpty(script)) commands += script;
      } else
        commands += StringUtil.lineEnding() + bootstrapSearchArguments + StringUtil.lineEnding();
      String saveTreesString = "";
      if (resamplingAllConsensusTrees) saveTreesString = " savetrees ";
      String bootSearchString = " [xmult; bb]";
      bootSearchString = "";

      if (parallel) {
        int numRepsPerSlave = bootstrapreps / numSlaves;
        if (numRepsPerSlave * numSlaves < bootstrapreps) numRepsPerSlave++;
        if (searchStyle == BOOTSTRAPSEARCH)
          commands +=
              getTNTCommand(
                  "resample boot cut 50 "
                      + saveTreesString
                      + " replications "
                      + numRepsPerSlave
                      + " [xmult; bb] savetrees"); // + getComDelim();
        else if (searchStyle == JACKKNIFESEARCH)
          commands +=
              getTNTCommand(
                  "resample jak cut 50 "
                      + saveTreesString
                      + " replications "
                      + numRepsPerSlave
                      + " [xmult; bb] savetrees"); // + getComDelim();
        else if (searchStyle == SYMSEARCH)
          commands +=
              getTNTCommand(
                  "resample sym cut 50 "
                      + saveTreesString
                      + " replications "
                      + numRepsPerSlave
                      + " [xmult; bb] savetrees"); // + getComDelim();
        else if (searchStyle == POISSONSEARCH)
          commands +=
              getTNTCommand(
                  "resample poisson cut 50 "
                      + saveTreesString
                      + " replications "
                      + numRepsPerSlave
                      + " [xmult; bb] savetrees"); // + getComDelim();
        commands += getTNTCommand("return");
        commands += getTNTCommand("ptnt wait parallelRun");
        commands += getTNTCommand("ptnt get parallelRun");
      } else {
        if (!resamplingAllConsensusTrees) {
          commands += getTNTCommand("macro=");
          commands += getTNTCommand("ttags =");
        }
        commands += getTNTCommand("tsave *" + treeFileName);
        if (bootstrapAllowed) {
          if (searchStyle == BOOTSTRAPSEARCH)
            commands +=
                getTNTCommand(
                    "resample boot "
                        + saveTreesString
                        + " replications "
                        + bootstrapreps
                        + bootSearchString); // + getComDelim();
          else if (searchStyle == JACKKNIFESEARCH)
            commands +=
                getTNTCommand(
                    "resample jak cut 50 "
                        + saveTreesString
                        + " replications "
                        + bootstrapreps
                        + bootSearchString); // + getComDelim();
          else if (searchStyle == SYMSEARCH)
            commands +=
                getTNTCommand(
                    "resample sym cut 50 "
                        + saveTreesString
                        + " replications "
                        + bootstrapreps
                        + bootSearchString); // + getComDelim();
          else if (searchStyle == POISSONSEARCH)
            commands +=
                getTNTCommand(
                    "resample poisson cut 50 "
                        + saveTreesString
                        + " replications "
                        + bootstrapreps
                        + bootSearchString); // + getComDelim();
        }
        if (!resamplingAllConsensusTrees) commands += getTNTCommand("save *");
        else commands += getTNTCommand("save");
        commands += getTNTCommand("tsave/");
        if (!resamplingAllConsensusTrees) {
          commands += getTNTCommand("ttags -/");
          commands += getTNTCommand("macro-");
        }
      }

      // commands += getTNTCommand("proc/") ;

      commands += getTNTCommand("log/");

      //	if (!parallel)
      commands += getTNTCommand("quit");
    } else {
      // commands += getTNTCommand("tsave !5 " + treeFileName) ;   // if showing intermediate trees
      commands += getTNTCommand("tsave *" + treeFileName);
      if (StringUtil.notEmpty(searchScriptPath)) {
        String script = MesquiteFile.getFileContentsAsString(searchScriptPath);
        if (StringUtil.notEmpty(script)) commands += script;
      } else commands += searchArguments;
      commands += otherOptions;
      if (harvestOnlyStrictConsensus) commands += getTNTCommand("nelsen *");
      commands += getTNTCommand("save");
      commands += getTNTCommand("log/");

      commands += getTNTCommand("tsave/");
      commands += getTNTCommand("quit");
    }
  }
  /*.................................................................................................................*/
  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);
    }
  }