X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FSequenceAnnotationReport.java;h=1a5072dde534ff8b4b5e7efe418af1ea61f3deb5;hb=c794c5033adeee182b03a5ea92c0a7495a29661f;hp=497e4c920df0b04fef00bf0f84fb0a5ab5540437;hpb=59d682209891099d46b960509907c79e3fb276fe;p=jalview.git
diff --git a/src/jalview/io/SequenceAnnotationReport.java b/src/jalview/io/SequenceAnnotationReport.java
index 497e4c9..1a5072d 100644
--- a/src/jalview/io/SequenceAnnotationReport.java
+++ b/src/jalview/io/SequenceAnnotationReport.java
@@ -1,30 +1,43 @@
/*
- * 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 .
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * 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 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.AlignmentAnnotation;
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.util.MessageManager;
+import jalview.util.StringUtils;
import jalview.util.UrlLink;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel;
/**
* generate HTML reports for a sequence
@@ -33,355 +46,604 @@ import jalview.util.UrlLink;
*/
public class SequenceAnnotationReport
{
- final String linkImageURL;
+ private static final int MAX_DESCRIPTION_LENGTH = 40;
- public SequenceAnnotationReport(String linkImageURL)
+ private static final String COMMA = ",";
+
+ private static final String ELLIPSIS = "...";
+
+ private static final int MAX_REFS_PER_SOURCE = 4;
+
+ private static final int MAX_SOURCES = 40;
+
+ private static String linkImageURL;
+
+ // public static final String[][] PRIMARY_SOURCES moved to DBRefSource.java
+
+ /*
+ * Comparator to order DBRefEntry by Source + accession id (case-insensitive),
+ * with 'Primary' sources placed before others, and 'chromosome' first of all
+ */
+ private static Comparator comparator = new Comparator()
+ {
+
+ @Override
+ public int compare(DBRefEntry ref1, DBRefEntry ref2)
+ {
+ if (ref1 instanceof GeneLociI)
+ {
+ return -1;
+ }
+ if (ref2 instanceof GeneLociI)
+ {
+ return 1;
+ }
+ String s1 = ref1.getSource();
+ String s2 = ref2.getSource();
+ boolean s1Primary = DBRefSource.isPrimarySource(s1);
+ boolean s2Primary = DBRefSource.isPrimarySource(s2);
+ if (s1Primary && !s2Primary)
+ {
+ return -1;
+ }
+ if (!s1Primary && s2Primary)
+ {
+ return 1;
+ }
+ int comp = s1 == null ? -1 : (s2 == null ? 1 : s1
+ .compareToIgnoreCase(s2));
+ if (comp == 0)
+ {
+ String a1 = ref1.getAccessionId();
+ String a2 = ref2.getAccessionId();
+ comp = a1 == null ? -1 : (a2 == null ? 1 : a1
+ .compareToIgnoreCase(a2));
+ }
+ return comp;
+ }
+
+// private boolean isPrimarySource(String source)
+// {
+// for (String[] primary : DBRefSource.PRIMARY_SOURCES)
+// {
+// for (String s : primary)
+// {
+// if (source.equals(s))
+// {
+// return true;
+// }
+// }
+// }
+// return false;
+// }
+ };
+
+ 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();
+ }
}
/**
- * 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. Returns the number of
+ * features not added if maxlength limit is (or would have been) reached.
*
- * @param tooltipText2
- * @param linkImageURL
- * @param rpos
+ * @param sb
+ * @param residuePos
* @param features
- * TODO refactor to Jalview 'utilities' somehow.
+ * @param minmax
+ * @param maxlength
+ */
+ public int appendFeatures(final StringBuilder sb,
+ int residuePos, List features,
+ FeatureRendererModel fr, int maxlength)
+ {
+ for (int i = 0; i < features.size(); i++)
+ {
+ SequenceFeature feature = features.get(i);
+ if (appendFeature(sb, residuePos, fr, feature, null, maxlength))
+ {
+ 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 void appendFeatures(final StringBuffer tooltipText2, int rpos,
- SequenceFeature[] features)
+ public int appendFeatures(StringBuilder sb, int residuePos,
+ MappedFeatures mf, FeatureRendererModel fr, int maxlength)
{
- appendFeatures(tooltipText2, rpos, features, null);
+ 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;
}
- public void appendFeatures(final StringBuffer tooltipText2, int rpos,
- SequenceFeature[] features, Hashtable minmax)
+ /**
+ * Appends the feature at rpos to the given buffer
+ *
+ * @param sb
+ * @param rpos
+ * @param minmax
+ * @param feature
+ */
+ boolean appendFeature(final StringBuilder sb0, int rpos,
+ FeatureRendererModel fr, SequenceFeature feature,
+ MappedFeatures mf, int maxlength)
{
- String tmpString;
- if (features != null)
+ 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())
{
- for (int i = 0; i < features.length; i++)
+ /*
+ * 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 (features[i].getType().equals("disulfide bond"))
+ if (sb0.length() > 6)
{
- if (features[i].getBegin() == rpos
- || features[i].getEnd() == rpos)
- {
- if (tooltipText2.length() > 6)
- {
- tooltipText2.append("
");
- }
- tooltipText2.append("disulfide bond " + features[i].getBegin()
- + ":" + features[i].getEnd());
- }
+ sb.append("
");
}
- else
+ sb.append(feature.getType()).append(" ").append(begin).append(":")
+ .append(end);
+ }
+ return appendText(sb0, sb, maxlength);
+ }
+
+ if (sb0.length() > 6)
+ {
+ 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)
+ {
+ // we are marking a positional feature
+ sb.append(begin);
+ if (begin != end)
{
- if (tooltipText2.length() > 6)
- {
- tooltipText2.append("
");
- }
- // TODO: remove this hack to display link only features
- boolean linkOnly = features[i].getValue("linkonly") != null;
- if (!linkOnly)
- {
- 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);
- }
+ sb.append(" ").append(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("");
- 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("