JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / renderer / AnnotationRenderer.java
index 2ba7aff..3fdcb3b 100644 (file)
@@ -22,6 +22,7 @@ package jalview.renderer;
 
 import jalview.analysis.AAFrequency;
 import jalview.analysis.CodingUtils;
 
 import jalview.analysis.AAFrequency;
 import jalview.analysis.CodingUtils;
+import jalview.analysis.Rna;
 import jalview.analysis.StructureFrequency;
 import jalview.api.AlignViewportI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.analysis.StructureFrequency;
 import jalview.api.AlignViewportI;
 import jalview.datamodel.AlignmentAnnotation;
@@ -29,6 +30,7 @@ import jalview.datamodel.Annotation;
 import jalview.datamodel.ColumnSelection;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ResidueProperties;
 import jalview.datamodel.ColumnSelection;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ResidueProperties;
+import jalview.util.Platform;
 
 import java.awt.BasicStroke;
 import java.awt.Color;
 
 import java.awt.BasicStroke;
 import java.awt.Color;
@@ -43,88 +45,18 @@ import java.awt.image.ImageObserver;
 import java.util.BitSet;
 import java.util.Hashtable;
 
 import java.util.BitSet;
 import java.util.Hashtable;
 
-import com.stevesoft.pat.Regex;
-
 public class AnnotationRenderer
 {
 public class AnnotationRenderer
 {
-  /**
-   * flag indicating if timing and redraw parameter info should be output
-   */
-  private final boolean debugRedraw;
+  private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
 
 
-  public AnnotationRenderer()
-  {
-    this(false);
-  }
+  private static final int CHAR_A = 'A'; // 65
+
+  private static final int CHAR_Z = 'Z'; // 90
 
   /**
 
   /**
-   * Create a new annotation Renderer
-   * 
-   * @param debugRedraw
-   *          flag indicating if timing and redraw parameter info should be
-   *          output
+   * flag indicating if timing and redraw parameter info should be output
    */
    */
-  public AnnotationRenderer(boolean debugRedraw)
-  {
-    this.debugRedraw = debugRedraw;
-  }
-
-  public void drawStemAnnot(Graphics g, Annotation[] row_annotations,
-          int lastSSX, int x, int y, int iconOffset, int startRes,
-          int column, boolean validRes, boolean validEnd)
-  {
-    g.setColor(STEM_COLOUR);
-    int sCol = (lastSSX / charWidth) + startRes;
-    int x1 = lastSSX;
-    int x2 = (x * charWidth);
-    Regex closeparen = new Regex("(\\))");
-
-    char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
-            : row_annotations[column - 1].secondaryStructure;
-
-    boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
-            || dc != row_annotations[sCol - 1].secondaryStructure;
-    boolean diffdownstream = !validRes || !validEnd
-            || row_annotations[column] == null
-            || dc != row_annotations[column].secondaryStructure;
-    // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
-    // If a closing base pair half of the stem, display a backward arrow
-    if (column > 0 && ResidueProperties.isCloseParenRNA(dc))
-    {
-
-      if (diffupstream)
-      // if (validRes && column>1 && row_annotations[column-2]!=null &&
-      // dc.equals(row_annotations[column-2].displayCharacter))
-      {
-        g.fillPolygon(new int[]
-        { lastSSX + 5, lastSSX + 5, lastSSX }, new int[]
-        { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
-        x1 += 5;
-      }
-      if (diffdownstream)
-      {
-        x2 -= 1;
-      }
-    }
-    else
-    {
-
-      // display a forward arrow
-      if (diffdownstream)
-      {
-        g.fillPolygon(new int[]
-        { x2 - 5, x2 - 5, x2 }, new int[]
-        { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
-        x2 -= 5;
-      }
-      if (diffupstream)
-      {
-        x1 += 1;
-      }
-    }
-    // draw arrow body
-    g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
-  }
+  private final boolean debugRedraw;
 
   private int charWidth, endRes, charHeight;
 
 
   private int charWidth, endRes, charHeight;
 
@@ -132,7 +64,7 @@ public class AnnotationRenderer
 
   private FontMetrics fm;
 
 
   private FontMetrics fm;
 
-  private final boolean MAC = new jalview.util.Platform().isAMac();
+  private final boolean MAC = Platform.isAMac();
 
   boolean av_renderHistogram = true, av_renderProfile = true,
           av_normaliseProfile = false;
 
   boolean av_renderHistogram = true, av_renderProfile = true,
           av_normaliseProfile = false;
@@ -190,7 +122,96 @@ public class AnnotationRenderer
    */
   private boolean canClip = false;
 
    */
   private boolean canClip = false;
 
-  public void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
+  public AnnotationRenderer()
+  {
+    this(false);
+  }
+
+  /**
+   * Create a new annotation Renderer
+   * 
+   * @param debugRedraw
+   *          flag indicating if timing and redraw parameter info should be
+   *          output
+   */
+  public AnnotationRenderer(boolean debugRedraw)
+  {
+    this.debugRedraw = debugRedraw;
+  }
+
+  /**
+   * Remove any references and resources when this object is no longer required
+   */
+  public void dispose()
+  {
+    hconsensus = null;
+    complementConsensus = null;
+    hStrucConsensus = null;
+    fadedImage = null;
+    annotationPanel = null;
+  }
+
+  void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
+          int x, int y, int iconOffset, int startRes, int column,
+          boolean validRes, boolean validEnd)
+  {
+    g.setColor(STEM_COLOUR);
+    int sCol = (lastSSX / charWidth) + startRes;
+    int x1 = lastSSX;
+    int x2 = (x * charWidth);
+
+    char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
+            : row_annotations[column - 1].secondaryStructure;
+
+    boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
+            || dc != row_annotations[sCol - 1].secondaryStructure;
+    boolean diffdownstream = !validRes || !validEnd
+            || row_annotations[column] == null
+            || dc != row_annotations[column].secondaryStructure;
+
+    if (column > 0 && Rna.isClosingParenthesis(dc))
+    {
+      if (diffupstream)
+      // if (validRes && column>1 && row_annotations[column-2]!=null &&
+      // dc.equals(row_annotations[column-2].displayCharacter))
+      {
+        /*
+         * if new annotation with a closing base pair half of the stem, 
+         * display a backward arrow
+         */
+        g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
+                new int[] { y + iconOffset, y + 14 + iconOffset,
+                    y + 8 + iconOffset }, 3);
+        x1 += 5;
+      }
+      if (diffdownstream)
+      {
+        x2 -= 1;
+      }
+    }
+    else
+    {
+      // display a forward arrow
+      if (diffdownstream)
+      {
+        /*
+         * if annotation ending with an opeing base pair half of the stem, 
+         * display a forward arrow
+         */
+        g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 }, new int[] {
+            y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
+        x2 -= 5;
+      }
+      if (diffupstream)
+      {
+        x1 += 1;
+      }
+    }
+    // draw arrow body
+    g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
+  }
+
+  void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
           Annotation[] row_annotations, int lastSSX, int x, int y,
           int iconOffset, int startRes, int column, boolean validRes,
           boolean validEnd)
           Annotation[] row_annotations, int lastSSX, int x, int y,
           int iconOffset, int startRes, int column, boolean validRes,
           boolean validEnd)
@@ -201,7 +222,6 @@ public class AnnotationRenderer
     int sCol = (lastSSX / charWidth) + startRes;
     int x1 = lastSSX;
     int x2 = (x * charWidth);
     int sCol = (lastSSX / charWidth) + startRes;
     int x1 = lastSSX;
     int x2 = (x * charWidth);
-    Regex closeparen = new Regex("}|]|<|[a-z]");
 
     String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
             : row_annotations[column - 1].displayCharacter;
 
     String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
             : row_annotations[column - 1].displayCharacter;
@@ -213,17 +233,16 @@ public class AnnotationRenderer
             || !dc.equals(row_annotations[column].displayCharacter);
     // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
     // If a closing base pair half of the stem, display a backward arrow
             || !dc.equals(row_annotations[column].displayCharacter);
     // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
     // If a closing base pair half of the stem, display a backward arrow
-    if (column > 0 && closeparen.search(dc))// closeletter_b.search(dc)||closeletter_c.search(dc)||closeletter_d.search(dc)||closecrochet.search(dc))
-                                            // )
+    if (column > 0 && Rna.isClosingParenthesis(dc))
     {
 
       if (diffupstream)
       // if (validRes && column>1 && row_annotations[column-2]!=null &&
       // dc.equals(row_annotations[column-2].displayCharacter))
       {
     {
 
       if (diffupstream)
       // if (validRes && column>1 && row_annotations[column-2]!=null &&
       // dc.equals(row_annotations[column-2].displayCharacter))
       {
-        g.fillPolygon(new int[]
-        { lastSSX + 5, lastSSX + 5, lastSSX }, new int[]
-        { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
+        g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
+                new int[] { y + iconOffset, y + 14 + iconOffset,
+                    y + 8 + iconOffset }, 3);
         x1 += 5;
       }
       if (diffdownstream)
         x1 += 5;
       }
       if (diffdownstream)
@@ -237,9 +256,8 @@ public class AnnotationRenderer
       // display a forward arrow
       if (diffdownstream)
       {
       // display a forward arrow
       if (diffdownstream)
       {
-        g.fillPolygon(new int[]
-        { x2 - 5, x2 - 5, x2 }, new int[]
-        { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
+        g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 }, new int[] {
+            y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
         x2 -= 5;
       }
       if (diffupstream)
         x2 -= 5;
       }
       if (diffupstream)
@@ -317,7 +335,7 @@ public class AnnotationRenderer
    * @param column
    * @return
    */
    * @param column
    * @return
    */
-  public int[] getProfileFor(AlignmentAnnotation aa, int column)
+  int[] getProfileFor(AlignmentAnnotation aa, int column)
   {
     // TODO : consider refactoring the global alignment calculation
     // properties/rendering attributes as a global 'alignment group' which holds
   {
     // TODO : consider refactoring the global alignment calculation
     // properties/rendering attributes as a global 'alignment group' which holds
@@ -594,14 +612,9 @@ public class AnnotationRenderer
 
               if (columnSelection != null)
               {
 
               if (columnSelection != null)
               {
-                for (int n = 0; n < columnSelection.size(); n++)
+                if (columnSelection.contains(column))
                 {
                 {
-                  int v = columnSelection.columnAt(n);
-
-                  if (v == column)
-                  {
-                    g.fillRect(x * charWidth, y, charWidth, charHeight);
-                  }
+                  g.fillRect(x * charWidth, y, charWidth, charHeight);
                 }
               }
             }
                 }
               }
             }
@@ -628,17 +641,17 @@ public class AnnotationRenderer
                 //
                 // if (scaleColLabel)
                 // {
                 //
                 // if (scaleColLabel)
                 // {
-                  // justify the label and scale to fit in column
-                  if (fmWidth > charWidth)
-                  {
-                    // scale only if the current font isn't already small enough
-                    fmScaling = charWidth;
-                    fmScaling /= fmWidth;
-                    g.setFont(ofont.deriveFont(AffineTransform
-                            .getScaleInstance(fmScaling, 1.0)));
-                    // and update the label's width to reflect the scaling.
-                    fmWidth = charWidth;
-                  }
+                // justify the label and scale to fit in column
+                if (fmWidth > charWidth)
+                {
+                  // scale only if the current font isn't already small enough
+                  fmScaling = charWidth;
+                  fmScaling /= fmWidth;
+                  g.setFont(ofont.deriveFont(AffineTransform
+                          .getScaleInstance(fmScaling, 1.0)));
+                  // and update the label's width to reflect the scaling.
+                  fmWidth = charWidth;
+                }
                 // }
               }
               // TODO is it ok to use width of / show all characters here?
                 // }
               }
               // TODO is it ok to use width of / show all characters here?
@@ -715,14 +728,14 @@ public class AnnotationRenderer
 
               }
             }
 
               }
             }
-            if (ss >= 65)
+            if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
             {
               // distinguish between forward/backward base-pairing
             {
               // distinguish between forward/backward base-pairing
-              if (displayChar.indexOf(ss + 32) > -1)
+              int ssLowerCase = ss + UPPER_TO_LOWER;
+              // TODO would .equals() be safer here? or charAt(0)?
+              if (displayChar.indexOf(ssLowerCase) > -1)
               {
               {
-
-                ss = (char) (ss + 32);
-
+                ss = (char) ssLowerCase;
               }
             }
 
               }
             }
 
@@ -751,7 +764,7 @@ public class AnnotationRenderer
                             validEnd);
                     break;
                   }
                             validEnd);
                     break;
                   }
