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