Program: 30670
Project: 30670
Buggy source:
/** 
 * Merges the alignment from the map file with the non-aligned records from the source BAM file.
 */
public void mergeAlignment(){
  SAMFileReader unmappedSam=null;
  if (this.unmappedBamFile != null) {
    unmappedSam=new SAMFileReader(IoUtil.openFileForReading(this.unmappedBamFile));
  }
  if (unmappedSam != null)   header.setReadGroups(unmappedSam.getFileHeader().getReadGroups());
  int aligned=0;
  int unmapped=0;
  final PeekableIterator<SAMRecord> alignedIterator=new PeekableIterator(getQuerynameSortedAlignedRecords());
  final SortingCollection<SAMRecord> alignmentSorted=SortingCollection.newInstance(SAMRecord.class,new BAMRecordCodec(header),new SAMRecordCoordinateComparator(),MAX_RECORDS_IN_RAM);
  final ClippedPairFixer pairFixer=new ClippedPairFixer(alignmentSorted,header);
  final CloseableIterator<SAMRecord> unmappedIterator=unmappedSam.iterator();
  SAMRecord nextAligned=alignedIterator.hasNext() ? alignedIterator.next() : null;
  SAMRecord lastAligned=null;
  final UnmappedReadSorter unmappedSorter=new UnmappedReadSorter(unmappedIterator);
  while (unmappedSorter.hasNext()) {
    final SAMRecord rec=unmappedSorter.next();
    rec.setReadName(cleanReadName(rec.getReadName()));
    if (nextAligned != null && rec.getReadName().compareTo(nextAligned.getReadName()) > 0) {
      throw new PicardException("String_Node_Str" + nextAligned.getReadName() + "String_Node_Str"+ rec.getReadName()+ "String_Node_Str");
    }
    rec.setHeader(header);
    if (isMatch(rec,nextAligned)) {
      if (!ignoreAlignment(nextAligned)) {
        setValuesFromAlignment(rec,nextAligned);
        if (programRecord != null) {
          rec.setAttribute(ReservedTagConstants.PROGRAM_GROUP_ID,programRecord.getProgramGroupId());
        }
        aligned++;
      }
      nextAligned=alignedIterator.hasNext() ? alignedIterator.next() : null;
    }
 else {
      unmapped++;
    }
    final boolean eitherReadMapped=!rec.getReadUnmappedFlag() || (rec.getReadPairedFlag() && !rec.getMateUnmappedFlag());
    if (eitherReadMapped || !alignedReadsOnly) {
      pairFixer.add(rec);
    }
  }
  unmappedIterator.close();
  alignedIterator.close();
  final SAMFileWriter writer=new SAMFileWriterFactory().makeBAMWriter(header,true,this.targetBamFile);
  int count=0;
  CloseableIterator<SAMRecord> it=alignmentSorted.iterator();
  while (it.hasNext()) {
    SAMRecord rec=it.next();
    if (!rec.getReadUnmappedFlag()) {
      if (refSeq != null) {
        byte referenceBases[]=refSeq.get(rec.getReferenceIndex()).getBases();
        rec.setAttribute(SAMTag.NM.name(),SequenceUtil.calculateSamNmTag(rec,referenceBases,0,bisulfiteSequence));
        rec.setAttribute(SAMTag.UQ.name(),SequenceUtil.sumQualitiesOfMismatches(rec,referenceBases,0,bisulfiteSequence));
      }
    }
    writer.addAlignment(rec);
    if (++count % 1000000 == 0) {
      log.info(count + "String_Node_Str" + targetBamFile.getName());
    }
  }
  writer.close();
  log.info("String_Node_Str" + aligned + "String_Node_Str"+ unmapped+ "String_Node_Str");
}

Fixed version (ground truth):
/** 
 * Merges the alignment from the map file with the non-aligned records from the source BAM file.
 */
public void mergeAlignment(){
  SAMFileReader unmappedSam=null;
  if (this.unmappedBamFile != null) {
    unmappedSam=new SAMFileReader(IoUtil.openFileForReading(this.unmappedBamFile));
  }
  if (unmappedSam != null)   header.setReadGroups(unmappedSam.getFileHeader().getReadGroups());
  int aligned=0;
  int unmapped=0;
  final PeekableIterator<SAMRecord> alignedIterator=new PeekableIterator(getQuerynameSortedAlignedRecords());
  final SortingCollection<SAMRecord> alignmentSorted=SortingCollection.newInstance(SAMRecord.class,new BAMRecordCodec(header),new SAMRecordCoordinateComparator(),MAX_RECORDS_IN_RAM);
  final ClippedPairFixer pairFixer=new ClippedPairFixer(alignmentSorted,header,alignedReadsOnly);
  final CloseableIterator<SAMRecord> unmappedIterator=unmappedSam.iterator();
  SAMRecord nextAligned=alignedIterator.hasNext() ? alignedIterator.next() : null;
  SAMRecord lastAligned=null;
  final UnmappedReadSorter unmappedSorter=new UnmappedReadSorter(unmappedIterator);
  while (unmappedSorter.hasNext()) {
    final SAMRecord rec=unmappedSorter.next();
    rec.setReadName(cleanReadName(rec.getReadName()));
    if (nextAligned != null && rec.getReadName().compareTo(nextAligned.getReadName()) > 0) {
      throw new PicardException("String_Node_Str" + nextAligned.getReadName() + "String_Node_Str"+ rec.getReadName()+ "String_Node_Str");
    }
    rec.setHeader(header);
    if (isMatch(rec,nextAligned)) {
      if (!ignoreAlignment(nextAligned)) {
        setValuesFromAlignment(rec,nextAligned);
        if (programRecord != null) {
          rec.setAttribute(ReservedTagConstants.PROGRAM_GROUP_ID,programRecord.getProgramGroupId());
        }
        aligned++;
      }
      nextAligned=alignedIterator.hasNext() ? alignedIterator.next() : null;
    }
 else {
      unmapped++;
    }
    final boolean eitherReadMapped=!rec.getReadUnmappedFlag() || (rec.getReadPairedFlag() && !rec.getMateUnmappedFlag());
    if (eitherReadMapped || !alignedReadsOnly) {
      pairFixer.add(rec);
    }
  }
  unmappedIterator.close();
  alignedIterator.close();
  final SAMFileWriter writer=new SAMFileWriterFactory().makeBAMWriter(header,true,this.targetBamFile);
  int count=0;
  CloseableIterator<SAMRecord> it=alignmentSorted.iterator();
  while (it.hasNext()) {
    SAMRecord rec=it.next();
    if (!rec.getReadUnmappedFlag()) {
      if (refSeq != null) {
        byte referenceBases[]=refSeq.get(rec.getReferenceIndex()).getBases();
        rec.setAttribute(SAMTag.NM.name(),SequenceUtil.calculateSamNmTag(rec,referenceBases,0,bisulfiteSequence));
        rec.setAttribute(SAMTag.UQ.name(),SequenceUtil.sumQualitiesOfMismatches(rec,referenceBases,0,bisulfiteSequence));
      }
    }
    writer.addAlignment(rec);
    if (++count % 1000000 == 0) {
      log.info(count + "String_Node_Str" + targetBamFile.getName());
    }
  }
  writer.close();
  log.info("String_Node_Str" + aligned + "String_Node_Str"+ (alignedReadsOnly ? 0 : unmapped)+ "String_Node_Str");
}