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.AlignmentAnnotation;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.DBRefSource;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.util.MessageManager;
30 import jalview.util.StringUtils;
31 import jalview.util.UrlLink;
32 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Comparator;
37 import java.util.LinkedHashMap;
38 import java.util.List;
42 * generate HTML reports for a sequence
46 public class SequenceAnnotationReport
48 private static final String COMMA = ",";
50 private static final String ELLIPSIS = "...";
52 private static final int MAX_REFS_PER_SOURCE = 4;
54 private static final int MAX_SOURCES = 40;
56 private static final String[][] PRIMARY_SOURCES = new String[][] {
57 DBRefSource.CODINGDBS, DBRefSource.DNACODINGDBS,
58 DBRefSource.PROTEINDBS };
60 final String linkImageURL;
63 * Comparator to order DBRefEntry by Source + accession id (case-insensitive),
64 * with 'Primary' sources placed before others, and 'chromosome' first of all
66 private static Comparator<DBRefEntry> comparator = new Comparator<DBRefEntry>()
70 public int compare(DBRefEntry ref1, DBRefEntry ref2)
72 if (ref1.isChromosome())
76 if (ref2.isChromosome())
80 String s1 = ref1.getSource();
81 String s2 = ref2.getSource();
82 boolean s1Primary = isPrimarySource(s1);
83 boolean s2Primary = isPrimarySource(s2);
84 if (s1Primary && !s2Primary)
88 if (!s1Primary && s2Primary)
92 int comp = s1 == null ? -1 : (s2 == null ? 1 : s1
93 .compareToIgnoreCase(s2));
96 String a1 = ref1.getAccessionId();
97 String a2 = ref2.getAccessionId();
98 comp = a1 == null ? -1 : (a2 == null ? 1 : a1
99 .compareToIgnoreCase(a2));
104 private boolean isPrimarySource(String source)
106 for (String[] primary : PRIMARY_SOURCES)
108 for (String s : primary)
110 if (source.equals(s))
120 public SequenceAnnotationReport(String linkURL)
122 this.linkImageURL = linkURL;
126 * Append text for the list of features to the tooltip
133 public void appendFeatures(final StringBuilder sb, int rpos,
134 List<SequenceFeature> features, FeatureRendererModel fr)
136 if (features != null)
138 for (SequenceFeature feature : features)
140 appendFeature(sb, rpos, fr, feature);
146 * Appends the feature at rpos to the given buffer
153 void appendFeature(final StringBuilder sb, int rpos,
154 FeatureRendererModel fr, SequenceFeature feature)
156 if (feature.isContactFeature())
158 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
164 sb.append(feature.getType()).append(" ").append(feature.getBegin())
165 .append(":").append(feature.getEnd());
174 // TODO: remove this hack to display link only features
175 boolean linkOnly = feature.getValue("linkonly") != null;
178 sb.append(feature.getType()).append(" ");
181 // we are marking a positional feature
182 sb.append(feature.begin);
184 if (feature.begin != feature.end)
186 sb.append(" ").append(feature.end);
189 String description = feature.getDescription();
190 if (description != null && !description.equals(feature.getType()))
192 description = StringUtils.stripHtmlTags(description);
193 sb.append("; ").append(description);
196 if (showScore(feature, fr))
198 sb.append(" Score=").append(String.valueOf(feature.getScore()));
200 String status = (String) feature.getValue("status");
201 if (status != null && status.length() > 0)
203 sb.append("; (").append(status).append(")");
207 * add attribute value if coloured by attribute
211 FeatureColourI fc = fr.getFeatureColours().get(feature.getType());
212 if (fc != null && fc.isColourByAttribute())
214 String[] attName = fc.getAttributeName();
215 String attVal = feature.getValueAsString(attName);
218 sb.append("; ").append(String.join(":", attName)).append("=")
227 * Answers true if score should be shown, else false. Score is shown if it is
228 * not NaN, and the feature type has a non-trivial min-max score range
230 boolean showScore(SequenceFeature feature, FeatureRendererModel fr)
232 if (Float.isNaN(feature.getScore()))
240 float[][] minMax = fr.getMinMax().get(feature.getType());
243 * minMax[0] is the [min, max] score range for positional features
245 if (minMax == null || minMax[0] == null || minMax[0][0] == minMax[0][1])
253 * Format and appends any hyperlinks for the sequence feature to the string
259 void appendLinks(final StringBuffer sb, SequenceFeature feature)
261 if (feature.links != null)
263 if (linkImageURL != null)
265 sb.append(" <img src=\"" + linkImageURL + "\">");
269 for (String urlstring : feature.links)
273 for (List<String> urllink : createLinksFrom(null, urlstring))
275 sb.append("<br/> <a href=\""
280 + (urllink.get(0).toLowerCase()
281 .equals(urllink.get(1).toLowerCase()) ? urllink
282 .get(0) : (urllink.get(0) + ":" + urllink
283 .get(1))) + "</a></br>");
285 } catch (Exception x)
287 System.err.println("problem when creating links from "
301 * @return Collection< List<String> > { List<String> { link target, link
302 * label, dynamic component inserted (if any), url }}
304 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
306 Map<String, List<String>> urlSets = new LinkedHashMap<>();
307 UrlLink urlLink = new UrlLink(link);
308 if (!urlLink.isValid())
310 System.err.println(urlLink.getInvalidMessage());
314 urlLink.createLinksFromSeq(seq, urlSets);
316 return urlSets.values();
319 public void createSequenceAnnotationReport(final StringBuilder tip,
320 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
321 FeatureRendererModel fr)
323 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
328 * Builds an html formatted report of sequence details and appends it to the
332 * buffer to append report to
334 * the sequence the report is for
336 * whether to include database references for the sequence
338 * whether to include non-positional sequence features
343 int createSequenceAnnotationReport(final StringBuilder sb,
344 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
345 FeatureRendererModel fr, boolean summary)
351 if (sequence.getDescription() != null)
353 tmp = sequence.getDescription();
354 sb.append("<br>").append(tmp);
355 maxWidth = Math.max(maxWidth, tmp.length());
358 SequenceI ds = sequence;
359 while (ds.getDatasetSequence() != null)
361 ds = ds.getDatasetSequence();
365 * add any annotation scores
367 AlignmentAnnotation[] anns = ds.getAnnotation();
368 for (int i = 0; anns != null && i < anns.length; i++)
370 AlignmentAnnotation aa = anns[i];
371 if (aa != null && aa.hasScore() && aa.sequenceRef != null)
373 sb.append("<br>").append(aa.label).append(": ")
374 .append(aa.getScore());
380 maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
384 * add non-positional features if wanted
388 for (SequenceFeature sf : sequence.getFeatures()
389 .getNonPositionalFeatures())
391 int sz = -sb.length();
392 appendFeature(sb, 0, fr, sf);
394 maxWidth = Math.max(maxWidth, sz);
402 * A helper method that appends any DBRefs, returning the maximum line length
410 protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
413 DBRefEntry[] dbrefs = ds.getDBRefs();
419 // note this sorts the refs held on the sequence!
420 Arrays.sort(dbrefs, comparator);
421 boolean ellipsis = false;
422 String source = null;
423 String lastSource = null;
424 int countForSource = 0;
426 boolean moreSources = false;
427 int maxLineLength = 0;
430 for (DBRefEntry ref : dbrefs)
432 source = ref.getSource();
438 boolean sourceChanged = !source.equals(lastSource);
445 if (sourceCount > MAX_SOURCES && summary)
453 if (countForSource == 1 || !summary)
457 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
459 String accessionId = ref.getAccessionId();
460 lineLength += accessionId.length() + 1;
461 if (countForSource > 1 && summary)
463 sb.append(", ").append(accessionId);
468 sb.append(source).append(" ").append(accessionId);
469 lineLength += source.length();
471 maxLineLength = Math.max(maxLineLength, lineLength);
473 if (countForSource == MAX_REFS_PER_SOURCE && summary)
475 sb.append(COMMA).append(ELLIPSIS);
481 sb.append("<br>").append(source).append(COMMA).append(ELLIPSIS);
486 sb.append(MessageManager.getString("label.output_seq_details"));
490 return maxLineLength;
493 public void createTooltipAnnotationReport(final StringBuilder tip,
494 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
495 FeatureRendererModel fr)
497 int maxWidth = createSequenceAnnotationReport(tip, sequence,
498 showDbRefs, showNpFeats, fr, true);
502 // ? not sure this serves any useful purpose
503 // tip.insert(0, "<table width=350 border=0><tr><td>");
504 // tip.append("</td></tr></table>");