-
+                  // no break if isRNA - falls through to drawNotCanonicalAnnot!
                 case 'E':
                   if (!isRNA)
                   {
                 case 'E':
                   if (!isRNA)
                   {
@@ -760,6 +773,7 @@ public class AnnotationRenderer
                             validEnd);
                     break;
                   }
                             validEnd);
                     break;
                   }
+                  // no break if isRNA - fall through to drawNotCanonicalAnnot!
 
                 case '{':
                 case '}':
 
                 case '{':
                 case '}':
@@ -867,7 +881,6 @@ public class AnnotationRenderer
         {
           validRes = true;
         }
         {
           validRes = true;
         }
-
         // x ++;
 
         if (row.hasIcons)
         // x ++;
 
         if (row.hasIcons)
@@ -882,6 +895,7 @@ public class AnnotationRenderer
                       startRes, column, validRes, validEnd);
               break;
             }
                       startRes, column, validRes, validEnd);
               break;
             }
+            // no break if isRNA - fall through to drawNotCanonicalAnnot!
 
           case 'E':
             if (!isRNA)
 
           case 'E':
             if (!isRNA)
@@ -890,6 +904,7 @@ public class AnnotationRenderer
                       startRes, column, validRes, validEnd);
               break;
             }
                       startRes, column, validRes, validEnd);
               break;
             }
