Features are an array, rendering is faster
authoramwaterhouse <Andrew Waterhouse>
Fri, 24 Feb 2006 11:35:27 +0000 (11:35 +0000)
committeramwaterhouse <Andrew Waterhouse>
Fri, 24 Feb 2006 11:35:27 +0000 (11:35 +0000)
src/jalview/appletgui/FeatureRenderer.java
src/jalview/appletgui/FeatureSettings.java
src/jalview/appletgui/OverviewPanel.java

index 4cb4f43..21c401a 100755 (executable)
@@ -33,19 +33,17 @@ import java.util.*;
 public class FeatureRenderer\r
 {\r
     AlignViewport av;\r
-    SequenceGroup currentSequenceGroup = null;\r
-    SequenceGroup[] allGroups = null;\r
-    Graphics graphics;\r
 \r
-    // The following vector holds the features which are\r
-    // to be added, in the correct order or rendering\r
-    Vector featuresDisplayed;\r
 \r
     // A higher level for grouping features of a\r
     // particular type\r
     Hashtable featureGroups = null;\r
 \r
-    Color currentColour = null;\r
+    // This is actually an Integer held in the hashtable,\r
+    // Retrieved using the key feature type\r
+    Object currentColour;\r
+\r
+    String [] renderOrder;\r
 \r
     boolean drawText = true;\r
     FontMetrics fm;\r
@@ -62,7 +60,70 @@ public class FeatureRenderer
         initColours();\r
     }\r
 \r
-    //boolean highlightTransparent = true;\r
+\r
+    public Color findFeatureColour(Color initialCol, SequenceI seq, int i)\r
+    {\r
+      if(!av.showSequenceFeatures)\r
+        return initialCol;\r
+\r
+        lastSequence = seq;\r
+        sequenceFeatures = lastSequence.getSequenceFeatures();\r
+        if(sequenceFeatures==null)\r
+          return initialCol;\r
+\r
+        sfSize = sequenceFeatures.length;\r
+\r
+      if(jalview.util.Comparison.isGap(lastSequence.getCharAt(i)))\r
+        return Color.white;\r
+\r
+      currentColour = null;\r
+\r
+      drawSequence(null, lastSequence, lastSequence.findPosition(i), -1,-1, -1, -1);\r
+\r
+      if(currentColour==null)\r
+        return initialCol;\r
+\r
+      return new Color( ((Integer)currentColour).intValue() );\r
+    }\r
+    /**\r
+     * This is used by the Molecule Viewer to get the accurate colour\r
+     * of the rendered sequence\r
+     */\r
+\r
+    boolean overview = false;\r
+\r
+    int white = Color.white.getRGB();\r
+    public int findFeatureColour(int initialCol, int seqIndex, int column)\r
+    {\r
+      if(!av.showSequenceFeatures)\r
+        return initialCol;\r
+\r
+      if(seqIndex!=lastSequenceIndex)\r
+      {\r
+        lastSequence = av.alignment.getSequenceAt(seqIndex);\r
+        lastSequenceIndex = seqIndex;\r
+        sequenceFeatures = lastSequence.getSequenceFeatures();\r
+        if(sequenceFeatures==null)\r
+          return initialCol;\r
+\r
+        sfSize = sequenceFeatures.length;\r
+      }\r
+\r
+\r
+      if(jalview.util.Comparison.isGap(lastSequence.getCharAt(column)))\r
+        return Color.white.getRGB();\r
+\r
+      currentColour = null;\r
+\r
+      drawSequence(null, lastSequence, lastSequence.findPosition(column), -1,-1, -1, -1);\r
+\r
+      if(currentColour==null)\r
+        return initialCol;\r
+\r
+      return  ((Integer)currentColour).intValue();\r
+    }\r
+\r
+\r
     /**\r
      * DOCUMENT ME!\r
      *\r
@@ -76,88 +137,117 @@ public class FeatureRenderer
      * @param width DOCUMENT ME!\r
      * @param height DOCUMENT ME!\r
      */\r
