Program: 50494
Project: 50494
Buggy source:
/** 
 * Interpret the CIGAR string and MD SAM attribute and reconstruct the reference sequence given the read sequence. The reference passed as a argument is cleared before appending bases.
 * @param CIGAR             The CIGAR string.
 * @param mdAttribute       The SAM MD attribute
 * @param readSequence      The sequence of the read.
 * @param referenceSequence The sequence of the reference that will be reconstructed.
 * @return alignedReadStartPosition The position on the read that starts to align.
 */
public static int produceReferenceSequence(String CIGAR,String mdAttribute,MutableString readSequence,MutableString readPostInsertions,MutableString referenceSequence){
  Pattern matchPattern=attributeMD_pattern;
  referenceSequence.setLength(0);
  readPostInsertions.setLength(0);
  int end=0;
  int positionInReadSequence=0;
  int[] positionAdjustment=new int[readSequence.length()];
  int readStartAlignedPosition=processInsertionsOnly(CIGAR,mdAttribute,readSequence,readPostInsertions,positionAdjustment);
  Matcher matchMatcher=matchPattern.matcher(mdAttribute);
  int currentAdjustment=0;
  while (matchMatcher.find(end)) {
    String matchChars=matchMatcher.group(1);
    String variationChars=matchMatcher.group(2);
    if (matchChars != null) {
      int matchLength=Integer.parseInt(matchChars);
      int regionAdjustment=currentAdjustment;
      for (int i=positionInReadSequence; i < matchLength; i++) {
        regionAdjustment+=positionAdjustment[i];
      }
      final CharSequence matchingSequence=readPostInsertions.subSequence(positionInReadSequence,positionInReadSequence + matchLength + regionAdjustment);
      referenceSequence.append(matchingSequence);
      positionInReadSequence+=matchLength;
    }
    if (variationChars != null) {
      if (variationChars.charAt(0) == '^') {
        int mutationLength=variationChars.length() - 1;
        final String toAppend=variationChars.substring(1);
        referenceSequence.append(toAppend);
        System.out.println("String_Node_Str" + variationChars + "String_Node_Str"+ toAppend);
        for (int i=0; i < mutationLength; ++i) {
          readSequence.insert(positionInReadSequence,'-');
          readPostInsertions.insert(positionInReadSequence,'-');
        }
        positionInReadSequence+=mutationLength;
      }
 else {
        int mutationLength=variationChars.length();
        referenceSequence.append(variationChars);
        positionInReadSequence+=mutationLength;
      }
    }
    end=matchMatcher.end();
  }
  return readStartAlignedPosition;
}

Fixed version (ground truth):
/** 
 * Interpret the CIGAR string and MD SAM attribute and reconstruct the reference sequence given the read sequence. The reference passed as a argument is cleared before appending bases.
 * @param CIGAR             The CIGAR string.
 * @param mdAttribute       The SAM MD attribute
 * @param readSequence      The sequence of the read.
 * @param referenceSequence The sequence of the reference that will be reconstructed.
 * @return alignedReadStartPosition The position on the read that starts to align.
 */
public static int produceReferenceSequence(String CIGAR,String mdAttribute,MutableString readSequence,MutableString readPostInsertions,MutableString referenceSequence){
  Pattern matchPattern=attributeMD_pattern;
  referenceSequence.setLength(0);
  readPostInsertions.setLength(0);
  int end=0;
  int positionInReadSequence=0;
  IntList positionAdjustment=new IntArrayList();
  positionAdjustment.size(readSequence.length());
  int readStartAlignedPosition=processInsertionsOnly(CIGAR,mdAttribute,readSequence,readPostInsertions,positionAdjustment);
  Matcher matchMatcher=matchPattern.matcher(mdAttribute);
  int currentAdjustment=0;
  while (matchMatcher.find(end)) {
    String matchChars=matchMatcher.group(1);
    String variationChars=matchMatcher.group(2);
    if (matchChars != null) {
      int matchLength=Integer.parseInt(matchChars);
      int regionAdjustment=currentAdjustment;
      for (int i=positionInReadSequence; i < matchLength; i++) {
        regionAdjustment+=positionAdjustment.getInt(i);
      }
      final CharSequence matchingSequence=readPostInsertions.subSequence(positionInReadSequence,positionInReadSequence + matchLength + regionAdjustment);
      referenceSequence.append(matchingSequence);
      positionInReadSequence+=matchLength;
    }
    if (variationChars != null) {
      if (variationChars.charAt(0) == '^') {
        int mutationLength=variationChars.length() - 1;
        final String toAppend=variationChars.substring(1);
        referenceSequence.append(toAppend);
        for (int i=0; i < mutationLength; ++i) {
          readSequence.insert(positionInReadSequence,'-');
          readPostInsertions.insert(positionInReadSequence,'-');
        }
        positionInReadSequence+=mutationLength;
      }
 else {
        int mutationLength=variationChars.length();
        referenceSequence.append(variationChars);
        positionInReadSequence+=mutationLength;
      }
    }
    end=matchMatcher.end();
  }
  return readStartAlignedPosition;
}