+            // no break if isRNA - fall through to drawNotCanonicalAnnot!
 
           case '(':
           case ')': // Stem case for RNA secondary structure
 
           case '(':
           case ')': // Stem case for RNA secondary structure
@@ -1070,15 +1085,15 @@ public class AnnotationRenderer
 
   private Color sdNOTCANONICAL_COLOUR;
 
 
   private Color sdNOTCANONICAL_COLOUR;
 
-  public void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX,
-          int x, int y, int iconOffset, int startRes, int column,
+  void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
+          int y, int iconOffset, int startRes, int column,
           boolean validRes, boolean validEnd)
   {
     g.setColor(GLYPHLINE_COLOR);
     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
   }
 
           boolean validRes, boolean validEnd)
   {
     g.setColor(GLYPHLINE_COLOR);
     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
   }
 
-  public void drawSheetAnnot(Graphics g, Annotation[] row,
+  void drawSheetAnnot(Graphics g, Annotation[] row,
 
   int lastSSX, int x, int y, int iconOffset, int startRes, int column,
           boolean validRes, boolean validEnd)
 
   int lastSSX, int x, int y, int iconOffset, int startRes, int column,
           boolean validRes, boolean validEnd)
@@ -1090,11 +1105,9 @@ public class AnnotationRenderer
     {
       g.fillRect(lastSSX, y + 4 + iconOffset,
               (x * charWidth) - lastSSX - 4, 7);
     {
       g.fillRect(lastSSX, y + 4 + iconOffset,
               (x * charWidth) - lastSSX - 4, 7);
-      g.fillPolygon(new int[]
-      { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
-              new int[]
-              { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
-              3);
+      g.fillPolygon(new int[] { (x * charWidth) - 4, (x * charWidth) - 4,
+          (x * charWidth) }, new int[] { y + iconOffset,
+          y + 14 + iconOffset, y + 7 + iconOffset }, 3);
     }
     else
     {
     }
     else
     {
@@ -1104,8 +1117,8 @@ public class AnnotationRenderer
 
   }
 
 
   }
 
-  public void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX,
-          int x, int y, int iconOffset, int startRes, int column,
+  void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
+          int y, int iconOffset, int startRes, int column,
           boolean validRes, boolean validEnd)
   {
     g.setColor(HELIX_COLOUR);
           boolean validRes, boolean validEnd)
   {
     g.setColor(HELIX_COLOUR);
@@ -1164,7 +1177,7 @@ public class AnnotationRenderer
     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
   }
 
     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
   }
 
