JAL-3725 restrict mapped virtual feature location to mapped region
[jalview.git] / src / jalview / io / SequenceAnnotationReport.java
index 9d7fcfc..9ffdf21 100644 (file)
@@ -20,6 +20,8 @@
  */
 package jalview.io;
 
+import java.util.Locale;
+
 import java.util.Collection;
 import java.util.Comparator;
 import java.util.LinkedHashMap;
@@ -55,9 +57,9 @@ public class SequenceAnnotationReport
 
   private static final int MAX_SOURCES = 40;
 
- // public static final String[][] PRIMARY_SOURCES  moved to DBRefSource.java
+  private static String linkImageURL;
 
-  final String linkImageURL;
+ // public static final String[][] PRIMARY_SOURCES  moved to DBRefSource.java
 
   /*
    * Comparator to order DBRefEntry by Source + accession id (case-insensitive),
@@ -117,14 +119,30 @@ public class SequenceAnnotationReport
 //    }
   };
 
-  public SequenceAnnotationReport(String linkURL)
+  private boolean forTooltip;
+
+  /**
+   * Constructor given a flag which affects behaviour
+   * <ul>
+   * <li>if true, generates feature details suitable to show in a tooltip</li>
+   * <li>if false, generates feature details in a form suitable for the sequence
+   * details report</li>
+   * </ul>
+   * 
+   * @param isForTooltip
+   */
+  public SequenceAnnotationReport(boolean isForTooltip)
   {
-    this.linkImageURL = linkURL;
+    this.forTooltip = isForTooltip;
+    if (linkImageURL == null)
+    {
+      linkImageURL = getClass().getResource("/images/link.gif").toString();
+    }
   }
 
   /**
-   * Append text for the list of features to the tooltip Returns number of
-   * features left if maxlength limit is (or would have been) reached
+   * Append text for the list of features to the tooltip. Returns the number of
+   * features not added if maxlength limit is (or would have been) reached.
    * 
    * @param sb
    * @param residuePos
@@ -132,7 +150,7 @@ public class SequenceAnnotationReport
    * @param minmax
    * @param maxlength
    */
