JAL-3583 remove debug logging, Javadoc added
[jalview.git] / src / jalview / io / SequenceAnnotationReport.java
index f2f0657..9d7fcfc 100644 (file)
  */
 package jalview.io;
 
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
 import jalview.api.FeatureColourI;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.DBRefSource;
@@ -32,13 +38,6 @@ import jalview.util.StringUtils;
 import jalview.util.UrlLink;
 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
 
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-
 /**
  * generate HTML reports for a sequence
  * 
@@ -46,6 +45,8 @@ import java.util.Map;
  */
 public class SequenceAnnotationReport
 {
+  private static final int MAX_DESCRIPTION_LENGTH = 40;
+
   private static final String COMMA = ",";
 
   private static final String ELLIPSIS = "...";
@@ -54,9 +55,7 @@ public class SequenceAnnotationReport
 
   private static final int MAX_SOURCES = 40;
 
-  private static final String[][] PRIMARY_SOURCES = new String[][] {
-      DBRefSource.CODINGDBS, DBRefSource.DNACODINGDBS,
-      DBRefSource.PROTEINDBS };
+ // public static final String[][] PRIMARY_SOURCES  moved to DBRefSource.java
 
   final String linkImageURL;
 
@@ -64,7 +63,7 @@ public class SequenceAnnotationReport
    * Comparator to order DBRefEntry by Source + accession id (case-insensitive),
    * with 'Primary' sources placed before others, and 'chromosome' first of all
    */
-  private static Comparator<DBRefEntry> comparator = new Comparator<>()
+  private static Comparator<DBRefEntry> comparator = new Comparator<DBRefEntry>()
   {
 
     @Override
@@ -80,8 +79,8 @@ public class SequenceAnnotationReport
       }
       String s1 = ref1.getSource();
       String s2 = ref2.getSource();
-      boolean s1Primary = isPrimarySource(s1);
-      boolean s2Primary = isPrimarySource(s2);
+      boolean s1Primary = DBRefSource.isPrimarySource(s1);
+      boolean s2Primary = DBRefSource.isPrimarySource(s2);
       if (s1Primary && !s2Primary)
       {
         return -1;
@@ -102,20 +101,20 @@ public class SequenceAnnotationReport
       return comp;
     }
 
-    private boolean isPrimarySource(String source)
-    {
-      for (String[] primary : PRIMARY_SOURCES)
-      {
-        for (String s : primary)
-        {
-          if (source.equals(s))
-          {
-            return true;
-          }
-        }
-      }
-      return false;
-    }
+//    private boolean isPrimarySource(String source)
+//    {
+//      for (String[] primary : DBRefSource.PRIMARY_SOURCES)
+//      {
+//        for (String s : primary)
+//        {
+//          if (source.equals(s))
+//          {
+//            return true;
+//          }
+//        }
+//      }
+//      return false;
+//    }
   };
 
   public SequenceAnnotationReport(String linkURL)
@@ -124,37 +123,65 @@ public class SequenceAnnotationReport
   }
 
   /**
-   * Append text for the list of features to the tooltip
+   * Append text for the list of features to the tooltip Returns number of
+   * features left if maxlength limit is (or would have been) reached
    * 
    * @param sb
    * @param residuePos
    * @param features
    * @param minmax
+   * @param maxlength
    */
-  public void appendFeatures(final StringBuilder sb, int residuePos,
-          List<SequenceFeature> features, FeatureRendererModel fr)
+  public int appendFeaturesLengthLimit(final StringBuilder sb,
+          int residuePos, List<SequenceFeature> features,
+          FeatureRendererModel fr, int maxlength)
   {
-    for (SequenceFeature feature : features)
+    for (int i = 0; i < features.size(); i++)
     {
-      appendFeature(sb, residuePos, fr, feature, null);
+      SequenceFeature feature = features.get(i);
+      if (appendFeature(sb, residuePos, fr, feature, null, maxlength))
+      {
+        return features.size() - i;
+      }
     }
+    return 0;
+  }
+
+  public void appendFeatures(final StringBuilder sb, int residuePos,
+          List<SequenceFeature> features, FeatureRendererModel fr)
+  {
+    appendFeaturesLengthLimit(sb, residuePos, features, fr, 0);
   }
 
   /**
    * Appends text for mapped features (e.g. CDS feature for peptide or vice versa)
+   * Returns number of features left if maxlength limit is (or would have been)
+   * reached
    * 
    * @param sb
    * @param residuePos
    * @param mf
    * @param fr
+   * @param maxlength
    */
-  public void appendFeatures(StringBuilder sb, int residuePos,
-          MappedFeatures mf, FeatureRendererModel fr)
+  public int appendFeaturesLengthLimit(StringBuilder sb, int residuePos,
+          MappedFeatures mf, FeatureRendererModel fr, int maxlength)
   {
-    for (SequenceFeature feature : mf.features)
+    for (int i = 0; i < mf.features.size(); i++)
     {
-      appendFeature(sb, residuePos, fr, feature, mf);
+      SequenceFeature feature = mf.features.get(i);
+      if (appendFeature(sb, residuePos, fr, feature, mf, maxlength))
+      {
+        return mf.features.size() - i;
+      }
     }
+    return 0;
+  }
+
+  public void appendFeatures(StringBuilder sb, int residuePos,
+          MappedFeatures mf, FeatureRendererModel fr)
+  {
+    appendFeaturesLengthLimit(sb, residuePos, mf, fr, 0);
   }
 
   /**
@@ -165,27 +192,28 @@ public class SequenceAnnotationReport
    * @param minmax
    * @param feature
    */