+   // String type;\r
+   // SequenceFeature sf;\r
+    int lastSequenceIndex=-1;\r
+    SequenceI lastSequence;\r
+    SequenceFeature [] sequenceFeatures;\r
+    int sfSize, sfindex, spos, epos;\r
+\r
     public void drawSequence(Graphics g, SequenceI seq,\r
-        int start, int end, int x1, int y1, int width, int height)\r
+                             int start, int end, int y1, int width, int height)\r
     {\r
-      fm = g.getFontMetrics();\r
-\r
-        if(seq.getSequenceFeatures()==null)\r
-          return;\r
+      if (   seq.getSequenceFeatures() == null\r
+          || seq.getSequenceFeatures().length==0)\r
+        return;\r
 \r
-        Enumeration e = null, e2;\r
-        String type;\r
-        if(featuresDisplayed!=null)\r
-          e = featuresDisplayed.elements();\r
-        else\r
-         e = seq.getSequenceFeatures().elements();\r
 \r
+      if(g!=null)\r
+        fm = g.getFontMetrics();\r
 \r
-        while (e.hasMoreElements())\r
+      if (av.featuresDisplayed == null || renderOrder==null)\r
+       {\r
+         findAllFeatures();\r
+         sequenceFeatures = seq.getSequenceFeatures();\r
+         sfSize = sequenceFeatures.length;\r
+       }\r
+       if(lastSequence==null || seq!=lastSequence)\r
+      {\r
+        lastSequence = seq;\r
+        sequenceFeatures = seq.getSequenceFeatures();\r
+        sfSize = sequenceFeatures.length;\r
+      }\r
+      if(!overview)\r
+      {\r
+        spos = lastSequence.findPosition(start);\r
+        epos = lastSequence.findPosition(end);\r
+      }\r
+      String type;\r
+      for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)\r
+       {\r
+        type =  renderOrder[renderIndex];\r
+\r
+        // loop through all features in sequence to find\r
+        // current feature to render\r
+        for (sfindex = 0; sfindex < sfSize; sfindex++)\r
         {\r
-            SequenceFeature sf=null;\r
-            if(featuresDisplayed!=null)\r
-            {\r
-              e2 = seq.getSequenceFeatures().elements();\r
-              type = e.nextElement().toString();\r
-              while(e2.hasMoreElements())\r
-              {\r
-                sf = (SequenceFeature) e2.nextElement();\r
-                if(sf.getType().equals(type))\r
-                  break;\r
-                else\r
-                  sf = null;\r
-              }\r
-            }\r
-            else\r
-            {\r
-              sf = (SequenceFeature) e.nextElement();\r
-              type = sf.getType();\r
-            }\r
+          if (!sequenceFeatures[sfindex].type.equals(type))\r
+            continue;\r
 \r
-            if(sf==null)\r
-              continue;\r
+          if (featureGroups != null\r
+              && sequenceFeatures[sfindex].featureGroup != null\r
+              &&\r
+              featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)\r
+              &&\r
+              ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].featureGroup)).\r
+              booleanValue())\r
+          {\r
+            continue;\r
+          }\r
 \r
-            if(featureGroups!=null\r
-               && sf.getFeatureGroup()!=null\r
-               && featureGroups.containsKey(sf.getFeatureGroup())\r
-               && !((Boolean)featureGroups.get(sf.getFeatureGroup())).booleanValue())\r
-            {\r
-              continue;\r
-            }\r
+          if (!overview && (sequenceFeatures[sfindex].getBegin() > epos\r
+                            || sequenceFeatures[sfindex].getEnd() < spos))\r
+            continue;\r
 \r
+          if (overview)\r
+          {\r
 \r
-            if (sf.getBegin() > seq.getEnd())\r
+            if (sequenceFeatures[sfindex].begin - 1 <= start &&\r
+                sequenceFeatures[sfindex].end + 1 >= start)\r
             {\r
-                continue;\r
+              currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].\r
+                  type);\r
             }\r
 \r
