Program: 30661
Project: 30661
Buggy source:
protected int doWork(){
  IoUtil.assertFileIsReadable(REFERENCE_SEQUENCE);
  IoUtil.assertFileIsReadable(INPUT);
  IoUtil.assertFileIsWritable(OUTPUT);
  IoUtil.assertFileIsWritable(CHART_OUTPUT);
  if (SUMMARY_OUTPUT != null)   IoUtil.assertFileIsWritable(SUMMARY_OUTPUT);
  int[] windowsByGc=new int[101];
  int[] readsByGc=new int[101];
  long[] basesByGc=new long[101];
  long[] errorsByGc=new long[101];
  SAMFileReader sam=new SAMFileReader(INPUT);
  PeekableIterator<SAMRecord> iterator=new PeekableIterator<SAMRecord>(sam.iterator());
  ReferenceSequenceFile referenceFile=ReferenceSequenceFileFactory.getReferenceSequenceFile(REFERENCE_SEQUENCE);
{
    SAMSequenceDictionary referenceDictionary=referenceFile.getSequenceDictionary();
    SAMSequenceDictionary samFileDictionary=sam.getFileHeader().getSequenceDictionary();
    if (referenceDictionary != null && samFileDictionary != null) {
      SequenceUtil.assertSequenceDictionariesEqual(referenceDictionary,samFileDictionary);
    }
  }
  ReferenceSequence ref=null;
  while ((ref=referenceFile.nextSequence()) != null) {
    final byte[] refBases=ref.getBases();
    StringUtil.toUpperCase(refBases);
    final int refLength=refBases.length;
    final int lastWindowStart=refLength - WINDOW_SIZE;
    byte[] gc=null;
    for (int countSeq=0; iterator.hasNext() && iterator.peek().getReferenceIndex() == ref.getContigIndex(); countSeq++) {
      SAMRecord rec=iterator.next();
      if (countSeq == 0) {
        gc=calculateAllGcs(refBases,windowsByGc,lastWindowStart);
      }
      if (!rec.getReadPairedFlag() || rec.getFirstOfPairFlag())       ++this.totalClusters;
      if (!rec.getReadUnmappedFlag()) {
        int pos=rec.getReadNegativeStrandFlag() ? rec.getAlignmentEnd() - WINDOW_SIZE : rec.getAlignmentStart();
        ++this.totalAlignedReads;
        if (pos > 0) {
          int windowGc=gc[pos];
          if (windowGc >= 0) {
            ++readsByGc[windowGc];
            basesByGc[windowGc]+=rec.getReadLength();
            errorsByGc[windowGc]+=SequenceUtil.countMismatches(rec,refBases);
          }
        }
      }
    }
    LOG.info("String_Node_Str" + ref.getName());
  }
  while (iterator.hasNext()) {
    SAMRecord rec=iterator.next();
    if (!rec.getReadPairedFlag() || rec.getFirstOfPairFlag())     ++this.totalClusters;
  }
  MetricsFile<GcBiasDetailMetrics,?> metricsFile=getMetricsFile();
  double totalWindows=sum(windowsByGc);
  double totalReads=sum(readsByGc);
  double meanReadsPerWindow=totalReads / totalWindows;
  double minimumWindowsToCountInSummary=totalWindows * this.MINIMUM_GENOME_FRACTION;
  for (int i=0; i < windowsByGc.length; ++i) {
    if (windowsByGc[i] == 0)     continue;
    GcBiasDetailMetrics m=new GcBiasDetailMetrics();
    m.GC=i;
    m.WINDOWS=windowsByGc[i];
    m.READ_STARTS=readsByGc[i];
    if (errorsByGc[i] > 0)     m.MEAN_BASE_QUALITY=QualityUtil.getPhredScoreFromObsAndErrors(basesByGc[i],errorsByGc[i]);
    m.NORMALIZED_COVERAGE=(m.READ_STARTS / (double)m.WINDOWS) / meanReadsPerWindow;
    m.ERROR_BAR_WIDTH=(Math.sqrt(m.READ_STARTS) / (double)m.WINDOWS) / meanReadsPerWindow;
    metricsFile.addMetric(m);
  }
  metricsFile.write(OUTPUT);
  if (SUMMARY_OUTPUT != null) {
    MetricsFile<GcBiasSummaryMetrics,?> summaryMetricsFile=getMetricsFile();
    GcBiasSummaryMetrics summary=new GcBiasSummaryMetrics();
    summary.WINDOW_SIZE=this.WINDOW_SIZE;
    summary.TOTAL_CLUSTERS=this.totalClusters;
    summary.ALIGNED_READS=this.totalAlignedReads;
    summary.LOW_GC_BIAS=calculateSummaryMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,0,33);
    summary.MID_GC_BIAS=calculateSummaryMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,34,66);
    summary.HIGH_GC_BIAS=calculateSummaryMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,67,100);
    summary.TOTAL_BIAS=calculateSummaryMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,0,100);
    summary.JAFFE_BIAS_METRIC=calculateJaffeMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,0,100);
    summaryMetricsFile.addMetric(summary);
    if (SUMMARY_OUTPUT != null)     summaryMetricsFile.write(SUMMARY_OUTPUT);
  }
  NumberFormat fmt=NumberFormat.getIntegerInstance();
  fmt.setGroupingUsed(true);
  String subtitle="String_Node_Str" + fmt.format(this.totalClusters) + "String_Node_Str"+ fmt.format(this.totalAlignedReads);
  RExecutor.executeFromClasspath(R_SCRIPT,OUTPUT.getAbsolutePath(),CHART_OUTPUT.getAbsolutePath(),INPUT.getName().replace("String_Node_Str","String_Node_Str").replace("String_Node_Str","String_Node_Str"),subtitle,String.valueOf(WINDOW_SIZE));
  return 0;
}