-  public int appendFeaturesLengthLimit(final StringBuilder sb,
+  public int appendFeatures(final StringBuilder sb,
           int residuePos, List<SequenceFeature> features,
           FeatureRendererModel fr, int maxlength)
   {
@@ -147,16 +165,10 @@ public class SequenceAnnotationReport
     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
+   * 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
@@ -164,7 +176,7 @@ public class SequenceAnnotationReport
    * @param fr
    * @param maxlength
    */
-  public int appendFeaturesLengthLimit(StringBuilder sb, int residuePos,
+  public int appendFeatures(StringBuilder sb, int residuePos,
           MappedFeatures mf, FeatureRendererModel fr, int maxlength)
   {
     for (int i = 0; i < mf.features.size(); i++)
@@ -178,12 +190,6 @@ public class SequenceAnnotationReport
     return 0;
   }
 
-  public void appendFeatures(StringBuilder sb, int residuePos,
-          MappedFeatures mf, FeatureRendererModel fr)
-  {
-    appendFeaturesLengthLimit(sb, residuePos, mf, fr, 0);
-  }
-
   /**
    * Appends the feature at rpos to the given buffer
    * 
@@ -196,19 +202,64 @@ public class SequenceAnnotationReport
           FeatureRendererModel fr, SequenceFeature feature,
           MappedFeatures mf, int maxlength)
   {
+    int begin = feature.getBegin();
+    int end = feature.getEnd();
+
+    /*
+     * if this is a virtual features, convert begin/end to the
+     * coordinates of the sequence it is mapped to
+     */
+    int[] beginRange = null; // feature start in local coordinates
+    int[] endRange = null; // feature end in local coordinates
+    if (mf != null)
+    {
+      if (feature.isContactFeature())
+      {
+        /*
+         * map start and end points individually
+         */
+        beginRange = mf.getMappedPositions(begin, begin);
+        endRange = begin == end ? beginRange
+                : mf.getMappedPositions(end, end);
+      }
+      else
+      {
+        /*
+         * map the feature extent
+         */
+        beginRange = mf.getMappedPositions(begin, end);
+        endRange = beginRange;
+      }
+      if (beginRange == null || endRange == null)
+      {
+        // something went wrong
+        return false;
+      }
+      begin = beginRange[0];
+      end = endRange[endRange.length - 1];
+    }
+
     StringBuilder sb = new StringBuilder();
     if (feature.isContactFeature())
     {
-      if (feature.getBegin() == rpos || feature.getEnd() == rpos)
+      /*
+       * include if rpos is at start or end position of [mapped] feature
+       */
+      boolean showContact = (mf == null) && (rpos == begin || rpos == end);
+      boolean showMappedContact = (mf != null) && ((rpos >= beginRange[0]
+              && rpos <= beginRange[beginRange.length - 1])
+              || (rpos >= endRange[0]
+                      && rpos <= endRange[endRange.length - 1]));
+      if (showContact || showMappedContact)
       {
         if (sb0.length() > 6)
         {
           sb.append("<br/>");
         }
-        sb.append(feature.getType()).append(" ").append(feature.getBegin())
-                .append(":").append(feature.getEnd());
+        sb.append(feature.getType()).append(" ").append(begin).append(":")
+                .append(end);
       }
-      return appendTextMaxLengthReached(sb0, sb, maxlength);
+      return appendText(sb0, sb, maxlength);
     }
 
     if (sb0.length() > 6)
@@ -223,11 +274,11 @@ public class SequenceAnnotationReport
       if (rpos != 0)
       {
         // we are marking a positional feature
-        sb.append(feature.begin);
-      }
-      if (feature.begin != feature.end)
-      {
-        sb.append(" ").append(feature.end);
+        sb.append(begin);
+        if (begin != end)
+        {
+          sb.append(" ").append(end);
+        }
       }
 
       String description = feature.getDescription();
@@ -239,7 +290,7 @@ public class SequenceAnnotationReport
          * truncate overlong descriptions unless they contain an href
          * before the truncation point (as truncation could leave corrupted html)
          */
-        int linkindex = description.toLowerCase().indexOf("<a ");
+        int linkindex = description.toLowerCase(Locale.ROOT).indexOf("<a ");
         boolean hasLink = linkindex > -1
                 && linkindex < MAX_DESCRIPTION_LENGTH;
         if (description.length() > MAX_DESCRIPTION_LENGTH && !hasLink)
@@ -288,29 +339,21 @@ 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);
+    return appendText(sb0, sb, maxlength);
   }
 
   /**
-   * 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.
+   * Appends sb to sb0, and returns false, unless maxlength is not zero and
+   * appending would make the result longer than or equal to maxlength, in which
+   * case the append is not done and returns true
    * 
    * @param sb0
    * @param sb
    * @param maxlength
    * @return
    */
-  private static boolean appendTextMaxLengthReached(StringBuilder sb0,
-          StringBuilder sb, int maxlength)
+  private static boolean appendText(StringBuilder sb0, StringBuilder sb,
+          int maxlength)
   {
     if (maxlength == 0 || sb0.length() + sb.length() < maxlength)
     {
@@ -374,8 +417,8 @@ public class SequenceAnnotationReport
                       + "\" target=\""
                       + urllink.get(0)
                       + "\">"
-                      + (urllink.get(0).toLowerCase()
-                              .equals(urllink.get(1).toLowerCase()) ? urllink
+                      + (urllink.get(0).toLowerCase(Locale.ROOT)
+                              .equals(urllink.get(1).toLowerCase(Locale.ROOT)) ? urllink
                               .get(0) : (urllink.get(0) + ":" + urllink
                                               .get(1)))
                       + "</a><br/>");
@@ -472,7 +515,7 @@ public class SequenceAnnotationReport
               .getNonPositionalFeatures())
       {
         int sz = -sb.length();
-        appendFeature(sb, 0, fr, sf, null);
+        appendFeature(sb, 0, fr, sf, null, 0);
         sz += sb.length();
         maxWidth = Math.max(maxWidth, sz);
       }