-            if (type.equals("disulfide bond"))\r
-            {\r
+          }\r
+          else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))\r
+          {\r
 \r
-              renderFeature(g, seq,\r
-                            seq.findIndex(sf.getBegin()) - 1,\r
-                            seq.findIndex(sf.getBegin()) - 1,\r
-                            type, start, end, x1, y1, width, height);\r
-              renderFeature(g, seq,\r
-                            seq.findIndex(sf.getEnd()) - 1,\r
-                            seq.findIndex(sf.getEnd()) - 1,\r
-                            type, start, end, x1, y1, width, height);\r
+            renderFeature(g, seq,\r
+                          seq.findIndex(sequenceFeatures[sfindex].begin) - 1,\r
+                          seq.findIndex(sequenceFeatures[sfindex].begin) - 1,\r
+                          new Color( ( (Integer) av.featuresDisplayed.get(\r
+                sequenceFeatures[sfindex].type)).intValue()),\r
+                          start, end, y1, width, height);\r
+            renderFeature(g, seq,\r
+                          seq.findIndex(sequenceFeatures[sfindex].end) - 1,\r
+                          seq.findIndex(sequenceFeatures[sfindex].end) - 1,\r
+                          new Color( ( (Integer) av.featuresDisplayed.get(\r
+                sequenceFeatures[sfindex].type)).intValue()),\r
+                          start, end, y1, width, height);\r
+\r
+          }\r
+          else\r
+            renderFeature(g, seq,\r
+                          seq.findIndex(sequenceFeatures[sfindex].begin) - 1,\r
+                          seq.findIndex(sequenceFeatures[sfindex].end) - 1,\r
+                          getColour(sequenceFeatures[sfindex].type),\r
+                          start, end, y1, width, height);\r
 \r
-            }\r
-            else\r
-              renderFeature(g, seq,\r
-                            seq.findIndex(sf.getBegin()) - 1,\r
-                            seq.findIndex(sf.getEnd()) - 1,\r
-                          type, start, end, x1, y1, width, height);\r
+\r
+        }\r
 \r
       }\r
     }\r
 \r
+\r
+    char s;\r
+    int i;\r
     void renderFeature(Graphics g, SequenceI seq,\r
-                       int fstart, int fend, String type, int start, int end,\r
-                       int x1, int y1, int width, int height)\r
+                       int fstart, int fend, Color featureColour, int start, int end,  int y1, int width, int height)\r
     {\r
-      if ( ( (fstart <= end) && (fend >= start)))\r
+\r
+      if (((fstart <= end) && (fend >= start)))\r
       {\r
         if (fstart < start)\r
         { // fix for if the feature we have starts before the sequence start,\r
@@ -168,97 +258,105 @@ public class FeatureRenderer
         {\r
           fend = end;\r
         }\r
-        for (int i = fstart; i <= fend; i++)\r
-        {\r
-          char s = seq.getSequence().charAt(i);\r
 \r
-          if (jalview.util.Comparison.isGap(s))\r
+          for (i = fstart; i <= fend; i++)\r
           {\r
-            continue;\r
-          }\r
+            s = seq.getSequence().charAt(i);\r
 \r
-          currentColour = getColour(type);\r
+            if (jalview.util.Comparison.isGap(s))\r
+            {\r
+              continue;\r
+            }\r
 \r
-          g.setColor(currentColour);\r
+            g.setColor(featureColour);\r
 \r
-          g.fillRect( (i - start) * width, y1, width, height);\r
+            g.fillRect( (i - start) * width, y1, width, height);\r
 \r
-          if (drawText)\r
-          {\r
             g.setColor(Color.white);\r
             charOffset = (width - fm.charWidth(s)) / 2;\r
             g.drawString(String.valueOf(s),\r
-                         charOffset + x1 + (width * (i - start)),\r
-                         (y1 + height) - height / 5);\r
+                         charOffset + (width * (i - start)),\r
+                         (y1 + height) - height / 5); //pady = height / 5;\r
+\r
           }\r
-        }\r
       }\r
     }\r
 \r
