JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / io / SequenceAnnotationReport.java
index 497e4c9..07b88bf 100644 (file)
@@ -1,31 +1,36 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
- * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
  *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
  * 
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.io;
 
-import java.util.ArrayList;
-import java.util.Hashtable;
-import java.util.Vector;
-
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
+import jalview.io.gff.GffConstants;
+import jalview.util.DBRefUtils;
 import jalview.util.UrlLink;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
 /**
  * generate HTML reports for a sequence
  * 
@@ -41,167 +46,187 @@ public class SequenceAnnotationReport
   }
 
   /**
-   * appends the features at rpos to the given stringbuffer ready for display in
-   * a tooltip
+   * Append text for the list of features to the tooltip
    * 
    * @param tooltipText2
-   * @param linkImageURL
    * @param rpos
    * @param features
-   *          TODO refactor to Jalview 'utilities' somehow.
+   * @param minmax
    */
   public void appendFeatures(final StringBuffer tooltipText2, int rpos,
-          SequenceFeature[] features)
+          List<SequenceFeature> features, Map<String, float[][]> minmax)
   {
-    appendFeatures(tooltipText2, rpos, features, null);
+    if (features != null)
+    {
+      for (SequenceFeature feature : features)
+      {
+        appendFeature(tooltipText2, rpos, minmax, feature);
+      }
+    }
   }
 
