Exemplo n.º 1
0
  public static void main(String[] argv) {
    SAMFileReader.setDefaultValidationStringency(SAMFileReader.ValidationStringency.SILENT);
    // STFU

    SAMResource sr = null;
    SAMRegion region = new SAMRegion();
    region.range = new Range();
    region.range.start = -1;
    region.range.end = -1;

    String outfile = null;
    String target_file = null;
    SAMCoverage sc = new SAMCoverage();

    for (int i = 0; i < argv.length; i++) {
      if (argv[i].equals("-bam")) {
        sr = new SAMResource();
        //	sr.import_data(SAMResourceTags.SAM_URL, argv[++i]);
        sr.set_file(argv[++i]);
        sr.detect_sample_id();
      } else if (argv[i].equals("-targets")) {
        target_file = argv[++i];
      } else if (argv[i].equals("-tname")) {
        region.tname = argv[++i];
      } else if (argv[i].equals("-verbose")) {
        sc.set_verbose(true);
      } else if (argv[i].equals("-tstart")) {
        region.range.start = Integer.parseInt(argv[++i]);
      } else if (argv[i].equals("-tend")) {
        region.range.end = Integer.parseInt(argv[++i]);
      } else if (argv[i].equals("-of")) {
        outfile = argv[++i];
      } else if (argv[i].equals("-min-quality")) {
        sc.set_min_quality(Integer.parseInt(argv[++i]));
      } else {
        System.err.println("error: unknown switch " + argv[i]); // debug
        System.exit(1);
      }
    }

    String error = null;
    if (sr == null) {
      error = "specify -bam [file]";
    } else if (target_file == null) {
      if (region.tname == null) {
        error = "specify -tname";
      } else if (region.range.start == -1) {
        error = "specify -tstart";
      } else if (region.range.end == -1) {
        error = "specify -tend";
      }
    }

    sr.set_region(region);

    if (error != null) {
      System.err.println("ERROR: " + error); // debug
    } else if (target_file != null) {
      try {
        File f = new File(target_file);
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line = br.readLine();
        String[] headers = line.split("\t");

        if (headers[0].equals("Name")
            && headers[1].equals("Chromosome")
            && headers[2].equals("Start")
            && headers[3].equals("End")) {

          WorkingFile wf = null;
          if (outfile != null) {
            wf = new WorkingFile(outfile);
            sc.setPrintStream(wf.getPrintStream());
          }

          while (true) {
            line = br.readLine();
            if (line == null) {
              // EOF
              break;
            } else {
              String[] row = line.split("\t");

              region.tname = row[1];
              region.range.start = Integer.parseInt(row[2]);
              region.range.end = Integer.parseInt(row[3]);
              //	      sc.set_name(new String(row[0]));
              sc.set_name(new String(row[0]) + "," + new String(row[1]));
              sc.find_coverage(sr);
            }
          }

          if (outfile != null) wf.finish();

        } else {
          throw new IOException("file format error");
        }

      } catch (Exception e) {
        System.err.println("ERROR: " + e); // debug
        e.printStackTrace();
        System.exit(1);
      }
    } else {
      sc.set_outfile(outfile);
      sc.find_coverage(sr);
    }
  }