Program: 80613
Project: 80613
Buggy source:
/** 
 * main execution code block expects alignments in SAM/BAM format read in alignment line by line, filter by alignment quality and mapping location determine if an alignment is false positive or not. outputs false positive alignments by type of sequence changes in the perturbed genome and a statistical report in json format
 * @param args command line arguments
 */
public void run(String[] args){
  if (!parseArguments(args)) {
    return;
  }
  OutputClass outputBlob=new OutputClass();
  outputBlob.setParams(new CompareParams(bamFilenames.get(0).substring(0,Math.min(64,bamFilenames.get(0).length())),wiggle,bedFilename));
  outputBlob.setStats(new MapRatioRecordSum());
  final SAMFileWriterFactory writerFactory=new SAMFileWriterFactory();
  final SamReaderFactory factory=SamReaderFactory.makeDefault().enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS,SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS).validationStringency(ValidationStringency.LENIENT);
  final Map<BlockType,SAMFileWriter> fpWriters=new EnumMap<>(BlockType.class);
  try (final PrintWriter jsonWriter=new PrintWriter(outPrefix + "String_Node_Str","String_Node_Str");final SAMFileWriter tumFPWriter=writerFactory.makeBAMWriter(factory.getFileHeader(new File(bamFilenames.get(0))),false,new File(outPrefix + "String_Node_Str"));final SAMFileWriter fpWriter=writerFactory.makeBAMWriter(factory.getFileHeader(new File(bamFilenames.get(0))),false,new File(outPrefix + "String_Node_Str"))){
    for (    final BlockType blockType : BlockType.values()) {
      if (blockType != BlockType.UNKNOWN) {
        fpWriters.put(blockType,writerFactory.makeBAMWriter(factory.getFileHeader(new File(bamFilenames.get(0))),false,new File(outPrefix + "String_Node_Str" + blockType.getShortName()+ "String_Node_Str")));
      }
    }
    ReadMap readMap=readMapFile != null ? new ReadMap(readMapFile) : null;
    final BedFile intersector=(bedFilename != null && new File(bedFilename).isFile()) ? new BedFile(bedFilename) : null;
    int numReads=0;
    for (    String filename : bamFilenames) {
      log.info("String_Node_Str" + filename);
      final SamReader reader=factory.open(new File(filename));
      for (      SAMRecord rec : reader) {
        if (!useNonPrimary && (rec.getNotPrimaryAlignmentFlag() || rec.getSupplementaryAlignmentFlag())) {
          continue;
        }
        numReads++;
        if (numReads % 100000 == 0) {
          log.info("String_Node_Str" + numReads + "String_Node_Str");
        }
        String name=rec.getReadName();
        int pair_idx=rec.getReadPairedFlag() ? getPairIdx(rec.getFirstOfPairFlag()) : 0;
        log.trace("String_Node_Str" + name);
        final Collection<GenomeLocation> trueLoci=readMap != null ? readMap.getReadMapRecord(name).getUnclippedStarts(pair_idx) : new SimulatedRead(name).getLocs(pair_idx);
        if (!isContainedInBed(intersector,trueLoci,rec)) {
          continue;
        }
        boolean isTrueUnmapped;
        Set<EventTypesForStats> features=EnumSet.noneOf(EventTypesForStats.class);
        features.add(EventTypesForStats.All);
        isTrueUnmapped=true;
        if (!trueLoci.isEmpty()) {
          for (          GenomeLocation loc : trueLoci) {
            final BlockType feat=loc.feature;
            features.add(EventTypesForStats.valueOf(feat.getLongName()));
            isTrueUnmapped&=!feat.isMappable();
          }
        }
        if (isTrueUnmapped) {
          features.add(EventTypesForStats.True_Unmapped);
        }
 else {
          outputBlob.getStats().incStat(features,-1,StatsNamespace.T);
        }
        boolean unmapped=rec.getReadUnmappedFlag();
        int mappingQuality=unmapped ? MAPQ_UNMAPPED : rec.getMappingQuality();
        StatsNamespace validationStatus=StatsNamespace.TP;
        if (unmapped) {
          validationStatus=isTrueUnmapped ? StatsNamespace.TN : StatsNamespace.FN;
        }
 else {
          boolean closeAln=false;
          if (isTrueUnmapped) {
            if (getIdentity(rec) <= identityThreshold && mappingQuality > mapqCutoff) {
              tumFPWriter.addAlignment(rec);
            }
          }
 else {
            final GenomeLocation mappedLocation=new GenomeLocation(new ChrString(rec.getReferenceName()),rec.getUnclippedStart());
            for (            GenomeLocation loc : trueLoci) {
              closeAln|=loc.feature.isMappable() && loc.isClose(mappedLocation,wiggle);
            }
            if (!closeAln) {
              if (getIdentity(rec) <= identityThreshold && mappingQuality > mapqCutoff) {
                fpWriter.addAlignment(rec);
                validationStatus=StatsNamespace.FP;
                for (                final BlockType blockType : BlockType.values()) {
                  if (fpWriters.containsKey(blockType) && features.contains(EventTypesForStats.valueOf(blockType.getLongName()))) {
                    fpWriters.get(blockType).addAlignment(rec);
                  }
                }
              }
            }
          }
        }
        outputBlob.getStats().incStat(features,mappingQuality,validationStatus);
      }
      reader.close();
    }
    log.info("String_Node_Str" + numReads);
    log.info("String_Node_Str");
    log.info(outputBlob.getStats());
    ObjectMapper mapper=new ObjectMapper();
    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET,false);
    String jsonStr="String_Node_Str";
    try {
      jsonStr=mapper.writeValueAsString(outputBlob);
    }
 catch (    JsonProcessingException e) {
      e.printStackTrace();
      System.exit(1);
    }
    jsonWriter.print(jsonStr);
    if (htmlFile != null) {
      FileUtils.writeStringToFile(new File(outPrefix + "String_Node_Str"),JSONInserter.insertJSON(FileUtils.readFileToString(htmlFile),jsonStr));
    }
    for (    final BlockType blockType : BlockType.values()) {
      if (blockType != BlockType.UNKNOWN) {
        fpWriters.get(blockType).close();
      }
    }
    fpWriters.values().stream().forEach(SAMFileWriter::close);
  }
 catch (  IOException e) {
    e.printStackTrace();
    System.exit(1);
  }
  log.info("String_Node_Str");
}

