Program: 161289
Project: 161289
Buggy source:
private BufferedImage build(final SamReader r){
  List<List<SAMRecord>> rows=new ArrayList<List<SAMRecord>>();
  SAMRecordIterator iter=null;
  if (bamFile != null) {
    iter=r.queryOverlapping(interval.getContig(),interval.getStart(),interval.getEnd());
  }
 else {
    iter=r.iterator();
  }
  int countReads=0;
  while (iter.hasNext()) {
    SAMRecord rec=iter.next();
    if (rec.getReadUnmappedFlag())     continue;
    if (this.bamFile == null) {
      if (!this.interval.getContig().equals(rec.getReferenceName()))       continue;
      if (rec.getAlignmentEnd() < this.interval.getStart())       continue;
      if (rec.getAlignmentStart() > this.interval.getEnd())       break;
    }
    countReads++;
    for (    List<SAMRecord> row : rows) {
      SAMRecord last=row.get(row.size() - 1);
      if (this.interval != null) {
        if (right(last) + this.minHDistance > left(rec))         continue;
      }
 else {
        if (last.getAlignmentEnd() + 1 > rec.getAlignmentStart())         continue;
      }
      row.add(rec);
      rec=null;
      break;
    }
    if (rec != null) {
      List<SAMRecord> row=new ArrayList<SAMRecord>();
      row.add(rec);
      rows.add(row);
    }
  }
  iter.close();
  LOG.info("String_Node_Str" + countReads);
  final int ruler_height=String.valueOf(this.interval.getEnd()).length() * 20;
  final int refw=(int)Math.max(1.0,WIDTH / (double)(1 + interval.getEnd() - interval.getStart()));
  LOG.info("String_Node_Str" + refw + "String_Node_Str"+ WIDTH+ "String_Node_Str"+ (1 + interval.getEnd() - interval.getStart()));
  final int margin_top=10 + (refw * 2) + ruler_height;
  Dimension imageSize=new Dimension(WIDTH,margin_top + rows.size() * (this.spaceYbetweenFeatures + this.featureHeight) + this.spaceYbetweenFeatures);
  BufferedImage img=new BufferedImage(imageSize.width,imageSize.height,BufferedImage.TYPE_INT_RGB);
  CharSequence genomicSequence=null;
  if (this.indexedFastaSequenceFile != null) {
    genomicSequence=new GenomicSequence(this.indexedFastaSequenceFile,this.interval.getContig());
  }
 else {
    genomicSequence=new AbstractCharSequence(){
      @Override public int length(){
        return interval.getStart() + 10;
      }
      @Override public char charAt(      int index){
        return 'N';
      }
    }
;
  }
  Graphics2D g=img.createGraphics();
  g.setColor(Color.WHITE);
  g.fillRect(0,0,imageSize.width,imageSize.height);
  LOG.info("String_Node_Str" + imageSize.width + "String_Node_Str"+ imageSize.height);
  Map<Integer,Counter<Character>> ref2consensus=new HashMap<Integer,Counter<Character>>();
  for (int x=this.interval.getStart(); x <= this.interval.getEnd(); ++x) {
    final double oneBaseWidth=convertToX(x + 1) - convertToX(x);
    g.setColor(x % 10 == 0 ? Color.BLACK : Color.LIGHT_GRAY);
    g.draw(new Line2D.Double(convertToX(x),0,convertToX(x),imageSize.height));
    if ((x - this.interval.getStart()) % 10 == 0) {
      g.setColor(Color.BLACK);
      String xStr=String.format("String_Node_Str",x);
      AffineTransform tr=g.getTransform();
      AffineTransform tr2=new AffineTransform(tr);
      tr2.translate(convertToX(x),0);
      tr2.rotate(Math.PI / 2.0);
      g.setTransform(tr2);
      hersheyFont.paint(g,xStr,0,0,ruler_height,oneBaseWidth);
      g.setTransform(tr);
    }
    char c=genomicSequence.charAt(x - 1);
    g.setColor(base2color(c));
    hersheyFont.paint(g,String.valueOf(c),convertToX(x) + 1,ruler_height,oneBaseWidth - 2,oneBaseWidth - 2);
  }
  int y=margin_top + this.spaceYbetweenFeatures;
  for (  List<SAMRecord> row : rows) {
    for (    SAMRecord rec : row) {
      double x0=left(rec);
      double x1=right(rec);
      double y0=y;
      double y1=y0 + this.featureHeight;
      Shape shapeRec=null;
      if (x1 - x0 < minArrowWidth) {
        shapeRec=new Rectangle2D.Double(x0,y0,x1 - x0,y1 - y0);
      }
 else {
        GeneralPath path=new GeneralPath();
        double arrow=Math.max(this.minArrowWidth,Math.min(this.maxArrowWidth,x1 - x0));
        if (!rec.getReadNegativeStrandFlag()) {
          path.moveTo(x0,y0);
          path.lineTo(x1 - arrow,y0);
          path.lineTo(x1,(y0 + y1) / 2);
          path.lineTo(x1 - arrow,y1);
          path.lineTo(x0,y1);
        }
 else {
          path.moveTo(x0 + arrow,y0);
          path.lineTo(x0,(y0 + y1) / 2);
          path.lineTo(x0 + arrow,y1);
          path.lineTo(x1,y1);
          path.lineTo(x1,y0);
        }
        path.closePath();
        shapeRec=path;
      }
      Stroke oldStroke=g.getStroke();
      g.setStroke(new BasicStroke(2f));
      Paint oldpaint=g.getPaint();
      LinearGradientPaint gradient=new LinearGradientPaint(0f,(float)shapeRec.getBounds2D().getY(),0f,(float)shapeRec.getBounds2D().getMaxY(),new float[]{0f,0.5f,1f},new Color[]{Color.DARK_GRAY,Color.WHITE,Color.DARK_GRAY});
      g.setPaint(gradient);
      g.fill(shapeRec);
      g.setPaint(oldpaint);
      g.setColor(this.strokeColorizer.getColor(rec));
      g.draw(shapeRec);
      g.setStroke(oldStroke);
      Shape oldClip=g.getClip();
      g.setClip(shapeRec);
      Cigar cigar=rec.getCigar();
      if (cigar != null) {
        byte bases[]=rec.getReadBases();
        int refpos=rec.getAlignmentStart();
        int readpos=0;
        for (        CigarElement ce : cigar.getCigarElements()) {
switch (ce.getOperator()) {
case H:
            break;
case S:
          readpos+=ce.getLength();
        break;
case I:
{
        g.setColor(Color.GREEN);
        g.fill(new Rectangle2D.Double(convertToX(refpos),y0,2,y1 - y0));
        readpos+=ce.getLength();
        break;
      }
case D:
case N:
case P:
{
      g.setColor(Color.ORANGE);
      g.fill(new Rectangle2D.Double(convertToX(refpos),y0,convertToX(refpos + ce.getLength()) - convertToX(refpos),y1 - y0));
      refpos+=ce.getLength();
      break;
    }
case EQ:
case X:
case M:
{
    for (int i=0; i < ce.getLength(); ++i) {
      if (readpos >= bases.length) {
        System.err.println(rec.getReadName() + "String_Node_Str" + rec.getCigarString()+ "String_Node_Str"+ rec.getReadString());
      }
      char c1=(char)bases[readpos];
      Counter<Character> consensus=ref2consensus.get(refpos);
      if (consensus == null) {
        consensus=new Counter<Character>();
        ref2consensus.put(refpos,consensus);
      }
      consensus.incr(Character.toUpperCase(c1));
      char c2=genomicSequence.charAt(refpos - 1);
      double mutW=convertToX(refpos + 1) - convertToX(refpos);
      g.setColor(Color.BLACK);
      Shape mut=new Rectangle2D.Double(convertToX(refpos),y0,mutW,y1 - y0);
      if (ce.getOperator() == CigarOperator.X || (c2 != 'N' && c2 != 'n' && Character.toUpperCase(c1) != Character.toUpperCase(c2))) {
        g.setColor(Color.RED);
        g.fill(mut);
        g.setColor(Color.WHITE);
      }
      if (isPrintName()) {
        if (readpos < rec.getReadName().length()) {
          c1=rec.getReadName().charAt(readpos);
          c1=rec.getReadNegativeStrandFlag() ? Character.toLowerCase(c1) : Character.toUpperCase(c1);
        }
 else {
          c1=' ';
        }
      }
 else       if (!super.disablePrintBases) {
        c1=' ';
      }
      this.hersheyFont.paint(g,String.valueOf(c1),mut);
      readpos++;
      refpos++;
    }
    break;
  }
default :
LOG.error("String_Node_Str" + ce.getOperator());
break;
}
}
}
g.setClip(oldClip);
}
y+=this.featureHeight + this.spaceYbetweenFeatures;
}
for (int x=this.interval.getStart(); x <= this.interval.getEnd(); ++x) {
Counter<Character> cons=ref2consensus.get(x);
if (cons == null || cons.getCountCategories() == 0) {
continue;
}
final double oneBaseWidth=(convertToX(x + 1) - convertToX(x)) - 1;
double x0=convertToX(x) + 1;
for (Character c : cons.keySetDecreasing()) {
double weight=oneBaseWidth * (cons.count(c) / (double)cons.getTotal());
g.setColor(Color.BLACK);
if (genomicSequence != null && Character.toUpperCase(genomicSequence.charAt(x - 1)) != Character.toUpperCase(c)) {
g.setColor(Color.RED);
}
hersheyFont.paint(g,String.valueOf(c),x0,ruler_height + refw,weight,oneBaseWidth - 2);
x0+=weight;
}
}
g.dispose();
return img;
}

