Program: 161296
Project: 161296
Buggy source:
@Override public int doWork(String[] args){
  Set<File> bamFiles=new HashSet<>();
  com.github.lindenb.jvarkit.util.cli.GetOpt opt=new com.github.lindenb.jvarkit.util.cli.GetOpt();
  int c;
  while ((c=opt.getopt(args,getGetOptDefault() + "String_Node_Str")) != -1) {
switch (c) {
case 'd':
      this.minDepth=Integer.parseInt(opt.getOptArg());
    break;
case 'f':
{
    try {
      BufferedReader in=IOUtils.openURIForBufferedReading(opt.getOptArg());
      String line;
      while ((line=in.readLine()) != null) {
        if (line.startsWith("String_Node_Str") || line.trim().isEmpty())         continue;
        bamFiles.add(new File(line));
      }
      in.close();
    }
 catch (    Exception e) {
      error(e);
      return -1;
    }
    break;
  }
default :
{
switch (handleOtherOptions(c,opt,args)) {
case EXIT_FAILURE:
    return -1;
case EXIT_SUCCESS:
  return 0;
default :
break;
}
}
}
}
VcfIterator in=null;
try {
SamReaderFactory srf=SamReaderFactory.makeDefault();
srf.validationStringency(ValidationStringency.SILENT);
for (File bam : bamFiles) {
info("String_Node_Str" + bam);
SamReader reader=srf.open(bam);
SAMFileHeader header=reader.getFileHeader();
for (SAMReadGroupRecord g : header.getReadGroups()) {
if (g.getSample() == null) continue;
String sample=g.getSample();
if (sample.isEmpty()) continue;
Set<File> set=this.sample2bam.get(sample);
if (set == null) {
set=new HashSet<>();
this.sample2bam.put(sample,set);
}
set.add(bam);
}
reader.close();
}
if (opt.getOptInd() == args.length) {
in=VCFUtils.createVcfIteratorStdin();
}
 else if (opt.getOptInd() + 1 == args.length) {
in=VCFUtils.createVcfIterator(args[opt.getOptInd()]);
}
 else {
error(getMessageBundle("String_Node_Str"));
return -1;
}
File tmpFile1=File.createTempFile("String_Node_Str","String_Node_Str",getTmpDirectories().get(0));
File tmpFile2=File.createTempFile("String_Node_Str","String_Node_Str",getTmpDirectories().get(0));
VCFHeader header=in.getHeader();
VCFHeader h2=new VCFHeader(header);
final String FIXED_TAG="String_Node_Str";
h2.addMetaDataLine(new VCFFormatHeaderLine(FIXED_TAG,1,VCFHeaderLineType.Integer,"String_Node_Str" + this.minDepth + "String_Node_Str"));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "String_Node_Str",String.valueOf(getProgramCommandLine())));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "String_Node_Str",String.valueOf(getVersion())));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "String_Node_Str",HtsjdkVersion.getVersion()));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "String_Node_Str",HtsjdkVersion.getHome()));
for (int i=0; i < header.getNGenotypeSamples(); ++i) {
int countFixed=0;
int countNonFixed=0;
int countTotal=0;
String sample=header.getSampleNamesInOrder().get(i);
info("String_Node_Str" + sample);
Set<File> bams=this.sample2bam.get(sample);
if (bamFiles == null || bamFiles.isEmpty()) {
warning("String_Node_Str" + sample);
}
List<SamReader> samReaders=new ArrayList<>(bams.size());
for (File bam : bams) {
info("String_Node_Str" + bam);
samReaders.add(srf.open(bam));
}
VariantContextWriter w=VCFUtils.createVariantContextWriter(i % 2 == 0 ? tmpFile1 : tmpFile2);
w.writeHeader(h2);
while (in.hasNext()) {
VariantContext ctx=in.next();
countTotal++;
if (samReaders.isEmpty()) {
w.add(ctx);
continue;
}
Genotype genotype=ctx.getGenotype(sample);
if (genotype != null && genotype.isCalled()) {
w.add(ctx);
continue;
}
int depth=0;
for (SamReader sr : samReaders) {
SAMRecordIterator iter=sr.query(ctx.getChr(),ctx.getStart(),ctx.getEnd(),false);
while (iter.hasNext()) {
SAMRecord rec=iter.next();
if (rec.getReadUnmappedFlag()) continue;
if (rec.getDuplicateReadFlag()) continue;
if (rec.isSecondaryOrSupplementary()) continue;
SAMReadGroupRecord rg=rec.getReadGroup();
if (!sample.equals(rg.getSample())) continue;
Cigar cigar=rec.getCigar();
if (cigar == null) continue;
int refPos=rec.getAlignmentStart();
for (CigarElement ce : cigar.getCigarElements()) {
if (!ce.getOperator().consumesReferenceBases()) continue;
if (ce.getOperator().consumesReadBases() && refPos <= ctx.getStart() && ctx.getStart() <= refPos + ce.getLength()) {
  depth++;
  break;
}
refPos=ce.getLength();
}
}
iter.close();
}
if (depth < this.minDepth) {
countNonFixed++;
w.add(ctx);
continue;
}
List<Allele> homozygous=new ArrayList<>(2);
homozygous.add(ctx.getReference());
homozygous.add(ctx.getReference());
GenotypeBuilder gb=new GenotypeBuilder(genotype);
gb.alleles(homozygous);
gb.attribute(FIXED_TAG,1);
if (header.getFormatHeaderLine("String_Node_Str") != null) {
gb.DP(depth);
}
GenotypesContext gtx=GenotypesContext.copy(ctx.getGenotypes());
gtx.replace(gb.make());
VariantContextBuilder vcb=new VariantContextBuilder(ctx);
vcb.genotypes(gtx);
w.add(vcb.make());
countFixed++;
}
w.close();
in.close();
for (SamReader r : samReaders) CloserUtil.close(r);
info("String_Node_Str" + sample + "String_Node_Str"+ countFixed+ "String_Node_Str"+ countNonFixed+ "String_Node_Str"+ countTotal+ "String_Node_Str");
in=VCFUtils.createVcfIteratorFromFile(i % 2 == 0 ? tmpFile1 : tmpFile2);
h2=in.getHeader();
}
VariantContextWriter w=VCFUtils.createVariantContextWriterToStdout();
w.writeHeader(h2);
while (in.hasNext()) {
w.add(in.next());
}
in.close();
w.close();
tmpFile1.delete();
tmpFile2.delete();
return 0;
}
 catch (Exception err) {
error(err);
return -1;
}
 finally {
CloserUtil.close(in);
}
}

