New files
authoramwaterhouse <Andrew Waterhouse>
Mon, 30 Jan 2006 14:46:15 +0000 (14:46 +0000)
committeramwaterhouse <Andrew Waterhouse>
Mon, 30 Jan 2006 14:46:15 +0000 (14:46 +0000)
src/jalview/datamodel/GraphLine.java [new file with mode: 0755]
src/jalview/io/AnnotationReader.java [new file with mode: 0755]
src/jalview/schemes/AnnotationColourGradient.java [new file with mode: 0755]

diff --git a/src/jalview/datamodel/GraphLine.java b/src/jalview/datamodel/GraphLine.java
new file mode 100755 (executable)
index 0000000..2f484ac
--- /dev/null
@@ -0,0 +1,37 @@
+/*\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.datamodel;\r
+\r
+public class GraphLine\r
+{\r
+    public float value;\r
+    public String label = "";\r
+    public java.awt.Color colour = java.awt.Color.black;\r
+    public boolean displayed = true;\r
+\r
+    public GraphLine(float value, String label, java.awt.Color col)\r
+    {\r
+      this.value = value;\r
+      if(label != null)\r
+        this.label = label;\r
+\r
+      if(col != null )\r
+        this.colour = col;\r
+    }\r
+}\r
diff --git a/src/jalview/io/AnnotationReader.java b/src/jalview/io/AnnotationReader.java
new file mode 100755 (executable)
index 0000000..5ba1e26
--- /dev/null
@@ -0,0 +1,319 @@
+package jalview.io;\r
+\r
+import java.io.*;\r
+import jalview.datamodel.*;\r
+import java.util.StringTokenizer;\r
+import jalview.schemes.UserColourScheme;\r
+import jalview.gui.FeatureRenderer;\r
+\r
+public class AnnotationReader\r
+{\r
+  public boolean readGroupsFile(FeatureRenderer fr, AlignmentI al, String file)\r
+  {\r
+    try\r
+    {\r
+      BufferedReader in = new BufferedReader(new FileReader(file));\r
+      SequenceI seq = null;\r
+      String line, type, desc, token;\r
+\r
+      int index, start, end;\r
+      StringTokenizer st;\r
+      SequenceFeature sf;\r
+      int lineNo = 0;\r
+      while ( (line = in.readLine()) != null)\r
+      {\r
+        lineNo++;\r
+        st = new StringTokenizer(line, "\t");\r
+        if (st.countTokens() == 2)\r
+        {\r
+          type = st.nextToken();\r
+          UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
+          fr.setColour(type, ucs.findColour("A"));\r
+          continue;\r
+        }\r
+\r
+        while (st.hasMoreElements())\r
+        {\r
+          desc = st.nextToken();\r
+          token = st.nextToken();\r
+          if (!token.equals("ID_NOT_SPECIFIED"))\r
+          {\r
+            index = al.findIndex(al.findName(token));\r
+            st.nextToken();\r
+          }\r
+          else\r
+          {\r
+            index = Integer.parseInt(st.nextToken());\r
+          }\r
+\r
+          start = Integer.parseInt(st.nextToken());\r
+          end = Integer.parseInt(st.nextToken());\r
+\r
+          seq = al.getSequenceAt(index);\r
+          start = seq.findIndex(start) - 1;\r
+          end = seq.findIndex(end) - 1;\r
+\r
+          type = st.nextToken();\r
+\r
+          if (fr.getColour(type) == null)\r
+          {\r
+            // Probably the old style groups file\r
+            UserColourScheme ucs = new UserColourScheme(type);\r
+            fr.setColour(type, ucs.findColour("A"));\r
+          }\r
+\r
+          sf = new SequenceFeature(type, desc, "", start, end);\r
+\r
+          seq.getDatasetSequence().addSequenceFeature(sf);\r
+        }\r
+      }\r
+    }\r
+    catch (Exception ex)\r
+    {\r
+      System.out.println("Error parsing groups file: " + ex);\r
+      return false;\r
+    }\r
+\r
+    return true;\r
+  }\r
+\r
+  public boolean readAnnotationFile(AlignmentI al, String file)\r
+  {\r
+    try\r
+    {\r
+      BufferedReader in = new BufferedReader(new FileReader(file));\r
+      String line, label, description, token;\r
+      int graphStyle, index;\r
+      SequenceI refSeq = null;\r
+      int refSeqIndex = -1;\r
+\r
+\r
+      StringTokenizer st;\r
+      Annotation[] annotations;\r
+      AlignmentAnnotation annotation = null;\r
+\r
+      // First confirm this is an Annotation file\r
+      boolean jvAnnotationFile = false;\r
+      while ( (line = in.readLine()) != null)\r
+      {\r
+        if (line.indexOf("#") == 0 )\r
+          continue;\r
+\r
+        if (line.indexOf("JALVIEW_ANNOTATION") > -1)\r
+        {\r
+          jvAnnotationFile = true;\r
+          break;\r
+        }\r
+      }\r
+\r
+      if(!jvAnnotationFile)\r
+      {\r
+        in.close();\r
+        return false;\r
+      }\r
+\r
+      while ( (line = in.readLine()) != null)\r
+      {\r
+        if(line.indexOf("#")==0\r
+           || line.indexOf("JALVIEW_ANNOTATION")>-1\r
+           || line.length()==0)\r
+          continue;\r
+\r
+        st = new StringTokenizer(line, "\t");\r
+        token = st.nextToken();\r
+        if(token.equalsIgnoreCase("COLOUR"))\r
+        {\r
+          colourAnnotations(al, st.nextToken(), st.nextToken());\r
+          continue;\r
+        }\r
+\r
+        if(token.equalsIgnoreCase("COMBINE") )\r
+        {\r
+          combineAnnotations(al, st);\r
+          continue;\r
+        }\r
+\r
+        if (token.equalsIgnoreCase("GRAPHLINE"))\r
+        {\r
+          addLine(al, st);\r
+          continue;\r
+        }\r
+\r
+\r
+        if(token.equalsIgnoreCase("SEQUENCE_REF") )\r
+        {\r
+          refSeq = al.findName(st.nextToken());\r
+          try{\r
+            refSeqIndex = Integer.parseInt(st.nextToken());\r
+          }\r
+          catch(NumberFormatException ex)\r
+          {\r
+            refSeqIndex = 0;\r
+          }\r
+\r
+          continue;\r
+        }\r
+\r
+        graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
+        label = description = st.nextToken();\r
+\r
+        line = st.nextToken();\r
+        st = new StringTokenizer(line, "|", true);\r
+        annotations = new Annotation[st.countTokens()];\r
+        index = 0;\r
+        boolean emptyColumn = true;\r
+        while (st.hasMoreElements())\r
+        {\r
+          token = st.nextToken().trim();\r
+\r
+          if(token.equals("|"))\r
+          {\r
+            if(emptyColumn)\r
+              annotations[index++] = parseAnnotation("");\r
+\r
+            emptyColumn = true;\r
+          }\r
+          else\r
+          {\r
+            annotations[index++] = parseAnnotation(token);\r
+            emptyColumn = false;\r
+          }\r
+        }\r
+\r
+       annotation = new AlignmentAnnotation(label,\r
+                                          description,\r
+                                          annotations,\r
+                                          0,\r
+                                          0,\r
+                                          graphStyle);\r
+\r
+       al.addAnnotation(annotation, refSeq, refSeqIndex);\r
+\r
+      }\r
+    }catch(Exception ex)\r
+    {ex.printStackTrace();\r
+    //  System.out.println("Problem reading annotation file: "+ex);\r
+      return false;\r
+    }\r
+    return true;\r
+  }\r
+\r
+  Annotation parseAnnotation(String string)\r
+  {\r
+    String desc = "", displayChar="";\r
+    char ss = ' '; // secondaryStructure\r
+    float value = 0;\r
+    StringTokenizer st = new StringTokenizer(string, ",");\r
+    String token;\r
+    while(st.hasMoreTokens())\r
+    {\r
+      token = st.nextToken().trim();\r
+      if(token.length()==0)\r
+        continue;\r
+\r
+      if(value==0)\r
+      {  try{\r
+          value = Float.parseFloat(token);\r
+        }catch(NumberFormatException ex){}\r
+      }\r
+\r
+      if(token.length()==1)\r
+      {\r
+        // Either this character represents a helix or sheet\r
+        // or an integer which can be displayed\r
+        if(token.equals("H") || token.equals("E"))\r
+        {\r
+          ss = token.charAt(0);\r
+        }\r
+        else //if(value!=0)\r
+        {\r
+          displayChar = token;\r
+        }\r
+      }\r
+      else if(desc.length()<1)\r
+        desc = token;\r
+      else\r
+        displayChar = token;\r
+    }\r
+\r
+    return new Annotation(displayChar, desc, ss, value);\r
+  }\r
+\r
+  void colourAnnotations(AlignmentI al, String label, String colour)\r
+  {\r
+    UserColourScheme ucs = new UserColourScheme(colour);\r
+    Annotation[] annotations;\r
+    for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
+    {\r
+      if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))\r
+      {\r
+        annotations = al.getAlignmentAnnotation()[i].annotations;\r
+        for(int j=0; j<annotations.length; j++)\r
+        {\r
+          if(annotations[j]!=null)\r
+            annotations[j].colour = ucs.findColour("A");\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  void combineAnnotations(AlignmentI al, StringTokenizer st)\r
+  {\r
+    int graphGroup = -1;\r
+    String group = st.nextToken();\r
+    //First make sure we are not overwriting the graphIndex\r
+    for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
+    {\r
+      if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
+      {\r
+        graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;\r
+        al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
+        break;\r
+      }\r
+    }\r
+\r
+    //Now update groups\r
+    while(st.hasMoreTokens())\r
+    {\r
+      group = st.nextToken();\r
+      for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
+      {\r
+        if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
+        {\r
+          al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
+          break;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  void addLine(AlignmentI al, StringTokenizer st)\r
+  {\r
+    String group = st.nextToken();\r
+    AlignmentAnnotation annotation = null;\r
+\r
+    for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
+    {\r
+      if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
+      {\r
+        annotation = al.getAlignmentAnnotation()[i];\r
+        break;\r
+      }\r
+    }\r
+\r
+    if(annotation==null)\r
+      return;\r
+\r
+    float value = Float.parseFloat(st.nextToken());\r
+    String label = st.hasMoreTokens() ?  st.nextToken() : null;\r
+    java.awt.Color colour = null;\r
+    if(st.hasMoreTokens())\r
+    {\r
+      UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
+      colour = ucs.findColour("A");\r
+    }\r
+\r
+    annotation.addGraphLine(new GraphLine(value, label, colour));\r
+\r
+  }\r
+}\r
diff --git a/src/jalview/schemes/AnnotationColourGradient.java b/src/jalview/schemes/AnnotationColourGradient.java
new file mode 100755 (executable)
index 0000000..b505aa6
--- /dev/null
@@ -0,0 +1,114 @@
+/*\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.schemes;\r
+\r
+import java.awt.*;\r
+\r
+import jalview.datamodel.*;\r
+\r
+public class AnnotationColourGradient extends ResidueColourScheme\r
+{\r
+    AlignmentAnnotation annotation;\r
+    float minCol, maxCol, minValue, maxValue;\r
+    boolean aboveAnnotationThreshold = false;\r
+\r
+    float r1, g1, b1, rr, gg, bb, dr, dg, db;\r
+    float range;\r
+\r
+    /**\r
+     * Creates a new AnnotationColourGradient object.\r
+     */\r
+    public AnnotationColourGradient(AlignmentAnnotation annotation,\r
+        Color minColour, Color maxColour, boolean aboveThreshold)\r
+    {\r
+      this.annotation = annotation;\r
+\r
+      aboveAnnotationThreshold = aboveThreshold;\r
+\r
+      r1 = minColour.getRed();\r
+      g1 = minColour.getGreen();\r
+      b1 = minColour.getBlue();\r
+\r
+      rr = maxColour.getRed() - r1;\r
+      gg = maxColour.getGreen() - g1;\r
+      bb = maxColour.getBlue() - b1;\r
+\r
+      range = annotation.graphMax - annotation.graphMin;\r
+\r
+    }\r
+\r
+    /**\r
+     * DOCUMENT ME!\r
+     *\r
+     * @param n DOCUMENT ME!\r
+     *\r
+     * @return DOCUMENT ME!\r
+     */\r
+    public Color findColour(String n)\r
+    {\r
+      System.out.println("AnnotationColourGradient findColour(string)");\r
+        return Color.RED;\r
+    }\r
+\r
+    /**\r
+     * DOCUMENT ME!\r
+     *\r
+     * @param n DOCUMENT ME!\r
+     * @param j DOCUMENT ME!\r
+     *\r
+     * @return DOCUMENT ME!\r
+     */\r
+    public Color findColour(String n, int j)\r
+    {\r
+        if ((threshold == 0) || aboveThreshold(n, j))\r
+        {\r
+          if(annotation.annotations[j]==null)\r
+            currentColour = Color.white;\r
+          else\r
+          {\r
+            if(!aboveAnnotationThreshold || (annotation.graphLines!=null\r
+               && annotation.annotations[j].value>=annotation.getGraphLine(0).value))\r
+            {\r
+                dr = rr *\r
+                     ((annotation.annotations[j].value-annotation.graphMin) / range )\r
+                     +r1;\r
+                dg = gg *\r
+                     ((annotation.annotations[j].value-annotation.graphMin)  / range )\r
+                     +g1;\r
+                db = bb *\r
+                     ((annotation.annotations[j].value-annotation.graphMin) / range )\r
+                     +b1;\r
+\r
+                 currentColour = new Color( (int) dr, (int) dg, (int) db);\r
+            }\r
+            else\r
+              currentColour = Color.white;\r
+          }\r
+        }\r
+        else\r
+        {\r
+            return Color.white;\r
+        }\r
+\r
+        if(conservationColouring)\r
+         applyConservation(j);\r
+\r
+       return currentColour;\r
+    }\r
+}\r