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