Program: 30655
Project: 30655
Buggy source:
/** 
 * Attempts to calculate the HS penalty incurred by the library in order to get 80% of target bases (in non-zero-covered targets) to a specific target coverage (e.g. 20X).
 * @param coverageGoal the desired coverage target (e.g. 20X)
 * @return the hs penalty - a multiplier that tells if you want, e.g. 20X coverage, then you willneed to produce this many PF aligned bases per target bases in your design!
 */
private double calculateHsPenalty(final int coverageGoal){
  if (this.metrics.HS_LIBRARY_SIZE == null)   return 0;
  final double meanCoverage=this.ON_TARGET_FROM_PAIR_BASES / (double)this.metrics.TARGET_TERRITORY;
  final double fold80=this.metrics.FOLD_80_BASE_PENALTY;
  final long hsLibrarySize=this.metrics.HS_LIBRARY_SIZE;
  final long pairs=this.PF_SELECTED_PAIRS;
  final long uniquePairs=this.PF_SELECTED_UNIQUE_PAIRS;
  final double onTargetPct=(double)this.metrics.ON_TARGET_BASES / (double)this.metrics.PF_UQ_BASES_ALIGNED;
  final double uniquePairGoalMultiplier=(coverageGoal / meanCoverage) * fold80;
  double pairMultiplier=uniquePairGoalMultiplier;
  double increment=1;
  boolean goingUp=uniquePairGoalMultiplier >= 1;
  double finalPairMultiplier=-1;
  for (int i=0; i < 10000; ++i) {
    double uniquePairMultiplier=DuplicationMetrics.estimateRoi(hsLibrarySize,pairMultiplier,pairs,uniquePairs);
    if (Math.abs(uniquePairMultiplier - uniquePairGoalMultiplier) <= 0.01) {
      finalPairMultiplier=pairMultiplier;
      break;
    }
 else     if ((uniquePairMultiplier > uniquePairGoalMultiplier && goingUp) || (uniquePairMultiplier < uniquePairGoalMultiplier && !goingUp)) {
      increment/=2;
      goingUp=!goingUp;
    }
    pairMultiplier+=(goingUp ? increment : -increment);
  }
  if (finalPairMultiplier == -1) {
    return -1;
  }
 else {
    double uniqueFraction=(uniquePairs * uniquePairGoalMultiplier) / (pairs * finalPairMultiplier);
    return (1 / uniqueFraction) * fold80 * (1 / onTargetPct);
  }
}

Fixed version (ground truth):
/** 
 * Attempts to calculate the HS penalty incurred by the library in order to get 80% of target bases (in non-zero-covered targets) to a specific target coverage (e.g. 20X).
 * @param coverageGoal the desired coverage target (e.g. 20X)
 * @return the hs penalty - a multiplier that tells if you want, e.g. 20X coverage, then you willneed to produce this many PF aligned bases per target bases in your design!
 */
private double calculateHsPenalty(final int coverageGoal){
  if (this.metrics.HS_LIBRARY_SIZE == null)   return 0;
  final double meanCoverage=this.ON_TARGET_FROM_PAIR_BASES / (double)this.metrics.TARGET_TERRITORY;
  final double fold80=this.metrics.FOLD_80_BASE_PENALTY;
  final long hsLibrarySize=this.metrics.HS_LIBRARY_SIZE;
  final long pairs=this.PF_SELECTED_PAIRS;
  final long uniquePairs=this.PF_SELECTED_UNIQUE_PAIRS;
  final double onTargetPct=(double)this.metrics.ON_TARGET_BASES / (double)this.metrics.PF_UQ_BASES_ALIGNED;
  final double uniquePairGoalMultiplier=(coverageGoal / meanCoverage) * fold80;
  double pairMultiplier=uniquePairGoalMultiplier;
  double increment=1;
  boolean goingUp=uniquePairGoalMultiplier >= 1;
  double finalPairMultiplier=-1;
  for (int i=0; i < 10000; ++i) {
    double uniquePairMultiplier=DuplicationMetrics.estimateRoi(hsLibrarySize,pairMultiplier,pairs,uniquePairs);
    if (Math.abs(uniquePairMultiplier - uniquePairGoalMultiplier) / uniquePairGoalMultiplier <= 0.001) {
      finalPairMultiplier=pairMultiplier;
      break;
    }
 else     if ((uniquePairMultiplier > uniquePairGoalMultiplier && goingUp) || (uniquePairMultiplier < uniquePairGoalMultiplier && !goingUp)) {
      increment/=2;
      goingUp=!goingUp;
    }
    pairMultiplier+=(goingUp ? increment : -increment);
  }
  if (finalPairMultiplier == -1) {
    return -1;
  }
 else {
    double uniqueFraction=(uniquePairs * uniquePairGoalMultiplier) / (pairs * finalPairMultiplier);
    return (1 / uniqueFraction) * fold80 * (1 / onTargetPct);
  }
}