Features added removed from alignment
[jalview.git] / src / jalview / gui / FeatureRenderer.java
index cd754c7..03080bd 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 jalview.datamodel.*;\r
-import jalview.schemes.*;\r
+\r
 import java.awt.*;\r
+\r
 import java.util.*;\r
 \r
-public class FeatureRenderer implements RendererI {\r
+import java.awt.image.*;\r
+\r
+\r
+/**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @author $author$\r
+ * @version $Revision$\r
+ */\r
+public class FeatureRenderer\r
+{\r
+    AlignViewport av;\r
+    Color resBoxColour;\r
+    float transparency = 1.0f;\r
+    FontMetrics fm;\r
+    int charOffset;\r
+    boolean drawText = true;\r
+\r
+    // The following vector holds the features which are\r
+    // to be added, in the correct order or rendering\r
+    Vector featuresDisplayed = null;\r
+\r
+    /**\r
+     * Creates a new FeatureRenderer object.\r
+     *\r
+     * @param av DOCUMENT ME!\r
+     */\r
+    public FeatureRenderer(AlignViewport av)\r
+    {\r
+        this.av = av;\r
+        initColours();\r
+    }\r
+\r
+    /**\r
+     * This is used by the Molecule Viewer to get the accurate colour\r
+     * of the rendered sequence\r
+     */\r
+    BufferedImage bi;\r
+    public synchronized Color findFeatureColour(Color initialCol, SequenceI seq, int i)\r
+    {\r
+      if(!av.showSequenceFeatures)\r
+        return initialCol;\r
+\r
+      if (bi == null)\r
+        bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);\r
+\r
+      bi.setRGB(0,0, initialCol.getRGB());\r
+\r
+      drawText = false;\r
 \r
+      drawSequence(bi.getGraphics(), seq, i, i, 0, 0, 1, 1);\r
+      drawText = true;\r
 \r
-    public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i) {\r
-       Color c = cs.findColour(seq,seq.getSequence(i,i+1),i,null);\r
-       return c;\r
+      return new Color(bi.getRGB(0, 0));\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,Vector pid, int seqnum,AlignViewport av) {\r
 \r
-       int i      = start;\r
-       int length = seq.getLength();\r
+    /**\r
+     * DOCUMENT ME!\r
+     *\r
+     * @param g DOCUMENT ME!\r
+     * @param seq DOCUMENT ME!\r
+     * @param sg DOCUMENT ME!\r
+     * @param start DOCUMENT ME!\r
+     * @param end DOCUMENT ME!\r
+     * @param x1 DOCUMENT ME!\r
+     * @param y1 DOCUMENT ME!\r
+     * @param width DOCUMENT ME!\r
+     * @param height DOCUMENT ME!\r
+     */\r
+    public void drawSequence(Graphics g, SequenceI seq,\r
+                             int start, int end, int x1, int y1, int width, int height)\r
+    {\r
 \r
-       Color currentColor = Color.black;\r
+//System.out.println(start+" "+end+" "+x1+" "+y1);\r
+      if (seq.getDatasetSequence().getSequenceFeatures() == null\r
+          || seq.getDatasetSequence().getSequenceFeatures().size()==0)\r
+        return;\r
 \r
+      fm = g.getFontMetrics();\r
+\r
+      if (transparency != 1)\r
+      {\r
+        Graphics2D g2 = (Graphics2D) g;\r
+        g2.setComposite(\r
+            AlphaComposite.getInstance(\r
+                AlphaComposite.SRC_OVER, transparency));\r
+      }\r
+\r
+      String type;\r
+      SequenceFeature sf;\r
+      if (featuresDisplayed == null)\r
+        findAllFeatures();\r
+\r
+      Enumeration e = featuresDisplayed.elements(), e2;\r
+\r
+      // Loop through each visible feature\r
+      while (e.hasMoreElements())\r
+      {\r
+\r
+        type = e.nextElement().toString();\r
+        e2 = seq.getDatasetSequence().getSequenceFeatures().elements();\r
+        // loop through all features in sequence to find\r
+        // current feature to render\r
+          while (e2.hasMoreElements())\r
+        {\r
+\r
+          sf = (SequenceFeature) e2.nextElement();\r
+          if (!type.equals(sf.getType()))\r
+            continue;\r
+\r
+          if (sf.getBegin() > seq.getEnd())\r
+            continue;\r
+\r
+          if (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
+\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
-       int    prev   = -1;\r
-        int    fstart = 0;\r
-        char   prevc   = '-';\r
+        if(transparency!=1.0f)\r
+        {\r
+          Graphics2D g2 = (Graphics2D) g;\r
+          g2.setComposite(\r
+              AlphaComposite.getInstance(\r
+                  AlphaComposite.SRC_OVER, 1.0f));\r
+        }\r
+    }\r
 \r
-       g.setColor(Color.magenta);\r
 \r
-       while (i <= end && i < length) {\r
-            char c = seq.getCharAt(i);\r
-           if (c != '-') {\r
+    void renderFeature(Graphics g, SequenceI seq,\r
+                       int fstart, int fend, String type, int start, int end, int x1, int y1, int width, int height)\r
+    {\r
 \r
-              if (prevc != c) {\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
+              fstart = start; // but the feature end is still valid!!\r
+          }\r
 \r
-                if (prevc == 'F') {\r
-                  g.setColor(Color.green);\r
-                } else if (prevc == 'R') {\r
-                  g.setColor(Color.cyan);\r
-                } else if (prevc == 'M') {\r
-                  g.setColor(Color.magenta);\r
-                } else if (prevc == 'P') {\r
-                  g.setColor(Color.yellow);\r
-                }\r
-               g.fillRect(x1+(int)(width*(i-fstart)),y1+height/4,(int)((prev-fstart+1)*width),height/2);\r
+          if (fend >= end)\r
+          {\r
+            fend = end;\r
+          }\r
+          for (int i = fstart; i <= fend; i++)\r
+          {\r
+            char s = seq.getSequence().charAt(i);\r
 \r
-                prevc   = c;\r
-                fstart = i;\r
+            if (jalview.util.Comparison.isGap(s))\r
+            {\r
+              continue;\r
             }\r
-          } else if (prevc != '-') {\r
-                if (prevc == 'F') {\r
-                  g.setColor(Color.green);\r
-                } else if (prevc == 'R') {\r
-                  g.setColor(Color.cyan);\r
-                } else if (prevc == 'M') {\r
-                  g.setColor(Color.magenta);\r
-                } else if (prevc == 'P') {\r
-                  g.setColor(Color.yellow);\r
-                }\r
-               g.fillRect(x1+(int)(width*(fstart-start)),y1+height/4,(int)((i-fstart+1)*width),height/2);\r
-\r
-                prevc = c;\r
-          } else {\r
-            fstart = -1;\r
+\r
+            g.setColor(getColour(type));\r
+\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); //pady = height / 5;\r
+           }\r
           }\r
-           i++;\r
-       }\r
-\r
-        if (fstart != -1) {\r
-        if (prevc == 'F') {\r
-          g.setColor(Color.green);\r
-        } else if (prevc == 'R') {\r
-          g.setColor(Color.cyan);\r
-        } else if (prevc == 'M') {\r
-          g.setColor(Color.magenta);\r
-        } else if (prevc == 'P') {\r
-          g.setColor(Color.yellow);\r
         }\r
+    }\r
 \r
-       //g.fillRect(x1+(int)(width*(fstart-start)),y1+height/4,(int)((end-fstart+1)*width),height/2);\r
+    void findAllFeatures()\r
+    {\r
+      Vector features = new Vector();\r
+      SequenceFeature sf;\r
+      featuresDisplayed = new Vector();\r
+      Enumeration e;\r
+      for (int i = 0; i < av.alignment.getHeight(); i++)\r
+      {\r
+        features = av.alignment.getSequenceAt(i).getDatasetSequence().\r
+            getSequenceFeatures();\r
+        if (features == null)\r
+          continue;\r
 \r
+        e = features.elements();\r
+        while (e.hasMoreElements())\r
+        {\r
+          sf = (SequenceFeature) e.nextElement();\r
+          if (!featuresDisplayed.contains(sf.getType()))\r
+          {\r
+            featuresDisplayed.addElement(sf.getType());\r
+          }\r
         }\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 setTransparency(float value)\r
+    {\r
+      transparency = value;\r
+    }\r
+\r
+    public float getTransparency()\r
+    {\r
+      return transparency;\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
+      featuresDisplayed.clear();\r
+      for(int i=data.length-1; i>-1; 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
+    }\r
+\r
+    Hashtable featureColours = new Hashtable();\r
+    void initColours()\r
+    {\r
+      featureColours.put("active site", new Color(255, 75, 0));\r
+      featureColours.put("binding site", new Color(245, 85, 0));\r
+      featureColours.put("calcium-binding region", new Color(235, 95, 0));\r
+      featureColours.put("chain", new Color(225, 105, 0));\r
+      featureColours.put("coiled-coil region", new Color(215, 115, 0));\r
+      featureColours.put("compositionally biased region", new Color(205, 125, 0));\r
+      featureColours.put("cross-link", new Color(195, 135, 0));\r
+      featureColours.put("disulfide bond", new Color(230,230,0));\r
+      featureColours.put("DNA-binding region", new Color(175, 155, 0));\r
+      featureColours.put("domain", new Color(165, 165, 0));\r
+      featureColours.put("glycosylation site", new Color(155, 175, 0));\r
+      featureColours.put("helix", new Color(145, 185, 0));\r
+      featureColours.put("initiator methionine", new Color(135, 195, 5));\r
+      featureColours.put("lipid moiety-binding region", new Color(125, 205, 15));\r
+      featureColours.put("metal ion-binding site", new Color(115, 215, 25));\r
+      featureColours.put("modified residue", new Color(105, 225, 35));\r
+      featureColours.put("mutagenesis site", new Color(95, 235, 45));\r
+      featureColours.put("non-consecutive residues", new Color(85, 245, 55));\r
+      featureColours.put("non-terminal residue", new Color(75, 255, 65));\r
+      featureColours.put("nucleotide phosphate-binding region",new Color(65, 245, 75));\r
+      featureColours.put("peptide", new Color(55, 235, 85));\r
+      featureColours.put("propeptide", new Color(45, 225, 95));\r
+      featureColours.put("region of interest", new Color(35, 215, 105));\r
+      featureColours.put("repeat", new Color(25, 205, 115));\r
+      featureColours.put("selenocysteine", new Color(15, 195, 125));\r
+      featureColours.put("sequence conflict", new Color(5, 185, 135));\r
+      featureColours.put("sequence variant", new Color(0, 175, 145));\r
+      featureColours.put("short sequence motif", new Color(0, 165, 155));\r
+      featureColours.put("signal peptide", new Color(0, 155, 165));\r
+      featureColours.put("site", new Color(0, 145, 175));\r
+      featureColours.put("splice variant", new Color(0, 135, 185));\r
+      featureColours.put("strand", new Color(0, 125, 195));\r
+      featureColours.put("topological domain", new Color(0, 115, 205));\r
+      featureColours.put("transit peptide", new Color(0, 105, 215));\r
+      featureColours.put("transmembrane region", new Color(0, 95, 225));\r
+      featureColours.put("turn", new Color(0, 85, 235));\r
+      featureColours.put("unsure residue", new Color(0, 75, 245));\r
+      featureColours.put("zinc finger region", new Color(0, 65, 255));\r
     }\r
 \r
 }\r