-    Image offscreen;\r
-    Frame nullframe;\r
-    public Color findFeatureColour(Color initialCol, SequenceI seq, int i)\r
+    void findAllFeatures()\r
     {\r
-      currentColour = initialCol;\r
-      if(offscreen == null)\r
+      av.featuresDisplayed = new Hashtable();\r
+      for (int i = 0; i < av.alignment.getHeight(); i++)\r
       {\r
-        nullframe = new Frame();\r
-        nullframe.addNotify();\r
-        offscreen = nullframe.createImage(1, 1);\r
-      }\r
+        SequenceFeature [] features = av.alignment.getSequenceAt(i).getSequenceFeatures();\r
 \r
-      drawSequence(offscreen.getGraphics(), seq, i, i, 0, 0, 1, 1);\r
-      return currentColour;\r
-    }\r
+        if (features == null)\r
+          continue;\r
 \r
+        int index = 0;\r
+        while (index < features.length)\r
+        {\r
+          if (!av.featuresDisplayed.containsKey(features[index].getType()))\r
+          {\r
+            av.featuresDisplayed.put(features[index].getType(),\r
+                                  new Integer( getColour(features[index].getType()).getRGB()) );\r
+          }\r
+          index++;\r
+        }\r
+      }\r
 \r
+      renderOrder = new String[av.featuresDisplayed.size()];\r
+      Enumeration en = av.featuresDisplayed.keys();\r
+      int i = 0;\r
+      while(en.hasMoreElements())\r
+      {\r
+        renderOrder[i] = en.nextElement().toString();\r
+        i++;\r
+      }\r
+    }\r
 \r
     public Color getColour(String featureType)\r
     {\r
       return (Color)featureColours.get(featureType);\r
     }\r
 \r
-    public void setColour(String featureType, Color col)\r
-    {\r
-      featureColours.put(featureType, col);\r
-    }\r
-\r
     public void addNewFeature(String name, Color col)\r
     {\r
+\r
       setColour(name, col);\r
-      if (featuresDisplayed == null)\r
-        featuresDisplayed = new Vector();\r
-      featuresDisplayed.insertElementAt(name, 0);\r
-    }\r
+      if(av.featuresDisplayed==null)\r
+        av.featuresDisplayed = new Hashtable();\r
 \r
 \r
- /*   public void setTransparency(float value)\r
-    {\r
-      transparency = value;\r
+      av.featuresDisplayed.put(name, "NOGROUP");\r
     }\r
 \r
-    public float getTransparency()\r
+    public void setColour(String featureType, Color col)\r
     {\r
-      return transparency;\r
-    }*/\r
+      featureColours.put(featureType, col);\r
+    }\r
 \r
     public void setFeaturePriority(Object [][] data)\r
     {\r
       // The feature table will display high priority\r
       // features at the top, but theses are the ones\r
       // we need to render last, so invert the data\r
-      if(featuresDisplayed!=null)\r
-        featuresDisplayed.removeAllElements();\r
-      else\r
-        featuresDisplayed = new Vector();\r
+      av.featuresDisplayed.clear();\r
+\r
+      renderOrder = new String[data.length];\r
 \r
-      for(int i=data.length-1; i>-1; i--)\r
+      if(data.length>0)\r
+      for(int i=0; i<data.length; i++)\r
       {\r
        String type = data[i][0].toString();\r
        setColour(type, (Color)data[i][1]);\r
        if( ((Boolean)data[i][2]).booleanValue() )\r
-         featuresDisplayed.addElement(type);\r
+       {\r
+         av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));\r
+       }\r
+\r
+       renderOrder[i] = type;\r
       }\r
+\r
     }\r
 \r
     Hashtable featureColours = new Hashtable();\r
