Exemple #1
0
  public void find_coverage(SAMResource sres) {
    int start_base = sres.region.range.start;
    int end_base = sres.region.range.end;

    int coverage_len = (end_base - start_base) + 1;
    int i, end, ref_i, read_i, len;

    int[] coverage = new int[coverage_len];
    Arrays.fill(coverage, 0);

    WorkingFile wf = null;
    if (outfile != null) {
      try {
        wf = new WorkingFile(outfile);
        ps = wf.getPrintStream();
      } catch (Exception e) {
        System.err.println("I/O error: " + e); // debug
        e.printStackTrace();
        System.exit(1);
      }
    }

    try {
      //
      //  gather coverage info:
      //
      CloseableIterator<SAMRecord> iterator = sres.get_iterator();
      int read_count = 0;
      int ref_min = -1;
      int ref_max = -1;

      while (iterator.hasNext()) {
        SAMRecord sr = iterator.next();
        read_count++;

        //	System.err.println(sr.getReadName() + ": " + sr.getAlignmentStart() + "-" +
        // sr.getAlignmentEnd());  // debug

        if (sr.getReadUnmappedFlag()) continue;
        if (sr.getDuplicateReadFlag()) {
          if (verbose_mode)
            System.err.println(
                sr.getReadName()
                    + "."
                    + (sr.getReadNegativeStrandFlag() ? "R" : "F")
                    + " ignoring, duplicate");
          continue;
        }

        byte[] read = sr.getReadBases();
        byte[] quals = sr.getBaseQualities();

        for (AlignmentBlock ab : sr.getAlignmentBlocks()) {
          len = ab.getLength();
          read_i = ab.getReadStart() - 1;
          ref_i = ab.getReferenceStart() - start_base;

          if (ref_min == -1 || ref_i < ref_min) ref_min = ref_i;

          for (i = read_i, end = read_i + len; i < end; i++, ref_i++) {
            if (ref_i >= 0 && ref_i < coverage_len) {
              if (quals[i] >= MIN_QUALITY) {
                if (verbose_mode)
                  System.err.println(
                      sr.getReadName()
                          + "."
                          + (sr.getReadNegativeStrandFlag() ? "R" : "F")
                          + " hit at "
                          + (ref_i + start_base)
                          + " as="
                          + sr.getAlignmentStart()
                          + " ae="
                          + sr.getAlignmentEnd());
                coverage[ref_i]++;
              } else if (verbose_mode) {
                System.err.println(
                    sr.getReadName()
                        + "."
                        + (sr.getReadNegativeStrandFlag() ? "R" : "F")
                        + " qual_reject at "
                        + (ref_i + start_base)
                        + " as="
                        + sr.getAlignmentStart()
                        + " ae="
                        + sr.getAlignmentEnd());
              }
            }
          }
          if (ref_max == -1 || ref_i > ref_max) ref_max = ref_i;
        }
      }
      sres.close();
      System.err.println(
          "records:"
              + read_count
              + " ref_min:"
              + (ref_min + start_base)
              + " ref_max:"
              + (ref_max + start_base)); // debug

      //
      //  report coverage info:
      //
      for (i = 0; i < coverage.length; i++) {
        if (name != null) ps.print(name + ",");
        ps.println((i + start_base) + "," + coverage[i]); // debug
      }
      if (wf != null) wf.finish();

    } catch (Exception e) {
      System.err.println("ERROR: " + e); // debug
      e.printStackTrace();
    }
  }