-  public void appendFeatures(final StringBuffer tooltipText2, int rpos,
-          SequenceFeature[] features, Hashtable minmax)
+  /**
+   * Appends text for one sequence feature to the string buffer
+   * 
+   * @param sb
+   * @param rpos
+   * @param minmax
+   *          {{min, max}, {min, max}} positional and non-positional feature
+   *          scores for this type
+   * @param feature
+   */
+  void appendFeature(final StringBuffer sb, int rpos,
+          Map<String, float[][]> minmax, SequenceFeature feature)
   {
-    String tmpString;
-    if (features != null)
+    if ("disulfide bond".equals(feature.getType()))
     {
-      for (int i = 0; i < features.length; i++)
+      if (feature.getBegin() == rpos || feature.getEnd() == rpos)
       {
-        if (features[i].getType().equals("disulfide bond"))
+        if (sb.length() > 6)
         {
-          if (features[i].getBegin() == rpos
-                  || features[i].getEnd() == rpos)
-          {
-            if (tooltipText2.length() > 6)
-            {
-              tooltipText2.append("<br>");
-            }
-            tooltipText2.append("disulfide bond " + features[i].getBegin()
-                    + ":" + features[i].getEnd());
-          }
+          sb.append("<br>");
+        }
+        sb.append("disulfide bond ").append(feature.getBegin()).append(":")
+                .append(feature.getEnd());
+      }
+    }
+    else
+    {
+      if (sb.length() > 6)
+      {
+        sb.append("<br>");
+      }
+      // TODO: remove this hack to display link only features
+      boolean linkOnly = feature.getValue("linkonly") != null;
+      if (!linkOnly)
+      {
+        sb.append(feature.getType()).append(" ");
+        if (rpos != 0)
+        {
+          // we are marking a positional feature
+          sb.append(feature.begin);
+        }
+        if (feature.begin != feature.end)
+        {
+          sb.append(" " + feature.end);
         }
-        else
+
+        if (feature.getDescription() != null
+                && !feature.description.equals(feature.getType()))
         {
-          if (tooltipText2.length() > 6)
+          String tmpString = feature.getDescription();
+          String tmp2up = tmpString.toUpperCase();
+          final int startTag = tmp2up.indexOf("<HTML>");
+          if (startTag > -1)
           {
-            tooltipText2.append("<br>");
+            tmpString = tmpString.substring(startTag + 6);
+            tmp2up = tmp2up.substring(startTag + 6);
           }
-          // TODO: remove this hack to display link only features
-          boolean linkOnly = features[i].getValue("linkonly") != null;
-          if (!linkOnly)
+          // TODO strips off </body> but not <body> - is that intended?
+          int endTag = tmp2up.indexOf("</BODY>");
+          if (endTag > -1)
           {
-            tooltipText2.append(features[i].getType() + " ");
-            if (rpos != 0)
-            {
-              // we are marking a positional feature
-              tooltipText2.append(features[i].begin);
-            }
-            if (features[i].begin != features[i].end)
-            {
-              tooltipText2.append(" " + features[i].end);
-            }
-
-            if (features[i].getDescription() != null
-                    && !features[i].description.equals(features[i]
-                            .getType()))
-            {
-              tmpString = features[i].getDescription();
-              String tmp2up = tmpString.toUpperCase();
-              int startTag = tmp2up.indexOf("<HTML>");
-              if (startTag > -1)
-              {
-                tmpString = tmpString.substring(startTag + 6);
-                tmp2up = tmp2up.substring(startTag + 6);
-              }
-              int endTag = tmp2up.indexOf("</BODY>");
-              if (endTag > -1)
-              {
-                tmpString = tmpString.substring(0, endTag);
-                tmp2up = tmp2up.substring(0, endTag);
-              }
-              endTag = tmp2up.indexOf("</HTML>");
-              if (endTag > -1)
-              {
-                tmpString = tmpString.substring(0, endTag);
-              }
-
-              if (startTag > -1)
-              {
-                tooltipText2.append("; " + tmpString);
-              }
-              else
-              {
-                if (tmpString.indexOf("<") > -1
-                        || tmpString.indexOf(">") > -1)
-                {
-                  // The description does not specify html is to
-                  // be used, so we must remove < > symbols
-                  tmpString = tmpString.replaceAll("<", "&lt;");
-                  tmpString = tmpString.replaceAll(">", "&gt;");
-
-                  tooltipText2.append("; ");
-                  tooltipText2.append(tmpString);
+            tmpString = tmpString.substring(0, endTag);
+            tmp2up = tmp2up.substring(0, endTag);
+          }
+          endTag = tmp2up.indexOf("</HTML>");
+          if (endTag > -1)
+          {
+            tmpString = tmpString.substring(0, endTag);
+          }
 
-                }
-                else
-                {
-                  tooltipText2.append("; " + tmpString);
-                }
-              }
-            }
-            // check score should be shown
-            if (features[i].getScore() != Float.NaN)
+          if (startTag > -1)
+          {
+            sb.append("; ").append(tmpString);
+          }
+          else
+          {
+            if (tmpString.indexOf("<") > -1 || tmpString.indexOf(">") > -1)
             {
-              float[][] rng = (minmax == null) ? null : ((float[][]) minmax
-                      .get(features[i].getType()));
-              if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
-              {
-                tooltipText2.append(" Score=" + features[i].getScore());
-              }
+              // The description does not specify html is to
+              // be used, so we must remove < > symbols
+              tmpString = tmpString.replaceAll("<", "&lt;");
+              tmpString = tmpString.replaceAll(">", "&gt;");
+              sb.append("; ").append(tmpString);
             }
-            if (features[i].getValue("status") != null)
+            else
             {
-              String status = features[i].getValue("status").toString();
-              if (status.length() > 0)
-              {
-                tooltipText2.append("; (" + features[i].getValue("status")
-                        + ")");
-              }
+              sb.append("; ").append(tmpString);
             }
           }
         }
-        if (features[i].links != null)
+
+        /*
+         * score should be shown if there is one, and min != max
+         * for this feature type (e.g. not all 0)
+         */
+        if (!Float.isNaN(feature.getScore()))
         {
-          if (linkImageURL != null)
+          float[][] rng = (minmax == null) ? null : minmax.get(feature
+                  .getType());
+          if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
           {
-            tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
+            sb.append(" Score=").append(String.valueOf(feature.getScore()));
           }
-          else
+        }
+        String status = (String) feature.getValue("status");
+        if (status != null && status.length() > 0)
+        {
+          sb.append("; (").append(status).append(")");
+        }
+        String clinSig = (String) feature
+                .getValue(GffConstants.CLINICAL_SIGNIFICANCE);
+        if (clinSig != null)
+        {
+          sb.append("; ").append(clinSig);
+        }
+      }
+    }
+    appendLinks(sb, feature);
+  }
+
+  /**
+   * Format and appends any hyperlinks for the sequence feature to the string
+   * buffer
+   * 
+   * @param sb
+   * @param feature
+   */
+  void appendLinks(final StringBuffer sb, SequenceFeature feature)
+  {
+    if (feature.links != null)
+    {
+      if (linkImageURL != null)
+      {
+        sb.append(" <img src=\"" + linkImageURL + "\">");
+      }
+      else
+      {
+        for (String urlstring : feature.links)
+        {
+          try
           {
-            for (String urlstring : (Vector<String>) features[i].links)
+            for (String[] urllink : createLinksFrom(null, urlstring))
             {
-              try
-              {
-                for (String[] urllink : createLinksFrom(null, urlstring))
-                {
-                  tooltipText2.append("<br/> <a href=\""
-                          + urllink[3]
-                          + "\" target=\""
-                          + urllink[0]
-                          + "\">"
-                          + (urllink[0].toLowerCase().equals(
-                                  urllink[1].toLowerCase()) ? urllink[0]
-                                  : (urllink[0] + ":" + urllink[1]))
-                          + "</a></br>");
-                }
-              } catch (Exception x)
-              {
-                System.err.println("problem when creating links from "
-                        + urlstring);
-                x.printStackTrace();
-              }
+              sb.append("<br/> <a href=\""
+                      + urllink[3]
+                      + "\" target=\""
+                      + urllink[0]
+                      + "\">"
+                      + (urllink[0].toLowerCase().equals(
+                              urllink[1].toLowerCase()) ? urllink[0]
+                              : (urllink[0] + ":" + urllink[1]))
+                      + "</a></br>");
             }
+          } catch (Exception x)
+          {
+            System.err.println("problem when creating links from "
+                    + urlstring);
+            x.printStackTrace();
           }
-
         }
       }
+
     }
   }
 
