public void checkPrintFiles() {
    LinkedHashMap<String, File> nameFile = new LinkedHashMap<String, File>();
    nameFile.put("Reference directory", referenceDir);
    nameFile.put("Pipeline.jar", pJar);
    nameFile.put("Pipeline properties", truncPipePropFile);
    nameFile.put("Coverage QC bed", bedForCoverageQC);
    nameFile.put("Variant calling bed", bedForVarCalling);
    nameFile.put("Fasta genome reference", fastaReference);
    nameFile.put("Unfiltered bam", unfilteredBam);
    nameFile.put("Final bam", finalBam);
    nameFile.put("Final vcf", finalVcf);
    if (webRootForLinks != null) nameFile.put("Web links dir", webRootForLinks);
    nameFile.put("Final output dir", outputDirectory);

    boolean missingFile = false;
    System.out.println("\nResources (name exists path):");
    for (String name : nameFile.keySet()) {
      File f = nameFile.get(name);
      boolean fExists = true;
      if (f == null) {
        fExists = false;
        missingFile = true;
      } else {
        fExists = f.exists();
        if (fExists == false) missingFile = true;
      }
      System.out.println(name + "\t" + fExists + "\t" + f);
    }
    if (missingFile) Misc.printErrAndExit("\nMissing resources! See above.");
  }
 public String fetchCleanupReviewDirLinkGenXml() {
   String s = null;
   try {
     s = // Organize and Clean Up Section
         "<ReviewDirGenerator class='operator.ReviewDirGenerator' destination.dir='"
             + outputDirectory.getCanonicalPath()
             + "' sample='"
             + outputDirectory.getName()
             + "' submitter='"
             + submitter
             + "' analysis.type='"
             + analysisType
             + "'> \n"
             + "    <finalVariants /> \n"
             + "    <ViewerCSV /> \n"
             + "    <InstanceLog class='buffer.InstanceLogFile' /> \n"
             + "    <finalBAM /> \n"
             + "    <QCOutputFile /> \n"
             + "    <bedForVars /> \n"
             + "</ReviewDirGenerator> \n\n";
   } catch (IOException e) {
     e.printStackTrace();
     Misc.printErrAndExit("\nError fetching clean up and review dir links.\n");
   }
   return s;
 }
  private void buildXmlPropertiesFile() {
    System.out.println(
        "\nBuilding and checking your pipeline properties file -> " + truncPipePropFile);
    StringBuilder toPrint = new StringBuilder();

    // walk through the prop file
    String[] prop = IO.loadFileIntoStringArray(truncPipePropFile);
    Pattern val = Pattern.compile("(<entry key.+>)([D|A|B].*)</entry>");
    boolean missingFile = false;
    for (String s : prop) {
      // does it match a file needing prepending? Data/, Apps/, Bed/
      Matcher mat = val.matcher(s);
      if (mat.matches()) {
        File test = new File(referenceDir, mat.group(2));
        if (test.exists()) {
          System.out.println("Found\t" + test);
          toPrint.append(mat.group(1));
          toPrint.append(test.toString());
          toPrint.append("</entry>");
        } else {
          System.out.println("Misssing\t" + test);
          missingFile = true;
        }
      }
      // threads?
      else if (s.contains("threads"))
        toPrint.append("<entry key=\"threads\">" + threads + "</entry>");

      // nope just save it and add a line return
      else toPrint.append(s);
      toPrint.append("\n");
    }
    // anything missing? if so exit
    if (missingFile)
      Misc.printErrAndExit(
          "\nFailed to find all of the files in your properties file, see above.\n");

    // OK, write it out
    completePipelinePropFile = new File(outputDirectory, "pipelineProperties.xml");
    if (IO.writeString(toPrint.toString(), completePipelinePropFile) == false)
      Misc.printErrAndExit("Problem writing -> " + truncPipePropFile);
  }
 private void checkForGzippedVcf() {
   String vcfName = finalVcf.getName();
   if (vcfName.endsWith(".gz")) {
     File uncomp = new File(finalVcf.getParentFile(), vcfName.substring(0, vcfName.length() - 3));
     if (uncomp.exists() == false) {
       deleteTempVcf = true;
       if (IO.uncompress(finalVcf, uncomp) == null)
         Misc.printErrAndExit("ERROR: failed to uncompress -> " + finalVcf);
     }
     finalVcf = uncomp;
   }
 }
  private void executePipelineJob() {
    String[] cmd = null;
    try {
      // write out tempTemplate
      File template = new File(outputDirectory, "pipelineTemplate.xml");
      if (IO.writeString(xmlTemplate, template) == false)
        Misc.printErrAndExit("Problem writing -> " + template);

      // build and execute cmd
      cmd =
          new String[] {
            "java",
            "-jar",
            "-Xmx2G",
            pJar.getCanonicalPath(),
            "-props",
            completePipelinePropFile.getCanonicalPath(),
            template.getCanonicalPath()
          };

      String stringCmd = Misc.stringArrayToString(cmd, " ");
      System.out.println("\nExecuting:\n" + stringCmd);
      System.out.println("\nPipelineOutput:");
      String[] out = IO.executeViaProcessBuilder(cmd, true);

      // check output for possible errors
      for (String line : out) {
        String lcLine = line.toLowerCase();
        // watch out for cases where error is mentioned in a warning output line.
        if (lcLine.contains("error") && lcLine.startsWith("warning") == false)
          Misc.printErrAndExit(
              "\n\nERROR found in Pipeline.jar output, see above. Aborting!\n" + line);
      }

    } catch (Exception e) {
      e.printStackTrace();
      Misc.printErrAndExit("ERROR: executing " + Misc.stringArrayToString(cmd, " "));
    }
  }
 /** This method will process each argument and assign new varibles */
 public void processArgs(String[] args) {
   Pattern pat = Pattern.compile("-[a-z]");
   for (int i = 0; i < args.length; i++) {
     String lcArg = args[i].toLowerCase();
     Matcher mat = pat.matcher(lcArg);
     if (mat.matches()) {
       char test = args[i].charAt(1);
       try {
         switch (test) {
           case 'f':
             fastas = IO.extractFiles(args[i + 1], "fasta");
             i++;
             break;
           case 'n':
             numberOfBases = Integer.parseInt(args[i + 1]);
             i++;
             break;
           case 'i':
             indexDirectory = new File(args[i + 1]);
             i++;
             break;
           case 'h':
             printDocs();
             System.exit(0);
           default:
             Misc.printExit("\nError: unknown option! " + mat.group());
         }
       } catch (Exception e) {
         Misc.printExit(
             "\nSorry, something doesn't look right with this parameter: -" + test + "\n");
       }
     }
   }
   // make index directory?
   if (indexDirectory == null) {
     indexDirectory = new File(fastas[0].getParentFile(), "IndexedSequences");
     indexDirectory.mkdir();
   }
 }
  public void checkPrintFields() {
    LinkedHashMap<String, String> nameField = new LinkedHashMap<String, String>();
    nameField.put("Job ID", jobId);
    nameField.put("Sample ID", sampleId);
    nameField.put("Submitter", submitter);
    nameField.put("Analysis Type", analysisType);
    nameField.put("Minimum Align Depth", minimumReadDepth);
    nameField.put("Threads", threads);
    nameField.put("SnpEff Genome", snpEffGenome);
    // set output of booleans
    nameField.put("Upload Vars to NGSWeb", uploadVarsToNGSWeb + "");

    boolean missingField = false;
    System.out.println("Fields:");
    for (String name : nameField.keySet()) {
      String f = nameField.get(name);
      if (f == null || f.length() == 0) missingField = true;
      System.out.println(name + "\t" + f);
    }
    if (missingField) Misc.printErrAndExit("\nMissing Fields! See above.");
  }
  /** This method will process each argument and assign new variables */
  public void processArgs(String[] args) {
    Pattern pat = Pattern.compile("-[a-z]");
    System.out.println(
        "\n" + IO.fetchUSeqVersion() + " Arguments: " + Misc.stringArrayToString(args, " ") + "\n");
    for (int i = 0; i < args.length; i++) {
      String lcArg = args[i].toLowerCase();
      Matcher mat = pat.matcher(lcArg);
      if (mat.matches()) {
        char test = args[i].charAt(1);
        try {
          switch (test) {
            case 'o':
              jobId = args[++i];
              break;
            case 's':
              sampleId = args[++i];
              break;
            case 'm':
              submitter = args[++i];
              break;
            case 'y':
              analysisType = args[++i];
              break;
            case 'w':
              webRootForLinks = new File(args[++i]);
              break;
            case 'e':
              snpEffGenome = args[++i];
              break;
            case 'i':
              minimumReadDepth = args[++i];
              break;
            case 't':
              threads = args[++i];
              break;
            case 'l':
              uploadVarsToNGSWeb = true;
              break;
            case 'j':
              pJar = new File(args[++i]);
              break;
            case 'p':
              truncPipePropFile = new File(args[++i]);
              break;
            case 'q':
              bedForCoverageQC = new File(args[++i]);
              break;
            case 'b':
              bedForVarCalling = new File(args[++i]);
              break;
            case 'r':
              fastaReference = new File(args[++i]);
              break;
            case 'u':
              unfilteredBam = new File(args[++i]);
              break;
            case 'f':
              finalBam = new File(args[++i]);
              break;
            case 'v':
              finalVcf = new File(args[++i]);
              break;
            case 'd':
              outputDirectory = new File(args[++i]);
              break;
            case 'c':
              referenceDir = new File(args[++i]);
              break;
            case 'h':
              printDocs();
              System.exit(0);
            default:
              Misc.printErrAndExit("\nProblem, unknown option! " + mat.group());
          }
        } catch (Exception e) {
          Misc.printErrAndExit(
              "\nSorry, something doesn't look right with this parameter: -" + test + "\n");
        }
      }
    }

    // check output dir and if needed set sampleId
    if (outputDirectory != null) {
      outputDirectory.mkdirs();
      if (sampleId.length() == 0) sampleId = outputDirectory.getName();
    }
    // check root directory if needed
    if (webRootForLinks != null) {
      if (webRootForLinks.exists() == false) webRootForLinks.mkdirs();
      // links dir?
      File links = new File(webRootForLinks, "links");
      if (links.exists() == false) links.mkdirs();
    }

    // look for required fields and files
    checkPrintFields();
    checkPrintFiles();
    checkForGzippedVcf();
  }