-  void appendFeature(final StringBuilder sb, int rpos,
+  boolean appendFeature(final StringBuilder sb0, int rpos,
           FeatureRendererModel fr, SequenceFeature feature,
-          MappedFeatures mf)
+          MappedFeatures mf, int maxlength)
   {
+    StringBuilder sb = new StringBuilder();
     if (feature.isContactFeature())
     {
       if (feature.getBegin() == rpos || feature.getEnd() == rpos)
       {
-        if (sb.length() > 6)
+        if (sb0.length() > 6)
         {
-          sb.append("<br>");
+          sb.append("<br/>");
         }
         sb.append(feature.getType()).append(" ").append(feature.getBegin())
                 .append(":").append(feature.getEnd());
       }
-      return;
+      return appendTextMaxLengthReached(sb0, sb, maxlength);
     }
 
-    if (sb.length() > 6)
+    if (sb0.length() > 6)
     {
-      sb.append("<br>");
+      sb.append("<br/>");
     }
     // TODO: remove this hack to display link only features
     boolean linkOnly = feature.getValue("linkonly") != null;
@@ -206,6 +234,20 @@ public class SequenceAnnotationReport
       if (description != null && !description.equals(feature.getType()))
       {
         description = StringUtils.stripHtmlTags(description);
+
+        /*
+         * truncate overlong descriptions unless they contain an href
+         * before the truncation point (as truncation could leave corrupted html)
+         */
+        int linkindex = description.toLowerCase().indexOf("<a ");
+        boolean hasLink = linkindex > -1
+                && linkindex < MAX_DESCRIPTION_LENGTH;
+        if (description.length() > MAX_DESCRIPTION_LENGTH && !hasLink)
+        {
+          description = description.substring(0, MAX_DESCRIPTION_LENGTH)
+                  + ELLIPSIS;
+        }
+
         sb.append("; ").append(description);
       }
 
@@ -246,6 +288,36 @@ public class SequenceAnnotationReport
         }
       }
     }
+    return appendTextMaxLengthReached(sb0, sb, maxlength);
+  }
+
+  void appendFeature(final StringBuilder sb, int rpos,
+          FeatureRendererModel fr, SequenceFeature feature,
+          MappedFeatures mf)
+  {
+    appendFeature(sb, rpos, fr, feature, mf, 0);
+  }
+
+  /**
+   * Appends {@code sb} to {@code sb0}, and returns false, unless
+   * {@code maxlength} is not zero and appending would make the total length
+   * greater than {@code maxlength}, in which case the text is not appended, and
+   * the method returns true.
+   * 
+   * @param sb0
+   * @param sb
+   * @param maxlength
+   * @return
+   */
+  private static boolean appendTextMaxLengthReached(StringBuilder sb0,
+          StringBuilder sb, int maxlength)
+  {
+    if (maxlength == 0 || sb0.length() + sb.length() < maxlength)
+    {
+      sb0.append(sb);
+      return false;
+    }
+    return true;
   }
 
   /**
@@ -305,7 +377,8 @@ public class SequenceAnnotationReport
                       + (urllink.get(0).toLowerCase()
                               .equals(urllink.get(1).toLowerCase()) ? urllink
                               .get(0) : (urllink.get(0) + ":" + urllink
-                              .get(1))) + "</a></br>");
+                                              .get(1)))
+                      + "</a><br/>");
             }
           } catch (Exception x)
           {
@@ -376,7 +449,7 @@ public class SequenceAnnotationReport
     if (sequence.getDescription() != null)
     {
       tmp = sequence.getDescription();
-      sb.append("<br>").append(tmp);
+      sb.append(tmp);
       maxWidth = Math.max(maxWidth, tmp.length());
     }
     SequenceI ds = sequence;
@@ -420,14 +493,14 @@ public class SequenceAnnotationReport
   protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
           boolean summary)
   {
-    DBRefEntry[] dbrefs = ds.getDBRefs();
+    List<DBRefEntry> dbrefs = ds.getDBRefs();
     if (dbrefs == null)
     {
       return 0;
     }
 
     // note this sorts the refs held on the sequence!
-    Arrays.sort(dbrefs, comparator);
+    dbrefs.sort(comparator);
     boolean ellipsis = false;
     String source = null;
     String lastSource = null;
@@ -462,7 +535,7 @@ public class SequenceAnnotationReport
       countForSource++;
       if (countForSource == 1 || !summary)
       {
-        sb.append("<br>");
+        sb.append("<br/>");
       }
       if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
       {
@@ -488,11 +561,11 @@ public class SequenceAnnotationReport
     }
     if (moreSources)
     {
-      sb.append("<br>").append(source).append(COMMA).append(ELLIPSIS);
+      sb.append("<br/>").append(source).append(COMMA).append(ELLIPSIS);
     }
     if (ellipsis)
     {
-      sb.append("<br>(");
+      sb.append("<br/>(");
       sb.append(MessageManager.getString("label.output_seq_details"));
       sb.append(")");
     }