Fixed version (ground truth):
/** 
 * main execution code block expects alignments in SAM/BAM format read in alignment line by line, filter by alignment quality and mapping location determine if an alignment is false positive or not. outputs false positive alignments by type of sequence changes in the perturbed genome and a statistical report in json format
 * @param args command line arguments
 */
public void run(String[] args){
  if (!parseArguments(args)) {
    return;
  }
  OutputClass outputBlob=new OutputClass();
  outputBlob.setParams(new CompareParams(bamFilenames.get(0).substring(0,Math.min(64,bamFilenames.get(0).length())),wiggle,bedFilename,identityThreshold));
  outputBlob.setStats(new MapRatioRecordSum());
  final SAMFileWriterFactory writerFactory=new SAMFileWriterFactory();
  final SamReaderFactory factory=SamReaderFactory.makeDefault().enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS,SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS).validationStringency(ValidationStringency.LENIENT);
  final Map<BlockType,SAMFileWriter> fpWriters=new EnumMap<>(BlockType.class);
  try (final PrintWriter jsonWriter=new PrintWriter(outPrefix + "String_Node_Str","String_Node_Str");final SAMFileWriter tumFPWriter=writerFactory.makeBAMWriter(factory.getFileHeader(new File(bamFilenames.get(0))),false,new File(outPrefix + "String_Node_Str"));final SAMFileWriter fpWriter=writerFactory.makeBAMWriter(factory.getFileHeader(new File(bamFilenames.get(0))),false,new File(outPrefix + "String_Node_Str"))){
    for (    final BlockType blockType : BlockType.values()) {
      if (blockType != BlockType.UNKNOWN) {
        fpWriters.put(blockType,writerFactory.makeBAMWriter(factory.getFileHeader(new File(bamFilenames.get(0))),false,new File(outPrefix + "String_Node_Str" + blockType.getShortName()+ "String_Node_Str")));
      }
    }
    ReadMap readMap=readMapFile != null ? new ReadMap(readMapFile) : null;
    final BedFile intersector=(bedFilename != null && new File(bedFilename).isFile()) ? new BedFile(bedFilename) : null;
    int numReads=0;
    for (    String filename : bamFilenames) {
      log.info("String_Node_Str" + filename);
      final SamReader reader=factory.open(new File(filename));
      for (      SAMRecord rec : reader) {
        if (!useNonPrimary && (rec.getNotPrimaryAlignmentFlag() || rec.getSupplementaryAlignmentFlag())) {
          continue;
        }
        numReads++;
        if (numReads % 100000 == 0) {
          log.info("String_Node_Str" + numReads + "String_Node_Str");
        }
        String name=rec.getReadName();
        int pair_idx=rec.getReadPairedFlag() ? getPairIdx(rec.getFirstOfPairFlag()) : 0;
        log.trace("String_Node_Str" + name);
        final Collection<GenomeLocation> trueLoci=readMap != null ? readMap.getReadMapRecord(name).getUnclippedStarts(pair_idx) : new SimulatedRead(name).getLocs(pair_idx);
        if (!isContainedInBed(intersector,trueLoci,rec)) {
          continue;
        }
        boolean isTrueUnmapped;
        Set<EventTypesForStats> features=EnumSet.noneOf(EventTypesForStats.class);
        features.add(EventTypesForStats.All);
        isTrueUnmapped=true;
        if (!trueLoci.isEmpty()) {
          for (          GenomeLocation loc : trueLoci) {
            final BlockType feat=loc.feature;
            features.add(EventTypesForStats.valueOf(feat.getLongName()));
            isTrueUnmapped&=!feat.isMappable();
          }
        }
        if (isTrueUnmapped) {
          features.add(EventTypesForStats.True_Unmapped);
        }
 else {
          outputBlob.getStats().incStat(features,-1,StatsNamespace.T);
        }
        boolean unmapped=rec.getReadUnmappedFlag();
        int mappingQuality=unmapped ? MAPQ_UNMAPPED : rec.getMappingQuality();
        StatsNamespace validationStatus=StatsNamespace.TP;
        if (unmapped) {
          validationStatus=isTrueUnmapped ? StatsNamespace.TN : StatsNamespace.FN;
        }
 else {
          boolean closeAln=false;
          if (isTrueUnmapped) {
            if (getIdentity(rec) <= identityThreshold && mappingQuality > mapqCutoff) {
              tumFPWriter.addAlignment(rec);
            }
          }
 else {
            final GenomeLocation mappedLocation=new GenomeLocation(new ChrString(rec.getReferenceName()),rec.getUnclippedStart());
            for (            GenomeLocation loc : trueLoci) {
              closeAln|=loc.feature.isMappable() && loc.isClose(mappedLocation,wiggle);
            }
            if (!closeAln) {
              if (getIdentity(rec) <= identityThreshold && mappingQuality > mapqCutoff) {
                fpWriter.addAlignment(rec);
                validationStatus=StatsNamespace.FP;
                for (                final BlockType blockType : BlockType.values()) {
                  if (fpWriters.containsKey(blockType) && features.contains(EventTypesForStats.valueOf(blockType.getLongName()))) {
                    fpWriters.get(blockType).addAlignment(rec);
                  }
                }
              }
            }
          }
        }
        outputBlob.getStats().incStat(features,mappingQuality,validationStatus);
      }
      reader.close();
    }
    log.info("String_Node_Str" + numReads);
    log.info("String_Node_Str");
    log.info(outputBlob.getStats());
    ObjectMapper mapper=new ObjectMapper();
    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET,false);
    String jsonStr="String_Node_Str";
    try {
      jsonStr=mapper.writeValueAsString(outputBlob);
    }
 catch (    JsonProcessingException e) {
      e.printStackTrace();
      System.exit(1);
    }
    jsonWriter.print(jsonStr);
    if (htmlFile != null) {
      FileUtils.writeStringToFile(new File(outPrefix + "String_Node_Str"),JSONInserter.insertJSON(FileUtils.readFileToString(htmlFile),jsonStr));
    }
    for (    final BlockType blockType : BlockType.values()) {
      if (blockType != BlockType.UNKNOWN) {
        fpWriters.get(blockType).close();
      }
    }
    fpWriters.values().stream().forEach(SAMFileWriter::close);
  }
 catch (  IOException e) {
    e.printStackTrace();
    System.exit(1);
  }
  log.info("String_Node_Str");
}