/** This method will process each argument and assign new varibles */
  public void processArgs(String[] args) {
    File file = null;
    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':
              file = new File(args[i + 1]);
              i++;
              break;
            case 'v':
              genomeVersion = args[i + 1];
              i++;
              break;
            case 's':
              sumScores = true;
              break;
            case 't':
              threshold = Float.parseFloat(args[++i]);
              break;
            case 'h':
              printDocs();
              System.exit(0);
            default:
              Misc.printExit("\nError, unknown option! " + mat.group());
          }
        } catch (Exception e) {
          System.out.print("\nSorry, something doesn't look right with this parameter: -" + test);
          System.out.println();
          System.exit(0);
        }
      }
    }
    if (file == null || file.exists() == false)
      Misc.printErrAndExit("Problem finding your bed files!\n");
    // pull files
    File[][] tot = new File[3][];
    tot[0] = IO.extractFiles(file, ".bed");
    tot[1] = IO.extractFiles(file, ".bed.zip");
    tot[2] = IO.extractFiles(file, ".bed.gz");
    bedFiles = IO.collapseFileArray(tot);
    if (bedFiles == null || bedFiles.length == 0)
      Misc.printErrAndExit("Problem finding your xxx.bed(.zip/.gz OK) files!\n");

    // genome version
    if (genomeVersion == null)
      Misc.printErrAndExit(
          "Please enter a genome version (e.g. H_sapiens_Mar_2006, see http://genome.ucsc.edu/FAQ/FAQreleases\n");
  }
  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, " "));
    }
  }
  // constructor
  public Sgr2Bar(String[] args) {
    try {
      // check for args
      processArgs(args);

      System.out.println("Genome version -> " + genomeVersion);
      System.out.println("Strand -> " + strand);
      System.out.println("Stair Step? -> " + stairStep);
      System.out.println();

      // load tagValues
      if (stairStep) tagValues.put(BarParser.GRAPH_TYPE_TAG, BarParser.GRAPH_TYPE_STAIRSTEP);
      else tagValues.put(BarParser.GRAPH_TYPE_TAG, BarParser.GRAPH_TYPE_BAR);

      for (int x = 0; x < sgrFiles.length; x++) {
        System.out.println("\tLoading -> " + sgrFiles[x]);
        GrGraph[] grs = GrGraph.loadSgrFile(sgrFiles[x]);

        // make save directory
        String dirName;

        dirName = Misc.removeExtension(sgrFiles[x].getCanonicalPath());

        File dir = new File(dirName);
        dir.mkdir();
        // print bar files
        System.out.println("\tSaving...");
        for (int i = 0; i < grs.length; i++) {
          File barFile = new File(dir, grs[i].getChromosome() + ".bar");
          barParser.writeBarFile(
              barFile,
              grs[i].getChromosome(),
              genomeVersion,
              strand.charAt(0),
              grs[i].getBasePositions(),
              grs[i].getValues(),
              tagValues);
        }
      }
      System.out.println("\nDone!\n");
    } catch (IOException e) {
      e.printStackTrace();
      Misc.printErrAndExit("\nFailed to parse file!");
    }
  }
  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();
  }