index 30d57e0..20e4a4d 100755 (executable)
@@ -33,6 +33,7 @@ public class FeatureSettings extends Panel implements ItemListener,
   final Frame frame;\r
   Panel groupPanel;\r
   Panel featurePanel = new Panel();\r
+  ScrollPane scrollPane;\r
 \r
   public FeatureSettings(AlignViewport av, final AlignmentPanel ap)\r
   {\r
@@ -43,17 +44,16 @@ public class FeatureSettings extends Panel implements ItemListener,
     setTableData();\r
 \r
     this.setLayout(new BorderLayout());\r
-    ScrollPane sp = new ScrollPane();\r
-    sp.add(featurePanel);\r
-    add(sp, BorderLayout.CENTER);\r
+    scrollPane = new ScrollPane();\r
+    scrollPane.add(featurePanel);\r
+    add(scrollPane, BorderLayout.CENTER);\r
     if(groupPanel!=null)\r
     {\r
-      sp = new ScrollPane();\r
-      sp.add(groupPanel);\r
+      groupPanel.setLayout(\r
+          new GridLayout(groupPanel.getComponentCount() / 4, 4));\r
+      groupPanel.validate();\r
 \r
-      add(sp, BorderLayout.NORTH);\r
-      sp.validate();\r
-      sp.setSize( 100, 55);\r
+      add(groupPanel, BorderLayout.NORTH);\r
     }\r
     frame = new Frame();\r
     frame.add(this);\r
@@ -61,7 +61,7 @@ public class FeatureSettings extends Panel implements ItemListener,
     if (height>400)\r
       height = 400;\r
 \r
-    jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", 200,\r
+    jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", 250,\r
                                      height);\r
   }\r
 \r
@@ -73,55 +73,98 @@ public class FeatureSettings extends Panel implements ItemListener,
       fr.featureGroups.clear();\r
 \r
     Vector allFeatures = new Vector();\r
-    Enumeration tmpfeatures;\r
-    SequenceFeature sf;\r
+    SequenceFeature[] tmpfeatures;\r
 \r
     for (int i = 0; i < av.alignment.getHeight(); i++)\r
     {\r
       if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)\r
         continue;\r
 \r
-      tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures().elements();\r
-      while (tmpfeatures.hasMoreElements())\r
+      tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();\r
+      int index = 0;\r
+      while (index < tmpfeatures.length)\r
       {\r
-        sf = (SequenceFeature) tmpfeatures.nextElement();\r
-        if (!allFeatures.contains(sf.getType()))\r
+        if(tmpfeatures[index].getFeatureGroup()!=null\r
+           && tmpfeatures[index].getFeatureGroup().length()>0\r
+           && !fr.featureGroups.containsKey(tmpfeatures[index].getFeatureGroup()))\r
         {\r
-          allFeatures.addElement(sf.getType());\r
-          Color col = fr.getColour(sf.getType());\r
-          boolean displayed = true;\r
-          if (fr.featuresDisplayed != null)\r
-            displayed = fr.featuresDisplayed.contains(sf.getType());\r
-\r
-          Checkbox check = new Checkbox(sf.getType(), displayed);\r
-          check.addMouseListener(this);\r
-          check.addMouseMotionListener(this);\r
-          check.setBackground(col);\r
-          check.addItemListener(this);\r
-          featurePanel.add(check);\r
-        }\r
-        if(sf.getFeatureGroup()!=null\r
-           && !fr.featureGroups.containsKey(sf.getFeatureGroup()))\r
-        {\r
-          fr.featureGroups.put(sf.getFeatureGroup(), new Boolean(true));\r
+          fr.featureGroups.put(tmpfeatures[index].getFeatureGroup(), new Boolean(true));\r
           if(groupPanel==null)\r
+          {\r
             groupPanel = new Panel();\r
+          }\r
 \r
-          Checkbox check = new Checkbox(sf.getFeatureGroup(), true);\r
+          Checkbox check = new Checkbox(tmpfeatures[index].getFeatureGroup(), true);\r
           check.setFont(new Font("Serif", Font.BOLD, 12));\r
           check.addItemListener(this);\r
           groupPanel.add(check);\r
         }\r
+\r
+        if (!allFeatures.contains(tmpfeatures[index].getType()))\r
+        {\r
+            allFeatures.addElement(tmpfeatures[index].getType());\r
+        }\r
+\r
+\r
+        index ++;\r
       }\r
     }\r
 \r