Exemple #2
0
  public void run() {
    String line;
    String id = null;
    config.refgenes = new ArrayList<RefGene>();
    config.dbsnp = new ArrayList<dbSNP>();

    try {
      //      System.err.println("quality read start");  // debug
      boolean need_ruler_rebuild = false;
      boolean need_title = false;
      SAMRegion sr = null;
      while (true) {
        line = br.readLine();
        if (line == null) break;
        // EOF
        String[] stuff = line.split("\t");
        if (stuff[0].equals("sample2id")) {
          Sample sample = Sample.get_sample(stuff[1]);
          for (int i = 2; i < stuff.length; i++) {
            config.read2sample.put(new String(stuff[i]), sample);
          }
        } else if (stuff[0].equals("consensus_label")) {
          config.CONSENSUS_TAG = new String(stuff[1]);
        } else if (stuff[0].equals("ruler_start")) {
          config.ruler_start = Integer.parseInt(stuff[1]);
          need_ruler_rebuild = true;
        } else if (stuff[0].equals("title")) {
          config.title = stuff[1];
          need_title = true;
        } else if (stuff[0].equals("refGene")) {
          config.refgenes.add(new RefGene(stuff));
        } else if (stuff[0].equals("dbsnp")) {
          config.dbsnp.add(new dbSNP(stuff));
        } else if (stuff[0].toUpperCase().indexOf("SAM_") == 0) {
          // FUGLY
          SAMResourceTags v = SAMResourceTags.valueOf(stuff[0].toUpperCase());
          if (v.equals(SAMResourceTags.SAM_URL)) config.sams.add(new SAMResource());
          config.sams.get(config.sams.size() - 1).import_data(v, stuff[1]);
        } else if (stuff[0].equals("reference_sequence")) {
          StringBuffer target_sequence = new StringBuffer();
          while (true) {
            line = br.readLine();
            if (line.equals(">")) {
              //	      config.target_sequence = target_sequence.toString().toCharArray();
              config.target_sequence = target_sequence.toString().getBytes();
              break;
            } else {
              target_sequence.append(line);
            }
          }
        } else if (stuff[0].equals("target_region")) {
          sr = new SAMRegion();
          sr.tname = new String(stuff[1]);
          sr.range = new Range(Integer.parseInt(stuff[2]), Integer.parseInt(stuff[3]));
          //	  System.err.println("range " + sr.tname + " " + sr.range.start + " " + sr.range.end);
          // // debug
        } else {
          System.err.println("error, don't recognize tag " + stuff[0]); // debug
        }
      }

      if (config.ruler_start > 0) {
        for (dbSNP snp : config.dbsnp) {
          snp.consensus_adjust(config.ruler_start);
        }

        for (RefGene rg : config.refgenes) {
          rg.consensus_adjust(config.ruler_start);
        }
      }

      if (config.dbsnp != null) {
        for (dbSNP snp : config.dbsnp) {
          //	  System.err.println("adding snp at " + snp.start + " = " + (snp.start +
          // config.ruler_start));  // debug
        }

        config.snp_config.snp_query = new dbSNPSet(config.dbsnp);
      }

      for (SAMResource sre : config.sams) {
        sre.set_region(sr);
      }

      if (av != null) {
        while (av.get_acepanel().is_built() == false) {
          // spin until dependency built
          try {
            System.err.println("MarkupReader spin..."); // debug
            Thread.sleep(50);
          } catch (Exception e) {
          }
        }

        PadMap pm = av.get_acepanel().get_assembly().get_padmap();
        for (RefGene rg : config.refgenes) {
          rg.consensus_setup(pm);
        }

        Runnable later;

        if (need_title) {
          //	  System.err.println("title="+config.title);  // debug
          later =
              new Runnable() {
                public void run() {
                  av.setTitle(config.title);
                  av.repaint();
                }
              };
          javax.swing.SwingUtilities.invokeLater(later);
        }

        if (need_ruler_rebuild) {
          //
          //  ruler labeling has changed.
          //
          later =
              new Runnable() {
                public void run() {
                  //		av.get_acepanel().get_canvas().build_ruler();

                  if (config.start_unpadded_offset != 0) {
                    AcePanel ap = av.get_acepanel();
                    PadMap pm = ap.get_assembly().get_padmap();
                    int upo = (config.start_unpadded_offset - config.ruler_start) + 1;
                    // +1: convert to 1-based offset
                    int po = pm.get_unpadded_to_padded(upo);
                    System.err.println("upo=" + upo + " po=" + po); // debug
                    SNPList sl = new SNPList();
                    sl.addElement(new SNP(po, 0.0));
                    ap.ace.set_snps(sl);
                    ap.get_canvas().center_on(po);
                    ap.get_canvas().repaint();
                  }
                }
              };
          javax.swing.SwingUtilities.invokeLater(later);
        }

        if (config.enable_exon_navigation) {
          if (false) {
            System.err.println("DEBUG: exon nav disabled");
          } else {
            av.get_acepanel().init_exon_navigation();
          }
        }

        av.get_acepanel().get_assembly().build_summary_info();
        // group samples by tumor/normal, if applicable
        //      av.repaint();

        later =
            new Runnable() {
              public void run() {
                av.repaint();
              }
            };
        javax.swing.SwingUtilities.invokeLater(later);
      }

      //      System.err.println("quality read end");  // debug
    } catch (Exception e) {
      e.printStackTrace(); // debug
    }
  }
Exemple #3
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);
    }
  }