Program: 50338
Project: 50338
Buggy source:
@Override protected int scan(final ReadSet readIndexFilter,final IndexedIdentifier targetIds,final AlignmentWriter writer,final AlignmentTooManyHitsWriter tmhWriter) throws IOException {
  int numAligns=0;
  final ProgressLogger progress=new ProgressLogger(LOG);
  SAMFileReader.setDefaultValidationStringency(SAMFileReader.ValidationStringency.SILENT);
  final InputStream stream="String_Node_Str".equals(inputFile) ? System.in : new FileInputStream(inputFile);
  final SAMFileReader parser=new SAMFileReader(stream);
  boolean hasPaired=false;
  progress.start();
  final SAMRecordIterator samIterator=parser.iterator();
  SamHelper samHelper=new SamHelper();
  samHelper.setQualityEncoding(qualityEncoding);
  numberOfReads=0;
  SAMRecord prevRecord=null;
  final SAMFileHeader fileHeader=parser.getFileHeader();
  if (fileHeader.getSequenceDictionary().isEmpty()) {
    System.err.println("String_Node_Str");
    if (runningFromCommandLine) {
      System.exit(0);
    }
  }
  if (sortedInput) {
    final int numTargets=fileHeader.getSequenceDictionary().size();
    final int[] targetLengths=new int[numTargets];
    for (int i=0; i < numTargets; i++) {
      final SAMSequenceRecord seq=fileHeader.getSequence(i);
      final int targetIndex=getTargetIndex(targetIds,seq.getSequenceName(),thirdPartyInput);
      targetLengths[targetIndex]=seq.getSequenceLength();
    }
    writer.setTargetLengths(targetLengths);
    writer.setSorted(true);
  }
  while (samIterator.hasNext()) {
    samHelper.reset();
    numberOfReads++;
    final SAMRecord samRecord=samIterator.next();
    if (samRecord.getReadUnmappedFlag()) {
      if (debug && LOG.isDebugEnabled()) {
        LOG.debug(String.format("String_Node_Str",samRecord.getReadName()));
      }
      continue;
    }
    if (sortedInput) {
      if (prevRecord != null) {
        final int compare=prevRecord.getAlignmentStart() - samRecord.getAlignmentStart();
        if (compare > 0) {
          final String message=String.format("String_Node_Str",samRecord.toString(),prevRecord.toString());
          System.err.println("String_Node_Str" + message);
          LOG.warn(message);
          System.exit(0);
        }
      }
    }
    prevRecord=samRecord;
    int readMaxOccurence=1;
    final boolean readIsPaired=samRecord.getReadPairedFlag();
    final boolean b=readIsPaired && !samRecord.getReadUnmappedFlag();
    if (b) {
      hasPaired=true;
      readMaxOccurence++;
    }
    final Object xoString=samRecord.getAttribute("String_Node_Str");
    final int numTotalHits=xoString == null ? 1 : hasPaired ? 1 : (Integer)xoString;
    if (hasPaired) {
      readMaxOccurence=readIsPaired ? 2 : 1;
    }
 else {
      readMaxOccurence=numTotalHits;
    }
    final String readName=samRecord.getReadName();
    final int queryIndex=thirdPartyInput ? nameToQueryIndices.getQueryIndex(readName,readMaxOccurence) : Integer.parseInt(readName);
    final int targetIndex=getTargetIndex(targetIds,samRecord.getReferenceName(),thirdPartyInput);
    final int fragmentIndex;
    final int mateFragmentIndex;
    if (readIsPaired) {
      if (samRecord.getFirstOfPairFlag()) {
        fragmentIndex=0;
        mateFragmentIndex=1;
      }
 else {
        fragmentIndex=1;
        mateFragmentIndex=0;
      }
    }
 else {
      fragmentIndex=0;
      mateFragmentIndex=1;
    }
    if (bsmap) {
      final String specifiedReference=(String)samRecord.getAttribute("String_Node_Str");
      final String directions=(String)samRecord.getAttribute("String_Node_Str");
      final boolean reverseStrand=directions.equals("String_Node_Str") || directions.equals("String_Node_Str");
      samHelper.setSourceWithReference(queryIndex,samRecord.getReadString(),samRecord.getBaseQualityString(),specifiedReference,samRecord.getAlignmentStart(),reverseStrand);
    }
 else {
      samHelper.setSource(queryIndex,samRecord.getReadString(),samRecord.getBaseQualityString(),samRecord.getCigarString(),(String)samRecord.getAttribute("String_Node_Str"),samRecord.getAlignmentStart(),samRecord.getReadNegativeStrandFlag());
    }
    int multiplicity=1;
    if (readIndexFilter != null) {
      multiplicity=readIndexFilter.getMultiplicity(queryIndex);
    }
    largestQueryIndex=Math.max(queryIndex,largestQueryIndex);
    smallestQueryIndex=Math.min(queryIndex,smallestQueryIndex);
    final Alignments.AlignmentEntry.Builder currentEntry=Alignments.AlignmentEntry.newBuilder();
    currentEntry.setMultiplicity(multiplicity);
    currentEntry.setQueryIndex(samHelper.getQueryIndex());
    currentEntry.setTargetIndex(targetIndex);
    currentEntry.setPosition(samHelper.getPosition());
    currentEntry.setQueryPosition(samHelper.getQueryPosition());
    currentEntry.setFragmentIndex(fragmentIndex);
    currentEntry.setQueryLength(samHelper.getQueryLength());
    currentEntry.setScore(samHelper.getScore());
    currentEntry.setNumberOfIndels(samHelper.getNumDeletions() + samHelper.getNumInsertions());
    currentEntry.setNumberOfMismatches(samHelper.getNumMisMatches());
    currentEntry.setMatchingReverseStrand(samHelper.isReverseStrand());
    currentEntry.setQueryAlignedLength(samHelper.getQueryAlignedLength());
    currentEntry.setTargetAlignedLength(samHelper.getTargetAlignedLength());
    currentEntry.setPairFlags(samRecord.getFlags());
    currentEntry.setMappingQuality(samRecord.getMappingQuality());
    if (hasPaired) {
      currentEntry.setInsertSize(samRecord.getInferredInsertSize());
    }
    if (readIsPaired) {
      if (!samRecord.getMateUnmappedFlag()) {
        final Alignments.RelatedAlignmentEntry.Builder relatedBuilder=Alignments.RelatedAlignmentEntry.newBuilder();
        final int mateTargetIndex=getTargetIndex(targetIds,samRecord.getMateReferenceName(),thirdPartyInput);
        final int mateAlignmentStart=samRecord.getMateAlignmentStart() - 1;
        relatedBuilder.setFragmentIndex(mateFragmentIndex);
        relatedBuilder.setPosition(mateAlignmentStart);
        relatedBuilder.setTargetIndex(mateTargetIndex);
        currentEntry.setPairAlignmentLink(relatedBuilder);
      }
    }
    for (    final SamSequenceVariation var : samHelper.getSequenceVariations()) {
      appendNewSequenceVariation(currentEntry,var,samHelper.getQueryLength());
      if (debug && LOG.isDebugEnabled()) {
        LOG.debug(String.format("String_Node_Str",var.toString(),queryIndex));
      }
    }
    final Alignments.AlignmentEntry alignmentEntry=currentEntry.build();
    if (qualityFilter.keepEntry(samHelper.getQueryLength(),alignmentEntry)) {
      if (numTotalHits <= mParameter) {
        writer.appendEntry(alignmentEntry);
        numAligns+=multiplicity;
        if (debug && LOG.isDebugEnabled()) {
          LOG.debug(String.format("String_Node_Str",queryIndex));
        }
      }
 else {
        tmhWriter.append(queryIndex,numTotalHits,samHelper.getQueryLength());
        if (debug && LOG.isDebugEnabled()) {
          LOG.debug(String.format("String_Node_Str",queryIndex));
        }
        while (queryIndex == nameToQueryIndices.getQueryIndex(samRecord.getReadName(),0))         ;
      }
    }
 else {
      if (debug && LOG.isDebugEnabled()) {
        LOG.debug(String.format("String_Node_Str",queryIndex));
      }
    }
    progress.lightUpdate();
  }
  samIterator.close();
  if (readIndexFilter != null) {
    writer.putStatistic("String_Node_Str",readIndexFilterFile.getName());
  }
  writer.putStatistic("String_Node_Str",numAligns);
  writer.setNumQueries(Math.max(numberOfReads,numberOfReadsFromCommandLine));
  writer.printStats(System.out);
  final SAMFileHeader samHeader=fileHeader;
  final SAMSequenceDictionary samSequenceDictionary=samHeader.getSequenceDictionary();
  final List<SAMSequenceRecord> samSequenceRecords=samSequenceDictionary.getSequences();
  int targetCount=targetIds.size();
  if (targetIds.size() != 0 && (targetIds.size() != samSequenceRecords.size())) {
    LOG.warn("String_Node_Str" + targetIds.size() + "String_Node_Str"+ samSequenceRecords.size());
  }
  targetCount=Math.max(samSequenceRecords.size(),targetCount);
  final int[] targetLengths=new int[targetCount];
  for (  final SAMSequenceRecord samSequenceRecord : samSequenceRecords) {
    final int index=samSequenceRecord.getSequenceIndex();
    if (debug && LOG.isDebugEnabled()) {
      LOG.debug("String_Node_Str" + samSequenceRecord.getSequenceName() + "String_Node_Str"+ index);
    }
    targetLengths[index]=samSequenceRecord.getSequenceLength();
  }
  writer.setTargetLengths(targetLengths);
  progress.stop();
  return numAligns;
}