+    resetTable();\r
     featurePanel.setLayout(new GridLayout(allFeatures.size(), 2, 10,5));\r
     featurePanel.validate();\r
   }\r
 \r
+  void resetTable()\r
+  {\r
+    featurePanel.removeAll();\r
+    Enumeration groups = fr.featureGroups.keys();\r
+    SequenceFeature [] tmpfeatures;\r
+    String group, type;\r
+    Vector checksAdded = new Vector();\r
+\r
+    while(groups.hasMoreElements())\r
+    {\r
+     group = groups.nextElement().toString();\r
+     if ( !( (Boolean) fr.featureGroups.get(group)).booleanValue())\r
+       continue;\r
+\r
+     for (int i = 0; i < av.alignment.getHeight(); i++)\r
+       {\r
+         if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)\r
+           continue;\r
+\r
+         tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();\r
+         int index = 0;\r
+         while (index < tmpfeatures.length)\r
+         {\r
+           if (tmpfeatures[index].getFeatureGroup() != null &&\r
+               tmpfeatures[index].getFeatureGroup().equals(group))\r
+           {\r
+             type = tmpfeatures[index].getType();\r
+             if(!checksAdded.contains(type))\r
+             {\r
+               checksAdded.addElement(type);\r
+               Checkbox check = new Checkbox(type, true);\r
+               check.addMouseListener(this);\r
+               check.addMouseMotionListener(this);\r
+               check.setBackground(fr.getColour(type));\r
+               check.addItemListener(this);\r
+               featurePanel.add(check);\r
+             }\r
+           }\r
+           index++;\r
+         }\r
+     }\r
+   }\r
+   featurePanel.validate();\r
+   if(scrollPane!=null)\r
+     scrollPane.validate();\r
+  }\r
+\r
   public void itemStateChanged(ItemEvent evt)\r
   {\r
-    if(evt!=null)\r
+    if (evt != null)\r
     {\r
       //Is the source a top level featureGroup?\r
       Checkbox source = (Checkbox) evt.getSource();\r
@@ -132,6 +175,7 @@ public class FeatureSettings extends Panel implements ItemListener,
         if (ap.overviewPanel != null)\r
           ap.overviewPanel.updateOverviewImage();\r
 \r
+        resetTable();\r
         return;\r
       }\r
     }\r
@@ -139,16 +183,23 @@ public class FeatureSettings extends Panel implements ItemListener,
       Component[] comps = featurePanel.getComponents();\r
       int cSize = comps.length;\r
 \r
-      Object[][] data = new Object[cSize][3];\r
-\r
+      Object[][] tmp = new Object[cSize][3];\r
+      int tmpSize = 0;\r
       for (int i = 0; i < cSize; i++)\r
       {\r
         Checkbox check = (Checkbox) comps[i];\r
-        data[i][0] = check.getLabel();\r
-        data[i][1] = fr.getColour(check.getLabel());\r
-        data[i][2] = new Boolean(check.getState());\r
+        if(!check.getState())\r
+          continue;\r
+\r
+        tmp[tmpSize][0] = check.getLabel();\r
+        tmp[tmpSize][1] = fr.getColour(check.getLabel());\r
+        tmp[tmpSize][2] = new Boolean(check.getState());\r
+        tmpSize++;\r
       }\r
 \r
+      Object[][]data = new Object[tmpSize][3];\r
+      System.arraycopy(tmp, 0, data,0, tmpSize);\r
+\r
       fr.setFeaturePriority(data);\r
       ap.seqPanel.seqCanvas.repaint();\r
       if (ap.overviewPanel != null)\r