Fixed version (ground truth):
protected int doWork(){
  IoUtil.assertFileIsReadable(REFERENCE_SEQUENCE);
  IoUtil.assertFileIsReadable(INPUT);
  IoUtil.assertFileIsWritable(OUTPUT);
  IoUtil.assertFileIsWritable(CHART_OUTPUT);
  if (SUMMARY_OUTPUT != null)   IoUtil.assertFileIsWritable(SUMMARY_OUTPUT);
  int[] windowsByGc=new int[101];
  int[] readsByGc=new int[101];
  long[] basesByGc=new long[101];
  long[] errorsByGc=new long[101];
  SAMFileReader sam=new SAMFileReader(INPUT);
  PeekableIterator<SAMRecord> iterator=new PeekableIterator<SAMRecord>(sam.iterator());
  ReferenceSequenceFile referenceFile=ReferenceSequenceFileFactory.getReferenceSequenceFile(REFERENCE_SEQUENCE);
{
    SAMSequenceDictionary referenceDictionary=referenceFile.getSequenceDictionary();
    SAMSequenceDictionary samFileDictionary=sam.getFileHeader().getSequenceDictionary();
    if (referenceDictionary != null && samFileDictionary != null) {
      SequenceUtil.assertSequenceDictionariesEqual(referenceDictionary,samFileDictionary);
    }
  }
  ReferenceSequence ref=null;
  while ((ref=referenceFile.nextSequence()) != null) {
    final byte[] refBases=ref.getBases();
    StringUtil.toUpperCase(refBases);
    final int refLength=refBases.length;
    final int lastWindowStart=refLength - WINDOW_SIZE;
    byte[] gc=calculateAllGcs(refBases,windowsByGc,lastWindowStart);
    while (iterator.hasNext() && iterator.peek().getReferenceIndex() == ref.getContigIndex()) {
      SAMRecord rec=iterator.next();
      if (!rec.getReadPairedFlag() || rec.getFirstOfPairFlag())       ++this.totalClusters;
      if (!rec.getReadUnmappedFlag()) {
        int pos=rec.getReadNegativeStrandFlag() ? rec.getAlignmentEnd() - WINDOW_SIZE : rec.getAlignmentStart();
        ++this.totalAlignedReads;
        if (pos > 0) {
          int windowGc=gc[pos];
          if (windowGc >= 0) {
            ++readsByGc[windowGc];
            basesByGc[windowGc]+=rec.getReadLength();
            errorsByGc[windowGc]+=SequenceUtil.countMismatches(rec,refBases);
          }
        }
      }
    }
    LOG.info("String_Node_Str" + ref.getName());
  }
  while (iterator.hasNext()) {
    SAMRecord rec=iterator.next();
    if (!rec.getReadPairedFlag() || rec.getFirstOfPairFlag())     ++this.totalClusters;
  }
  MetricsFile<GcBiasDetailMetrics,?> metricsFile=getMetricsFile();
  double totalWindows=sum(windowsByGc);
  double totalReads=sum(readsByGc);
  double meanReadsPerWindow=totalReads / totalWindows;
  double minimumWindowsToCountInSummary=totalWindows * this.MINIMUM_GENOME_FRACTION;
  for (int i=0; i < windowsByGc.length; ++i) {
    if (windowsByGc[i] == 0)     continue;
    GcBiasDetailMetrics m=new GcBiasDetailMetrics();
    m.GC=i;
    m.WINDOWS=windowsByGc[i];
    m.READ_STARTS=readsByGc[i];
    if (errorsByGc[i] > 0)     m.MEAN_BASE_QUALITY=QualityUtil.getPhredScoreFromObsAndErrors(basesByGc[i],errorsByGc[i]);
    m.NORMALIZED_COVERAGE=(m.READ_STARTS / (double)m.WINDOWS) / meanReadsPerWindow;
    m.ERROR_BAR_WIDTH=(Math.sqrt(m.READ_STARTS) / (double)m.WINDOWS) / meanReadsPerWindow;
    metricsFile.addMetric(m);
  }
  metricsFile.write(OUTPUT);
  if (SUMMARY_OUTPUT != null) {
    MetricsFile<GcBiasSummaryMetrics,?> summaryMetricsFile=getMetricsFile();
    GcBiasSummaryMetrics summary=new GcBiasSummaryMetrics();
    summary.WINDOW_SIZE=this.WINDOW_SIZE;
    summary.TOTAL_CLUSTERS=this.totalClusters;
    summary.ALIGNED_READS=this.totalAlignedReads;
    summary.LOW_GC_BIAS=calculateSummaryMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,0,33);
    summary.MID_GC_BIAS=calculateSummaryMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,34,66);
    summary.HIGH_GC_BIAS=calculateSummaryMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,67,100);
    summary.TOTAL_BIAS=calculateSummaryMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,0,100);
    summary.JAFFE_BIAS_METRIC=calculateJaffeMetric(metricsFile.getMetrics(),minimumWindowsToCountInSummary,0,100);
    summaryMetricsFile.addMetric(summary);
    if (SUMMARY_OUTPUT != null)     summaryMetricsFile.write(SUMMARY_OUTPUT);
  }
  NumberFormat fmt=NumberFormat.getIntegerInstance();
  fmt.setGroupingUsed(true);
  String subtitle="String_Node_Str" + fmt.format(this.totalClusters) + "String_Node_Str"+ fmt.format(this.totalAlignedReads);
  RExecutor.executeFromClasspath(R_SCRIPT,OUTPUT.getAbsolutePath(),CHART_OUTPUT.getAbsolutePath(),INPUT.getName().replace("String_Node_Str","String_Node_Str").replace("String_Node_Str","String_Node_Str"),subtitle,String.valueOf(WINDOW_SIZE));
  return 0;
}