Program: 141161
Project: 141161
Buggy source:
/** 
 * Count the number of alignments for all the features of the annotation file.
 * @param samFile SAM file that contains alignments.
 * @param gffFile annotation file.
 * @param outFile output file.
 * @param stranded strand to consider.
 * @param overlapMode overlap mode to consider.
 * @param removeAmbiguousCases if true : ambiguous cases will be removed.
 * @param featureType annotation feature type to consider.
 * @param attributeId annotation attribute id to consider.
 * @param quiet if true : suppress progress report and warnings.
 * @param minAverageQual minimum value for alignment quality.
 * @param samOutFile output SAM file annotating each line with its assignmentto a feature or a special counter (as an optional field with tag 'XF').
 * @param reporter Reporter object.
 * @param counterGroup counter group for the Reporter object.
 * @throws EoulsanException
 * @throws IOException
 * @throws BadBioEntryException
 */
private static void countReadsInFeatures(final DataFile samFile,final DataFile gffFile,final DataFile outFile,final StrandUsage stranded,final OverlapMode overlapMode,final boolean removeAmbiguousCases,final String featureType,final String attributeId,final boolean quiet,final int minAverageQual,final DataFile samOutFile,final DataFile genomeDescFile,final Reporter reporter,String counterGroup) throws EoulsanException, IOException, BadBioEntryException {
  final GenomicArray<String> features=new GenomicArray<String>(GenomeDescription.load(genomeDescFile.open()));
  final Map<String,Integer> counts=new HashMap<>();
  final Writer writer=FileUtils.createBufferedWriter(outFile.create());
  boolean pairedEnd=false;
  HTSeqUtils.storeAnnotation(features,gffFile.open(),featureType,stranded,attributeId,counts);
  if (counts.size() == 0) {
    writer.close();
    throw new EoulsanException("String_Node_Str" + featureType + "String_Node_Str");
  }
  List<GenomicInterval> ivSeq=new ArrayList<GenomicInterval>();
  final SAMFileReader inputSam=new SAMFileReader(samFile.open());
  pairedEnd=isPairedData(samFile.open());
  int empty=0;
  int ambiguous=0;
  int notaligned=0;
  int lowqual=0;
  int nonunique=0;
  SAMRecord sam1=null, sam2=null;
  for (  final SAMRecord samRecord : inputSam) {
    reporter.incrCounter(counterGroup,ExpressionCounters.TOTAL_ALIGNMENTS_COUNTER.counterName(),1);
    if (!pairedEnd) {
      ivSeq.clear();
      if (samRecord.getReadUnmappedFlag()) {
        notaligned++;
        continue;
      }
      if (samRecord.getAttribute("String_Node_Str") != null && samRecord.getIntegerAttribute("String_Node_Str") > 1) {
        nonunique++;
        continue;
      }
      if (samRecord.getMappingQuality() < minAverageQual) {
        lowqual++;
        continue;
      }
      ivSeq.addAll(HTSeqUtils.addIntervals(samRecord,stranded));
    }
 else {
      if (sam1 != null && sam2 != null) {
        sam1=null;
        sam2=null;
        ivSeq.clear();
      }
      if (samRecord.getFirstOfPairFlag())       sam1=samRecord;
 else       sam2=samRecord;
      if (sam1 == null || sam2 == null)       continue;
      if (!sam1.getReadName().equals(sam2.getReadName())) {
        sam1=sam2;
        sam2=null;
        continue;
      }
      if (sam1 != null && !sam1.getReadUnmappedFlag()) {
        ivSeq.addAll(HTSeqUtils.addIntervals(sam1,stranded));
      }
      if (sam2 != null && !sam2.getReadUnmappedFlag()) {
        ivSeq.addAll(HTSeqUtils.addIntervals(sam2,stranded));
      }
      if (sam1.getReadUnmappedFlag() && sam2.getReadUnmappedFlag()) {
        notaligned++;
        continue;
      }
      if ((sam1.getAttribute("String_Node_Str") != null && sam1.getIntegerAttribute("String_Node_Str") > 1) || (sam2.getAttribute("String_Node_Str") != null && sam2.getIntegerAttribute("String_Node_Str") > 1)) {
        nonunique++;
        continue;
      }
      if (sam1.getMappingQuality() < minAverageQual || sam2.getMappingQuality() < minAverageQual) {
        lowqual++;
        continue;
      }
    }
    Set<String> fs=null;
    fs=HTSeqUtils.featuresOverlapped(ivSeq,features,overlapMode,stranded);
    if (fs == null)     fs=Collections.emptySet();
switch (fs.size()) {
case 0:
      empty++;
    break;
case 1:
  final String id1=fs.iterator().next();
counts.put(id1,counts.get(id1) + 1);
break;
default :
if (removeAmbiguousCases) {
ambiguous++;
}
 else {
for (String id2 : fs) counts.put(id2,counts.get(id2) + 1);
}
break;
}
}
inputSam.close();
final List<String> keysSorted=new ArrayList<String>(counts.keySet());
Collections.sort(keysSorted);
writer.write("String_Node_Str");
for (String key : keysSorted) {
writer.write(key + "String_Node_Str" + counts.get(key)+ "String_Node_Str");
}
reporter.incrCounter(counterGroup,ExpressionCounters.EMPTY_ALIGNMENTS_COUNTER.counterName(),empty);
reporter.incrCounter(counterGroup,ExpressionCounters.AMBIGUOUS_ALIGNMENTS_COUNTER.counterName(),ambiguous);
reporter.incrCounter(counterGroup,ExpressionCounters.LOW_QUAL_ALIGNMENTS_COUNTER.counterName(),lowqual);
reporter.incrCounter(counterGroup,ExpressionCounters.NOT_ALIGNED_ALIGNMENTS_COUNTER.counterName(),notaligned);
reporter.incrCounter(counterGroup,ExpressionCounters.NOT_UNIQUE_ALIGNMENTS_COUNTER.counterName(),nonunique);
reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),empty + ambiguous + lowqual+ notaligned+ nonunique);
writer.close();
}

