JAL-2588 (and JAL-2610, JAL-2603) mid refactor
authorkiramt <k.mourao@dundee.ac.uk>
Tue, 11 Jul 2017 11:15:09 +0000 (12:15 +0100)
committerkiramt <k.mourao@dundee.ac.uk>
Tue, 11 Jul 2017 11:15:09 +0000 (12:15 +0100)
src/jalview/appletgui/OverviewCanvas.java
src/jalview/appletgui/SequenceRenderer.java
src/jalview/gui/OverviewCanvas.java
src/jalview/gui/SequenceRenderer.java
src/jalview/renderer/OverviewRenderer.java
test/jalview/gui/SequenceRendererTest.java

index a0466d3..2543718 100644 (file)
@@ -68,7 +68,7 @@ public class OverviewCanvas extends Component
     sr = new SequenceRenderer(av);
     sr.graphics = nullFrame.getGraphics();
     sr.renderGaps = false;
-    sr.forOverview = true;
+    // sr.forOverview = true;
     fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
   }
 
@@ -119,7 +119,8 @@ public class OverviewCanvas extends Component
 
     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
 
-    or = new OverviewRenderer(sr, fr, od);
+    or = new OverviewRenderer(sr, fr, od, av.getAlignment(),
+            av.getResidueShading());
     miniMe = nullFrame.createImage(od.getWidth(), od.getHeight());
     offscreen = nullFrame.createImage(od.getWidth(), od.getHeight());
 
index 38031e4..c774a19 100755 (executable)
@@ -48,8 +48,6 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
 
   Graphics graphics;
 
