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.GeneLociI;
27 import jalview.datamodel.MappedFeatures;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.util.MessageManager;
31 import jalview.util.StringUtils;
32 import jalview.util.UrlLink;
33 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.Comparator;
38 import java.util.LinkedHashMap;
39 import java.util.List;
43 * generate HTML reports for a sequence
47 public class SequenceAnnotationReport
49 private static final int MAX_DESCRIPTION_LENGTH = 40;
51 private static final String COMMA = ",";
53 private static final String ELLIPSIS = "...";
55 private static final int MAX_REFS_PER_SOURCE = 4;
57 private static final int MAX_SOURCES = 40;
59 private static final String[][] PRIMARY_SOURCES = new String[][] {
60 DBRefSource.CODINGDBS, DBRefSource.DNACODINGDBS,
61 DBRefSource.PROTEINDBS };
63 final String linkImageURL;
66 * Comparator to order DBRefEntry by Source + accession id (case-insensitive),
67 * with 'Primary' sources placed before others, and 'chromosome' first of all
69 private static Comparator<DBRefEntry> comparator = new Comparator<>()
73 public int compare(DBRefEntry ref1, DBRefEntry ref2)
75 if (ref1 instanceof GeneLociI)
79 if (ref2 instanceof GeneLociI)
83 String s1 = ref1.getSource();
84 String s2 = ref2.getSource();
85 boolean s1Primary = isPrimarySource(s1);
86 boolean s2Primary = isPrimarySource(s2);
87 if (s1Primary && !s2Primary)
91 if (!s1Primary && s2Primary)
95 int comp = s1 == null ? -1 : (s2 == null ? 1 : s1
96 .compareToIgnoreCase(s2));
99 String a1 = ref1.getAccessionId();
100 String a2 = ref2.getAccessionId();
101 comp = a1 == null ? -1 : (a2 == null ? 1 : a1
102 .compareToIgnoreCase(a2));
107 private boolean isPrimarySource(String source)
109 for (String[] primary : PRIMARY_SOURCES)
111 for (String s : primary)
113 if (source.equals(s))
123 public SequenceAnnotationReport(String linkURL)
125 this.linkImageURL = linkURL;
129 * Append text for the list of features to the tooltip
136 public void appendFeatures(final StringBuilder sb, int residuePos,
137 List<SequenceFeature> features, FeatureRendererModel fr)
139 for (SequenceFeature feature : features)
141 appendFeature(sb, residuePos, fr, feature, null);
146 * Appends text for mapped features (e.g. CDS feature for peptide or vice versa)
153 public void appendFeatures(StringBuilder sb, int residuePos,
154 MappedFeatures mf, FeatureRendererModel fr)
156 for (SequenceFeature feature : mf.features)
158 appendFeature(sb, residuePos, fr, feature, mf);
163 * Appends the feature at rpos to the given buffer
170 void appendFeature(final StringBuilder sb, int rpos,
171 FeatureRendererModel fr, SequenceFeature feature,
174 if (feature.isContactFeature())
176 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
182 sb.append(feature.getType()).append(" ").append(feature.getBegin())
183 .append(":").append(feature.getEnd());
192 // TODO: remove this hack to display link only features
193 boolean linkOnly = feature.getValue("linkonly") != null;
196 sb.append(feature.getType()).append(" ");
199 // we are marking a positional feature
200 sb.append(feature.begin);
202 if (feature.begin != feature.end)
204 sb.append(" ").append(feature.end);
207 String description = feature.getDescription();
208 if (description != null && !description.equals(feature.getType()))
210 description = StringUtils.stripHtmlTags(description);
213 * truncate overlong descriptions unless they contain an href
214 * (as truncation could leave corrupted html)
216 boolean hasLink = description.indexOf("a href") > -1;
217 if (description.length() > MAX_DESCRIPTION_LENGTH && !hasLink)
219 description = description.substring(0, MAX_DESCRIPTION_LENGTH)
222 sb.append("; ").append(description);
225 if (showScore(feature, fr))
227 sb.append(" Score=").append(String.valueOf(feature.getScore()));
229 String status = (String) feature.getValue("status");
230 if (status != null && status.length() > 0)
232 sb.append("; (").append(status).append(")");
236 * add attribute value if coloured by attribute
240 FeatureColourI fc = fr.getFeatureColours().get(feature.getType());
241 if (fc != null && fc.isColourByAttribute())
243 String[] attName = fc.getAttributeName();
244 String attVal = feature.getValueAsString(attName);
247 sb.append("; ").append(String.join(":", attName)).append("=")
255 String variants = mf.findProteinVariants(feature);
256 if (!variants.isEmpty())
258 sb.append(" ").append(variants);
265 * Answers true if score should be shown, else false. Score is shown if it is
266 * not NaN, and the feature type has a non-trivial min-max score range
268 boolean showScore(SequenceFeature feature, FeatureRendererModel fr)
270 if (Float.isNaN(feature.getScore()))
278 float[][] minMax = fr.getMinMax().get(feature.getType());
281 * minMax[0] is the [min, max] score range for positional features
283 if (minMax == null || minMax[0] == null || minMax[0][0] == minMax[0][1])
291 * Format and appends any hyperlinks for the sequence feature to the string
297 void appendLinks(final StringBuffer sb, SequenceFeature feature)
299 if (feature.links != null)
301 if (linkImageURL != null)
303 sb.append(" <img src=\"" + linkImageURL + "\">");
307 for (String urlstring : feature.links)
311 for (List<String> urllink : createLinksFrom(null, urlstring))
313 sb.append("<br/> <a href=\""
318 + (urllink.get(0).toLowerCase()
319 .equals(urllink.get(1).toLowerCase()) ? urllink
320 .get(0) : (urllink.get(0) + ":" + urllink
321 .get(1))) + "</a></br>");
323 } catch (Exception x)
325 System.err.println("problem when creating links from "
339 * @return Collection< List<String> > { List<String> { link target, link
340 * label, dynamic component inserted (if any), url }}
342 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
344 Map<String, List<String>> urlSets = new LinkedHashMap<>();
345 UrlLink urlLink = new UrlLink(link);
346 if (!urlLink.isValid())
348 System.err.println(urlLink.getInvalidMessage());
352 urlLink.createLinksFromSeq(seq, urlSets);
354 return urlSets.values();
357 public void createSequenceAnnotationReport(final StringBuilder tip,
358 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
359 FeatureRendererModel fr)
361 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
366 * Builds an html formatted report of sequence details and appends it to the
370 * buffer to append report to
372 * the sequence the report is for
374 * whether to include database references for the sequence
376 * whether to include non-positional sequence features
381 int createSequenceAnnotationReport(final StringBuilder sb,
382 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
383 FeatureRendererModel fr, boolean summary)
389 if (sequence.getDescription() != null)
391 tmp = sequence.getDescription();
393 maxWidth = Math.max(maxWidth, tmp.length());
395 SequenceI ds = sequence;
396 while (ds.getDatasetSequence() != null)
398 ds = ds.getDatasetSequence();
403 maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
407 * add non-positional features if wanted
411 for (SequenceFeature sf : sequence.getFeatures()
412 .getNonPositionalFeatures())
414 int sz = -sb.length();
415 appendFeature(sb, 0, fr, sf, null);
417 maxWidth = Math.max(maxWidth, sz);
425 * A helper method that appends any DBRefs, returning the maximum line length
433 protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
436 DBRefEntry[] dbrefs = ds.getDBRefs();
442 // note this sorts the refs held on the sequence!
443 Arrays.sort(dbrefs, comparator);
444 boolean ellipsis = false;
445 String source = null;
446 String lastSource = null;
447 int countForSource = 0;
449 boolean moreSources = false;
450 int maxLineLength = 0;
453 for (DBRefEntry ref : dbrefs)
455 source = ref.getSource();
461 boolean sourceChanged = !source.equals(lastSource);
468 if (sourceCount > MAX_SOURCES && summary)
476 if (countForSource == 1 || !summary)
480 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
482 String accessionId = ref.getAccessionId();
483 lineLength += accessionId.length() + 1;
484 if (countForSource > 1 && summary)
486 sb.append(", ").append(accessionId);
491 sb.append(source).append(" ").append(accessionId);
492 lineLength += source.length();
494 maxLineLength = Math.max(maxLineLength, lineLength);
496 if (countForSource == MAX_REFS_PER_SOURCE && summary)
498 sb.append(COMMA).append(ELLIPSIS);
504 sb.append("<br>").append(source).append(COMMA).append(ELLIPSIS);
509 sb.append(MessageManager.getString("label.output_seq_details"));
513 return maxLineLength;
516 public void createTooltipAnnotationReport(final StringBuilder tip,
517 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
518 FeatureRendererModel fr)
520 int maxWidth = createSequenceAnnotationReport(tip, sequence,
521 showDbRefs, showNpFeats, fr, true);
525 // ? not sure this serves any useful purpose
526 // tip.insert(0, "<table width=350 border=0><tr><td>");
527 // tip.append("</td></tr></table>");