Fixed version (ground truth):
@Override public int doWork(String[] args){
  Set<File> bamFiles=new HashSet<>();
  com.github.lindenb.jvarkit.util.cli.GetOpt opt=new com.github.lindenb.jvarkit.util.cli.GetOpt();
  int c;
  while ((c=opt.getopt(args,getGetOptDefault() + "String_Node_Str")) != -1) {
switch (c) {
case 'd':
      this.minDepth=Integer.parseInt(opt.getOptArg());
    break;
case 'f':
{
    try {
      BufferedReader in=IOUtils.openURIForBufferedReading(opt.getOptArg());
      String line;
      while ((line=in.readLine()) != null) {
        if (line.startsWith("String_Node_Str") || line.trim().isEmpty())         continue;
        bamFiles.add(new File(line));
      }
      in.close();
    }
 catch (    Exception e) {
      error(e);
      return -1;
    }
    break;
  }
default :
{
switch (handleOtherOptions(c,opt,args)) {
case EXIT_FAILURE:
    return -1;
case EXIT_SUCCESS:
  return 0;
default :
break;
}
}
}
}
VcfIterator in=null;
try {
SamReaderFactory srf=SamReaderFactory.makeDefault();
srf.validationStringency(ValidationStringency.SILENT);
for (File bam : bamFiles) {
info("String_Node_Str" + bam);
SamReader reader=srf.open(bam);
SAMFileHeader header=reader.getFileHeader();
for (SAMReadGroupRecord g : header.getReadGroups()) {
if (g.getSample() == null) continue;
String sample=g.getSample();
if (sample.isEmpty()) continue;
Set<File> set=this.sample2bam.get(sample);
if (set == null) {
set=new HashSet<>();
this.sample2bam.put(sample,set);
}
set.add(bam);
}
reader.close();
}
if (opt.getOptInd() == args.length) {
in=VCFUtils.createVcfIteratorStdin();
}
 else if (opt.getOptInd() + 1 == args.length) {
in=VCFUtils.createVcfIterator(args[opt.getOptInd()]);
}
 else {
error(getMessageBundle("String_Node_Str"));
return -1;
}
File tmpFile1=File.createTempFile("String_Node_Str","String_Node_Str",getTmpDirectories().get(0));
File tmpFile2=File.createTempFile("String_Node_Str","String_Node_Str",getTmpDirectories().get(0));
VCFHeader header=in.getHeader();
VCFHeader h2=new VCFHeader(header);
final String FIXED_TAG="String_Node_Str";
h2.addMetaDataLine(new VCFFormatHeaderLine(FIXED_TAG,1,VCFHeaderLineType.Integer,"String_Node_Str" + this.minDepth + "String_Node_Str"));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "String_Node_Str",String.valueOf(getProgramCommandLine())));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "String_Node_Str",String.valueOf(getVersion())));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "String_Node_Str",HtsjdkVersion.getVersion()));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "String_Node_Str",HtsjdkVersion.getHome()));
for (int i=0; i < header.getNGenotypeSamples(); ++i) {
int countFixed=0;
int countNonFixed=0;
int countTotal=0;
String sample=header.getSampleNamesInOrder().get(i);
info("String_Node_Str" + sample);
Set<File> bams=this.sample2bam.get(sample);
if (bams == null) bams=new HashSet<File>();
if (bams.isEmpty()) {
warning("String_Node_Str" + sample);
}
List<SamReader> samReaders=new ArrayList<>(bams.size());
for (File bam : bams) {
info("String_Node_Str" + bam);
samReaders.add(srf.open(bam));
}
VariantContextWriter w=VCFUtils.createVariantContextWriter(i % 2 == 0 ? tmpFile1 : tmpFile2);
w.writeHeader(h2);
while (in.hasNext()) {
VariantContext ctx=in.next();
countTotal++;
if (samReaders.isEmpty()) {
w.add(ctx);
continue;
}
Genotype genotype=ctx.getGenotype(sample);
if (genotype != null && genotype.isCalled()) {
w.add(ctx);
continue;
}
int depth=0;
for (SamReader sr : samReaders) {
SAMRecordIterator iter=sr.query(ctx.getChr(),ctx.getStart(),ctx.getEnd(),false);
while (iter.hasNext()) {
SAMRecord rec=iter.next();
if (rec.getReadUnmappedFlag()) continue;
if (rec.getDuplicateReadFlag()) continue;
if (rec.isSecondaryOrSupplementary()) continue;
SAMReadGroupRecord rg=rec.getReadGroup();
if (!sample.equals(rg.getSample())) continue;
Cigar cigar=rec.getCigar();
if (cigar == null) continue;
int refPos=rec.getAlignmentStart();
for (CigarElement ce : cigar.getCigarElements()) {
if (!ce.getOperator().consumesReferenceBases()) continue;
if (ce.getOperator().consumesReadBases() && refPos <= ctx.getStart() && ctx.getStart() <= refPos + ce.getLength()) {
  depth++;
  break;
}
refPos=ce.getLength();
}
}
iter.close();
}
if (depth < this.minDepth) {
countNonFixed++;
w.add(ctx);
continue;
}
List<Allele> homozygous=new ArrayList<>(2);
homozygous.add(ctx.getReference());
homozygous.add(ctx.getReference());
GenotypeBuilder gb=new GenotypeBuilder(genotype);
gb.alleles(homozygous);
gb.attribute(FIXED_TAG,1);
if (header.getFormatHeaderLine("String_Node_Str") != null) {
gb.DP(depth);
}
GenotypesContext gtx=GenotypesContext.copy(ctx.getGenotypes());
gtx.replace(gb.make());
VariantContextBuilder vcb=new VariantContextBuilder(ctx);
vcb.genotypes(gtx);
w.add(vcb.make());
countFixed++;
}
w.close();
in.close();
for (SamReader r : samReaders) CloserUtil.close(r);
info("String_Node_Str" + sample + "String_Node_Str"+ countFixed+ "String_Node_Str"+ countNonFixed+ "String_Node_Str"+ countTotal+ "String_Node_Str");
in=VCFUtils.createVcfIteratorFromFile(i % 2 == 0 ? tmpFile1 : tmpFile2);
h2=in.getHeader();
}
VariantContextWriter w=VCFUtils.createVariantContextWriterToStdout();
w.writeHeader(h2);
while (in.hasNext()) {
w.add(in.next());
}
in.close();
w.close();
tmpFile1.delete();
tmpFile2.delete();
return 0;
}
 catch (Exception err) {
error(err);
return -1;
}
 finally {
CloserUtil.close(in);
}
}