-  boolean forOverview = false;
-
   public SequenceRenderer(AlignViewport av)
   {
     this.av = av;
@@ -119,11 +117,6 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
     {
       resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
     }
-    else if (forOverview
-            && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
-    {
-      resBoxColour = Color.lightGray;
-    }
     else
     {
       resBoxColour = Color.white;
index 63075dc..9356565 100644 (file)
@@ -65,7 +65,7 @@ public class OverviewCanvas extends JComponent
 
     sr = new SequenceRenderer(av);
     sr.renderGaps = false;
-    sr.forOverview = true;
+    // sr.forOverview = true;
     fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
   }
 
@@ -119,29 +119,17 @@ public class OverviewCanvas extends JComponent
   {
     miniMe = null;
 
-    if (!av.getShowBoxes())
+    if (showSequenceFeatures)
     {
-      // boxes are not shown on main viewport
-      or = new OverviewRenderer(sr, fr, od);
-      miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
-              BufferedImage.TYPE_INT_RGB);
-      miniMe.getGraphics().setColor(Color.WHITE);
-      miniMe.getGraphics().fillRect(0, 0, od.getWidth(),
-              od.getSequencesHeight());
+      fr.transferSettings(transferRenderer);
     }
-    else
-    {
-      if (showSequenceFeatures)
-      {
-        fr.transferSettings(transferRenderer);
-      }
 
-      setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
+    setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
 
-      or = new OverviewRenderer(sr, fr, od);
-      miniMe = or.draw(od.getRows(av.getAlignment()),
-              od.getColumns(av.getAlignment()));
-    }
+    or = new OverviewRenderer(sr, fr, od, av.getAlignment(),
+            av.getResidueShading());
+    miniMe = or.draw(od.getRows(av.getAlignment()),
+            od.getColumns(av.getAlignment()));
 
     Graphics mg = miniMe.getGraphics();
 
index 7a1e263..9874fd2 100755 (executable)
@@ -25,7 +25,6 @@ import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.renderer.ResidueShaderI;
 import jalview.renderer.seqfeatures.FeatureColourFinder;
-import jalview.util.Comparison;
 
 import java.awt.Color;
 import java.awt.FontMetrics;
@@ -41,8 +40,6 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
 
   boolean renderGaps = true;
 
-  SequenceGroup currentSequenceGroup = null;
-
   SequenceGroup[] allGroups = null;
 
   Color resBoxColour;
@@ -51,8 +48,6 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
 
   boolean monospacedFont;
 
-  boolean forOverview = false;
-
   /**
    * Creates a new SequenceRenderer object
    * 
@@ -88,7 +83,8 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
     // rate limiting step when rendering overview for lots of groups
     allGroups = av.getAlignment().findAllGroups(seq);
 
-    if (inCurrentSequenceGroup(i))
+    SequenceGroup currentSequenceGroup = inCurrentSequenceGroup(i);
+    if (currentSequenceGroup != null)
     {
       if (currentSequenceGroup.getDisplayBoxes())
       {
@@ -140,20 +136,7 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
   {
     if (shader.getColourScheme() != null)
     {
-      if (forOverview && Comparison.isGap(seq.getCharAt(i))
-              && !shader.getColourScheme().hasGapColour())
-      {
-        resBoxColour = Color.lightGray;
-      }
-      else
-      {
-        resBoxColour = shader.findColour(seq.getCharAt(i),
-              i, seq);
-      }
-    }
-    else if (forOverview && Comparison.isGap(seq.getCharAt(i)))
-    {
-      resBoxColour = Color.lightGray;
+      resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
     }
     else
     {
@@ -236,7 +219,8 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
 
       if (i < length)
       {
-        if (inCurrentSequenceGroup(i))
+        SequenceGroup currentSequenceGroup = inCurrentSequenceGroup(i);
+        if (currentSequenceGroup != null)
         {
           if (currentSequenceGroup.getDisplayBoxes())
           {
@@ -325,9 +309,7 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
       boolean srep = av.isDisplayReferenceSeq();
       boolean getboxColour = false;
       boolean isarep = av.getAlignment().getSeqrep() == seq;
-      boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
-              .getSeqrep() == seq : false;
-      char sr_c;
+
       for (int i = start; i <= end; i++)
       {
 
@@ -340,7 +322,8 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
           continue;
         }
 
-        if (inCurrentSequenceGroup(i))
+        SequenceGroup currentSequenceGroup = inCurrentSequenceGroup(i);
+        if (currentSequenceGroup != null)
         {
           if (!currentSequenceGroup.getDisplayText())
           {
@@ -372,6 +355,8 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
           {
             graphics.setColor(currentSequenceGroup.textColour);
           }
+          boolean isgrep = currentSequenceGroup != null
+                  ? currentSequenceGroup.getSeqrep() == seq : false;
           if (!isarep && !isgrep
                   && currentSequenceGroup.getShowNonconserved()) // todo
                                                                  // optimize
@@ -480,11 +465,11 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
    * 
    * @return DOCUMENT ME!
    */
-  boolean inCurrentSequenceGroup(int res)
+  SequenceGroup inCurrentSequenceGroup(int res)
   {
     if (allGroups == null)
     {
-      return false;
+      return null;
     }
 
     for (int i = 0; i < allGroups.length; i++)
@@ -492,13 +477,11 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
       if ((allGroups[i].getStartRes() <= res)
               && (allGroups[i].getEndRes() >= res))
       {
-        currentSequenceGroup = allGroups[i];
-
-        return true;
+        return (allGroups[i]);
       }
     }
 
-    return false;
+    return null;
   }
 
   /**
index 46490cd..77c3700 100644 (file)
@@ -23,10 +23,13 @@ package jalview.renderer;
 import jalview.api.AlignmentColsCollectionI;
 import jalview.api.AlignmentRowsCollectionI;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
+import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.renderer.seqfeatures.FeatureColourFinder;
 import jalview.renderer.seqfeatures.FeatureRenderer;
+import jalview.util.Comparison;
 import jalview.viewmodel.OverviewDimensions;
 
 import java.awt.Color;
@@ -51,12 +54,21 @@ public class OverviewRenderer
   // flag to indicate whether to halt drawing
   private volatile boolean redraw = false;
 
+  // reference to alignment, needed to get sequence groups
+  private AlignmentI al;
+
+  private ResidueShaderI shader;
+
   public OverviewRenderer(jalview.api.SequenceRenderer seqRenderer,
-          FeatureRenderer fr, OverviewDimensions od)
+          FeatureRenderer fr, OverviewDimensions od, AlignmentI alignment,
+          ResidueShaderI resshader)
   {
     sr = seqRenderer;
     finder = new FeatureColourFinder(fr);
 
+    al = alignment;
+    shader = resshader;
+
     pixelsPerCol = od.getPixelsPerCol();
     pixelsPerSeq = od.getPixelsPerSeq();
     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
@@ -89,6 +101,8 @@ public class OverviewRenderer
       // get details of this alignment row
       boolean hidden = rows.isHidden(alignmentRow);
       SequenceI seq = rows.getSequence(alignmentRow);
+      // rate limiting step when rendering overview for lots of groups
+      SequenceGroup[] allGroups = al.findAllGroups(seq);
 
       // calculate where this row extends to in pixels
       int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
@@ -114,7 +128,7 @@ public class OverviewRenderer
         if (pixelCol <= endCol)
         {
           // determine the colour based on the sequence and column position
-          rgbcolor = getColumnColourFromSequence(seq,
+          rgbcolor = getColumnColourFromSequence(allGroups, seq,
                   hidden || cols.isHidden(alignmentCol), alignmentCol,
                   finder);
 
@@ -140,14 +154,15 @@ public class OverviewRenderer
   /*
    * Find the colour of a sequence at a specified column position
    */
-  private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
+  private int getColumnColourFromSequence(SequenceGroup[] allGroups,
+          jalview.datamodel.SequenceI seq,
           boolean isHidden, int lastcol, FeatureColourFinder fcfinder)
   {
     Color color = Color.white;
 
     if ((seq != null) && (seq.getLength() > lastcol))
     {
-      color = sr.getResidueColour(seq, lastcol, fcfinder);
+      color = getResidueColour(allGroups, seq, lastcol, fcfinder);
     }
 
     if (isHidden)
@@ -158,6 +173,83 @@ public class OverviewRenderer
     return color.getRGB();
   }
 
+  private Color getResidueColour(SequenceGroup[] allGroups,
+          final SequenceI seq, int position,
+          FeatureColourFinder finder)
+  {
+    Color col = getResidueBoxColour(allGroups, seq, position);
+
+    if (finder != null)
+    {
+      col = finder.findFeatureColour(col, seq, position);
+    }
+    return col;
+  }
+
+  protected Color getResidueBoxColour(SequenceGroup[] allGroups,
+          SequenceI seq, int i)
+  {
+
+    ResidueShaderI currentShader;
+
+    SequenceGroup currentSequenceGroup = inCurrentSequenceGroup(allGroups,
+            i);
+    if (currentSequenceGroup != null)
+    {
+      currentShader = currentSequenceGroup.getGroupColourScheme();
+    }
+    else
+    {
+      currentShader = shader;
+    }
+
+    return getBoxColour(currentShader, seq, i);
+  }
+
+  SequenceGroup inCurrentSequenceGroup(SequenceGroup[] allGroups, int res)
+  {
+    if (allGroups == null)
+    {
+      return null;
+    }
+
+    for (int i = 0; i < allGroups.length; i++)
+    {
+      if ((allGroups[i].getStartRes() <= res)
+              && (allGroups[i].getEndRes() >= res))
+      {
+        return (allGroups[i]);
+      }
+    }
+
+    return null;
+  }
+
+  Color getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
+  {
+    Color resBoxColour = Color.white;
+    char currentChar = seq.getCharAt(i);
+
+    if (shader.getColourScheme() != null)
+    {
+      if (Comparison.isGap(currentChar)
+              && !shader.getColourScheme().hasGapColour())
+      {
+        resBoxColour = Color.lightGray;
+      }
+      else
+      {
+        resBoxColour = shader.findColour(currentChar, i, seq);
+      }
+    }
+    else if (Comparison.isGap(currentChar))
+    {
+      resBoxColour = Color.lightGray;
+    }
+
+    return resBoxColour;
+  }
+
   /**
    * Draw the alignment annotation in the overview panel
    * 
index c80b830..569f1e6 100644 (file)
@@ -71,9 +71,9 @@ public class SequenceRendererTest
     assertEquals(Color.white, sr.getResidueBoxColour(seq, 2));
 
     // set for overview
-    sr.forOverview = true;
+    /*  sr.forOverview = true;
     assertEquals(Color.lightGray, sr.getResidueBoxColour(seq, 0));
-    assertEquals(Color.white, sr.getResidueBoxColour(seq, 2));
+    assertEquals(Color.white, sr.getResidueBoxColour(seq, 2));*/
   }
 
   // TODO more tests for getResidueBoxColour covering groups, feature rendering,