Program: 50307
Project: 50307
Buggy source:
/** 
 * Build an ExportableAlignmentEntryData object from an alignment entry.
 * @param alignmentEntry     a Goby alignment entry
 * @param actualReadsSrc     the actual reads from the original reads file. Only provided during tests.
 * @param actualQualitiesSrc the actual qualities from the original reads file. Only provided during tests.
 */
public void buildFrom(final Alignments.AlignmentEntry alignmentEntry,final CharList actualReadsSrc,final ByteList actualQualitiesSrc){
  reset();
  reverseStrand=alignmentEntry.getMatchingReverseStrand();
  setActualReads(actualReadsSrc,reverseStrand);
  setActualQuals(actualQualitiesSrc,reverseStrand);
  startClip=alignmentEntry.getQueryPosition();
  queryLength=alignmentEntry.getQueryLength();
  queryAlignedLength=alignmentEntry.getQueryAlignedLength();
  targetAlignedLength=alignmentEntry.getTargetAlignedLength();
  endClip=queryLength - queryAlignedLength - startClip;
  final int startPosition=alignmentEntry.getPosition();
  this.alignmentEntry=alignmentEntry;
  if (hasReadGroups) {
    final int readOriginIndex=alignmentEntry.getReadOriginIndex();
    final Alignments.ReadOriginInfo info=readOriginInfo.getInfo(readOriginIndex);
    if (info == null) {
      invalid=true;
      invalidMessage.append(String.format("String_Node_Str",readOriginIndex));
      if (debug) {
        LOG.debug(invalidMessage.toString());
      }
    }
 else {
      readGroup=info.getOriginId();
    }
  }
  int numInserts=0;
  for (  final Alignments.SequenceVariation seqvar : alignmentEntry.getSequenceVariationsList()) {
    final String froms=seqvar.getFrom();
    final String tos=seqvar.getTo();
    final int fromsLength=froms.length();
    final int tosLength=tos.length();
    if (fromsLength != tosLength) {
      invalid=true;
      invalidMessage.append("String_Node_Str").append(alignmentEntry.getQueryIndex()).append("String_Node_Str");
      if (debug) {
        LOG.debug(invalidMessage.toString());
      }
      return;
    }
    for (int i=0; i < froms.length(); i++) {
      final char from=froms.charAt(i);
      final char to=tos.charAt(i);
      if (from == '-' && to == '-') {
        invalid=true;
        invalidMessage.append("String_Node_Str").append(alignmentEntry.getQueryIndex()).append("String_Node_Str");
        if (debug) {
          LOG.debug(invalidMessage.toString());
        }
        return;
      }
      if (from == '-') {
        numInserts+=1;
      }
    }
  }
  final boolean predefinedQuals=alignmentEntry.hasReadQualityScores();
  final int endOfLoop=targetAlignedLength + startClip + endClip+ numInserts;
  final int targetIndex=alignmentEntry.getTargetIndex();
  final char[] predefStartClips=alignmentEntry.hasSoftClippedBasesLeft() ? alignmentEntry.getSoftClippedBasesLeft().toCharArray() : null;
  final char[] predefEndClips=alignmentEntry.hasSoftClippedBasesRight() ? alignmentEntry.getSoftClippedBasesRight().toCharArray() : null;
  for (int i=0; i < endOfLoop; i++) {
    final char base=genome.get(targetIndex,i + startPosition - startClip);
    if (i < startClip) {
      if (predefStartClips != null) {
        final char clipBase=predefStartClips[i];
        if (clipBase == '=') {
          readBases.add(base);
        }
 else {
          readBases.add(clipBase);
        }
      }
 else {
        readBases.add('N');
      }
    }
 else {
      readBases.add(base);
    }
    refBases.add(base);
    if (!predefinedQuals) {
      qualities.add(UNKNOWN_MAPPING_VALUE);
    }
  }
  if (predefinedQuals) {
    for (    final byte value : alignmentEntry.getReadQualityScores().toByteArray()) {
      qualities.add(value);
    }
    hasQualities=true;
  }
  if (!alignmentEntry.getSequenceVariationsList().isEmpty()) {
    if (debug) {
      LOG.debug("String_Node_Str");
      LOG.debug("String_Node_Str" + toString());
    }
  }
  for (  final Alignments.SequenceVariation seqvar : alignmentEntry.getSequenceVariationsList()) {
    if (debug) {
      LOG.debug(seqVarToString(seqvar));
    }
    final String froms=seqvar.getFrom();
    final String tos=seqvar.getTo();
    final int startRefPosition=seqvar.getPosition();
    final byte[] toQuals=seqvar.hasToQuality() ? seqvar.getToQuality().toByteArray() : null;
    for (int i=0; i < froms.length(); i++) {
      final char from=froms.charAt(i);
      final char to=tos.charAt(i);
      final Byte toQual=toQuals == null ? null : (byte)qualityEncoding.phredQualityScoreToAsciiEncoding(toQuals[i]);
      final int refPosition=startRefPosition + i - 1;
      if (from == '-') {
        refBases.add(refPosition + 1,from);
        readBases.add(refPosition + 1,to);
        if (toQual != null) {
          qualities.add(refPosition + 1,toQual);
          hasQualities=true;
        }
      }
 else       if (to == '-') {
        if (refBases.get(refPosition) != from) {
          invalid=false;
          invalidMessage.append("String_Node_Str").append(alignmentEntry.getQueryIndex()).append("String_Node_Str").append(from).append("String_Node_Str").append(refBases.get(refPosition)).append('\n');
          if (debug) {
            LOG.debug(invalidMessage.toString());
          }
          if (invalid) {
            return;
          }
        }
        readBases.set(refPosition,to);
        deleteQualityIndexes.addFirst(refPosition);
      }
 else {
        if (refBases.get(refPosition) != from) {
          invalid=false;
          invalidMessage.append("String_Node_Str").append(alignmentEntry.getQueryIndex()).append("String_Node_Str").append(from).append("String_Node_Str").append(refBases.get(refPosition)).append('\n');
          if (debug) {
            LOG.debug(invalidMessage.toString());
          }
          if (invalid) {
            return;
          }
        }
        readBases.set(refPosition,to);
        if (toQual != null) {
          qualities.set(refPosition,toQual);
          hasQualities=true;
        }
      }
    }
  }
  for (  final int deleteQualityIndex : deleteQualityIndexes) {
    qualities.remove(deleteQualityIndex);
  }
  if (numInserts > 0) {
    refBases.size(refBases.size() - numInserts);
    readBases.size(readBases.size() - numInserts);
    qualities.size(qualities.size() - numInserts);
  }
  if (endClip > 0) {
    final int readSize=readBases.size();
    for (int i=0; i < endClip; i++) {
      char clipBase;
      if (predefEndClips == null) {
        clipBase='N';
      }
 else {
        clipBase=predefEndClips[i];
        if (clipBase == '=') {
          clipBase='N';
        }
      }
      readBases.set(readSize - i - 1,clipBase);
    }
  }
  for (  final char readBase : readBases) {
    if (readBase != '-') {
      readBasesOriginal.append(readBase);
    }
  }
  observeReadRefDifferences();
  endTargetPositionZeroBased=alignmentEntry.getPosition() + startClip + alignmentEntry.getTargetAlignedLength();
  if (debug) {
    LOG.debug("String_Node_Str" + toString());
  }
}

