Program: 141251
Project: 141251
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 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 File samFile,final InputStream gffFile,final File outFile,final StrandUsage stranded,final OverlapMode overlapMode,final String featureType,final String attributeId,final boolean quiet,final int minAverageQual,final File samOutFile,Reporter reporter,String counterGroup) throws EoulsanException, IOException, BadBioEntryException {
  final GenomicArray<String> features=new GenomicArray<String>();
  final Map<String,Integer> counts=Utils.newHashMap();
  Writer writer=new FileWriter(outFile);
  boolean pairedEnd=false;
  final GFFReader gffReader=new GFFReader(gffFile);
  for (  final GFFEntry gff : gffReader) {
    if (featureType.equals(gff.getType())) {
      final String featureId=gff.getAttributeValue(attributeId);
      if (featureId == null)       throw new EoulsanException("String_Node_Str" + featureType + "String_Node_Str"+ attributeId+ "String_Node_Str");
      if ((stranded == StrandUsage.YES || stranded == StrandUsage.REVERSE) && '.' == gff.getStrand())       throw new EoulsanException("String_Node_Str" + featureType + "String_Node_Str"+ "String_Node_Str");
      features.addEntry(new GenomicInterval(gff,stranded.isSaveStrandInfo()),featureId);
      counts.put(featureId,0);
    }
  }
  gffReader.throwException();
  gffReader.close();
  if (counts.size() == 0)   throw new EoulsanException("String_Node_Str" + featureType + "String_Node_Str");
  List<GenomicInterval> ivSeq=new ArrayList<GenomicInterval>();
  final SAMFileReader inputSam=new SAMFileReader(samFile);
  final SAMFileReader input=new SAMFileReader(samFile);
  SAMRecordIterator samIterator=input.iterator();
  SAMRecord firstRecord=samIterator.next();
  if (firstRecord.getReadPairedFlag())   pairedEnd=true;
  input.close();
  int empty=0;
  int ambiguous=0;
  int notaligned=0;
  int lowqual=0;
  int nonunique=0;
  int i=0;
  SAMRecord sam1=null, sam2=null;
  for (  final SAMRecord samRecord : inputSam) {
    reporter.incrCounter(counterGroup,ExpressionCounters.TOTAL_ALIGNMENTS_COUNTER.counterName(),1);
    i++;
    if (i % 1000000 == 0)     System.out.println(i + "String_Node_Str");
    if (!pairedEnd) {
      ivSeq.clear();
      if (samRecord.getReadUnmappedFlag()) {
        notaligned++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
      if (samRecord.getAttribute("String_Node_Str") != null && samRecord.getIntegerAttribute("String_Node_Str") > 1) {
        nonunique++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
      if (samRecord.getMappingQuality() < minAverageQual) {
        lowqual++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
      ivSeq.addAll(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(addIntervals(sam1,stranded));
      }
      if (sam2 != null && !sam2.getReadUnmappedFlag()) {
        ivSeq.addAll(addIntervals(sam2,stranded));
      }
      if (sam1.getReadUnmappedFlag() && sam2.getReadUnmappedFlag()) {
        notaligned++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        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++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
      if (sam1.getMappingQuality() < minAverageQual || sam2.getMappingQuality() < minAverageQual) {
        lowqual++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
    }
    Set<String> fs=null;
    fs=featuresOverlapped(ivSeq,features,overlapMode,stranded);
    if (fs == null)     fs=new HashSet<String>();
switch (fs.size()) {
case 0:
      empty++;
    reporter.incrCounter(counterGroup,ExpressionCounters.UNMAPPED_READS_COUNTER.counterName(),1);
  break;
case 1:
final String id=fs.iterator().next();
counts.put(id,counts.get(id) + 1);
break;
default :
ambiguous++;
reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
break;
}
}
inputSam.close();
final List<String> keysSorted=new ArrayList<String>(counts.keySet());
Collections.sort(keysSorted);
for (String key : keysSorted) {
writer.write(key + "String_Node_Str" + counts.get(key)+ "String_Node_Str");
}
writer.write(String.format("String_Node_Str",empty));
writer.write(String.format("String_Node_Str",ambiguous));
writer.write(String.format("String_Node_Str",lowqual));
writer.write(String.format("String_Node_Str",notaligned));
writer.write(String.format("String_Node_Str",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 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 File samFile,final InputStream gffFile,final File outFile,final StrandUsage stranded,final OverlapMode overlapMode,final String featureType,final String attributeId,final boolean quiet,final int minAverageQual,final File samOutFile,Reporter reporter,String counterGroup) throws EoulsanException, IOException, BadBioEntryException {
  final GenomicArray<String> features=new GenomicArray<String>();
  final Map<String,Integer> counts=Utils.newHashMap();
  Writer writer=new FileWriter(outFile);
  boolean pairedEnd=false;
  final GFFReader gffReader=new GFFReader(gffFile);
  for (  final GFFEntry gff : gffReader) {
    if (featureType.equals(gff.getType())) {
      final String featureId=gff.getAttributeValue(attributeId);
      if (featureId == null)       throw new EoulsanException("String_Node_Str" + featureType + "String_Node_Str"+ attributeId+ "String_Node_Str");
      if ((stranded == StrandUsage.YES || stranded == StrandUsage.REVERSE) && '.' == gff.getStrand())       throw new EoulsanException("String_Node_Str" + featureType + "String_Node_Str"+ "String_Node_Str");
      features.addEntry(new GenomicInterval(gff,stranded.isSaveStrandInfo()),featureId);
      counts.put(featureId,0);
    }
  }
  gffReader.throwException();
  gffReader.close();
  if (counts.size() == 0)   throw new EoulsanException("String_Node_Str" + featureType + "String_Node_Str");
  List<GenomicInterval> ivSeq=new ArrayList<GenomicInterval>();
  final SAMFileReader inputSam=new SAMFileReader(samFile);
  final SAMFileReader input=new SAMFileReader(samFile);
  SAMRecordIterator samIterator=input.iterator();
  SAMRecord firstRecord=samIterator.next();
  if (firstRecord.getReadPairedFlag())   pairedEnd=true;
  input.close();
  int empty=0;
  int ambiguous=0;
  int notaligned=0;
  int lowqual=0;
  int nonunique=0;
  int i=0;
  SAMRecord sam1=null, sam2=null;
  for (  final SAMRecord samRecord : inputSam) {
    reporter.incrCounter(counterGroup,ExpressionCounters.TOTAL_ALIGNMENTS_COUNTER.counterName(),1);
    i++;
    if (i % 1000000 == 0)     System.out.println(i + "String_Node_Str");
    if (!pairedEnd) {
      ivSeq.clear();
      if (samRecord.getReadUnmappedFlag()) {
        notaligned++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
      if (samRecord.getAttribute("String_Node_Str") != null && samRecord.getIntegerAttribute("String_Node_Str") > 1) {
        nonunique++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
      if (samRecord.getMappingQuality() < minAverageQual) {
        lowqual++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
      ivSeq.addAll(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(addIntervals(sam1,stranded));
      }
      if (sam2 != null && !sam2.getReadUnmappedFlag()) {
        ivSeq.addAll(addIntervals(sam2,stranded));
      }
      if (sam1.getReadUnmappedFlag() && sam2.getReadUnmappedFlag()) {
        notaligned++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        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++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
      if (sam1.getMappingQuality() < minAverageQual || sam2.getMappingQuality() < minAverageQual) {
        lowqual++;
        reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
        continue;
      }
    }
    Set<String> fs=null;
    fs=featuresOverlapped(ivSeq,features,overlapMode,stranded);
    if (fs == null)     fs=new HashSet<String>();
switch (fs.size()) {
case 0:
      empty++;
    reporter.incrCounter(counterGroup,ExpressionCounters.UNMAPPED_READS_COUNTER.counterName(),1);
  break;
case 1:
final String id=fs.iterator().next();
counts.put(id,counts.get(id) + 1);
break;
default :
ambiguous++;
reporter.incrCounter(counterGroup,ExpressionCounters.ELIMINATED_READS_COUNTER.counterName(),1);
break;
}
}
inputSam.close();
final List<String> keysSorted=new ArrayList<String>(counts.keySet());
Collections.sort(keysSorted);
for (String key : keysSorted) {
writer.write(key + "String_Node_Str" + counts.get(key)+ "String_Node_Str");
}
writer.write("String_Node_Str" + empty + '\n');
writer.write("String_Node_Str" + ambiguous + '\n');
writer.write("String_Node_Str" + lowqual + '\n');
writer.write("String_Node_Str" + notaligned + '\n');
writer.write("String_Node_Str" + nonunique + '\n');
writer.close();
}