@@ -212,64 +237,72 @@ public class SequenceAnnotationReport
    * @return String[][] { String[] { link target, link label, dynamic component
    *         inserted (if any), url }}
    */
-  public String[][] createLinksFrom(SequenceI seq, String link)
+  String[][] createLinksFrom(SequenceI seq, String link)
   {
-    ArrayList<String[]> urlSets = new ArrayList<String[]>();
-    ArrayList<String> uniques = new ArrayList<String>();
+    List<String[]> urlSets = new ArrayList<String[]>();
+    List<String> uniques = new ArrayList<String>();
     UrlLink urlLink = new UrlLink(link);
     if (!urlLink.isValid())
     {
       System.err.println(urlLink.getInvalidMessage());
       return null;
     }
-    final String target = urlLink.getTarget(); // link.substring(0,
-    // link.indexOf("|"));
-    final String label = urlLink.getLabel();
     if (seq != null && urlLink.isDynamic())
     {
-
-      // collect matching db-refs
-      DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(),
-              new String[]
-              { target });
-      // collect id string too
-      String id = seq.getName();
-      String descr = seq.getDescription();
-      if (descr != null && descr.length() < 1)
+      urlSets.addAll(createDynamicLinks(seq, urlLink, uniques));
+    }
+    else
+    {
+      String target = urlLink.getTarget();
+      String label = urlLink.getLabel();
+      String unq = label + "|" + urlLink.getUrl_prefix();
+      if (!uniques.contains(unq))
       {
-        descr = null;
+        uniques.add(unq);
+        urlSets.add(new String[] { target, label, null,
+            urlLink.getUrl_prefix() });
       }
-      if (dbr != null)
+    }
+
+    return urlSets.toArray(new String[][] {});
+  }
+
+  /**
+   * Formats and returns a list of dynamic href links
+   * 
+   * @param seq
+   * @param urlLink
+   * @param uniques
+   */
+  List<String[]> createDynamicLinks(SequenceI seq, UrlLink urlLink,
+          List<String> uniques)
+  {
+    List<String[]> result = new ArrayList<String[]>();
+    final String target = urlLink.getTarget();
+    final String label = urlLink.getLabel();
+
+    // collect matching db-refs
+    DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(),
+            new String[] { target });
+    // collect id string too
+    String id = seq.getName();
+    String descr = seq.getDescription();
+    if (descr != null && descr.length() < 1)
+    {
+      descr = null;
+    }
+    if (dbr != null)
+    {
+      for (int r = 0; r < dbr.length; r++)
       {
-        for (int r = 0; r < dbr.length; r++)
+        if (id != null && dbr[r].getAccessionId().equals(id))
         {
-          if (id != null && dbr[r].getAccessionId().equals(id))
-          {
-            // suppress duplicate link creation for the bare sequence ID
-            // string with this link
-            id = null;
-          }
-          // create Bare ID link for this RUL
-          String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
-          if (urls != null)
-          {
-            for (int u = 0; u < urls.length; u += 2)
-            {
-              String unq = urls[u] + "|" + urls[u + 1];
-              if (!uniques.contains(unq))
-              {
-                urlSets.add(new String[]
-                { target, label, urls[u], urls[u + 1] });
-                uniques.add(unq);
-              }
-            }
-          }
+          // suppress duplicate link creation for the bare sequence ID
+          // string with this link
+          id = null;
         }
-      }
-      if (id != null)
-      {
-        // create Bare ID link for this RUL
-        String[] urls = urlLink.makeUrls(id, true);
+        // create Bare ID link for this URL
+        String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
         if (urls != null)
         {
           for (int u = 0; u < urls.length; u += 2)
@@ -277,52 +310,53 @@ public class SequenceAnnotationReport
             String unq = urls[u] + "|" + urls[u + 1];
             if (!uniques.contains(unq))
             {
-              urlSets.add(new String[]
-              { target, label, urls[u], urls[u + 1] });
+              result.add(new String[] { target, label, urls[u], urls[u + 1] });
               uniques.add(unq);
             }
           }
         }
       }