Fixed version (ground truth):
private BufferedImage build(final SamReader r){
  List<List<SAMRecord>> rows=new ArrayList<List<SAMRecord>>();
  SAMRecordIterator iter=null;
  if (bamFile != null) {
    iter=r.queryOverlapping(interval.getContig(),interval.getStart(),interval.getEnd());
  }
 else {
    iter=r.iterator();
  }
  int countReads=0;
  while (iter.hasNext()) {
    SAMRecord rec=iter.next();
    if (rec.getReadUnmappedFlag())     continue;
    if (this.bamFile == null) {
      if (!this.interval.getContig().equals(rec.getReferenceName()))       continue;
      if (rec.getAlignmentEnd() < this.interval.getStart())       continue;
      if (rec.getAlignmentStart() > this.interval.getEnd())       break;
    }
    countReads++;
    for (    List<SAMRecord> row : rows) {
      SAMRecord last=row.get(row.size() - 1);
      if (this.interval != null) {
        if (right(last) + this.minHDistance > left(rec))         continue;
      }
 else {
        if (last.getAlignmentEnd() + 1 > rec.getAlignmentStart())         continue;
      }
      row.add(rec);
      rec=null;
      break;
    }
    if (rec != null) {
      List<SAMRecord> row=new ArrayList<SAMRecord>();
      row.add(rec);
      rows.add(row);
    }
  }
  iter.close();
  LOG.info("String_Node_Str" + countReads);
  final int ruler_height=String.valueOf(this.interval.getEnd()).length() * 20;
  final int refw=(int)Math.max(1.0,WIDTH / (double)(1 + interval.getEnd() - interval.getStart()));
  LOG.info("String_Node_Str" + refw + "String_Node_Str"+ WIDTH+ "String_Node_Str"+ (1 + interval.getEnd() - interval.getStart()));
  final int margin_top=10 + (refw * 2) + ruler_height;
  Dimension imageSize=new Dimension(WIDTH,margin_top + rows.size() * (this.spaceYbetweenFeatures + this.featureHeight) + this.spaceYbetweenFeatures);
  BufferedImage img=new BufferedImage(imageSize.width,imageSize.height,BufferedImage.TYPE_INT_RGB);
  CharSequence genomicSequence=null;
  if (this.indexedFastaSequenceFile != null) {
    genomicSequence=new GenomicSequence(this.indexedFastaSequenceFile,this.interval.getContig());
  }
 else {
    genomicSequence=new AbstractCharSequence(){
      @Override public int length(){
        return interval.getStart() + 10;
      }
      @Override public char charAt(      int index){
        return 'N';
      }
    }
;
  }
  Graphics2D g=img.createGraphics();
  g.setColor(Color.WHITE);
  g.fillRect(0,0,imageSize.width,imageSize.height);
  LOG.info("String_Node_Str" + imageSize.width + "String_Node_Str"+ imageSize.height);
  Map<Integer,Counter<Character>> ref2consensus=new HashMap<Integer,Counter<Character>>();
  for (int x=this.interval.getStart(); x <= this.interval.getEnd(); ++x) {
    final double oneBaseWidth=convertToX(x + 1) - convertToX(x);
    g.setColor(x % 10 == 0 ? Color.BLACK : Color.LIGHT_GRAY);
    g.draw(new Line2D.Double(convertToX(x),0,convertToX(x),imageSize.height));
    if ((x - this.interval.getStart()) % 10 == 0) {
      g.setColor(Color.BLACK);
      final String xStr=String.format("String_Node_Str",x);
      final AffineTransform tr=g.getTransform();
      final AffineTransform tr2=new AffineTransform(tr);
      tr2.translate(convertToX(x + 1),0);
      tr2.rotate(Math.PI / 2.0);
      g.setTransform(tr2);
      hersheyFont.paint(g,xStr,0,0,ruler_height,oneBaseWidth);
      g.setTransform(tr);
    }
    char c=genomicSequence.charAt(x - 1);
    g.setColor(base2color(c));
    hersheyFont.paint(g,String.valueOf(c),convertToX(x) + 1,ruler_height,oneBaseWidth - 2,oneBaseWidth - 2);
  }
  int y=margin_top + this.spaceYbetweenFeatures;
  for (  List<SAMRecord> row : rows) {
    for (    SAMRecord rec : row) {
      double x0=left(rec);
      double x1=right(rec);
      double y0=y;
      double y1=y0 + this.featureHeight;
      Shape shapeRec=null;
      if (x1 - x0 < minArrowWidth) {
        shapeRec=new Rectangle2D.Double(x0,y0,x1 - x0,y1 - y0);
      }
 else {
        GeneralPath path=new GeneralPath();
        double arrow=Math.max(this.minArrowWidth,Math.min(this.maxArrowWidth,x1 - x0));
        if (!rec.getReadNegativeStrandFlag()) {
          path.moveTo(x0,y0);
          path.lineTo(x1 - arrow,y0);
          path.lineTo(x1,(y0 + y1) / 2);
          path.lineTo(x1 - arrow,y1);
          path.lineTo(x0,y1);
        }
 else {
          path.moveTo(x0 + arrow,y0);
          path.lineTo(x0,(y0 + y1) / 2);
          path.lineTo(x0 + arrow,y1);
          path.lineTo(x1,y1);
          path.lineTo(x1,y0);
        }
        path.closePath();
        shapeRec=path;
      }
      Stroke oldStroke=g.getStroke();
      g.setStroke(new BasicStroke(2f));
      Paint oldpaint=g.getPaint();
      LinearGradientPaint gradient=new LinearGradientPaint(0f,(float)shapeRec.getBounds2D().getY(),0f,(float)shapeRec.getBounds2D().getMaxY(),new float[]{0f,0.5f,1f},new Color[]{Color.DARK_GRAY,Color.WHITE,Color.DARK_GRAY});
      g.setPaint(gradient);
      g.fill(shapeRec);
      g.setPaint(oldpaint);
      g.setColor(this.strokeColorizer.getColor(rec));
      g.draw(shapeRec);
      g.setStroke(oldStroke);
      Shape oldClip=g.getClip();
      g.setClip(shapeRec);
      Cigar cigar=rec.getCigar();
      if (cigar != null) {
        byte bases[]=rec.getReadBases();
        int refpos=rec.getAlignmentStart();
        int readpos=0;
        for (        CigarElement ce : cigar.getCigarElements()) {
switch (ce.getOperator()) {
case H:
            break;
case S:
          readpos+=ce.getLength();
        break;
case I:
{
        g.setColor(Color.GREEN);
        g.fill(new Rectangle2D.Double(convertToX(refpos),y0,2,y1 - y0));
        readpos+=ce.getLength();
        break;
      }
case D:
case N:
case P:
{
      g.setColor(Color.ORANGE);
      g.fill(new Rectangle2D.Double(convertToX(refpos),y0,convertToX(refpos + ce.getLength()) - convertToX(refpos),y1 - y0));
      refpos+=ce.getLength();
      break;
    }
case EQ:
case X:
case M:
{
    for (int i=0; i < ce.getLength(); ++i) {
      if (readpos >= bases.length) {
        System.err.println(rec.getReadName() + "String_Node_Str" + rec.getCigarString()+ "String_Node_Str"+ rec.getReadString());
      }
      char c1=(char)bases[readpos];
      Counter<Character> consensus=ref2consensus.get(refpos);
      if (consensus == null) {
        consensus=new Counter<Character>();
        ref2consensus.put(refpos,consensus);
      }
      consensus.incr(Character.toUpperCase(c1));
      char c2=genomicSequence.charAt(refpos - 1);
      double mutW=convertToX(refpos + 1) - convertToX(refpos);
      g.setColor(Color.BLACK);
      Shape mut=new Rectangle2D.Double(convertToX(refpos),y0,mutW,y1 - y0);
      if (ce.getOperator() == CigarOperator.X || (c2 != 'N' && c2 != 'n' && Character.toUpperCase(c1) != Character.toUpperCase(c2))) {
        g.setColor(Color.RED);
        g.fill(mut);
        g.setColor(Color.WHITE);
      }
      if (isPrintName()) {
        if (readpos < rec.getReadName().length()) {
          c1=rec.getReadName().charAt(readpos);
          c1=rec.getReadNegativeStrandFlag() ? Character.toLowerCase(c1) : Character.toUpperCase(c1);
        }
 else {
          c1=' ';
        }
      }
 else       if (!super.disablePrintBases) {
        c1=' ';
      }
      this.hersheyFont.paint(g,String.valueOf(c1),mut);
      readpos++;
      refpos++;
    }
    break;
  }
default :
LOG.error("String_Node_Str" + ce.getOperator());
break;
}
}
}
g.setClip(oldClip);
}
y+=this.featureHeight + this.spaceYbetweenFeatures;
}
for (int x=this.interval.getStart(); x <= this.interval.getEnd(); ++x) {
Counter<Character> cons=ref2consensus.get(x);
if (cons == null || cons.getCountCategories() == 0) {
continue;
}
final double oneBaseWidth=(convertToX(x + 1) - convertToX(x)) - 1;
double x0=convertToX(x) + 1;
for (Character c : cons.keySetDecreasing()) {
double weight=oneBaseWidth * (cons.count(c) / (double)cons.getTotal());
g.setColor(Color.BLACK);
if (genomicSequence != null && Character.toUpperCase(genomicSequence.charAt(x - 1)) != Character.toUpperCase(c)) {
g.setColor(Color.RED);
}
hersheyFont.paint(g,String.valueOf(c),x0,ruler_height + refw,weight,oneBaseWidth - 2);
x0+=weight;
}
}
g.dispose();
return img;
}