apply jalview code style
[jalview.git] / src / jalview / io / FeaturesFile.java
index 8f5e4bd..7c70b54 100755 (executable)
@@ -1,5 +1,5 @@
 /*\r
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)\r
  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle\r
  * \r
  * This file is part of Jalview.\r
@@ -104,9 +104,10 @@ public class FeaturesFile extends AlignFile
       SequenceFeature sf;\r
       String featureGroup = null, groupLink = null;\r
       Hashtable typeLink = new Hashtable();\r
-\r
+      /**\r
+       * when true, assume GFF style features rather than Jalview style.\r
+       */\r
       boolean GFFFile = true;\r
-\r
       while ((line = nextLine()) != null)\r
       {\r
         if (line.startsWith("#"))\r
@@ -115,6 +116,15 @@ public class FeaturesFile extends AlignFile
         }\r
 \r
         st = new StringTokenizer(line, "\t");\r
+        if (st.countTokens() == 1)\r
+        {\r
+          if (line.trim().equalsIgnoreCase("GFF"))\r
+          {\r
+            // Start parsing file as if it might be GFF again.\r
+            GFFFile = true;\r
+            continue;\r
+          }\r
+        }\r
         if (st.countTokens() > 1 && st.countTokens() < 4)\r
         {\r
           GFFFile = false;\r
@@ -140,58 +150,117 @@ public class FeaturesFile extends AlignFile
           {\r
             Object colour = null;\r
             String colscheme = st.nextToken();\r
-            if (colscheme.indexOf("|") > -1)\r
+            if (colscheme.indexOf("|") > -1\r
+                    || colscheme.trim().equalsIgnoreCase("label"))\r
             {\r
               // Parse '|' separated graduated colourscheme fields:\r
-              // mincolour|maxcolour|[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue\r
-              // first four are required.\r
-              // first two are hexadecimal or word equivalent colours.\r
-              // second two are values parsed as floats.\r
+              // [label|][mincolour|maxcolour|[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue]\r
+              // can either provide 'label' only, first is optional, next two\r
+              // colors are required (but may be\r
+              // left blank), next is optional, nxt two min/max are required.\r
+              // first is either 'label'\r
+              // first/second and third are both hexadecimal or word equivalent\r
+              // colour.\r
+              // next two are values parsed as floats.\r
               // fifth is either 'above','below', or 'none'.\r
               // sixth is a float value and only required when fifth is either\r
               // 'above' or 'below'.\r
-              StringTokenizer gcol = new StringTokenizer(colscheme, "|");\r
-              String mincol = gcol.nextToken(), maxcol = gcol.nextToken();\r
-              String abso = gcol.nextToken(), minval, maxval;\r
-              if (abso.toLowerCase().indexOf("abso") != 0)\r
-              {\r
-                minval = abso;\r
-                abso = null;\r
-              }\r
-              else\r
-              {\r
-                minval = gcol.nextToken();\r
-              }\r
-              maxval = gcol.nextToken();\r
+              StringTokenizer gcol = new StringTokenizer(colscheme, "|",\r
+                      true);\r
               // set defaults\r
               int threshtype = AnnotationColourGradient.NO_THRESHOLD;\r
               float min = Float.MIN_VALUE, max = Float.MAX_VALUE, threshval = Float.NaN;\r
-              try\r
-              {\r
-                if (minval.length() > 0)\r
-                {\r
-                  min = new Float(minval).floatValue();\r
-                }\r
-              } catch (Exception e)\r
+              boolean labelCol = false;\r
+              // Parse spec line\r
+              String mincol = gcol.nextToken();\r
+              if (mincol == "|")\r
               {\r
                 System.err\r
-                        .println("Couldn't parse the minimum value for graduated colour for type ("\r
-                                + colscheme\r
-                                + ") - did you misspell 'auto' for the optional automatic colour switch ?");\r
-                e.printStackTrace();\r
+                        .println("Expected either 'label' or a colour specification in the line: "\r
+                                + line);\r
+                continue;\r
               }\r
-              try\r
+              String maxcol = null;\r
+              if (mincol.toLowerCase().indexOf("label") == 0)\r
+              {\r
+                labelCol = true;\r
+                mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null); // skip\r
+                                                                           // '|'\r
+                mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);\r
+              }\r
+              String abso = null, minval, maxval;\r
+              if (mincol != null)\r
               {\r
-                if (maxval.length() > 0)\r
+                // at least four more tokens\r
+                if (mincol.equals("|"))\r
                 {\r
-                  max = new Float(maxval).floatValue();\r
+                  mincol = "";\r
                 }\r
-              } catch (Exception e)\r
+                else\r
+                {\r
+                  gcol.nextToken(); // skip next '|'\r
+                }\r
+                // continue parsing rest of line\r
+                maxcol = gcol.nextToken();\r
+                if (maxcol.equals("|"))\r
+                {\r
+                  maxcol = "";\r
+                }\r
+                else\r
+                {\r
+                  gcol.nextToken(); // skip next '|'\r
+                }\r
+                abso = gcol.nextToken();\r
+                gcol.nextToken(); // skip next '|'\r
+                if (abso.toLowerCase().indexOf("abso") != 0)\r
+                {\r
+                  minval = abso;\r
+                  abso = null;\r
+                }\r
+                else\r
+                {\r
+                  minval = gcol.nextToken();\r
+                  gcol.nextToken(); // skip next '|'\r
+                }\r
+                maxval = gcol.nextToken();\r
+                if (gcol.hasMoreTokens())\r
+                {\r
+                  gcol.nextToken(); // skip next '|'\r
+                }\r
+                try\r
+                {\r
+                  if (minval.length() > 0)\r
+                  {\r
+                    min = new Float(minval).floatValue();\r
+                  }\r
+                } catch (Exception e)\r
+                {\r
+                  System.err\r
+                          .println("Couldn't parse the minimum value for graduated colour for type ("\r
+                                  + colscheme\r
+                                  + ") - did you misspell 'auto' for the optional automatic colour switch ?");\r
+                  e.printStackTrace();\r
+                }\r
+                try\r
+                {\r
+                  if (maxval.length() > 0)\r
+                  {\r
+                    max = new Float(maxval).floatValue();\r
+                  }\r
+                } catch (Exception e)\r
+                {\r
+                  System.err\r
+                          .println("Couldn't parse the maximum value for graduated colour for type ("\r
+                                  + colscheme + ")");\r
+                  e.printStackTrace();\r
+                }\r
+              }\r
+              else\r
               {\r
-                System.err\r
-                        .println("Couldn't parse the maximum value for graduated colour for type ("\r
-                                + colscheme + ")");\r
-                e.printStackTrace();\r
+                // add in some dummy min/max colours for the label-only\r
+                // colourscheme.\r
+                mincol = "FFFFFF";\r
+                maxcol = "000000";\r
               }\r
               try\r
               {\r
@@ -209,6 +278,8 @@ public class FeaturesFile extends AlignFile
               if (colour != null)\r
               {\r
                 ((jalview.schemes.GraduatedColor) colour)\r
+                        .setColourByLabel(labelCol);\r
+                ((jalview.schemes.GraduatedColor) colour)\r
                         .setAutoScaled(abso == null);\r
                 // add in any additional parameters\r
                 String ttype = null, tval = null;\r
@@ -240,9 +311,10 @@ public class FeaturesFile extends AlignFile
                 }\r
                 if (((GraduatedColor) colour).getThreshType() != AnnotationColourGradient.NO_THRESHOLD)\r
                 {\r
-                  tval = gcol.nextToken();\r
                   try\r
                   {\r
+                    gcol.nextToken();\r
+                    tval = gcol.nextToken();\r
                     ((jalview.schemes.GraduatedColor) colour)\r
                             .setThresh(new Float(tval).floatValue());\r
                   } catch (Exception e)\r
@@ -260,7 +332,7 @@ public class FeaturesFile extends AlignFile
                           .println("Ignoring additional tokens in parameters in graduated colour specification\n");\r
                   while (gcol.hasMoreTokens())\r
                   {\r
-                    System.err.println("|" + gcol);\r
+                    System.err.println("|" + gcol.nextToken());\r
                   }\r
                   System.err.println("\n");\r
                 }\r
@@ -616,8 +688,8 @@ public class FeaturesFile extends AlignFile
         if (visible.get(type) instanceof GraduatedColor)\r
         {\r
           GraduatedColor gc = (GraduatedColor) visible.get(type);\r
-          // TODO: NOW: colour by label, autoscale flags.\r
-          color = Format.getHexString(gc.getMinColor()) + "|"\r
+          color = (gc.isColourByLabel() ? "label|" : "")\r
+                  + Format.getHexString(gc.getMinColor()) + "|"\r
                   + Format.getHexString(gc.getMaxColor())\r
                   + (gc.isAutoScale() ? "|" : "|abso|") + gc.getMin() + "|"\r
                   + gc.getMax() + "|";\r
@@ -754,9 +826,7 @@ public class FeaturesFile extends AlignFile
 \r
                   if (next[j].description.indexOf(href) == -1)\r
                   {\r
-                    out\r
-                            .append("<a href=\"" + href + "\">" + label\r
-                                    + "</a>");\r
+                    out.append("<a href=\"" + href + "\">" + label + "</a>");\r
                   }\r
                 }\r
 \r