JAL-1291 conditional output of rendering debugging info
[jalview.git] / src / jalview / renderer / AnnotationRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.renderer;
19
20 import jalview.analysis.AAFrequency;
21 import jalview.analysis.StructureFrequency;
22 import jalview.api.AlignViewportI;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.schemes.ColourSchemeI;
27
28 import java.awt.BasicStroke;
29 import java.awt.Color;
30 import java.awt.Font;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.Image;
35 import java.awt.font.LineMetrics;
36 import java.awt.geom.AffineTransform;
37 import java.awt.image.ImageObserver;
38 import java.util.BitSet;
39 import java.util.Hashtable;
40
41 import com.stevesoft.pat.Regex;
42
43 public class AnnotationRenderer
44 {
45   /**
46    * flag indicating if timing and redraw parameter info should be output
47    */
48   private final boolean debugRedraw;
49
50   public AnnotationRenderer()
51   {
52     this(false);
53   }
54   /**
55    * Create a new annotation Renderer
56    * @param debugRedraw flag indicating if timing and redraw parameter info should be output
57    */
58   public AnnotationRenderer(boolean debugRedraw)
59   {
60     this.debugRedraw=debugRedraw;
61   }
62
63   public void drawStemAnnot(Graphics g, Annotation[] row_annotations,
64           int lastSSX, int x, int y, int iconOffset, int startRes,
65           int column, boolean validRes, boolean validEnd)
66   {
67     g.setColor(STEM_COLOUR);
68     int sCol = (lastSSX / charWidth) + startRes;
69     int x1 = lastSSX;
70     int x2 = (x * charWidth);
71     Regex closeparen = new Regex("(\\))");
72
73     String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
74             : row_annotations[column - 1].displayCharacter;
75
76     boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
77             || !dc.equals(row_annotations[sCol - 1].displayCharacter);
78     boolean diffdownstream = !validRes || !validEnd
79             || row_annotations[column] == null
80             || !dc.equals(row_annotations[column].displayCharacter);
81     // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
82     // If a closing base pair half of the stem, display a backward arrow
83     if (column > 0 && closeparen.search(dc))
84     {
85       if (diffupstream)
86       // if (validRes && column>1 && row_annotations[column-2]!=null &&
87       // dc.equals(row_annotations[column-2].displayCharacter))
88       {
89         g.fillPolygon(new int[]
90         { lastSSX + 5, lastSSX + 5, lastSSX }, new int[]
91         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
92         x1 += 5;
93       }
94       if (diffdownstream)
95       {
96         x2 -= 1;
97       }
98     }
99     else
100     {
101       // display a forward arrow
102       if (diffdownstream)
103       {
104         g.fillPolygon(new int[]
105         { x2 - 5, x2 - 5, x2 }, new int[]
106         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
107         x2 -= 5;
108       }
109       if (diffupstream)
110       {
111         x1 += 1;
112       }
113     }
114     // draw arrow body
115     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
116   }
117
118   private int charWidth, endRes, charHeight;
119
120   private boolean validCharWidth, hasHiddenColumns;
121
122   private FontMetrics fm;
123
124   private final boolean MAC = new jalview.util.Platform().isAMac();
125
126   boolean av_renderHistogram = true, av_renderProfile = true,
127           av_normaliseProfile = false;
128
129   ColourSchemeI profcolour = null;
130
131   private ColumnSelection columnSelection;
132
133   private Hashtable[] hconsensus;
134
135   private Hashtable[] hStrucConsensus;
136
137   private boolean av_ignoreGapsConsensus;
138
139   /**
140    * attributes set from AwtRenderPanelI
141    */
142   /**
143    * old image used when data is currently being calculated and cannot be
144    * rendered
145    */
146   private Image fadedImage;
147
148   /**
149    * panel being rendered into
150    */
151   private ImageObserver annotationPanel;
152
153   /**
154    * width of image to render in panel
155    */
156   private int imgWidth;
157   /**
158    * offset to beginning of visible area
159    */
160   private int sOffset;
161   /**
162    * offset to end of visible area
163    */
164   private int visHeight;
165   /**
166    * indicate if the renderer should only render the visible portion of the annotation given the current view settings
167    */
168   private boolean useClip=true;
169   /**
170    * master flag indicating if renderer should ever try to clip. not enabled for jalview 2.8.1 
171    */
172   private boolean canClip=false;
173   
174   // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
175   // av)
176   public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
177           AlignViewportI av)
178   {
179     fm = annotPanel.getFontMetrics();
180     annotationPanel = annotPanel;
181     fadedImage = annotPanel.getFadedImage();
182     imgWidth = annotPanel.getFadedImageWidth();
183     // visible area for rendering
184     int[] bounds=annotPanel.getVisibleVRange();
185     if (bounds!=null)
186     {
187       sOffset = bounds[0];
188       visHeight = bounds[1];
189       if (visHeight==0)
190       {
191         useClip=false;
192       } else {
193         useClip=canClip;
194       }
195     } else {
196       useClip=false;
197     }
198
199     updateFromAlignViewport(av);
200   }
201
202   public void updateFromAlignViewport(AlignViewportI av)
203   {
204     charWidth = av.getCharWidth();
205     endRes = av.getEndRes();
206     charHeight = av.getCharHeight();
207     hasHiddenColumns = av.hasHiddenColumns();
208     validCharWidth = av.isValidCharWidth();
209     av_renderHistogram = av.isShowConsensusHistogram();
210     av_renderProfile = av.isShowSequenceLogo();
211     av_normaliseProfile = av.isNormaliseSequenceLogo();
212     profcolour = av.getGlobalColourScheme();
213     if (profcolour == null)
214     {
215       // Set the default colour for sequence logo if the alignnent has no
216       // colourscheme set
217       profcolour = av.getAlignment().isNucleotide() ? new jalview.schemes.NucleotideColourScheme()
218               : new jalview.schemes.ZappoColourScheme();
219     }
220     columnSelection = av.getColumnSelection();
221     hconsensus = av.getSequenceConsensusHash();// hconsensus;
222     hStrucConsensus = av.getRnaStructureConsensusHash(); // hStrucConsensus;
223     av_ignoreGapsConsensus = av.getIgnoreGapsConsensus();
224   }
225
226   public int[] getProfileFor(AlignmentAnnotation aa, int column)
227   {
228     // TODO : consider refactoring the global alignment calculation
229     // properties/rendering attributes as a global 'alignment group' which holds
230     // all vis settings for the alignment as a whole rather than a subset
231     //
232     if (aa.autoCalculated && aa.label.startsWith("Consensus"))
233     {
234       if (aa.groupRef != null && aa.groupRef.consensusData != null
235               && aa.groupRef.isShowSequenceLogo())
236       {
237         return AAFrequency.extractProfile(
238                 aa.groupRef.consensusData[column],
239                 aa.groupRef.getIgnoreGapsConsensus());
240       }
241       // TODO extend annotation row to enable dynamic and static profile data to
242       // be stored
243       if (aa.groupRef == null && aa.sequenceRef == null && av_renderProfile)
244       {
245         return AAFrequency.extractProfile(hconsensus[column],
246                 av_ignoreGapsConsensus);
247       }
248     }
249     else
250     {
251       if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
252       {
253         // TODO implement group structure consensus
254         /*
255          * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
256          * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
257          * group selections return StructureFrequency.extractProfile(
258          * aa.groupRef.consensusData[column], aa.groupRef
259          * .getIgnoreGapsConsensus()); }
260          */
261         // TODO extend annotation row to enable dynamic and static profile data
262         // to
263         // be stored
264         if (aa.groupRef == null && aa.sequenceRef == null
265                 && av_renderProfile && hStrucConsensus != null
266                 && hStrucConsensus.length > column)
267         {
268           return StructureFrequency.extractProfile(hStrucConsensus[column],
269                   av_ignoreGapsConsensus);
270         }
271       }
272     }
273     return null;
274   }
275
276   /**
277    * Render the annotation rows associated with an alignment.
278    * 
279    * @param annotPanel
280    *          container frame
281    * @param av
282    *          data and view settings to render
283    * @param g
284    *          destination for graphics
285    * @param activeRow
286    *          row where a mouse event occured (or -1)
287    * @param startRes
288    *          first column that will be drawn
289    * @param endRes
290    *          last column that will be drawn
291    * @return true if the fadedImage was used for any alignment annotation rows
292    *         currently being calculated
293    */
294   public boolean drawComponent(AwtRenderPanelI annotPanel,
295           AlignViewportI av, Graphics g, int activeRow, int startRes,
296           int endRes)
297   {
298     long stime=System.currentTimeMillis();
299     boolean usedFaded = false;
300     // NOTES:
301     // AnnotationPanel needs to implement: ImageObserver, access to
302     // AlignViewport
303     updateFromAwtRenderPanel(annotPanel, av);
304     fm = g.getFontMetrics();
305     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
306     if (aa==null)
307     {
308       return false;
309     }
310     int x = 0, y = 0;
311     int column = 0;
312     char lastSS;
313     int lastSSX;
314     int iconOffset = 0;
315     boolean validRes = false;
316     boolean validEnd = false;
317     boolean labelAllCols = false;
318     boolean centreColLabels, centreColLabelsDef = av
319             .getCentreColumnLabels();
320     boolean scaleColLabel = false;
321
322     BitSet graphGroupDrawn = new BitSet();
323     int charOffset = 0; // offset for a label
324     float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a
325     // column.
326     Font ofont = g.getFont();
327     // \u03B2 \u03B1
328     // debug ints
329     int yfrom=0,f_i=0,yto=0,f_to=0;
330     boolean clipst=false,clipend=false;
331     for (int i = 0; i < aa.length; i++)
332     {
333       AlignmentAnnotation row = aa[i];
334       Annotation[] row_annotations = row.annotations;
335       if (!row.visible)
336       {
337         continue;
338       }
339       centreColLabels = row.centreColLabels || centreColLabelsDef;
340       labelAllCols = row.showAllColLabels;
341       scaleColLabel = row.scaleColLabel;
342       lastSS = ' ';
343       lastSSX = 0;
344       
345       if (!useClip || ((y-charHeight)<visHeight && (y+row.height+charHeight*2)>=sOffset)) 
346       {// if_in_visible_region
347         if (!clipst)
348         {
349           clipst=true;
350           yfrom=y;
351           f_i=i;
352         }
353         yto = y;
354         f_to=i;
355       if (row.graph > 0)
356       {
357         if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup)) {
358           continue;
359         }
360
361         // this is so that we draw the characters below the graph
362         y += row.height;
363
364         if (row.hasText)
365         {
366           iconOffset = charHeight - fm.getDescent();
367           y -= charHeight;
368         }
369       }
370       else if (row.hasText)
371       {
372         iconOffset = charHeight - fm.getDescent();
373
374       }
375       else
376       {
377         iconOffset = 0;
378       }
379
380       if (row.autoCalculated && av.isCalculationInProgress(row))
381       {
382         y += charHeight;
383         usedFaded = true;
384         g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
385                 - row.height, imgWidth, y, annotationPanel);
386         g.setColor(Color.black);
387         // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
388
389         continue;
390       }
391
392       /*
393        * else if (annotationPanel.av.updatingConservation &&
394        * aa[i].label.equals("Conservation")) {
395        * 
396        * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
397        * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
398        * annotationPanel.imgWidth, y, annotationPanel);
399        * 
400        * g.setColor(Color.black); //
401        * g.drawString("Calculating Conservation.....",20, y-row.height/2);
402        * 
403        * continue; } else if (annotationPanel.av.updatingConservation &&
404        * aa[i].label.equals("Quality")) {
405        * 
406        * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
407        * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
408        * annotationPanel.imgWidth, y, annotationPanel); g.setColor(Color.black);
409        * // / g.drawString("Calculating Quality....",20, y-row.height/2);
410        * 
411        * continue; }
412        */
413       // first pass sets up state for drawing continuation from left-hand column
414       // of startRes
415       x = (startRes == 0) ? 0 : -1;
416       while (x < endRes - startRes)
417       {
418         if (hasHiddenColumns)
419         {
420           column = columnSelection.adjustForHiddenColumns(startRes + x);
421           if (column > row_annotations.length - 1)
422           {
423             break;
424           }
425         }
426         else
427         {
428           column = startRes + x;
429         }
430
431         if ((row_annotations == null) || (row_annotations.length <= column)
432                 || (row_annotations[column] == null))
433         {
434           validRes = false;
435         }
436         else
437         {
438           validRes = true;
439         }
440         if (x > -1)
441         {
442           if (activeRow == i)
443           {
444             g.setColor(Color.red);
445
446             if (columnSelection != null)
447             {
448               for (int n = 0; n < columnSelection.size(); n++)
449               {
450                 int v = columnSelection.columnAt(n);
451
452                 if (v == column)
453                 {
454                   g.fillRect(x * charWidth, y, charWidth, charHeight);
455                 }
456               }
457             }
458           }
459           if (!row.isValidStruc())
460           {
461             g.setColor(Color.orange);
462             g.fillRect((int) row.getInvalidStrucPos() * charWidth, y,
463                     charWidth, charHeight);
464           }
465           if (validCharWidth
466                   && validRes
467                   && row_annotations[column].displayCharacter != null
468                   && (row_annotations[column].displayCharacter.length() > 0))
469           {
470
471             if (centreColLabels || scaleColLabel)
472             {
473               fmWidth = fm.charsWidth(
474                       row_annotations[column].displayCharacter
475                               .toCharArray(), 0,
476                       row_annotations[column].displayCharacter.length());
477
478               if (scaleColLabel)
479               {
480                 // justify the label and scale to fit in column
481                 if (fmWidth > charWidth)
482                 {
483                   // scale only if the current font isn't already small enough
484                   fmScaling = charWidth;
485                   fmScaling /= fmWidth;
486                   g.setFont(ofont.deriveFont(AffineTransform
487                           .getScaleInstance(fmScaling, 1.0)));
488                   // and update the label's width to reflect the scaling.
489                   fmWidth = charWidth;
490                 }
491               }
492             }
493             else
494             {
495               fmWidth = fm
496                       .charWidth(row_annotations[column].displayCharacter
497                               .charAt(0));
498             }
499             charOffset = (int) ((charWidth - fmWidth) / 2f);
500
501             if (row_annotations[column].colour == null)
502               g.setColor(Color.black);
503             else
504               g.setColor(row_annotations[column].colour);
505
506             if (column == 0 || row.graph > 0)
507             {
508               g.drawString(row_annotations[column].displayCharacter,
509                       (x * charWidth) + charOffset, y + iconOffset);
510             }
511             else if (row_annotations[column - 1] == null
512                     || (labelAllCols
513                             || !row_annotations[column].displayCharacter
514                                     .equals(row_annotations[column - 1].displayCharacter) || (row_annotations[column].displayCharacter
515                             .length() < 2 && row_annotations[column].secondaryStructure == ' ')))
516             {
517               g.drawString(row_annotations[column].displayCharacter, x
518                       * charWidth + charOffset, y + iconOffset);
519             }
520             g.setFont(ofont);
521           }
522         }
523         if (row.hasIcons)
524         {
525           char ss = validRes ? row_annotations[column].secondaryStructure
526                   : ' ';
527           if (ss == 'S')
528           {
529             // distinguish between forward/backward base-pairing
530             if (row_annotations[column].displayCharacter.indexOf(')') > -1)
531             {
532               ss = 's';
533             }
534           }
535           if (!validRes || (ss != lastSS))
536           {
537             if (x > -1)
538             {
539               switch (lastSS)
540               {
541               case 'H':
542                 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
543                         iconOffset, startRes, column, validRes, validEnd);
544                 break;
545
546               case 'E':
547                 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
548                         iconOffset, startRes, column, validRes, validEnd);
549                 break;
550
551               case 'S': // Stem case for RNA secondary structure
552               case 's': // and opposite direction
553                 drawStemAnnot(g, row_annotations, lastSSX, x, y,
554                         iconOffset, startRes, column, validRes, validEnd);
555                 break;
556
557               default:
558                 g.setColor(Color.gray);
559                 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth)
560                         - lastSSX, 2);
561
562                 break;
563               }
564             }
565             if (validRes)
566             {
567               lastSS = ss;
568             }
569             else
570             {
571               lastSS = ' ';
572             }
573             if (x > -1)
574             {
575               lastSSX = (x * charWidth);
576             }
577           }
578         }
579         column++;
580         x++;
581       }
582       if (column >= row_annotations.length)
583       {
584         column = row_annotations.length - 1;
585         validEnd = false;
586       }
587       else
588       {
589         validEnd = true;
590       }
591       if ((row_annotations == null) || (row_annotations.length <= column)
592               || (row_annotations[column] == null))
593       {
594         validRes = false;
595       }
596       else
597       {
598         validRes = true;
599       }
600
601       // x ++;
602
603       if (row.hasIcons)
604       {
605         switch (lastSS)
606         {
607         case 'H':
608           drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
609                   startRes, column, validRes, validEnd);
610           break;
611
612         case 'E':
613           drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
614                   startRes, column, validRes, validEnd);
615           break;
616         case 's':
617         case 'S': // Stem case for RNA secondary structure
618           drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
619                   startRes, column, validRes, validEnd);
620           break;
621         default:
622           drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
623                   startRes, column, validRes, validEnd);
624           break;
625         }
626       }
627
628       if (row.graph > 0 && row.graphHeight > 0)
629       {
630         if (row.graph == AlignmentAnnotation.LINE_GRAPH)
631         {
632           if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
633           {
634             float groupmax = -999999, groupmin = 9999999;
635             for (int gg = 0; gg < aa.length; gg++)
636             {
637               if (aa[gg].graphGroup != row.graphGroup)
638               {
639                 continue;
640               }
641
642               if (aa[gg] != row)
643               {
644                 aa[gg].visible = false;
645               }
646               if (aa[gg].graphMax > groupmax)
647               {
648                 groupmax = aa[gg].graphMax;
649               }
650               if (aa[gg].graphMin < groupmin)
651               {
652                 groupmin = aa[gg].graphMin;
653               }
654             }
655
656             for (int gg = 0; gg < aa.length; gg++)
657             {
658               if (aa[gg].graphGroup == row.graphGroup)
659               {
660                 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
661                         endRes, y, groupmin, groupmax, row.graphHeight);
662               }
663             }
664
665             graphGroupDrawn.set(row.graphGroup);
666           }
667           else
668           {
669             drawLineGraph(g, row, row_annotations, startRes, endRes, y,
670                     row.graphMin, row.graphMax, row.graphHeight);
671           }
672         }
673         else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
674         {
675           drawBarGraph(g, row, row_annotations, startRes, endRes,
676                   row.graphMin, row.graphMax, y);
677         }
678       }
679     } else {
680       if (clipst && !clipend)
681       {
682         clipend = true;
683       }
684     }// end if_in_visible_region
685       if (row.graph > 0 && row.hasText)
686       {
687         y += charHeight;
688       }
689
690       if (row.graph == 0)
691       {
692         y += aa[i].height;
693       }
694     }
695     if (debugRedraw)
696     {
697       if (canClip)
698       {
699         if (clipst)
700         {
701           System.err.println("Start clip at : " + yfrom + " (index " + f_i
702                   + ")");
703         }
704         if (clipend)
705         {
706           System.err.println("End clip at : " + yto + " (index " + f_to
707                   + ")");
708         }
709       }
710       ;
711       System.err.println("Annotation Rendering time:"
712               + (System.currentTimeMillis() - stime));
713     }
714     ;
715
716     return !usedFaded;
717   }
718
719   private final Color GLYPHLINE_COLOR = Color.gray;
720
721   private final Color SHEET_COLOUR = Color.green;
722
723   private final Color HELIX_COLOUR = Color.red;
724
725   private final Color STEM_COLOUR = Color.blue;
726
727   public void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX,
728           int x, int y, int iconOffset, int startRes, int column,
729           boolean validRes, boolean validEnd)
730   {
731     g.setColor(GLYPHLINE_COLOR);
732     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
733   }
734
735   public void drawSheetAnnot(Graphics g, Annotation[] row, int lastSSX,
736           int x, int y, int iconOffset, int startRes, int column,
737           boolean validRes, boolean validEnd)
738   {
739     g.setColor(SHEET_COLOUR);
740
741     if (!validEnd || !validRes || row == null || row[column] == null
742             || row[column].secondaryStructure != 'E')
743     {
744       g.fillRect(lastSSX, y + 4 + iconOffset,
745               (x * charWidth) - lastSSX - 4, 7);
746       g.fillPolygon(new int[]
747       { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
748               new int[]
749               { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
750               3);
751     }
752     else
753     {
754       g.fillRect(lastSSX, y + 4 + iconOffset,
755               (x + 1) * charWidth - lastSSX, 7);
756     }
757
758   }
759
760   public void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX,
761           int x, int y, int iconOffset, int startRes, int column,
762           boolean validRes, boolean validEnd)
763   {
764     g.setColor(HELIX_COLOUR);
765
766     int sCol = (lastSSX / charWidth) + startRes;
767     int x1 = lastSSX;
768     int x2 = (x * charWidth);
769
770     if (MAC)
771     {
772       int ofs = charWidth / 2;
773       // Off by 1 offset when drawing rects and ovals
774       // to offscreen image on the MAC
775       g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
776       if (sCol == 0 || row[sCol - 1] == null
777               || row[sCol - 1].secondaryStructure != 'H')
778       {
779       }
780       else
781       {
782         // g.setColor(Color.orange);
783         g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
784                 0, 0);
785       }
786       if (!validRes || row[column] == null
787               || row[column].secondaryStructure != 'H')
788       {
789
790       }
791       else
792       {
793         // g.setColor(Color.magenta);
794         g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset, x2 - x1 - ofs
795                 + 1, 8, 0, 0);
796
797       }
798
799       return;
800     }
801
802     if (sCol == 0 || row[sCol - 1] == null
803             || row[sCol - 1].secondaryStructure != 'H')
804     {
805       g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
806       x1 += charWidth / 2;
807     }
808
809     if (!validRes || row[column] == null
810             || row[column].secondaryStructure != 'H')
811     {
812       g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
813               8, 270, 180);
814       x2 -= charWidth / 2;
815     }
816
817     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
818   }
819
820   public void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
821           Annotation[] aa_annotations, int sRes, int eRes, int y,
822           float min, float max, int graphHeight)
823   {
824     if (sRes > aa_annotations.length)
825     {
826       return;
827     }
828
829     int x = 0;
830
831     // Adjustment for fastpaint to left
832     if (eRes < endRes)
833     {
834       eRes++;
835     }
836
837     eRes = Math.min(eRes, aa_annotations.length);
838
839     if (sRes == 0)
840     {
841       x++;
842     }
843
844     int y1 = y, y2 = y;
845     float range = max - min;
846
847     // //Draw origin
848     if (min < 0)
849     {
850       y2 = y - (int) ((0 - min / range) * graphHeight);
851     }
852
853     g.setColor(Color.gray);
854     g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
855
856     eRes = Math.min(eRes, aa_annotations.length);
857
858     int column;
859     int aaMax = aa_annotations.length - 1;
860
861     while (x < eRes - sRes)
862     {
863       column = sRes + x;
864       if (hasHiddenColumns)
865       {
866         column = columnSelection.adjustForHiddenColumns(column);
867       }
868
869       if (column > aaMax)
870       {
871         break;
872       }
873
874       if (aa_annotations[column] == null
875               || aa_annotations[column - 1] == null)
876       {
877         x++;
878         continue;
879       }
880
881       if (aa_annotations[column].colour == null)
882         g.setColor(Color.black);
883       else
884         g.setColor(aa_annotations[column].colour);
885
886       y1 = y
887               - (int) (((aa_annotations[column - 1].value - min) / range) * graphHeight);
888       y2 = y
889               - (int) (((aa_annotations[column].value - min) / range) * graphHeight);
890
891       g.drawLine(x * charWidth - charWidth / 2, y1, x * charWidth
892               + charWidth / 2, y2);
893       x++;
894     }
895
896     if (_aa.threshold != null)
897     {
898       g.setColor(_aa.threshold.colour);
899       Graphics2D g2 = (Graphics2D) g;
900       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
901               BasicStroke.JOIN_ROUND, 3f, new float[]
902               { 5f, 3f }, 0f));
903
904       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
905       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
906       g2.setStroke(new BasicStroke());
907     }
908   }
909
910   public void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
911           Annotation[] aa_annotations, int sRes, int eRes, float min,
912           float max, int y)
913   {
914     if (sRes > aa_annotations.length)
915     {
916       return;
917     }
918     Font ofont = g.getFont();
919     eRes = Math.min(eRes, aa_annotations.length);
920
921     int x = 0, y1 = y, y2 = y;
922
923     float range = max - min;
924
925     if (min < 0)
926     {
927       y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
928     }
929
930     g.setColor(Color.gray);
931
932     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
933
934     int column;
935     int aaMax = aa_annotations.length - 1;
936     boolean renderHistogram = true, renderProfile = true, normaliseProfile = false;
937     // if (aa.autoCalculated && aa.label.startsWith("Consensus"))
938     {
939       // TODO: generalise this to have render styles for consensus/profile data
940       if (_aa.groupRef != null)
941       {
942         renderHistogram = _aa.groupRef.isShowConsensusHistogram();
943         renderProfile = _aa.groupRef.isShowSequenceLogo();
944         normaliseProfile = _aa.groupRef.isNormaliseSequenceLogo();
945       }
946       else
947       {
948         renderHistogram = av_renderHistogram;
949         renderProfile = av_renderProfile;
950         normaliseProfile = av_normaliseProfile;
951       }
952     }
953     while (x < eRes - sRes)
954     {
955       column = sRes + x;
956       if (hasHiddenColumns)
957       {
958         column = columnSelection.adjustForHiddenColumns(column);
959       }
960
961       if (column > aaMax)
962       {
963         break;
964       }
965
966       if (aa_annotations[column] == null)
967       {
968         x++;
969         continue;
970       }
971       if (aa_annotations[column].colour == null)
972         g.setColor(Color.black);
973       else
974         g.setColor(aa_annotations[column].colour);
975
976       y1 = y
977               - (int) (((aa_annotations[column].value - min) / (range)) * _aa.graphHeight);
978
979       if (renderHistogram)
980       {
981         if (y1 - y2 > 0)
982         {
983           g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
984         }
985         else
986         {
987           g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
988         }
989       }
990       // draw profile if available
991       if (renderProfile)
992       {
993
994         int profl[] = getProfileFor(_aa, column);
995         // just try to draw the logo if profl is not null
996         if (profl != null && profl[1] != 0)
997         {
998           float ht = normaliseProfile ? y - _aa.graphHeight : y1;
999           double htn = normaliseProfile ? _aa.graphHeight : (y2 - y1);// aa.graphHeight;
1000           double hght;
1001           float wdth;
1002           double ht2 = 0;
1003           char[] dc;
1004
1005           /**
1006            * profl.length == 74 indicates that the profile of a secondary
1007            * structure conservation row was accesed. Therefore dc gets length 2,
1008            * to have space for a basepair instead of just a single nucleotide
1009            */
1010           if (profl.length == 74)
1011           {
1012             dc = new char[2];
1013           }
1014           else
1015           {
1016             dc = new char[1];
1017           }
1018           LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1019           double scale = 1f / (normaliseProfile ? profl[1] : 100f);
1020           float ofontHeight = 1f / lm.getAscent();// magnify to fill box
1021           double scl = 0.0;
1022           for (int c = 2; c < profl[0];)
1023           {
1024             dc[0] = (char) profl[c++];
1025
1026             if (_aa.label.startsWith("StrucConsensus"))
1027             {
1028               dc[1] = (char) profl[c++];
1029             }
1030
1031             wdth = charWidth;
1032             wdth /= fm.charsWidth(dc, 0, dc.length);
1033
1034             ht += scl;
1035             {
1036               scl = htn * scale * profl[c++];
1037               lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics()
1038                       .getFontRenderContext());
1039               g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(
1040                       wdth, scl / lm.getAscent())));
1041               lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
1042
1043               // Debug - render boxes around characters
1044               // g.setColor(Color.red);
1045               // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1046               // (int)(scl));
1047               // g.setColor(profcolour.findColour(dc[0]).darker());
1048               g.setColor(profcolour.findColour(dc[0], column, null));
1049
1050               hght = (ht + (scl - lm.getDescent() - lm.getBaselineOffsets()[lm
1051                       .getBaselineIndex()]));
1052
1053               g.drawChars(dc, 0, dc.length, x * charWidth, (int) hght);
1054             }
1055           }
1056           g.setFont(ofont);
1057         }
1058       }
1059       x++;
1060     }
1061     if (_aa.threshold != null)
1062     {
1063       g.setColor(_aa.threshold.colour);
1064       Graphics2D g2 = (Graphics2D) g;
1065       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1066               BasicStroke.JOIN_ROUND, 3f, new float[]
1067               { 5f, 3f }, 0f));
1068
1069       y2 = (int) (y - ((_aa.threshold.value - min) / range)
1070               * _aa.graphHeight);
1071       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1072       g2.setStroke(new BasicStroke());
1073     }
1074   }
1075
1076   // used by overview window
1077   public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1078           Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1079   {
1080     eRes = Math.min(eRes, aa_annotations.length);
1081     g.setColor(Color.white);
1082     g.fillRect(0, 0, width, y);
1083     g.setColor(new Color(0, 0, 180));
1084
1085     int x = 0, height;
1086
1087     for (int j = sRes; j < eRes; j++)
1088     {
1089       if (aa_annotations[j] != null)
1090       {
1091         if (aa_annotations[j].colour == null)
1092           g.setColor(Color.black);
1093         else
1094           g.setColor(aa_annotations[j].colour);
1095
1096         height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1097         if (height > y)
1098         {
1099           height = y;
1100         }
1101
1102         g.fillRect(x, y - height, charWidth, height);
1103       }
1104       x += charWidth;
1105     }
1106   }
1107 }