Merge branch 'alpha/JAL-3362_Jalview_212_alpha' into alpha/merge_212_JalviewJS_2112
[jalview.git] / src / jalview / io / SequenceAnnotationReport.java
index 9d7fcfc..1a5072d 100644 (file)
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Map;
 
 import jalview.api.FeatureColourI;
+import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.DBRefSource;
 import jalview.datamodel.GeneLociI;
@@ -55,9 +56,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 +118,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 +149,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 +164,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 +175,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 +189,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 +201,49 @@ 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;
+    int[] endRange = null;
+    if (mf != null)
+    {
+      beginRange = mf.getMappedPositions(begin, begin);
+      endRange = mf.getMappedPositions(end, end);
+      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 +258,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();
@@ -288,29 +323,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)
     {
@@ -452,12 +479,27 @@ public class SequenceAnnotationReport
       sb.append(tmp);
       maxWidth = Math.max(maxWidth, tmp.length());
     }
+
     SequenceI ds = sequence;
     while (ds.getDatasetSequence() != null)
     {
       ds = ds.getDatasetSequence();
     }
 
+    /*
+     * add any annotation scores
+     */
+    AlignmentAnnotation[] anns = ds.getAnnotation();
+    for (int i = 0; anns != null && i < anns.length; i++)
+    {
+      AlignmentAnnotation aa = anns[i];
+      if (aa != null && aa.hasScore() && aa.sequenceRef != null)
+      {
+        sb.append("<br>").append(aa.label).append(": ")
+                .append(aa.getScore());
+      }
+    }
+
     if (showDbRefs)
     {
       maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
@@ -472,12 +514,29 @@ 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);
       }
     }
+
+
+    if (sequence.getAnnotation("Search Scores") != null)
+    {
+      sb.append("<br>");
+      String eValue = " E-Value: "
+              + sequence.getAnnotation("Search Scores")[0].getEValue();
+      String bitScore = " Bit Score: "
+              + sequence.getAnnotation("Search Scores")[0].getBitScore();
+      sb.append(eValue);
+      sb.append("<br>");
+      sb.append(bitScore);
+      maxWidth = Math.max(maxWidth, eValue.length());
+      maxWidth = Math.max(maxWidth, bitScore.length());
+    }
+    sb.append("<br>");
     sb.append("</i>");
+
     return maxWidth;
   }