protected now public
[jalview.git] / src / jalview / gui / SequenceRenderer.java
index 0c1bfdc..896fda5 100755 (executable)
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer\r
+ * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+ */\r
 package jalview.gui;\r
 \r
+import java.awt.*;\r
+\r
 import jalview.datamodel.*;\r
 import jalview.schemes.*;\r
 \r
-import java.awt.*;\r
-import java.util.*;\r
-\r
-public class SequenceRenderer implements RendererI\r
+public class SequenceRenderer\r
 {\r
+  AlignViewport av;\r
   FontMetrics fm;\r
+  boolean renderGaps = true;\r
+  SequenceGroup currentSequenceGroup = null;\r
+  SequenceGroup[] allGroups = null;\r
+  Color resBoxColour;\r
+  Graphics graphics;\r
 \r
-  public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
+  public SequenceRenderer(AlignViewport av)\r
   {\r
-   Color c = Color.white;\r
-    try{\r
-       c = cs.findColour(seq, seq.getSequence(i, i + 1), i, null);\r
-    }catch(Exception ex){}\r
-\r
-    return c;\r
+    this.av = av;\r
   }\r
 \r
+  public void renderGaps(boolean b)\r
+  {\r
+    renderGaps = b;\r
+  }\r
 \r
-  public void drawSequence(Graphics g,ColourSchemeI cs,SequenceI seq,int start, int end, int x1, int y1, double width, int height,boolean showScores, boolean displayBoxes, boolean displayText, boolean colourText,Vector freq, int seqnum) {\r
-\r
+  public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
+  {\r
+    getBoxColour(cs, seq, i);\r
 \r
-    if (displayBoxes)\r
-        drawBoxes(g,cs,seq,start,end,x1,y1,(int)width, height,freq);\r
+    return resBoxColour;\r
+  }\r
 \r
-    if (displayText)\r
+  void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
+  {\r
+    if (cs != null)\r
+    {\r
+      resBoxColour = cs.findColour(seq.getSequence(i, i + 1), i);\r
+    }\r
+    else\r
     {\r
-        fm = g.getFontMetrics();\r
-        drawText(g,cs,colourText,seq,start,end,x1,y1,(int)width,height);\r
+      resBoxColour = Color.white;\r
     }\r
   }\r
 \r
-  public void drawBoxes(Graphics g,ColourSchemeI cs, SequenceI seq,int start, int end, int x1, int y1, int width, int height,Vector freq) {\r
-    int i      = start;\r
-    int length = seq.getLength();\r
+  public void drawSequence(Graphics g, SequenceI seq, SequenceGroup[] sg,\r
+                           int start, int end, int x1, int y1, int width,\r
+                           int height)\r
+  {\r
+    allGroups = sg;\r
 \r
-    Color currentColor = Color.WHITE;\r
+    graphics = g;\r
 \r
-    int curStart = x1;\r
-    int curWidth = width;\r
+    drawBoxes(seq, start, end, x1, y1, (int) width, height);\r
+\r
+    fm = g.getFontMetrics();\r
+    drawText(seq, start, end, x1, y1, (int) width, height);\r
+  }\r
 \r
-  //  int threshold = 80;\r
+  public void drawBoxes(SequenceI seq, int start, int end, int x1, int y1,\r
+                        int width, int height)\r
+  {\r
+    int i = start;\r
+    int length = seq.getLength();\r
 \r
-    while (i <= end && i < length) {\r
-      Color c = getResidueBoxColour(cs,seq,i);\r
+    int curStart = -1;\r
+    int curWidth = width;\r
 \r
-    //  Hashtable hash  = (Hashtable)freq.elementAt(i-start);\r
-    //  String    s     = (String)hash.get("maxResidue");\r
-    //  int       count = ((Integer)hash.get("maxCount")).intValue();\r
-  //    int       max   = ((Integer)hash.get("size")).intValue();\r
-   //   int       nongap = ((Integer)hash.get("nongap")).intValue();\r
-   //   float     frac  = (float)(count*1.0/(1.0*nongap));\r
+    Color tempColour = null;\r
 \r
-      //System.out.println("Frac/count/nongap " + frac + " " + count + " " + nongap);\r
-    /*  if (!seq.getSequence().substring(i,i+1).equals(s) ||\r
-         s.equals("-") ||\r
-         s.equals(".") ||\r
-         s.equals(" "))\r
+    while ( (i <= end) && (i < length))\r
+    {\r
+      if (inCurrentSequenceGroup(i))\r
+      {\r
+        if (currentSequenceGroup.getDisplayBoxes())\r
+        {\r
+          getBoxColour(currentSequenceGroup.cs, seq, i);\r
+        }\r
+        else\r
+        {\r
+          resBoxColour = Color.white;\r
+        }\r
+      }\r
+      else if (av.getShowBoxes())\r
+      {\r
+        getBoxColour(av.getGlobalColourScheme(), seq, i);\r
+      }\r
+      else\r
       {\r
-       c = Color.white;\r
-      } else {\r
-       if (frac > 0.9) {\r
-         c = Color.red;\r
-       } else if (frac > 0.8) {\r
-         c = Color.orange;\r
-       } else if (frac > 0.7) {\r
-         c = Color.pink;\r
-       } else if (frac > 0.5) {\r
-         c = Color.yellow;\r
-       } else if (frac> 0.3) {\r
-           c = Color.lightGray;\r
-       }\r
-      }*/\r
-\r
-      if (c != currentColor || c != null)\r
+        resBoxColour = Color.white;\r
+      }\r
+\r
+      if (resBoxColour != tempColour)\r
       {\r
-        g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
+        if (tempColour != null)\r
+        {\r
+          graphics.fillRect(x1 + (width * (curStart - start)), y1,\r
+                            curWidth, height);\r
+        }\r
 \r
-        currentColor = c;\r
-        g.setColor(c);\r
+        graphics.setColor(resBoxColour);\r
 \r
         curStart = i;\r
         curWidth = width;\r
+        tempColour = resBoxColour;\r
       }\r
       else\r
+      {\r
         curWidth += width;\r
+      }\r
 \r
       i++;\r
     }\r
-    g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
+\r
+    graphics.fillRect(x1 + (width * (curStart - start)), y1, curWidth,\r
+                      height);\r
   }\r
 \r
-  public void drawText(Graphics g, ColourSchemeI cs, boolean colText, SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
+  public void drawText(SequenceI seq, int start, int end, int x1, int y1,\r
+                       int width, int height)\r
   {\r
-    int pady = height/5;\r
-    int charOffset=0;\r
-    g.setColor(Color.black);\r
-\r
+    int pady = height / 5;\r
+    int charOffset = 0;\r
     char s;\r
+\r
     // Need to find the sequence position here.\r
+    String sequence = seq.getSequence();\r
+\r
     for (int i = start; i <= end; i++)\r
     {\r
-        if (i < end && i < seq.getLength())\r
-          s = seq.getSequence().charAt(i);\r
-        else if(i<seq.getLength())\r
-          s = seq.getSequence().charAt(i);\r
-        else\r
-          s = ' ';\r
+      graphics.setColor(Color.black);\r
+\r
+      if (i < sequence.length())\r
+      {\r
+        s = sequence.charAt(i);\r
+      }\r
+      else\r
+      {\r
+        s = ' ';\r
+      }\r
+\r
+      if (!renderGaps && jalview.util.Comparison.isGap(s))\r
+      {\r
+        continue;\r
+      }\r
+\r
+      if (inCurrentSequenceGroup(i))\r
+      {\r
+        if (!currentSequenceGroup.getDisplayText())\r
+        {\r
+          continue;\r
+        }\r
+\r
+        if (currentSequenceGroup.getColourText())\r
+        {\r
+          getBoxColour(currentSequenceGroup.cs, seq, i);\r
+          graphics.setColor(resBoxColour.darker());\r
+        }\r
+      }\r
+      else\r
+      {\r
+        if (!av.getShowText())\r
+        {\r
+          continue;\r
+        }\r
 \r
-        if (colText)\r
+        if (av.getColourText())\r
         {\r
-            Color c = getResidueBoxColour(cs, seq, i);\r
-            g.setColor(c.darker());\r
+          getBoxColour(av.getGlobalColourScheme(), seq, i);\r
+\r
+          if (av.getShowBoxes())\r
+          {\r
+            graphics.setColor(resBoxColour.darker());\r
+          }\r
+          else\r
+          {\r
+            graphics.setColor(resBoxColour);\r
+          }\r
         }\r
+      }\r
 \r
-      charOffset =  (width - fm.charWidth(s))/2;\r
+      charOffset = (width - fm.charWidth(s)) / 2;\r
+      graphics.drawString(String.valueOf(s),\r
+                          charOffset + x1 + (width * (i - start)),\r
+                          (y1 + height) - pady);\r
+    }\r
+  }\r
 \r
+  boolean inCurrentSequenceGroup(int res)\r
+  {\r
+    if (allGroups == null)\r
+    {\r
+      return false;\r
+    }\r
 \r
-      g.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
+    for (int i = 0; i < allGroups.length; i++)\r
+    {\r
+      if ( (allGroups[i].getStartRes() <= res) &&\r
+          (allGroups[i].getEndRes() >= res))\r
+      {\r
+        currentSequenceGroup = allGroups[i];\r
+\r
+        return true;\r
+      }\r
     }\r
+\r
+    return false;\r
   }\r
 \r
+  public void drawHighlightedText(SequenceI seq, int start, int end, int x1,\r
+                                  int y1, int width, int height)\r
+  {\r
+    int pady = height / 5;\r
+    int charOffset = 0;\r
+    graphics.setColor(Color.BLACK);\r
+    graphics.fillRect(x1, y1, width * (end - start + 1), height);\r
+    graphics.setColor(Color.white);\r
+\r
+    char s = '~';\r
+\r
+    // Need to find the sequence position here.\r
+    for (int i = start; i <= end; i++)\r
+    {\r
+      if (i < seq.getLength())\r
+      {\r
+        s = seq.getSequence().charAt(i);\r
+      }\r
+\r
+      charOffset = (width - fm.charWidth(s)) / 2;\r
+      graphics.drawString(String.valueOf(s),\r
+                          charOffset + x1 + (width * (i - start)),\r
+                          (y1 + height) - pady);\r
+    }\r
+  }\r
 }\r