Fixed version (ground truth):
/** 
 * Build an ExportableAlignmentEntryData object from an alignment entry.
 * @param alignmentEntry     a Goby alignment entry
 * @param actualReadsSrc     the actual reads from the original reads file. Only provided during tests.
 * @param actualQualitiesSrc the actual qualities from the original reads file. Only provided during tests.
 */
public void buildFrom(final Alignments.AlignmentEntry alignmentEntry,final CharList actualReadsSrc,final ByteList actualQualitiesSrc){
  reset();
  reverseStrand=alignmentEntry.getMatchingReverseStrand();
  setActualReads(actualReadsSrc,reverseStrand);
  setActualQuals(actualQualitiesSrc,reverseStrand);
  startClip=alignmentEntry.getQueryPosition();
  queryLength=alignmentEntry.getQueryLength();
  queryAlignedLength=alignmentEntry.getQueryAlignedLength();
  targetAlignedLength=alignmentEntry.getTargetAlignedLength();
  endClip=queryLength - queryAlignedLength - startClip;
  final int startPosition=alignmentEntry.getPosition();
  this.alignmentEntry=alignmentEntry;
  if (hasReadGroups) {
    final int readOriginIndex=alignmentEntry.getReadOriginIndex();
    final Alignments.ReadOriginInfo info=readOriginInfo.getInfo(readOriginIndex);
    if (info == null) {
      invalid=true;
      invalidMessage.append(String.format("String_Node_Str",readOriginIndex));
      if (debug) {
        LOG.debug(invalidMessage.toString());
      }
    }
 else {
      readGroup=info.getOriginId();
    }
  }
  int numInserts=0;
  for (  final Alignments.SequenceVariation seqvar : alignmentEntry.getSequenceVariationsList()) {
    final String froms=seqvar.getFrom();
    final String tos=seqvar.getTo();
    final int fromsLength=froms.length();
    final int tosLength=tos.length();
    if (fromsLength != tosLength) {
      invalid=true;
      invalidMessage.append("String_Node_Str").append(alignmentEntry.getQueryIndex()).append("String_Node_Str");
      if (debug) {
        LOG.debug(invalidMessage.toString());
      }
      return;
    }
    for (int i=0; i < froms.length(); i++) {
      final char from=froms.charAt(i);
      final char to=tos.charAt(i);
      if (from == '-' && to == '-') {
        invalid=true;
        invalidMessage.append("String_Node_Str").append(alignmentEntry.getQueryIndex()).append("String_Node_Str");
        if (debug) {
          LOG.debug(invalidMessage.toString());
        }
        return;
      }
      if (from == '-') {
        numInserts+=1;
      }
    }
  }
  final boolean predefinedQuals=alignmentEntry.hasReadQualityScores();
  final int endOfLoop=targetAlignedLength + startClip + endClip+ numInserts;
  final int targetIndex=alignmentEntry.getTargetIndex();
  final char[] predefStartClips=alignmentEntry.hasSoftClippedBasesLeft() ? alignmentEntry.getSoftClippedBasesLeft().toCharArray() : null;
  final char[] predefEndClips=alignmentEntry.hasSoftClippedBasesRight() ? alignmentEntry.getSoftClippedBasesRight().toCharArray() : null;
  for (int i=0; i < endOfLoop; i++) {
    final int genomePosition=i + startPosition - startClip;
    final char base=genomePosition >= 0 ? genome.get(targetIndex,i + startPosition - startClip) : 'N';
    if (i < startClip) {
      if (predefStartClips != null) {
        final char clipBase=predefStartClips[i];
        if (clipBase == '=') {
          readBases.add(base);
        }
 else {
          readBases.add(clipBase);
        }
      }
 else {
        readBases.add('N');
      }
    }
 else {
      readBases.add(base);
    }
    refBases.add(base);
    if (!predefinedQuals) {
      qualities.add(UNKNOWN_MAPPING_VALUE);
    }
  }
  if (predefinedQuals) {
    for (    final byte value : alignmentEntry.getReadQualityScores().toByteArray()) {
      qualities.add(value);
    }
    hasQualities=true;
  }
  if (!alignmentEntry.getSequenceVariationsList().isEmpty()) {
    if (debug) {
      LOG.debug("String_Node_Str");
      LOG.debug("String_Node_Str" + toString());
    }
  }
  for (  final Alignments.SequenceVariation seqvar : alignmentEntry.getSequenceVariationsList()) {
    if (debug) {
      LOG.debug(seqVarToString(seqvar));
    }
    final String froms=seqvar.getFrom();
    final String tos=seqvar.getTo();
    final int startRefPosition=seqvar.getPosition();
    final byte[] toQuals=seqvar.hasToQuality() ? seqvar.getToQuality().toByteArray() : null;
    for (int i=0; i < froms.length(); i++) {
      final char from=froms.charAt(i);
      final char to=tos.charAt(i);
      final Byte toQual=toQuals == null ? null : (byte)qualityEncoding.phredQualityScoreToAsciiEncoding(toQuals[i]);
      final int refPosition=startRefPosition + i - 1;
      if (from == '-') {
        refBases.add(refPosition + 1,from);
        readBases.add(refPosition + 1,to);
        if (toQual != null) {
          qualities.add(refPosition + 1,toQual);
          hasQualities=true;
        }
      }
 else       if (to == '-') {
        if (refBases.get(refPosition) != from) {
          invalid=false;
          invalidMessage.append("String_Node_Str").append(alignmentEntry.getQueryIndex()).append("String_Node_Str").append(from).append("String_Node_Str").append(refBases.get(refPosition)).append('\n');
          if (debug) {
            LOG.debug(invalidMessage.toString());
          }
          if (invalid) {
            return;
          }
        }
        readBases.set(refPosition,to);
        deleteQualityIndexes.addFirst(refPosition);
      }
 else {
        if (refBases.get(refPosition) != from) {
          invalid=false;
          invalidMessage.append("String_Node_Str").append(alignmentEntry.getQueryIndex()).append("String_Node_Str").append(from).append("String_Node_Str").append(refBases.get(refPosition)).append('\n');
          if (debug) {
            LOG.debug(invalidMessage.toString());
          }
          if (invalid) {
            return;
          }
        }
        readBases.set(refPosition,to);
        if (toQual != null) {
          qualities.set(refPosition,toQual);
          hasQualities=true;
        }
      }
    }
  }
  for (  final int deleteQualityIndex : deleteQualityIndexes) {
    qualities.remove(deleteQualityIndex);
  }
  if (numInserts > 0) {
    refBases.size(refBases.size() - numInserts);
    readBases.size(readBases.size() - numInserts);
    qualities.size(qualities.size() - numInserts);
  }
  if (endClip > 0) {
    final int readSize=readBases.size();
    for (int i=0; i < endClip; i++) {
      char clipBase;
      if (predefEndClips == null) {
        readBases.set(readSize - i - 1,'N');
      }
 else {
        readBases.set(readSize - i - 1,predefEndClips[i]);
      }
    }
  }
  for (  final char readBase : readBases) {
    if (readBase != '-') {
      readBasesOriginal.append(readBase);
    }
  }
  observeReadRefDifferences();
  endTargetPositionZeroBased=alignmentEntry.getPosition() + startClip + targetAlignedLength;
  if (debug) {
    LOG.debug("String_Node_Str" + toString());
  }
}