JAL-2792 capture feature metadata and render in Feature Details
[jalview.git] / src / jalview / datamodel / SequenceFeature.java
index 420ade1..a2d91b1 100755 (executable)
  */
 package jalview.datamodel;
 
+import jalview.datamodel.features.FeatureAttributeType;
 import jalview.datamodel.features.FeatureLocationI;
+import jalview.datamodel.features.FeatureSourceI;
+import jalview.datamodel.features.FeatureSources;
 import jalview.util.StringUtils;
 
 import java.util.HashMap;
@@ -52,7 +55,7 @@ public class SequenceFeature implements FeatureLocationI
   // private key for ENA location designed not to conflict with real GFF data
   private static final String LOCATION = "!Location";
 
-  private static final String ROW_DATA = "<tr><td>%s</td><td>%s</td></tr>";
+  private static final String ROW_DATA = "<tr><td width=\"10%%\">%s</td><td width=\"50%%\">%s</td><td width=\"40%%\">%s</td></tr>";
 
   /*
    * map of otherDetails special keys, and their value fields' delimiter
@@ -99,6 +102,12 @@ public class SequenceFeature implements FeatureLocationI
 
   public Vector<String> links;
 
+  /*
+   * the identifier (if known) for the FeatureSource held in FeatureSources,
+   * as a provider of metadata about feature attributes 
+   */
+  private String source;
+
   /**
    * Constructs a duplicate feature. Note: Uses makes a shallow copy of the
    * otherDetails map, so the new and original SequenceFeature may reference the
@@ -558,21 +567,24 @@ public class SequenceFeature implements FeatureLocationI
    */
   public String getDetailsReport()
   {
+    FeatureSourceI metadata = FeatureSources.getInstance()
+            .getSource(source);
+
     StringBuilder sb = new StringBuilder(128);
     sb.append("<br>");
     sb.append("<table>");
-    sb.append(String.format(ROW_DATA, "Type", type));
+    sb.append(String.format(ROW_DATA, "Type", type, ""));
     sb.append(String.format(ROW_DATA, "Start/end", begin == end ? begin
-            : begin + (isContactFeature() ? ":" : "-") + end));
+            : begin + (isContactFeature() ? ":" : "-") + end, ""));
     String desc = StringUtils.stripHtmlTags(description);
-    sb.append(String.format(ROW_DATA, "Description", desc));
+    sb.append(String.format(ROW_DATA, "Description", desc, ""));
     if (!Float.isNaN(score) && score != 0f)
     {
-      sb.append(String.format(ROW_DATA, "Score", score));
+      sb.append(String.format(ROW_DATA, "Score", score, ""));
     }
     if (featureGroup != null)
     {
-      sb.append(String.format(ROW_DATA, "Group", featureGroup));
+      sb.append(String.format(ROW_DATA, "Group", featureGroup, ""));
     }
 
     if (otherDetails != null)
@@ -597,15 +609,22 @@ public class SequenceFeature implements FeatureLocationI
           String[] values = entry.getValue().toString().split(delimiter);
           for (String value : values)
           {
-            sb.append("<tr><td>").append(key).append("</td><td>")
-                    .append(value)
-                    .append("</td></tr>");
+            sb.append(String.format(ROW_DATA, key, "", value));
           }
         }
         else
         { // tried <td title="key"> but it failed to provide a tooltip :-(
-          sb.append("<tr><td>").append(key).append("</td><td>");
-          sb.append(entry.getValue().toString()).append("</td></tr>");
+          String attDesc = null;
+          if (metadata != null)
+          {
+            attDesc = metadata.getAttributeName(key);
+          }
+          String value = entry.getValue().toString();
+          if (isValueInteresting(key, value, metadata))
+          {
+            sb.append(String.format(ROW_DATA, key, attDesc == null ? ""
+                    : attDesc, value));
+          }
         }
       }
     }
@@ -614,4 +633,60 @@ public class SequenceFeature implements FeatureLocationI
     String text = sb.toString();
     return text;
   }
+
+  /**
+   * Answers true if we judge the value is worth displaying, by some heuristic
+   * rules, else false
+   * 
+   * @param key
+   * @param value
+   * @param metadata
+   * @return
+   */
+  boolean isValueInteresting(String key, String value,
+          FeatureSourceI metadata)
+  {
+    /*
+     * currently suppressing zero values as well as null or empty
+     */
+    if (value == null || "".equals(value) || ".".equals(value)
+            || "0".equals(value))
+    {
+      return false;
+    }
+
+    if (metadata == null)
+    {
+      return true;
+    }
+
+    FeatureAttributeType attributeType = metadata.getAttributeType(key);
+    if (attributeType == FeatureAttributeType.Float
+            || attributeType.equals(FeatureAttributeType.Integer))
+    {
+      try
+      {
+        float fval = Float.valueOf(value);
+        if (fval == 0f)
+        {
+          return false;
+        }
+      } catch (NumberFormatException e)
+      {
+        // ignore
+      }
+    }
+
+    return true; // default to interesting
+  }
+
+  /**
+   * Sets the feature source identifier
+   * 
+   * @param theSource
+   */
+  public void setSource(String theSource)
+  {
+    source = theSource;
+  }
 }