Fixed version (ground truth):
@Override protected int scan(final ReadSet readIndexFilter,final IndexedIdentifier targetIds,final AlignmentWriter writer,final AlignmentTooManyHitsWriter tmhWriter) throws IOException {
  int numAligns=0;
  final ProgressLogger progress=new ProgressLogger(LOG);
  SAMFileReader.setDefaultValidationStringency(SAMFileReader.ValidationStringency.SILENT);
  final InputStream stream="String_Node_Str".equals(inputFile) ? System.in : new FileInputStream(inputFile);
  final SAMFileReader parser=new SAMFileReader(stream);
  boolean hasPaired=false;
  progress.start();
  final SAMRecordIterator samIterator=parser.iterator();
  SamHelper samHelper=new SamHelper();
  samHelper.setQualityEncoding(qualityEncoding);
  numberOfReads=0;
  SAMRecord prevRecord=null;
  final SAMFileHeader fileHeader=parser.getFileHeader();
  if (fileHeader.getSequenceDictionary().isEmpty()) {
    System.err.println("String_Node_Str");
    if (runningFromCommandLine) {
      System.exit(0);
    }
  }
  if (sortedInput) {
    final int numTargets=fileHeader.getSequenceDictionary().size();
    final int[] targetLengths=new int[numTargets];
    for (int i=0; i < numTargets; i++) {
      final SAMSequenceRecord seq=fileHeader.getSequence(i);
      final int targetIndex=getTargetIndex(targetIds,seq.getSequenceName(),thirdPartyInput);
      targetLengths[targetIndex]=seq.getSequenceLength();
    }
    writer.setTargetLengths(targetLengths);
    writer.setSorted(true);
  }
  while (samIterator.hasNext()) {
    samHelper.reset();
    numberOfReads++;
    final SAMRecord samRecord=samIterator.next();
    if (samRecord.getReadUnmappedFlag()) {
      if (debug && LOG.isDebugEnabled()) {
        LOG.debug(String.format("String_Node_Str",samRecord.getReadName()));
      }
      continue;
    }
    final int targetIndex=getTargetIndex(targetIds,samRecord.getReferenceName(),thirdPartyInput);
    if (sortedInput) {
      if (prevRecord != null && prevRecord.getReferenceIndex() == targetIndex) {
        final int compare=prevRecord.getAlignmentStart() - samRecord.getAlignmentStart();
        if (compare > 0) {
          final String message=String.format("String_Node_Str",samRecord.toString(),prevRecord.toString());
          System.err.println("String_Node_Str" + message);
          LOG.warn(message);
          System.exit(0);
        }
      }
    }
    prevRecord=samRecord;
    int readMaxOccurence=1;
    final boolean readIsPaired=samRecord.getReadPairedFlag();
    final boolean b=readIsPaired && !samRecord.getReadUnmappedFlag();
    if (b) {
      hasPaired=true;
      readMaxOccurence++;
    }
    final Object xoString=samRecord.getAttribute("String_Node_Str");
    final int numTotalHits=xoString == null ? 1 : hasPaired ? 1 : (Integer)xoString;
    if (hasPaired) {
      readMaxOccurence=readIsPaired ? 2 : 1;
    }
 else {
      readMaxOccurence=numTotalHits;
    }
    final String readName=samRecord.getReadName();
    final int queryIndex=thirdPartyInput ? nameToQueryIndices.getQueryIndex(readName,readMaxOccurence) : Integer.parseInt(readName);
    final int fragmentIndex;
    final int mateFragmentIndex;
    if (readIsPaired) {
      if (samRecord.getFirstOfPairFlag()) {
        fragmentIndex=0;
        mateFragmentIndex=1;
      }
 else {
        fragmentIndex=1;
        mateFragmentIndex=0;
      }
    }
 else {
      fragmentIndex=0;
      mateFragmentIndex=1;
    }
    if (bsmap) {
      final String specifiedReference=(String)samRecord.getAttribute("String_Node_Str");
      final String directions=(String)samRecord.getAttribute("String_Node_Str");
      final boolean reverseStrand=directions.equals("String_Node_Str") || directions.equals("String_Node_Str");
      samHelper.setSourceWithReference(queryIndex,samRecord.getReadString(),samRecord.getBaseQualityString(),specifiedReference,samRecord.getAlignmentStart(),reverseStrand);
    }
 else {
      samHelper.setSource(queryIndex,samRecord.getReadString(),samRecord.getBaseQualityString(),samRecord.getCigarString(),(String)samRecord.getAttribute("String_Node_Str"),samRecord.getAlignmentStart(),samRecord.getReadNegativeStrandFlag());
    }
    int multiplicity=1;
    if (readIndexFilter != null) {
      multiplicity=readIndexFilter.getMultiplicity(queryIndex);
    }
    largestQueryIndex=Math.max(queryIndex,largestQueryIndex);
    smallestQueryIndex=Math.min(queryIndex,smallestQueryIndex);
    final Alignments.AlignmentEntry.Builder currentEntry=Alignments.AlignmentEntry.newBuilder();
    currentEntry.setMultiplicity(multiplicity);
    currentEntry.setQueryIndex(samHelper.getQueryIndex());
    currentEntry.setTargetIndex(targetIndex);
    currentEntry.setPosition(samHelper.getPosition());
    currentEntry.setQueryPosition(samHelper.getQueryPosition());
    currentEntry.setFragmentIndex(fragmentIndex);
    currentEntry.setQueryLength(samHelper.getQueryLength());
    currentEntry.setScore(samHelper.getScore());
    currentEntry.setNumberOfIndels(samHelper.getNumDeletions() + samHelper.getNumInsertions());
    currentEntry.setNumberOfMismatches(samHelper.getNumMisMatches());
    currentEntry.setMatchingReverseStrand(samHelper.isReverseStrand());
    currentEntry.setQueryAlignedLength(samHelper.getQueryAlignedLength());
    currentEntry.setTargetAlignedLength(samHelper.getTargetAlignedLength());
    currentEntry.setPairFlags(samRecord.getFlags());
    currentEntry.setMappingQuality(samRecord.getMappingQuality());
    if (hasPaired) {
      currentEntry.setInsertSize(samRecord.getInferredInsertSize());
    }
    if (readIsPaired) {
      if (!samRecord.getMateUnmappedFlag()) {
        final Alignments.RelatedAlignmentEntry.Builder relatedBuilder=Alignments.RelatedAlignmentEntry.newBuilder();
        final int mateTargetIndex=getTargetIndex(targetIds,samRecord.getMateReferenceName(),thirdPartyInput);
        final int mateAlignmentStart=samRecord.getMateAlignmentStart() - 1;
        relatedBuilder.setFragmentIndex(mateFragmentIndex);
        relatedBuilder.setPosition(mateAlignmentStart);
        relatedBuilder.setTargetIndex(mateTargetIndex);
        currentEntry.setPairAlignmentLink(relatedBuilder);
      }
    }
    for (    final SamSequenceVariation var : samHelper.getSequenceVariations()) {
      appendNewSequenceVariation(currentEntry,var,samHelper.getQueryLength());
      if (debug && LOG.isDebugEnabled()) {
        LOG.debug(String.format("String_Node_Str",var.toString(),queryIndex));
      }
    }
    final Alignments.AlignmentEntry alignmentEntry=currentEntry.build();
    if (qualityFilter.keepEntry(samHelper.getQueryLength(),alignmentEntry)) {
      if (numTotalHits <= mParameter) {
        writer.appendEntry(alignmentEntry);
        numAligns+=multiplicity;
        if (debug && LOG.isDebugEnabled()) {
          LOG.debug(String.format("String_Node_Str",queryIndex));
        }
      }
 else {
        tmhWriter.append(queryIndex,numTotalHits,samHelper.getQueryLength());
        if (debug && LOG.isDebugEnabled()) {
          LOG.debug(String.format("String_Node_Str",queryIndex));
        }
        while (queryIndex == nameToQueryIndices.getQueryIndex(samRecord.getReadName(),0))         ;
      }
    }
 else {
      if (debug && LOG.isDebugEnabled()) {
        LOG.debug(String.format("String_Node_Str",queryIndex));
      }
    }
    progress.lightUpdate();
  }
  samIterator.close();
  if (readIndexFilter != null) {
    writer.putStatistic("String_Node_Str",readIndexFilterFile.getName());
  }
  writer.putStatistic("String_Node_Str",numAligns);
  writer.setNumQueries(Math.max(numberOfReads,numberOfReadsFromCommandLine));
  writer.printStats(System.out);
  final SAMFileHeader samHeader=fileHeader;
  final SAMSequenceDictionary samSequenceDictionary=samHeader.getSequenceDictionary();
  final List<SAMSequenceRecord> samSequenceRecords=samSequenceDictionary.getSequences();
  int targetCount=targetIds.size();
  if (targetIds.size() != 0 && (targetIds.size() != samSequenceRecords.size())) {
    LOG.warn("String_Node_Str" + targetIds.size() + "String_Node_Str"+ samSequenceRecords.size());
  }
  targetCount=Math.max(samSequenceRecords.size(),targetCount);
  final int[] targetLengths=new int[targetCount];
  for (  final SAMSequenceRecord samSequenceRecord : samSequenceRecords) {
    final int index=samSequenceRecord.getSequenceIndex();
    if (debug && LOG.isDebugEnabled()) {
      LOG.debug("String_Node_Str" + samSequenceRecord.getSequenceName() + "String_Node_Str"+ index);
    }
    targetLengths[index]=samSequenceRecord.getSequenceLength();
  }
  writer.setTargetLengths(targetLengths);
  progress.stop();
  return numAligns;
}