-  public void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
+  void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
           Annotation[] aa_annotations, int sRes, int eRes, int y,
           float min, float max, int graphHeight)
   {
           Annotation[] aa_annotations, int sRes, int eRes, int y,
           float min, float max, int graphHeight)
   {
@@ -1249,8 +1262,7 @@ public class AnnotationRenderer
       g.setColor(_aa.threshold.colour);
       Graphics2D g2 = (Graphics2D) g;
       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
       g.setColor(_aa.threshold.colour);
       Graphics2D g2 = (Graphics2D) g;
       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
-              BasicStroke.JOIN_ROUND, 3f, new float[]
-              { 5f, 3f }, 0f));
+              BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f }, 0f));
 
       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
 
       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
@@ -1258,7 +1270,7 @@ public class AnnotationRenderer
     }
   }
 
     }
   }
 
-  public void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
+  void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
           Annotation[] aa_annotations, int sRes, int eRes, float min,
           float max, int y, boolean renderHistogram, boolean renderProfile,
           boolean normaliseProfile)
           Annotation[] aa_annotations, int sRes, int eRes, float min,
           float max, int y, boolean renderHistogram, boolean renderProfile,
           boolean normaliseProfile)
@@ -1389,8 +1401,9 @@ public class AnnotationRenderer
             scl = htn * scale * profl[c++];
             lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics()
                     .getFontRenderContext());
             scl = htn * scale * profl[c++];
             lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics()
                     .getFontRenderContext());
-            g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(
-                    wdth, scl / lm.getAscent())));
+            Font font = ofont.deriveFont(AffineTransform.getScaleInstance(
+                    wdth, scl / lm.getAscent()));
+            g.setFont(font);
             lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
 
             // Debug - render boxes around characters
             lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
 
             // Debug - render boxes around characters
@@ -1433,8 +1446,7 @@ public class AnnotationRenderer
       g.setColor(_aa.threshold.colour);
       Graphics2D g2 = (Graphics2D) g;
       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
       g.setColor(_aa.threshold.colour);
       Graphics2D g2 = (Graphics2D) g;
       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
-              BasicStroke.JOIN_ROUND, 3f, new float[]
-              { 5f, 3f }, 0f));
+              BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f }, 0f));
 
       y2 = (int) (y - ((_aa.threshold.value - min) / range)
               * _aa.graphHeight);
 
       y2 = (int) (y - ((_aa.threshold.value - min) / range)
               * _aa.graphHeight);