JAL-2015 JAL-1956 rollout of FeatureColourI in place of
[jalview.git] / src / jalview / io / FeaturesFile.java
index aa5583c..6bc0374 100755 (executable)
 package jalview.io;
 
 import jalview.analysis.SequenceIdMatcher;
+import jalview.api.FeatureColourI;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceDummy;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.schemes.AnnotationColourGradient;
-import jalview.schemes.GraduatedColor;
+import jalview.schemes.FeatureColour;
 import jalview.schemes.UserColourScheme;
 import jalview.util.Format;
 import jalview.util.MapList;
@@ -277,7 +278,7 @@ public class FeaturesFile extends AlignFile
           }
           else
           {
-            Object colour = null;
+            FeatureColourI colour = null;
             String colscheme = st.nextToken();
             if (colscheme.indexOf("|") > -1
                     || colscheme.trim().equalsIgnoreCase("label"))
@@ -393,7 +394,7 @@ public class FeaturesFile extends AlignFile
               }
               try
               {
-                colour = new jalview.schemes.GraduatedColor(
+                colour = new FeatureColour(
                         new UserColourScheme(mincol).findColour('A'),
                         new UserColourScheme(maxcol).findColour('A'), min,
                         max);
@@ -406,10 +407,8 @@ public class FeaturesFile extends AlignFile
               }
               if (colour != null)
               {
-                ((jalview.schemes.GraduatedColor) colour)
-                        .setColourByLabel(labelCol);
-                ((jalview.schemes.GraduatedColor) colour)
-                        .setAutoScaled(abso == null);
+                colour.setColourByLabel(labelCol);
+                colour.setAutoScaled(abso == null);
                 // add in any additional parameters
                 String ttype = null, tval = null;
                 if (gcol.hasMoreTokens())
@@ -418,18 +417,14 @@ public class FeaturesFile extends AlignFile
                   ttype = gcol.nextToken();
                   if (ttype.toLowerCase().startsWith("below"))
                   {
-                    ((jalview.schemes.GraduatedColor) colour)
-                            .setThreshType(AnnotationColourGradient.BELOW_THRESHOLD);
+                    colour.setBelowThreshold(true);
                   }
                   else if (ttype.toLowerCase().startsWith("above"))
                   {
-                    ((jalview.schemes.GraduatedColor) colour)
-                            .setThreshType(AnnotationColourGradient.ABOVE_THRESHOLD);
+                    colour.setAboveThreshold(true);
                   }
                   else
                   {
-                    ((jalview.schemes.GraduatedColor) colour)
-                            .setThreshType(AnnotationColourGradient.NO_THRESHOLD);
                     if (!ttype.toLowerCase().startsWith("no"))
                     {
                       System.err
@@ -438,14 +433,13 @@ public class FeaturesFile extends AlignFile
                     }
                   }
                 }
-                if (((GraduatedColor) colour).getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
+                if (colour.hasThreshold())
                 {
                   try
                   {
                     gcol.nextToken();
                     tval = gcol.nextToken();
-                    ((jalview.schemes.GraduatedColor) colour)
-                            .setThresh(new Float(tval).floatValue());
+                    colour.setThreshold(new Float(tval).floatValue());
                   } catch (Exception e)
                   {
                     System.err
@@ -470,7 +464,7 @@ public class FeaturesFile extends AlignFile
             else
             {
               UserColourScheme ucs = new UserColourScheme(colscheme);
-              colour = ucs.findColour('A');
+              colour = new FeatureColour(ucs.findColour('A'));
             }
             if (colour != null)
             {
@@ -1176,20 +1170,20 @@ public class FeaturesFile extends AlignFile
    * 
    * @param seqs
    *          source of sequence features
-   * @param visible
+   * @param map
    *          hash of feature types and colours
    * @return features file contents
    */
   public String printJalviewFormat(SequenceI[] seqs,
-          Map<String, Object> visible)
+          Map<String, FeatureColourI> map)
   {
-    return printJalviewFormat(seqs, visible, true, true);
+    return printJalviewFormat(seqs, map, true, true);
   }
 
   /**
    * generate a features file for seqs with colours from visible (if any)
    * 
-   * @param seqs
+   * @param sequences
    *          source of features
    * @param visible
    *          hash of Colours for each feature type
@@ -1200,7 +1194,8 @@ public class FeaturesFile extends AlignFile
    *          of group or type)
    * @return features file contents
    */
-  public String printJalviewFormat(SequenceI[] seqs, Map visible,
+  public String printJalviewFormat(SequenceI[] sequences,
+          Map<String, FeatureColourI> visible,
           boolean visOnly, boolean nonpos)
   {
     StringBuffer out = new StringBuffer();
@@ -1217,54 +1212,46 @@ public class FeaturesFile extends AlignFile
       // write feature colours only if we're given them and we are generating
       // viewed features
       // TODO: decide if feature links should also be written here ?
-      Iterator en = visible.keySet().iterator();
-      String type, color;
+      Iterator<String> en = visible.keySet().iterator();
+      String feature, color;
       while (en.hasNext())
       {
-        type = en.next().toString();
+        feature = en.next();
 
-        if (visible.get(type) instanceof GraduatedColor)
+        FeatureColourI gc = visible.get(feature);
+        if (!gc.isSimpleColour())
         {
-          GraduatedColor gc = (GraduatedColor) visible.get(type);
           color = (gc.isColourByLabel() ? "label|" : "")
-                  + Format.getHexString(gc.getMinColor()) + "|"
-                  + Format.getHexString(gc.getMaxColor())
-                  + (gc.isAutoScale() ? "|" : "|abso|") + gc.getMin() + "|"
+                  + Format.getHexString(gc.getMinColour()) + "|"
+                  + Format.getHexString(gc.getMaxColour())
+                  + (gc.isAutoScaled() ? "|" : "|abso|") + gc.getMin()
+                  + "|"
                   + gc.getMax() + "|";
-          if (gc.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
+          if (gc.isBelowThreshold())
           {
-            if (gc.getThreshType() == AnnotationColourGradient.BELOW_THRESHOLD)
-            {
-              color += "below";
-            }
-            else
-            {
-              if (gc.getThreshType() != AnnotationColourGradient.ABOVE_THRESHOLD)
-              {
-                System.err.println("WARNING: Unsupported threshold type ("
-                        + gc.getThreshType() + ") : Assuming 'above'");
-              }
-              color += "above";
-            }
-            // add the value
-            color += "|" + gc.getThresh();
+            color += "below|" + gc.getThreshold();
+          }
+          else if (gc.isAboveThreshold())
+          {
+            color += "above|" + gc.getThreshold();
           }
           else
           {
             color += "none";
           }
         }
-        else if (visible.get(type) instanceof java.awt.Color)
-        {
-          color = Format.getHexString((java.awt.Color) visible.get(type));
-        }
         else
         {
-          // legacy support for integer objects containing colour triplet values
-          color = Format.getHexString(new java.awt.Color(Integer
-                  .parseInt(visible.get(type).toString())));
+          color = Format.getHexString(gc.getColour());
         }
-        out.append(type);
+        // else
+        // {
+        // // legacy support for integer objects containing colour triplet
+        // values
+        // color = Format.getHexString(new java.awt.Color(Integer
+        // .parseInt(visible.get(type).toString())));
+        // }
+        out.append(feature);
         out.append("\t");
         out.append(color);
         out.append(newline);
@@ -1275,9 +1262,9 @@ public class FeaturesFile extends AlignFile
     int groupIndex = 0;
     boolean isnonpos = false;
 
-    for (int i = 0; i < seqs.length; i++)
+    for (int i = 0; i < sequences.length; i++)
     {
-      next = seqs[i].getSequenceFeatures();
+      next = sequences[i].getSequenceFeatures();
       if (next != null)
       {
         for (int j = 0; j < next.length; j++)
@@ -1316,9 +1303,9 @@ public class FeaturesFile extends AlignFile
         group = null;
       }
 
-      for (int i = 0; i < seqs.length; i++)
+      for (int i = 0; i < sequences.length; i++)
       {
-        next = seqs[i].getSequenceFeatures();
+        next = sequences[i].getSequenceFeatures();
         if (next != null)
         {
           for (int j = 0; j < next.length; j++)
@@ -1382,7 +1369,7 @@ public class FeaturesFile extends AlignFile
 
               out.append("\t");
             }
-            out.append(seqs[i].getName());
+            out.append(sequences[i].getName());
             out.append("\t-1\t");
             out.append(next[j].begin);
             out.append("\t");
@@ -1426,16 +1413,17 @@ public class FeaturesFile extends AlignFile
    * default.
    * 
    * @param seqs
-   * @param visible
+   * @param map
    * @return
    */
-  public String printGFFFormat(SequenceI[] seqs, Map<String, Object> visible)
+  public String printGFFFormat(SequenceI[] seqs,
+          Map<String, FeatureColourI> map)
   {
-    return printGFFFormat(seqs, visible, true, true);
+    return printGFFFormat(seqs, map, true, true);
   }
 
   public String printGFFFormat(SequenceI[] seqs,
-          Map<String, Object> visible, boolean visOnly, boolean nonpos)
+          Map<String, FeatureColourI> map, boolean visOnly, boolean nonpos)
   {
     StringBuffer out = new StringBuffer();
     SequenceFeature[] next;
@@ -1450,7 +1438,7 @@ public class FeaturesFile extends AlignFile
         {
           isnonpos = next[j].begin == 0 && next[j].end == 0;
           if ((!nonpos && isnonpos)
-                  || (!isnonpos && visOnly && !visible
+                  || (!isnonpos && visOnly && !map
                           .containsKey(next[j].type)))
           {
             continue;
@@ -1513,6 +1501,7 @@ public class FeaturesFile extends AlignFile
   /**
    * this is only for the benefit of object polymorphism - method does nothing.
    */
+  @Override
   public void parse()
   {
     // IGNORED
@@ -1523,6 +1512,7 @@ public class FeaturesFile extends AlignFile
    * 
    * @return error message
    */
+  @Override
   public String print()
   {
     return "USE printGFFFormat() or printJalviewFormat()";