Fixed version (ground truth):
/** 
 * Count the number of alignments for all the features of the annotation file.
 * @param samFile SAM file that contains alignments.
 * @param gffFile annotation file.
 * @param outFile output file.
 * @param stranded strand to consider.
 * @param overlapMode overlap mode to consider.
 * @param removeAmbiguousCases if true : ambiguous cases will be removed.
 * @param featureType annotation feature type to consider.
 * @param attributeId annotation attribute id to consider.
 * @param quiet if true : suppress progress report and warnings.
 * @param minAverageQual minimum value for alignment quality.
 * @param samOutFile output SAM file annotating each line with its assignmentto a feature or a special counter (as an optional field with tag 'XF').
 * @param reporter Reporter object.
 * @param counterGroup counter group for the Reporter object.
 * @throws EoulsanException
 * @throws IOException
 * @throws BadBioEntryException
 */
private static void countReadsInFeatures(final DataFile samFile,final DataFile gffFile,final DataFile outFile,final StrandUsage stranded,final OverlapMode overlapMode,final boolean removeAmbiguousCases,final String featureType,final String attributeId,final boolean quiet,final int minAverageQual,final DataFile samOutFile,final DataFile genomeDescFile,final Reporter reporter,String counterGroup) throws EoulsanException, IOException, BadBioEntryException {
  final GenomicArray<String> features=new GenomicArray<String>(GenomeDescription.load(genomeDescFile.open()));
  final Map<String,Integer> counts=new HashMap<>();
  final Writer writer=FileUtils.createBufferedWriter(outFile.create());
  boolean pairedEnd=false;
  HTSeqUtils.storeAnnotation(features,gffFile.open(),featureType,stranded,attributeId,counts);
  if (counts.size() == 0) {
    writer.close();
    throw new EoulsanException("String_Node_Str" + featureType + "String_Node_Str");
  }
  List<GenomicInterval> ivSeq=new ArrayList<GenomicInterval>();
  final SAMFileReader inputSam=new SAMFileReader(samFile.open());
  pairedEnd=isPairedData(samFile.open());
  int empty=0;
  int ambiguous=0;
  int notaligned=0;
  int lowqual=0;
  int nonunique=0;
  SAMRecord sam1=null, sam2=null;
  for (  final SAMRecord samRecord : inputSam) {
    reporter.incrCounter(counterGroup,ExpressionCounters.TOTAL_ALIGNMENTS_COUNTER.counterName(),1);
    if (!pairedEnd) {
      ivSeq.clear();
      if (samRecord.getReadUnmappedFlag()) {
        notaligned++;
        continue;
      }
      if (samRecord.getAttribute("String_Node_Str") != null && samRecord.getIntegerAttribute("String_Node_Str") > 1) {
        nonunique++;
        continue;
      }
      if (samRecord.getMappingQuality() < minAverageQual) {
        lowqual++;
        continue;
      }
      ivSeq.addAll(HTSeqUtils.addIntervals(samRecord,stranded));
    }
 else {
      if (sam1 != null && sam2 != null) {
        sam1=null;
        sam2=null;
        ivSeq.clear();
      }
      if (samRecord.getFirstOfPairFlag())       sam1=samRecord;
 else       sam2=samRecord;
      if (sam1 == null || sam2 == null)       continue;
      if (!sam1.getReadName().equals(sam2.getReadName())) {
        sam1=sam2;
        sam2=null;
        continue;
      }
      if (!sam1.getReadUnmappedFlag()) {
        ivSeq.addAll(HTSeqUtils.addIntervals(sam1,stranded));
      }
      if (!sam2.getReadUnmappedFlag()) {
        ivSeq.addAll(HTSeqUtils.addIntervals(sam2,stranded));
      }
      if (sam1.getReadUnmappedFlag() && sam2.getReadUnmappedFlag()) {
        notaligned++;
        continue;
      }
      if ((sam1.getAttribute("String_Node_Str") != null && sam1.getIntegerAttribute("String_Node_Str") > 1) || (sam2.getAttribute("String_Node_Str") != null && sam2.getIntegerAttribute("String_Node_Str") > 1)) {
        nonunique++;
        continue;
      }
      if (sam1.getMappingQuality() < minAverageQual || sam2.getMappingQuality() < minAverageQual) {
        lowqual++;
        continue;
      }
    }
    Set<String> fs=null;
    fs=HTSeqUtils.featuresOverlapped(ivSeq,features,overlapMode,stranded);
    if (fs == null)     fs=Collections.emptySet();
switch (fs.size()) {
case 0:
      empty++;
    break;
case 1:
  final String id1=fs.iterator().next();
counts.put(id1,counts.get(id1) + 1);
break;
default :
if (removeAmbiguousCases) {
ambiguous++;
}
 else {
for (String id2 : fs) counts.put(id2,counts.get(id2) + 1);
}
break;
}
}
inputSam.close();
final List<String> keysSorted=new ArrayList<String>(counts.keySet());
Collections.sort(keysSorted);
writer.write("String_Node_Str");
for (String key : keysSorted) {
writer.write(key + "String_Node_Str" + counts.get(key)+ "String_Node_Str");
}
reporter.incrCounter(counterGroup,ExpressionCounters.EMPTY_ALIGNMENTS_COUNTER.counterName(),empty);
reporter.incrCounter(counterGroup,ExpressionCounters.AMBIGUOUS_ALIGNMENTS_COUNTER.counterName(),ambiguous);
reporter.incrCounter(counterGroup,ExpressionCounters.LOW_QUAL_ALIGNMENTS_COUNTER.counterName(),lowqual);
reporter.incrCounter(counterGroup,ExpressionCounters.NOT_ALIGNED_ALIGNMENTS_COUNTER.counterName(),notaligned);
reporter.incrCounter(counterGroup,ExpressionCounters.NOT_UNIQUE_ALIGNMENTS_COUNTER.counterName(),nonunique);
reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),empty + ambiguous + lowqual+ notaligned+ nonunique);
writer.close();
}