-      if (descr != null && urlLink.getRegexReplace() != null)
+    }
+    if (id != null)
+    {
+      // create Bare ID link for this URL
+      String[] urls = urlLink.makeUrls(id, true);
+      if (urls != null)
       {
-        // create link for this URL from description only if regex matches
-        String[] urls = urlLink.makeUrls(descr, true);
-        if (urls != null)
+        for (int u = 0; u < urls.length; u += 2)
         {
-          for (int u = 0; u < urls.length; u += 2)
+          String unq = urls[u] + "|" + urls[u + 1];
+          if (!uniques.contains(unq))
           {
-            String unq = urls[u] + "|" + urls[u + 1];
-            if (!uniques.contains(unq))
-            {
-              urlSets.add(new String[]
-              { target, label, urls[u], urls[u + 1] });
-              uniques.add(unq);
-            }
+            result.add(new String[] { target, label, urls[u], urls[u + 1] });
+            uniques.add(unq);
           }
         }
       }
-
     }
-    else
+    if (descr != null && urlLink.getRegexReplace() != null)
     {
-      String unq = label + "|" + urlLink.getUrl_prefix();
-      if (!uniques.contains(unq))
+      // create link for this URL from description only if regex matches
+      String[] urls = urlLink.makeUrls(descr, true);
+      if (urls != null)
       {
-        uniques.add(unq);
-        // Add a non-dynamic link
-        urlSets.add(new String[]
-        { target, label, null, urlLink.getUrl_prefix() });
+        for (int u = 0; u < urls.length; u += 2)
+        {
+          String unq = urls[u] + "|" + urls[u + 1];
+          if (!uniques.contains(unq))
+          {
+            result.add(new String[] { target, label, urls[u], urls[u + 1] });
+            uniques.add(unq);
+          }
+        }
       }
     }
-
-    return urlSets.toArray(new String[][]
-    {});
+    return result;
   }
 
   public void createSequenceAnnotationReport(final StringBuffer tip,
           SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
-          Hashtable minmax)
+          Map<String, float[][]> minmax)
   {
     createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
             true, minmax);
@@ -330,7 +364,7 @@ public class SequenceAnnotationReport
 
   public void createSequenceAnnotationReport(final StringBuffer tip,
           SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
-          boolean tableWrap, Hashtable minmax)
+          boolean tableWrap, Map<String, float[][]> minmax)
   {
     String tmp;
     tip.append("<i>");
@@ -347,7 +381,7 @@ public class SequenceAnnotationReport
     {
       ds = ds.getDatasetSequence();
     }
-    DBRefEntry[] dbrefs = ds.getDBRef();
+    DBRefEntry[] dbrefs = ds.getDBRefs();
     if (showDbRefs && dbrefs != null)
     {
       for (int i = 0; i < dbrefs.length; i++)
@@ -360,8 +394,7 @@ public class SequenceAnnotationReport
     }
 
     // ADD NON POSITIONAL SEQUENCE INFO
-    SequenceFeature[] features = ds.getSequenceFeatures();
-    SequenceFeature[] tfeat = new SequenceFeature[1];
+    SequenceFeature[] features = sequence.getSequenceFeatures();
     if (showNpFeats && features != null)
     {
       for (int i = 0; i < features.length; i++)
@@ -369,7 +402,8 @@ public class SequenceAnnotationReport
         if (features[i].begin == 0 && features[i].end == 0)
         {
           int sz = -tip.length();
-          tfeat[0] = features[i];
+          List<SequenceFeature> tfeat = new ArrayList<SequenceFeature>();
+          tfeat.add(features[i]);
           appendFeatures(tip, 0, tfeat, minmax);
           sz += tip.length();
           maxWidth = Math.max(maxWidth, sz);