source formatting
[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.Hashtable;
39
40 import com.stevesoft.pat.Regex;
41
42 public class AnnotationRenderer
43 {
44
45   public AnnotationRenderer()
46   {
47     // TODO Auto-generated constructor stub
48   }
49
50   public void drawStemAnnot(Graphics g, Annotation[] row_annotations,
51           int lastSSX, int x, int y, int iconOffset, int startRes,
52           int column, boolean validRes, boolean validEnd)
53   {
54     g.setColor(STEM_COLOUR);
55     int sCol = (lastSSX / charWidth) + startRes;
56     int x1 = lastSSX;
57     int x2 = (x * charWidth);
58     Regex closeparen = new Regex("(\\))");
59
60     String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
61             : row_annotations[column - 1].displayCharacter;
62
63     boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
64             || !dc.equals(row_annotations[sCol - 1].displayCharacter);
65     boolean diffdownstream = !validRes || !validEnd
66             || row_annotations[column] == null
67             || !dc.equals(row_annotations[column].displayCharacter);
68     // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
69     // If a closing base pair half of the stem, display a backward arrow
70     if (column > 0 && closeparen.search(dc))
71     {
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         
90       // display a forward arrow
91       if (diffdownstream)
92       {
93         g.fillPolygon(new int[]
94         { x2 - 5, x2 - 5, x2 }, new int[]
95         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
96         x2 -= 5;
97       }
98       if (diffupstream)
99       {
100         x1 += 1;
101       }
102     }
103     // draw arrow body
104     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
105   }
106
107   private int charWidth, endRes, charHeight;
108
109   private boolean validCharWidth, hasHiddenColumns;
110
111   private FontMetrics fm;
112
113   private final boolean MAC = new jalview.util.Platform().isAMac();
114
115   boolean av_renderHistogram = true, av_renderProfile = true,
116           av_normaliseProfile = false;
117
118   ColourSchemeI profcolour = null;
119
120   private ColumnSelection columnSelection;
121
122   private Hashtable[] hconsensus;
123
124   private Hashtable[] hStrucConsensus;
125
126   private boolean av_ignoreGapsConsensus;
127
128   /**
129    * attributes set from AwtRenderPanelI
130    */
131   /**
132    * old image used when data is currently being calculated and cannot be
133    * rendered
134    */
135   private Image fadedImage;
136
137   /**
138    * panel being rendered into
139    */
140   private ImageObserver annotationPanel;
141
142   /**
143    * width of image to render in panel
144    */
145   private int imgWidth;
146
147   
148   public void drawNotCanonicalAnnot(Graphics g, Color nonCanColor, Annotation[] row_annotations,
149           int lastSSX, int x, int y, int iconOffset, int startRes,
150           int column, boolean validRes, boolean validEnd)
151   {
152         //System.out.println(nonCanColor);
153         
154     g.setColor(nonCanColor);
155     int sCol = (lastSSX / charWidth) + startRes;
156     int x1 = lastSSX;
157     int x2 = (x * charWidth);
158     Regex closeparen = new Regex("}|]|<|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z");
159     
160     String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
161             : row_annotations[column - 1].displayCharacter;
162
163     boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
164             || !dc.equals(row_annotations[sCol - 1].displayCharacter);
165     boolean diffdownstream = !validRes || !validEnd
166             || row_annotations[column] == null
167             || !dc.equals(row_annotations[column].displayCharacter);
168     // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
169     // If a closing base pair half of the stem, display a backward arrow
170     if (column > 0 && closeparen.search(dc))//  closeletter_b.search(dc)||closeletter_c.search(dc)||closeletter_d.search(dc)||closecrochet.search(dc)) )
171     {
172         
173       if (diffupstream)
174       // if (validRes && column>1 && row_annotations[column-2]!=null &&
175       // dc.equals(row_annotations[column-2].displayCharacter))
176       {
177         g.fillPolygon(new int[]
178         { lastSSX + 5, lastSSX + 5, lastSSX }, new int[]
179         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
180         x1 += 5;
181       }
182       if (diffdownstream)
183       {
184         x2 -= 1;
185       }
186     }
187     else
188     {
189         
190       // display a forward arrow
191       if (diffdownstream)
192       {
193         g.fillPolygon(new int[]
194         { x2 - 5, x2 - 5, x2 }, new int[]
195         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
196         x2 -= 5;
197       }
198       if (diffupstream)
199       {
200         x1 += 1;
201       }
202     }
203     // draw arrow body
204     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
205   }
206   // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
207   // av)
208   public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
209           AlignViewportI av)
210   {
211     fm = annotPanel.getFontMetrics();
212     annotationPanel = annotPanel;
213     fadedImage = annotPanel.getFadedImage();
214     imgWidth = annotPanel.getFadedImageWidth();
215     updateFromAlignViewport(av);
216   }
217
218   public void updateFromAlignViewport(AlignViewportI av)
219   {
220     charWidth = av.getCharWidth();
221     endRes = av.getEndRes();
222     charHeight = av.getCharHeight();
223     hasHiddenColumns = av.hasHiddenColumns();
224     validCharWidth = av.isValidCharWidth();
225     av_renderHistogram = av.isShowConsensusHistogram();
226     av_renderProfile = av.isShowSequenceLogo();
227     av_normaliseProfile = av.isNormaliseSequenceLogo();
228     profcolour = av.getGlobalColourScheme();
229     if (profcolour == null)
230     {
231       // Set the default colour for sequence logo if the alignnent has no
232       // colourscheme set
233       profcolour = av.getAlignment().isNucleotide() ? new jalview.schemes.NucleotideColourScheme()
234               : new jalview.schemes.ZappoColourScheme();
235     }
236     columnSelection = av.getColumnSelection();
237     hconsensus = av.getSequenceConsensusHash();// hconsensus;
238     hStrucConsensus = av.getRnaStructureConsensusHash(); // hStrucConsensus;
239     av_ignoreGapsConsensus = av.getIgnoreGapsConsensus();
240   }
241
242   public int[] getProfileFor(AlignmentAnnotation aa, int column)
243   {
244     // TODO : consider refactoring the global alignment calculation
245     // properties/rendering attributes as a global 'alignment group' which holds
246     // all vis settings for the alignment as a whole rather than a subset
247     //
248     if (aa.autoCalculated && aa.label.startsWith("Consensus"))
249     {
250       if (aa.groupRef != null && aa.groupRef.consensusData != null
251               && aa.groupRef.isShowSequenceLogo())
252       {
253         return AAFrequency.extractProfile(
254                 aa.groupRef.consensusData[column],
255                 aa.groupRef.getIgnoreGapsConsensus());
256       }
257       // TODO extend annotation row to enable dynamic and static profile data to
258       // be stored
259       if (aa.groupRef == null && aa.sequenceRef == null && av_renderProfile)
260       {
261         return AAFrequency.extractProfile(hconsensus[column],
262                 av_ignoreGapsConsensus);
263       }
264     }
265     else
266     {
267       if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
268       {
269         // TODO implement group structure consensus
270         /*
271          * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
272          * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
273          * group selections return StructureFrequency.extractProfile(
274          * aa.groupRef.consensusData[column], aa.groupRef
275          * .getIgnoreGapsConsensus()); }
276          */
277         // TODO extend annotation row to enable dynamic and static profile data
278         // to
279         // be stored
280         if (aa.groupRef == null && aa.sequenceRef == null
281                 && av_renderProfile && hStrucConsensus != null
282                 && hStrucConsensus.length > column)
283         {
284           return StructureFrequency.extractProfile(hStrucConsensus[column],
285                   av_ignoreGapsConsensus);
286         }
287       }
288     }
289     return null;
290   }
291
292   /**
293    * Render the annotation rows associated with an alignment.
294    * 
295    * @param annotPanel
296    *          container frame
297    * @param av
298    *          data and view settings to render
299    * @param g
300    *          destination for graphics
301    * @param activeRow
302    *          row where a mouse event occured (or -1)
303    * @param startRes
304    *          first column that will be drawn
305    * @param endRes
306    *          last column that will be drawn
307    * @return true if the fadedImage was used for any alignment annotation rows
308    *         currently being calculated
309    */
310   public boolean drawComponent(AwtRenderPanelI annotPanel,
311           AlignViewportI av, Graphics g, int activeRow, int startRes,
312           int endRes)
313   {
314     boolean usedFaded = false;
315     // NOTES:
316     // AnnotationPanel needs to implement: ImageObserver, access to
317     // AlignViewport
318     updateFromAwtRenderPanel(annotPanel, av);
319     fm = g.getFontMetrics();
320     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
321
322     int x = 0, y = 0;
323     int column = 0;
324     char lastSS;
325     int lastSSX;
326     int iconOffset = 0;
327     boolean validRes = false;
328     boolean validEnd = false;
329     boolean labelAllCols = false;
330     boolean centreColLabels, centreColLabelsDef = av
331             .getCentreColumnLabels();
332     boolean scaleColLabel = false;
333     boolean[] graphGroupDrawn = new boolean[aa.length];
334     int charOffset = 0; // offset for a label
335     float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a
336     // column.
337     Font ofont = g.getFont();
338     // \u03B2 \u03B1
339     for (int i = 0; i < aa.length; i++)
340     {
341       AlignmentAnnotation row = aa[i];
342       Annotation[] row_annotations = row.annotations;
343       if (!row.visible)
344       {
345         continue;
346       }
347       centreColLabels = row.centreColLabels || centreColLabelsDef;
348       labelAllCols = row.showAllColLabels;
349       scaleColLabel = row.scaleColLabel;
350       lastSS = ' ';
351       lastSSX = 0;
352       if (row.graph > 0)
353       {
354         if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
355         {
356           continue;
357         }
358
359         // this is so that we draw the characters below the graph
360         y += row.height;
361
362         if (row.hasText)
363         {
364           iconOffset = charHeight - fm.getDescent();
365           y -= charHeight;
366         }
367       }
368       else if (row.hasText)
369       {
370         iconOffset = charHeight - fm.getDescent();
371
372       }
373       else
374       {
375         iconOffset = 0;
376       }
377
378       if (row.autoCalculated && av.isCalculationInProgress(row))
379       {
380         y += charHeight;
381         usedFaded = true;
382         g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
383                 - row.height, imgWidth, y, annotationPanel);
384         g.setColor(Color.black);
385         // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
386
387         continue;
388       }
389
390       /*
391        * else if (annotationPanel.av.updatingConservation &&
392        * aa[i].label.equals("Conservation")) {
393        * 
394        * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
395        * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
396        * annotationPanel.imgWidth, y, annotationPanel);
397        * 
398        * g.setColor(Color.black); //
399        * g.drawString("Calculating Conservation.....",20, y-row.height/2);
400        * 
401        * continue; } else if (annotationPanel.av.updatingConservation &&
402        * aa[i].label.equals("Quality")) {
403        * 
404        * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
405        * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
406        * annotationPanel.imgWidth, y, annotationPanel); g.setColor(Color.black);
407        * // / g.drawString("Calculating Quality....",20, y-row.height/2);
408        * 
409        * continue; }
410        */
411       // first pass sets up state for drawing continuation from left-hand column
412       // of startRes
413       x = (startRes == 0) ? 0 : -1;
414       while (x < endRes - startRes)
415       {
416         if (hasHiddenColumns)
417         {
418           column = columnSelection.adjustForHiddenColumns(startRes + x);
419           if (column > row_annotations.length - 1)
420           {
421             break;
422           }
423         }
424         else
425         {
426           column = startRes + x;
427         }
428
429         if ((row_annotations == null) || (row_annotations.length <= column)
430                 || (row_annotations[column] == null))
431         {
432           validRes = false;
433         }
434         else
435         {
436           validRes = true;
437         }
438         if (x > -1)
439         {
440           if (activeRow == i)
441           {
442             g.setColor(Color.red);
443
444             if (columnSelection != null)
445             {
446               for (int n = 0; n < columnSelection.size(); n++)
447               {
448                 int v = columnSelection.columnAt(n);
449
450                 if (v == column)
451                 {
452                   g.fillRect(x * charWidth, y, charWidth, charHeight);
453                 }
454               }
455             }
456           }
457           if (!row.isValidStruc())
458           {
459             g.setColor(Color.orange);
460             g.fillRect((int) row.getInvalidStrucPos() * charWidth, y,
461                     charWidth, charHeight);
462           }
463           if (validCharWidth
464                   && validRes
465                   && row_annotations[column].displayCharacter != null
466                   && (row_annotations[column].displayCharacter.length() > 0))
467           {
468
469             if (centreColLabels || scaleColLabel)
470             {
471               fmWidth = fm.charsWidth(
472                       row_annotations[column].displayCharacter
473                               .toCharArray(), 0,
474                       row_annotations[column].displayCharacter.length());
475
476               if (scaleColLabel)
477               {
478                 // justify the label and scale to fit in column
479                 if (fmWidth > charWidth)
480                 {
481                   // scale only if the current font isn't already small enough
482                   fmScaling = charWidth;
483                   fmScaling /= fmWidth;
484                   g.setFont(ofont.deriveFont(AffineTransform
485                           .getScaleInstance(fmScaling, 1.0)));
486                   // and update the label's width to reflect the scaling.
487                   fmWidth = charWidth;
488                 }
489               }
490             }
491             else
492             {
493               fmWidth = fm
494                       .charWidth(row_annotations[column].displayCharacter
495                               .charAt(0));
496             }
497             charOffset = (int) ((charWidth - fmWidth) / 2f);
498
499             if (row_annotations[column].colour == null)
500               g.setColor(Color.black);
501             else
502               g.setColor(row_annotations[column].colour);
503
504             if (column == 0 || row.graph > 0)
505             {
506               g.drawString(row_annotations[column].displayCharacter,
507                       (x * charWidth) + charOffset, y + iconOffset);
508             }
509             else if (row_annotations[column - 1] == null
510                     || (labelAllCols
511                             || !row_annotations[column].displayCharacter
512                                     .equals(row_annotations[column - 1].displayCharacter) || (row_annotations[column].displayCharacter
513                             .length() < 2 && row_annotations[column].secondaryStructure == ' ')))
514             {
515                 g.drawString(row_annotations[column].displayCharacter
516                           , x
517                       * charWidth + charOffset, y + iconOffset);
518             }
519             g.setFont(ofont);
520           }
521         }
522         if (row.hasIcons)
523         {
524           char ss = validRes ? row_annotations[column].secondaryStructure
525                   : '-';
526           
527           if (ss == '(')
528           {
529             // distinguish between forward/backward base-pairing
530             if (row_annotations[column].displayCharacter.indexOf(')') > -1)
531             {
532             
533               ss = ')';
534               
535             }
536           }
537            if (ss == '[')
538           {
539             if ((row_annotations[column].displayCharacter.indexOf(']') > -1))
540             {
541                 ss = ']';
542                 
543                 
544             }
545           }
546            if (ss == '{')
547            {
548              // distinguish between forward/backward base-pairing
549              if (row_annotations[column].displayCharacter.indexOf('}') > -1)
550              {
551                ss = '}';
552                
553                
554              }
555            }
556            if (ss == '<')
557            {
558              // distinguish between forward/backward base-pairing
559              if (row_annotations[column].displayCharacter.indexOf('<') > -1)
560              {
561                ss = '>';
562                
563                
564              }
565            }
566            if (ss == 'A')
567            {
568              // distinguish between forward/backward base-pairing
569              if (row_annotations[column].displayCharacter.indexOf('a') > -1)
570              {
571                ss = 'a';
572               
573                
574              }
575            }
576            
577            if (ss == 'B')
578            {
579              // distinguish between forward/backward base-pairing
580              if (row_annotations[column].displayCharacter.indexOf('b') > -1)
581              {
582                ss = 'b';
583                
584              }
585            }
586            
587            if (ss == 'C')
588            {
589              // distinguish between forward/backward base-pairing
590              if (row_annotations[column].displayCharacter.indexOf('c') > -1)
591              {
592                ss = 'c';
593                
594              }
595            }
596            if (ss == 'D')
597            {
598              // distinguish between forward/backward base-pairing
599              if (row_annotations[column].displayCharacter.indexOf('d') > -1)
600              {
601                ss = 'd';
602                
603              }
604            }
605            if (ss == '1')
606            {
607              // distinguish between forward/backward base-pairing
608              if (row_annotations[column].displayCharacter.indexOf('e') > -1)
609              {
610                ss = 'e';
611                
612              }
613            }
614            if (ss == 'F')
615            {
616              // distinguish between forward/backward base-pairing
617              if (row_annotations[column].displayCharacter.indexOf('f') > -1)
618              {
619                ss = 'f';
620                
621              }
622            }
623            if (ss == 'G')
624            {
625              // distinguish between forward/backward base-pairing
626              if (row_annotations[column].displayCharacter.indexOf('g') > -1)
627              {
628                ss = 'g';
629                
630              }
631            }
632            if (ss == '2')
633            {
634              // distinguish between forward/backward base-pairing
635              if (row_annotations[column].displayCharacter.indexOf('h') > -1)
636              {
637                ss = 'h';
638                
639              }
640            }
641            if (ss == 'I')
642            {
643              // distinguish between forward/backward base-pairing
644              if (row_annotations[column].displayCharacter.indexOf('i') > -1)
645              {
646                ss = 'i';
647                
648              }
649            }
650            if (ss == 'J')
651            {
652              // distinguish between forward/backward base-pairing
653              if (row_annotations[column].displayCharacter.indexOf('j') > -1)
654              {
655                ss = 'j';
656                
657              }
658            }
659            if (ss == 'K')
660            {
661              // distinguish between forward/backward base-pairing
662              if (row_annotations[column].displayCharacter.indexOf('k') > -1)
663              {
664                ss = 'k';
665                
666              }
667            }
668            if (ss == 'L')
669            {
670              // distinguish between forward/backward base-pairing
671              if (row_annotations[column].displayCharacter.indexOf('l') > -1)
672              {
673                ss = 'l';
674                
675              }
676            }
677            if (ss == 'M')
678            {
679              // distinguish between forward/backward base-pairing
680              if (row_annotations[column].displayCharacter.indexOf('m') > -1)
681              {
682                ss = 'm';
683                
684              }
685            }
686            if (ss == 'N')
687            {
688              // distinguish between forward/backward base-pairing
689              if (row_annotations[column].displayCharacter.indexOf('n') > -1)
690              {
691                ss = 'n';
692                
693              }
694            }
695            if (ss == 'O')
696            {
697              // distinguish between forward/backward base-pairing
698              if (row_annotations[column].displayCharacter.indexOf('o') > -1)
699              {
700                ss = 'o';
701                
702              }
703            }
704            if (ss == 'P')
705            {
706              // distinguish between forward/backward base-pairing
707              if (row_annotations[column].displayCharacter.indexOf('p') > -1)
708              {
709                ss = 'p';
710                
711              }
712            }
713            if (ss == 'Q')
714            {
715              // distinguish between forward/backward base-pairing
716              if (row_annotations[column].displayCharacter.indexOf('q') > -1)
717              {
718                ss = 'q';
719                
720              }
721            }
722            if (ss == 'R')
723            {
724              // distinguish between forward/backward base-pairing
725              if (row_annotations[column].displayCharacter.indexOf('r') > -1)
726              {
727                ss = 'r';
728                
729              }
730            }
731            if (ss == 'S')
732            {
733              // distinguish between forward/backward base-pairing
734              if (row_annotations[column].displayCharacter.indexOf('s') > -1)
735              {
736                ss = 's';
737                
738              }
739            }
740            if (ss == 'T')
741            {
742              // distinguish between forward/backward base-pairing
743              if (row_annotations[column].displayCharacter.indexOf('t') > -1)
744              {
745                ss = 't';
746                
747              }
748            }
749            if (ss == 'U')
750            {
751              // distinguish between forward/backward base-pairing
752              if (row_annotations[column].displayCharacter.indexOf('u') > -1)
753              {
754                ss = 'u';
755                
756              }
757            }
758            if (ss == 'V')
759            {
760              // distinguish between forward/backward base-pairing
761              if (row_annotations[column].displayCharacter.indexOf('v') > -1)
762              {
763                ss = 'v';
764                
765              }
766            }
767            if (ss == 'W')
768            {
769              // distinguish between forward/backward base-pairing
770              if (row_annotations[column].displayCharacter.indexOf('w') > -1)
771              {
772                ss = 'w';
773                
774              }
775            }
776            if (ss == 'X')
777            {
778              // distinguish between forward/backward base-pairing
779              if (row_annotations[column].displayCharacter.indexOf('x') > -1)
780              {
781                ss = 'x';
782                
783              }
784            }
785            if (ss == 'Y')
786            {
787              // distinguish between forward/backward base-pairing
788              if (row_annotations[column].displayCharacter.indexOf('y') > -1)
789              {
790                ss = 'y';
791                
792              }
793            }
794            if (ss == 'Z')
795            {
796              // distinguish between forward/backward base-pairing
797              if (row_annotations[column].displayCharacter.indexOf('z') > -1)
798              {
799                ss = 'z';
800                
801              }
802            }
803           if (!validRes || (ss != lastSS))
804           {
805             if (x > -1)
806             {
807               switch (lastSS)
808               {
809               case 'H':
810                 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
811                         iconOffset, startRes, column, validRes, validEnd);
812                 break;
813
814               case 'E':
815                 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
816                         iconOffset, startRes, column, validRes, validEnd);
817                 break;
818
819               case '(': // Stem case for RNA secondary structure
820               case ')': // and opposite direction
821                 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
822                         column, validRes, validEnd);
823 //              case 'S': // Stem case for RNA secondary structure
824 //              case 's': // and opposite direction
825 //                drawStemAnnot(g, row_annotations, lastSSX, x, y,
826 //                        iconOffset, startRes, column, validRes, validEnd);
827                 break;
828               case '{':
829               case '}':
830               case '[':
831               case ']':
832               case '>':
833               case '<':
834               case 'A':
835               case 'a':
836               case 'B':
837               case 'b':
838               case 'C':
839               case 'c':
840               case 'D':
841               case 'd':
842               case '1':
843               case 'e':
844               case 'F':
845               case 'f':
846               case 'G':
847               case 'g':
848               case '2':
849               case 'h':
850               case 'I':
851               case 'i':
852               case 'J':
853               case 'j':
854               case 'K':
855               case 'k':
856               case 'L':
857               case 'l':
858               case 'M':
859               case 'm':
860               case 'N':
861               case 'n':
862               case 'O':
863               case 'o':
864               case 'P':
865               case 'p':
866               case 'Q':
867               case 'q':
868               case 'R':
869               case 'r':
870               case 'S':
871               case 's':
872               case 'T':
873               case 't':
874               case 'U':
875               case 'u':
876               case 'V':
877               case 'v':
878               case 'W':
879               case 'w':
880               case 'X':
881               case 'x':
882               case 'Y':
883               case 'y':
884               case 'Z':
885               case 'z':
886                   //System.out.println(lastSS);
887                   Color nonCanColor= getNotCanonicalColor(lastSS);
888                   drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX, x, y, iconOffset, startRes,
889                           column, validRes, validEnd);
890                   break;
891               default:
892                 g.setColor(Color.gray);
893                 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth)
894                         - lastSSX, 2);
895
896                 break;
897               }
898             }
899             if (validRes)
900             {
901               lastSS = ss;
902             }
903             else
904             {
905               lastSS = ' ';
906             }
907             if (x > -1)
908             {
909               lastSSX = (x * charWidth);
910             }
911           }
912         }
913         column++;
914         x++;
915       }
916       if (column >= row_annotations.length)
917       {
918         column = row_annotations.length - 1;
919         validEnd = false;
920       }
921       else
922       {
923         validEnd = true;
924       }
925       if ((row_annotations == null) || (row_annotations.length <= column)
926               || (row_annotations[column] == null))
927       {
928         validRes = false;
929       }
930       else
931       {
932         validRes = true;
933       }
934
935       // x ++;
936
937       if (row.hasIcons)
938       {
939         switch (lastSS)
940         {
941         case 'H':
942           drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
943                   startRes, column, validRes, validEnd);
944           break;
945
946         case 'E':
947           drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
948                   startRes, column, validRes, validEnd);
949           break;
950         case 's':
951         case 'S': // Stem case for RNA secondary structure
952           drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
953                   startRes, column, validRes, validEnd);
954           break;
955         case '{':
956         case '}':
957         case '[':
958         case ']':
959         case '>':
960         case '<':
961         case 'A':
962         case 'a':
963         case 'B':
964         case 'b':
965         case 'C':
966         case 'c':
967         case 'D':
968         case 'd':
969         case '1':
970         case 'e':
971         case 'F':
972         case 'f':
973         case 'G':
974         case 'g':
975         case '2':
976         case 'h':
977         case 'I':
978         case 'i':
979         case 'J':
980         case 'j':
981         case 'K':
982         case 'k':
983         case 'L':
984         case 'l':
985         case 'M':
986         case 'm':
987         case 'N':
988         case 'n':
989         case 'O':
990         case 'o':
991         case 'P':
992         case 'p':
993         case 'Q':
994         case 'q':
995         case 'R':
996         case 'r':
997         case 'T':
998         case 't':
999         case 'U':
1000         case 'u':
1001         case 'V':
1002         case 'v':
1003         case 'W':
1004         case 'w':
1005         case 'X':
1006         case 'x':
1007         case 'Y':
1008         case 'y':
1009         case 'Z':
1010         case 'z':
1011                 //System.out.println(lastSS);
1012           Color nonCanColor = getNotCanonicalColor(lastSS);
1013           drawNotCanonicalAnnot(g,nonCanColor, row_annotations, lastSSX, x, y, iconOffset, startRes,
1014                     column, validRes, validEnd);
1015           break;
1016         default:
1017           drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1018                   startRes, column, validRes, validEnd);
1019           break;
1020         }
1021       }
1022
1023       if (row.graph > 0 && row.graphHeight > 0)
1024       {
1025         if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1026         {
1027           if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
1028           {
1029             float groupmax = -999999, groupmin = 9999999;
1030             for (int gg = 0; gg < aa.length; gg++)
1031             {
1032               if (aa[gg].graphGroup != row.graphGroup)
1033               {
1034                 continue;
1035               }
1036
1037               if (aa[gg] != row)
1038               {
1039                 aa[gg].visible = false;
1040               }
1041               if (aa[gg].graphMax > groupmax)
1042               {
1043                 groupmax = aa[gg].graphMax;
1044               }
1045               if (aa[gg].graphMin < groupmin)
1046               {
1047                 groupmin = aa[gg].graphMin;
1048               }
1049             }
1050
1051             for (int gg = 0; gg < aa.length; gg++)
1052             {
1053               if (aa[gg].graphGroup == row.graphGroup)
1054               {
1055                 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1056                         endRes, y, groupmin, groupmax, row.graphHeight);
1057               }
1058             }
1059
1060             graphGroupDrawn[row.graphGroup] = true;
1061           }
1062           else
1063           {
1064             drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1065                     row.graphMin, row.graphMax, row.graphHeight);
1066           }
1067         }
1068         else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1069         {
1070           drawBarGraph(g, row, row_annotations, startRes, endRes,
1071                   row.graphMin, row.graphMax, y);
1072         }
1073       }
1074
1075       if (row.graph > 0 && row.hasText)
1076       {
1077         y += charHeight;
1078       }
1079
1080       if (row.graph == 0)
1081       {
1082         y += aa[i].height;
1083       }
1084     }
1085     return !usedFaded;
1086   }
1087
1088   private final Color GLYPHLINE_COLOR = Color.gray;
1089
1090   private final Color SHEET_COLOUR = Color.green;
1091
1092   private final Color HELIX_COLOUR = Color.red;
1093
1094   private final Color STEM_COLOUR = Color.blue;
1095   
1096   private  Color sdNOTCANONICAL_COLOUR;
1097
1098   public void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX,
1099           int x, int y, int iconOffset, int startRes, int column,
1100           boolean validRes, boolean validEnd)
1101   {
1102     g.setColor(GLYPHLINE_COLOR);
1103     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1104   }
1105
1106   public void drawSheetAnnot(Graphics g, Annotation[] row, int lastSSX,
1107           int x, int y, int iconOffset, int startRes, int column,
1108           boolean validRes, boolean validEnd)
1109   {
1110     g.setColor(SHEET_COLOUR);
1111
1112     if (!validEnd || !validRes || row == null || row[column] == null
1113             || row[column].secondaryStructure != 'E')
1114     {
1115       g.fillRect(lastSSX, y + 4 + iconOffset,
1116               (x * charWidth) - lastSSX - 4, 7);
1117       g.fillPolygon(new int[]
1118       { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1119               new int[]
1120               { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1121               3);
1122     }
1123     else
1124     {
1125       g.fillRect(lastSSX, y + 4 + iconOffset,
1126               (x + 1) * charWidth - lastSSX, 7);
1127     }
1128
1129   }
1130
1131   public void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX,
1132           int x, int y, int iconOffset, int startRes, int column,
1133           boolean validRes, boolean validEnd)
1134   {
1135     g.setColor(HELIX_COLOUR);
1136
1137     int sCol = (lastSSX / charWidth) + startRes;
1138     int x1 = lastSSX;
1139     int x2 = (x * charWidth);
1140
1141     if (MAC)
1142     {
1143       int ofs = charWidth / 2;
1144       // Off by 1 offset when drawing rects and ovals
1145       // to offscreen image on the MAC
1146       g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1147       if (sCol == 0 || row[sCol - 1] == null
1148               || row[sCol - 1].secondaryStructure != 'H')
1149       {
1150       }
1151       else
1152       {
1153         // g.setColor(Color.orange);
1154         g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1155                 0, 0);
1156       }
1157       if (!validRes || row[column] == null
1158               || row[column].secondaryStructure != 'H')
1159       {
1160
1161       }
1162       else
1163       {
1164         // g.setColor(Color.magenta);
1165         g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset, x2 - x1 - ofs
1166                 + 1, 8, 0, 0);
1167
1168       }
1169
1170       return;
1171     }
1172
1173     if (sCol == 0 || row[sCol - 1] == null
1174             || row[sCol - 1].secondaryStructure != 'H')
1175     {
1176       g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1177       x1 += charWidth / 2;
1178     }
1179
1180     if (!validRes || row[column] == null
1181             || row[column].secondaryStructure != 'H')
1182     {
1183       g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1184               8, 270, 180);
1185       x2 -= charWidth / 2;
1186     }
1187
1188     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1189   }
1190
1191   public void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1192           Annotation[] aa_annotations, int sRes, int eRes, int y,
1193           float min, float max, int graphHeight)
1194   {
1195     if (sRes > aa_annotations.length)
1196     {
1197       return;
1198     }
1199
1200     int x = 0;
1201
1202     // Adjustment for fastpaint to left
1203     if (eRes < endRes)
1204     {
1205       eRes++;
1206     }
1207
1208     eRes = Math.min(eRes, aa_annotations.length);
1209
1210     if (sRes == 0)
1211     {
1212       x++;
1213     }
1214
1215     int y1 = y, y2 = y;
1216     float range = max - min;
1217
1218     // //Draw origin
1219     if (min < 0)
1220     {
1221       y2 = y - (int) ((0 - min / range) * graphHeight);
1222     }
1223
1224     g.setColor(Color.gray);
1225     g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1226
1227     eRes = Math.min(eRes, aa_annotations.length);
1228
1229     int column;
1230     int aaMax = aa_annotations.length - 1;
1231
1232     while (x < eRes - sRes)
1233     {
1234       column = sRes + x;
1235       if (hasHiddenColumns)
1236       {
1237         column = columnSelection.adjustForHiddenColumns(column);
1238       }
1239
1240       if (column > aaMax)
1241       {
1242         break;
1243       }
1244
1245       if (aa_annotations[column] == null
1246               || aa_annotations[column - 1] == null)
1247       {
1248         x++;
1249         continue;
1250       }
1251
1252       if (aa_annotations[column].colour == null)
1253         g.setColor(Color.black);
1254       else
1255         g.setColor(aa_annotations[column].colour);
1256
1257       y1 = y
1258               - (int) (((aa_annotations[column - 1].value - min) / range) * graphHeight);
1259       y2 = y
1260               - (int) (((aa_annotations[column].value - min) / range) * graphHeight);
1261
1262       g.drawLine(x * charWidth - charWidth / 2, y1, x * charWidth
1263               + charWidth / 2, y2);
1264       x++;
1265     }
1266
1267     if (_aa.threshold != null)
1268     {
1269       g.setColor(_aa.threshold.colour);
1270       Graphics2D g2 = (Graphics2D) g;
1271       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1272               BasicStroke.JOIN_ROUND, 3f, new float[]
1273               { 5f, 3f }, 0f));
1274
1275       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1276       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1277       g2.setStroke(new BasicStroke());
1278     }
1279   }
1280
1281   public void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1282           Annotation[] aa_annotations, int sRes, int eRes, float min,
1283           float max, int y)
1284   {
1285     if (sRes > aa_annotations.length)
1286     {
1287       return;
1288     }
1289     Font ofont = g.getFont();
1290     eRes = Math.min(eRes, aa_annotations.length);
1291
1292     int x = 0, y1 = y, y2 = y;
1293
1294     float range = max - min;
1295
1296     if (min < 0)
1297     {
1298       y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1299     }
1300
1301     g.setColor(Color.gray);
1302
1303     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1304
1305     int column;
1306     int aaMax = aa_annotations.length - 1;
1307     boolean renderHistogram = true, renderProfile = true, normaliseProfile = false;
1308     // if (aa.autoCalculated && aa.label.startsWith("Consensus"))
1309     {
1310       // TODO: generalise this to have render styles for consensus/profile data
1311       if (_aa.groupRef != null)
1312       {
1313         renderHistogram = _aa.groupRef.isShowConsensusHistogram();
1314         renderProfile = _aa.groupRef.isShowSequenceLogo();
1315         normaliseProfile = _aa.groupRef.isNormaliseSequenceLogo();
1316       }
1317       else
1318       {
1319         renderHistogram = av_renderHistogram;
1320         renderProfile = av_renderProfile;
1321         normaliseProfile = av_normaliseProfile;
1322       }
1323     }
1324     while (x < eRes - sRes)
1325     {
1326       column = sRes + x;
1327       if (hasHiddenColumns)
1328       {
1329         column = columnSelection.adjustForHiddenColumns(column);
1330       }
1331
1332       if (column > aaMax)
1333       {
1334         break;
1335       }
1336
1337       if (aa_annotations[column] == null)
1338       {
1339         x++;
1340         continue;
1341       }
1342       if (aa_annotations[column].colour == null)
1343         g.setColor(Color.black);
1344       else
1345         g.setColor(aa_annotations[column].colour);
1346
1347       y1 = y
1348               - (int) (((aa_annotations[column].value - min) / (range)) * _aa.graphHeight);
1349
1350       if (renderHistogram)
1351       {
1352         if (y1 - y2 > 0)
1353         {
1354           g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1355         }
1356         else
1357         {
1358           g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1359         }
1360       }
1361       // draw profile if available
1362       if (renderProfile)
1363       {
1364
1365         int profl[] = getProfileFor(_aa, column);
1366         // just try to draw the logo if profl is not null
1367         if (profl != null && profl[1] != 0)
1368         {
1369           float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1370           double htn = normaliseProfile ? _aa.graphHeight : (y2 - y1);// aa.graphHeight;
1371           double hght;
1372           float wdth;
1373           double ht2 = 0;
1374           char[] dc;
1375
1376           /**
1377            * profl.length == 74 indicates that the profile of a secondary
1378            * structure conservation row was accesed. Therefore dc gets length 2,
1379            * to have space for a basepair instead of just a single nucleotide
1380            */
1381           if (profl.length == 74)
1382           {
1383             dc = new char[2];
1384           }
1385           else
1386           {
1387             dc = new char[1];
1388           }
1389           LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1390           double scale = 1f / (normaliseProfile ? profl[1] : 100f);
1391           float ofontHeight = 1f / lm.getAscent();// magnify to fill box
1392           double scl = 0.0;
1393           for (int c = 2; c < profl[0];)
1394           {
1395             dc[0] = (char) profl[c++];
1396
1397             if (_aa.label.startsWith("StrucConsensus"))
1398             {
1399               dc[1] = (char) profl[c++];
1400             }
1401
1402             wdth = charWidth;
1403             wdth /= fm.charsWidth(dc, 0, dc.length);
1404
1405             ht += scl;
1406             {
1407               scl = htn * scale * profl[c++];
1408               lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics()
1409                       .getFontRenderContext());
1410               g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(
1411                       wdth, scl / lm.getAscent())));
1412               lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
1413
1414               // Debug - render boxes around characters
1415               // g.setColor(Color.red);
1416               // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1417               // (int)(scl));
1418               // g.setColor(profcolour.findColour(dc[0]).darker());
1419               g.setColor(profcolour.findColour(dc[0], column, null));
1420
1421               hght = (ht + (scl - lm.getDescent() - lm.getBaselineOffsets()[lm
1422                       .getBaselineIndex()]));
1423
1424               g.drawChars(dc, 0, dc.length, x * charWidth, (int) hght);
1425             }
1426           }
1427           g.setFont(ofont);
1428         }
1429       }
1430       x++;
1431     }
1432     if (_aa.threshold != null)
1433     {
1434       g.setColor(_aa.threshold.colour);
1435       Graphics2D g2 = (Graphics2D) g;
1436       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1437               BasicStroke.JOIN_ROUND, 3f, new float[]
1438               { 5f, 3f }, 0f));
1439
1440       y2 = (int) (y - ((_aa.threshold.value - min) / range)
1441               * _aa.graphHeight);
1442       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1443       g2.setStroke(new BasicStroke());
1444     }
1445   }
1446
1447   // used by overview window
1448   public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1449           Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1450   {
1451     eRes = Math.min(eRes, aa_annotations.length);
1452     g.setColor(Color.white);
1453     g.fillRect(0, 0, width, y);
1454     g.setColor(new Color(0, 0, 180));
1455
1456     int x = 0, height;
1457
1458     for (int j = sRes; j < eRes; j++)
1459     {
1460       if (aa_annotations[j] != null)
1461       {
1462         if (aa_annotations[j].colour == null)
1463           g.setColor(Color.black);
1464         else
1465           g.setColor(aa_annotations[j].colour);
1466
1467         height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1468         if (height > y)
1469         {
1470           height = y;
1471         }
1472
1473         g.fillRect(x, y - height, charWidth, height);
1474       }
1475       x += charWidth;
1476     }
1477   }
1478
1479   
1480   Color getNotCanonicalColor(char lastss)
1481         {
1482           switch (lastss)
1483       {
1484           case '{':  
1485               case '}':
1486                   return Color.cyan;
1487                  
1488               case '[':
1489               case ']':
1490                   return Color.green;
1491                  
1492               case '>':
1493               case '<':
1494                   return Color.magenta;
1495                   
1496               case 'A':
1497               case 'a':
1498                   return Color.orange;
1499                 
1500               case 'B':
1501               case 'b':
1502                   return Color.pink;
1503                   
1504               case 'C':
1505               case 'c':
1506                   return Color.red;
1507                  
1508               case 'D':
1509               case 'd':
1510                   return Color.yellow;
1511                   
1512               case '1':
1513               case 'e':
1514                   return Color.black;
1515                  
1516               case 'F':
1517               case 'f':
1518                   return Color.darkGray;
1519                  
1520               case 'G':
1521               case 'g':
1522                   return Color.gray;
1523                 
1524               case '2':
1525               case 'h':
1526                   return Color.lightGray;
1527                   
1528               case 'I':
1529               case 'i':
1530                   return Color.white;
1531                  
1532               case 'J':
1533               case 'j':
1534                   return Color.cyan;
1535                   
1536               case 'K':
1537               case 'k':
1538                   return Color.magenta;
1539                 
1540               case 'L':
1541               case 'l':
1542                   return Color.orange;
1543         
1544               case 'M':
1545               case 'm':
1546                   return Color.red;
1547                 
1548               case 'N':
1549               case 'n':
1550                   return Color.yellow;
1551                   
1552               case 'O':
1553               case 'o':
1554                   return Color.pink;
1555                 
1556               case 'P':
1557               case 'p':
1558                   return Color.black;
1559                 
1560               case 'Q':
1561               case 'q':
1562                   return Color.blue;
1563         
1564               case 'R':
1565               case 'r':
1566                   return Color.cyan;
1567         
1568               case 'S':
1569               case 's':
1570                   return Color.magenta;
1571                 
1572               case 'T':
1573               case 't':
1574                   return Color.darkGray;
1575                  
1576               case 'U':
1577               case 'u':
1578                   return Color.yellow;
1579                   
1580               case 'V':
1581               case 'v':
1582                   return Color.blue;
1583                  
1584               case 'W':
1585               case 'w':
1586                   return Color.orange;
1587                   
1588               case 'X':
1589               case 'x':
1590                   return Color.magenta;
1591                  
1592               case 'Y':
1593               case 'y':
1594                   return Color.blue;
1595                   
1596               case 'Z':
1597               case 'z':
1598                   return Color.blue;
1599                  
1600         default :
1601                 System.out.println("This is not a interaction");
1602                 return null;
1603                 
1604       }
1605         }
1606 }
1607
1608         
1609