X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FSequenceAnnotationReport.java;h=95bd1cc2ca4008a1cfe4248467e7628ff5ab5429;hb=a6f0678764ea06034460236c7a811bbfcad682aa;hp=13f41d4ccba86afe65e154e87769b3a9b3bb8e10;hpb=353109c11d706b29ae5bc9606f0e12223aa45a98;p=jalview.git
diff --git a/src/jalview/io/SequenceAnnotationReport.java b/src/jalview/io/SequenceAnnotationReport.java
index 13f41d4..95bd1cc 100644
--- a/src/jalview/io/SequenceAnnotationReport.java
+++ b/src/jalview/io/SequenceAnnotationReport.java
@@ -20,20 +20,25 @@
*/
package jalview.io;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import jalview.api.FeatureColourI;
import jalview.datamodel.DBRefEntry;
import jalview.datamodel.DBRefSource;
+import jalview.datamodel.GeneLociI;
+import jalview.datamodel.MappedFeatures;
import jalview.datamodel.SequenceFeature;
import jalview.datamodel.SequenceI;
-import jalview.io.gff.GffConstants;
import jalview.util.MessageManager;
+import jalview.util.StringUtils;
import jalview.util.UrlLink;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel;
/**
* generate HTML reports for a sequence
@@ -42,6 +47,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 = "...";
@@ -50,11 +57,9 @@ public class SequenceAnnotationReport
private static final int MAX_SOURCES = 40;
- private static final String[][] PRIMARY_SOURCES = new String[][] {
- DBRefSource.CODINGDBS, DBRefSource.DNACODINGDBS,
- DBRefSource.PROTEINDBS };
+ 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),
@@ -66,18 +71,18 @@ public class SequenceAnnotationReport
@Override
public int compare(DBRefEntry ref1, DBRefEntry ref2)
{
- if (ref1.isChromosome())
+ if (ref1 instanceof GeneLociI)
{
return -1;
}
- if (ref2.isChromosome())
+ if (ref2 instanceof GeneLociI)
{
return 1;
}
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;
@@ -98,45 +103,91 @@ 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 linkImageURL)
+ private boolean forTooltip;
+
+ /**
+ * Constructor given a flag which affects behaviour
+ *
+ * - if true, generates feature details suitable to show in a tooltip
+ * - if false, generates feature details in a form suitable for the sequence
+ * details report
+ *
+ *
+ * @param isForTooltip
+ */
+ public SequenceAnnotationReport(boolean isForTooltip)
{
- this.linkImageURL = linkImageURL;
+ this.forTooltip = isForTooltip;
+ if (linkImageURL == null)
+ {
+ linkImageURL = getClass().getResource("/images/link.gif").toString();
+ }
}
/**
- * Append text for the list of features to the tooltip
+ * 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 rpos
+ * @param residuePos
* @param features
* @param minmax
+ * @param maxlength
*/
- public void appendFeatures(final StringBuilder sb, int rpos,
- List features, Map minmax)
+ public int appendFeatures(final StringBuilder sb, int residuePos,
+ List features, FeatureRendererModel fr,
+ int maxlength)
{
- if (features != null)
+ for (int i = 0; i < features.size(); i++)
{
- for (SequenceFeature feature : features)
+ SequenceFeature feature = features.get(i);
+ if (appendFeature(sb, residuePos, fr, feature, null, maxlength))
{
- appendFeature(sb, rpos, minmax, feature);
+ return features.size() - i;
}
}
+ return 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 int appendFeatures(StringBuilder sb, int residuePos,
+ MappedFeatures mf, FeatureRendererModel fr, int maxlength)
+ {
+ for (int i = 0; i < mf.features.size(); i++)
+ {
+ SequenceFeature feature = mf.features.get(i);
+ if (appendFeature(sb, residuePos, fr, feature, mf, maxlength))
+ {
+ return mf.features.size() - i;
+ }
+ }
+ return 0;
}
/**
@@ -147,110 +198,195 @@ public class SequenceAnnotationReport
* @param minmax
* @param feature
*/
- void appendFeature(final StringBuilder sb, int rpos,
- Map minmax, SequenceFeature feature)
+ boolean appendFeature(final StringBuilder sb0, int rpos,
+ 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 (sb.length() > 6)
+ if (sb0.length() > 6)
{
- sb.append("
");
+ sb.append("
");
}
- sb.append(feature.getType()).append(" ").append(feature.getBegin())
- .append(":").append(feature.getEnd());
+ sb.append(feature.getType()).append(" ").append(begin).append(":")
+ .append(end);
}
+ return appendText(sb0, sb, maxlength);
}
- else
+
+ if (sb0.length() > 6)
{
- if (sb.length() > 6)
- {
- sb.append("
");
- }
- // TODO: remove this hack to display link only features
- boolean linkOnly = feature.getValue("linkonly") != null;
- if (!linkOnly)
+ sb.append("
");
+ }
+ // 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)
{
- sb.append(feature.getType()).append(" ");
- if (rpos != 0)
+ // we are marking a positional feature
+ sb.append(begin);
+ if (begin != end)
{
- // we are marking a positional feature
- sb.append(feature.begin);
+ sb.append(" ").append(end);
}
- if (feature.begin != feature.end)
+ }
+
+ String description = feature.getDescription();
+ 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(Locale.ROOT).indexOf(" -1
+ && linkindex < MAX_DESCRIPTION_LENGTH;
+ if (description.length() > MAX_DESCRIPTION_LENGTH && !hasLink)
{
- sb.append(" ").append(feature.end);
+ description = description.substring(0, MAX_DESCRIPTION_LENGTH)
+ + ELLIPSIS;
}
- if (feature.getDescription() != null
- && !feature.description.equals(feature.getType()))
- {
- String tmpString = feature.getDescription();
- String tmp2up = tmpString.toUpperCase();
- int startTag = tmp2up.indexOf("");
- if (startTag > -1)
- {
- tmpString = tmpString.substring(startTag + 6);
- tmp2up = tmp2up.substring(startTag + 6);
- }
- int endTag = tmp2up.indexOf("");
- if (endTag > -1)
- {
- tmpString = tmpString.substring(0, endTag);
- tmp2up = tmp2up.substring(0, endTag);
- }
- endTag = tmp2up.indexOf("