index 47c781b..8d8472e 100755 (executable)
@@ -57,8 +57,10 @@ public class OverviewPanel
     sr = new SequenceRenderer(av);\r
     sr.graphics = nullFrame.getGraphics();\r
     sr.renderGaps( false );\r
+    sr.forOverview = true;\r
     fr = new FeatureRenderer(av);\r
     fr.drawText = false;\r
+    fr.overview = true;\r
 \r
 \r
 \r
@@ -185,10 +187,9 @@ public class OverviewPanel
 \r
     if (av.showSequenceFeatures)\r
    {\r
-     fr.featuresDisplayed = ap.seqPanel.seqCanvas.getFeatureRenderer().\r
-         featuresDisplayed;\r
      fr.featureGroups = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups;\r
      fr.featureColours = ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours;\r
+     fr.sequenceFeatures = ap.seqPanel.seqCanvas.getFeatureRenderer().sequenceFeatures;\r
    }\r
 \r
 \r
@@ -237,12 +238,14 @@ public class OverviewPanel
     float sampleCol = (float) alwidth / (float) width;\r
     float sampleRow = (float) alheight / (float) sequencesHeight;\r
 \r
-    int lastcol=-1, lastrow=-1;\r
+    int lastcol=-1, lastrow=0;\r
     Color color = Color.yellow;\r
     int row, col;\r
-    for (col = 0; col < width; col++)\r
+    jalview.datamodel.SequenceI sequence;\r
+    for (row = 0; row < sequencesHeight; row++)\r
     {\r
-      for (row = 0; row < sequencesHeight; row++)\r
+      sequence = av.getAlignment().getSequenceAt(lastrow);\r
+      for (col = 0; col < width; col++)\r
       {\r
         if((int)(col*sampleCol) == lastcol && (int)(row*sampleRow)==lastrow)\r
         {\r
@@ -250,29 +253,18 @@ public class OverviewPanel
           continue;\r
         }\r
 \r
-        if (av.conservation != null)\r
-        {\r
-          mg.translate(col, sequencesHeight);\r
-          ap.annotationPanel.drawGraph(mg, av.conservation,\r
-                                       (int) (sampleCol) + 1,\r
-                                       graphHeight,\r
-                                       (int) (col * sampleCol),\r
-                                       (int) (col * sampleCol) + 1);\r
-          mg.translate( -col, -sequencesHeight);\r
-        }\r
-\r
         lastrow = (int)(row*sampleRow);\r
         lastcol = (int)(col*sampleCol);\r
 \r
         if(av.alignment.getSequenceAt(lastrow).getLength()>lastcol)\r
         {\r
-          color = sr.findSequenceColour(av.alignment.getSequenceAt(lastrow), lastcol);\r
 \r
+          color = sr.findSequenceColour(sequence, lastcol);\r
 \r
           if (av.showSequenceFeatures)\r
             color = fr.findFeatureColour(color,\r
-                                        av.alignment.getSequenceAt(lastrow),\r
-                                         lastcol);\r
+                                        sequence,\r
+                                        lastcol);\r
         }\r
 \r
         mg.setColor(color);\r
@@ -281,6 +273,24 @@ public class OverviewPanel
       }\r
     }\r
 \r
+    if (av.conservation != null)\r
+    {\r
+      for (col = 0; col < width; col++)\r
+      {\r
+        lastcol = (int) (col * sampleCol);\r
+        {\r
+          mg.translate(col, sequencesHeight);\r
+          ap.annotationPanel.drawGraph(mg, av.conservation,\r
+                                       (int) (sampleCol) + 1,\r
+                                       graphHeight,\r
+                                       (int) (col * sampleCol),\r
+                                       (int) (col * sampleCol) + 1);\r
+          mg.translate( -col, -sequencesHeight);\r
+        }\r
+      }\r
+    }\r
+\r
+\r
 \r
     System.gc();\r
 \r