2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.api.FeatureColourI;
24 import jalview.datamodel.DBRefEntry;
25 import jalview.datamodel.DBRefSource;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.MessageManager;
29 import jalview.util.StringUtils;
30 import jalview.util.UrlLink;
31 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Comparator;
36 import java.util.LinkedHashMap;
37 import java.util.List;
41 * generate HTML reports for a sequence
45 public class SequenceAnnotationReport
47 private static final String COMMA = ",";
49 private static final String ELLIPSIS = "...";
51 private static final int MAX_REFS_PER_SOURCE = 4;
53 private static final int MAX_SOURCES = 40;
55 private static final String[][] PRIMARY_SOURCES = new String[][] {
56 DBRefSource.CODINGDBS, DBRefSource.DNACODINGDBS,
57 DBRefSource.PROTEINDBS };
59 final String linkImageURL;
62 * Comparator to order DBRefEntry by Source + accession id (case-insensitive),
63 * with 'Primary' sources placed before others, and 'chromosome' first of all
65 private static Comparator<DBRefEntry> comparator = new Comparator<DBRefEntry>()
69 public int compare(DBRefEntry ref1, DBRefEntry ref2)
71 if (ref1.isChromosome())
75 if (ref2.isChromosome())
79 String s1 = ref1.getSource();
80 String s2 = ref2.getSource();
81 boolean s1Primary = isPrimarySource(s1);
82 boolean s2Primary = isPrimarySource(s2);
83 if (s1Primary && !s2Primary)
87 if (!s1Primary && s2Primary)
91 int comp = s1 == null ? -1 : (s2 == null ? 1 : s1
92 .compareToIgnoreCase(s2));
95 String a1 = ref1.getAccessionId();
96 String a2 = ref2.getAccessionId();
97 comp = a1 == null ? -1 : (a2 == null ? 1 : a1
98 .compareToIgnoreCase(a2));
103 private boolean isPrimarySource(String source)
105 for (String[] primary : PRIMARY_SOURCES)
107 for (String s : primary)
109 if (source.equals(s))
119 public SequenceAnnotationReport(String linkURL)
121 this.linkImageURL = linkURL;
125 * Append text for the list of features to the tooltip
132 public void appendFeatures(final StringBuilder sb, int rpos,
133 List<SequenceFeature> features, FeatureRendererModel fr)
135 if (features != null)
137 for (SequenceFeature feature : features)
139 appendFeature(sb, rpos, fr, feature);
145 * Appends the feature at rpos to the given buffer
152 void appendFeature(final StringBuilder sb, int rpos,
153 FeatureRendererModel fr, SequenceFeature feature)
155 if (feature.isContactFeature())
157 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
163 sb.append(feature.getType()).append(" ").append(feature.getBegin())
164 .append(":").append(feature.getEnd());
173 // TODO: remove this hack to display link only features
174 boolean linkOnly = feature.getValue("linkonly") != null;
177 sb.append(feature.getType()).append(" ");
180 // we are marking a positional feature
181 sb.append(feature.begin);
183 if (feature.begin != feature.end)
185 sb.append(" ").append(feature.end);
188 String description = feature.getDescription();
189 if (description != null && !description.equals(feature.getType()))
191 description = StringUtils.stripHtmlTags(description);
192 sb.append("; ").append(description);
195 if (showScore(feature, fr))
197 sb.append(" Score=").append(String.valueOf(feature.getScore()));
199 String status = (String) feature.getValue("status");
200 if (status != null && status.length() > 0)
202 sb.append("; (").append(status).append(")");
206 * add attribute value if coloured by attribute
210 FeatureColourI fc = fr.getFeatureColours().get(feature.getType());
211 if (fc != null && fc.isColourByAttribute())
213 String[] attName = fc.getAttributeName();
214 String attVal = feature.getValueAsString(attName);
217 sb.append("; ").append(String.join(":", attName)).append("=")
226 * Answers true if score should be shown, else false. Score is shown if it is
227 * not NaN, and the feature type has a non-trivial min-max score range
229 boolean showScore(SequenceFeature feature, FeatureRendererModel fr)
231 if (Float.isNaN(feature.getScore()))
239 float[][] minMax = fr.getMinMax().get(feature.getType());
242 * minMax[0] is the [min, max] score range for positional features
244 if (minMax == null || minMax[0] == null || minMax[0][0] == minMax[0][1])
252 * Format and appends any hyperlinks for the sequence feature to the string
258 void appendLinks(final StringBuffer sb, SequenceFeature feature)
260 if (feature.links != null)
262 if (linkImageURL != null)
264 sb.append(" <img src=\"" + linkImageURL + "\">");
268 for (String urlstring : feature.links)
272 for (List<String> urllink : createLinksFrom(null, urlstring))
274 sb.append("<br/> <a href=\""
279 + (urllink.get(0).toLowerCase()
280 .equals(urllink.get(1).toLowerCase()) ? urllink
281 .get(0) : (urllink.get(0) + ":" + urllink
282 .get(1))) + "</a></br>");
284 } catch (Exception x)
286 System.err.println("problem when creating links from "
300 * @return Collection< List<String> > { List<String> { link target, link
301 * label, dynamic component inserted (if any), url }}
303 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
305 Map<String, List<String>> urlSets = new LinkedHashMap<>();
306 UrlLink urlLink = new UrlLink(link);
307 if (!urlLink.isValid())
309 System.err.println(urlLink.getInvalidMessage());
313 urlLink.createLinksFromSeq(seq, urlSets);
315 return urlSets.values();
318 public void createSequenceAnnotationReport(final StringBuilder tip,
319 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
320 FeatureRendererModel fr)
322 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
327 * Builds an html formatted report of sequence details and appends it to the
331 * buffer to append report to
333 * the sequence the report is for
335 * whether to include database references for the sequence
337 * whether to include non-positional sequence features
342 int createSequenceAnnotationReport(final StringBuilder sb,
343 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
344 FeatureRendererModel fr, boolean summary)
350 if (sequence.getDescription() != null)
352 tmp = sequence.getDescription();
353 sb.append("<br>").append(tmp);
354 maxWidth = Math.max(maxWidth, tmp.length());
356 SequenceI ds = sequence;
357 while (ds.getDatasetSequence() != null)
359 ds = ds.getDatasetSequence();
364 maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
368 * add non-positional features if wanted
372 for (SequenceFeature sf : sequence.getFeatures()
373 .getNonPositionalFeatures())
375 int sz = -sb.length();
376 appendFeature(sb, 0, fr, sf);
378 maxWidth = Math.max(maxWidth, sz);
386 * A helper method that appends any DBRefs, returning the maximum line length
394 protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
397 DBRefEntry[] dbrefs = ds.getDBRefs();
403 // note this sorts the refs held on the sequence!
404 Arrays.sort(dbrefs, comparator);
405 boolean ellipsis = false;
406 String source = null;
407 String lastSource = null;
408 int countForSource = 0;
410 boolean moreSources = false;
411 int maxLineLength = 0;
414 for (DBRefEntry ref : dbrefs)
416 source = ref.getSource();
422 boolean sourceChanged = !source.equals(lastSource);
429 if (sourceCount > MAX_SOURCES && summary)
437 if (countForSource == 1 || !summary)
441 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
443 String accessionId = ref.getAccessionId();
444 lineLength += accessionId.length() + 1;
445 if (countForSource > 1 && summary)
447 sb.append(", ").append(accessionId);
452 sb.append(source).append(" ").append(accessionId);
453 lineLength += source.length();
455 maxLineLength = Math.max(maxLineLength, lineLength);
457 if (countForSource == MAX_REFS_PER_SOURCE && summary)
459 sb.append(COMMA).append(ELLIPSIS);
465 sb.append("<br>").append(source).append(COMMA).append(ELLIPSIS);
470 sb.append(MessageManager.getString("label.output_seq_details"));
474 return maxLineLength;
477 public void createTooltipAnnotationReport(final StringBuilder tip,
478 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
479 FeatureRendererModel fr)
481 int maxWidth = createSequenceAnnotationReport(tip, sequence,
482 showDbRefs, showNpFeats, fr, true);
486 // ? not sure this serves any useful purpose
487 // tip.insert(0, "<table width=350 border=0><tr><td>");
488 // tip.append("</td></tr></table>");