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.GeneLociI;
28 import jalview.datamodel.MappedFeatures;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31 import jalview.util.MessageManager;
32 import jalview.util.StringUtils;
33 import jalview.util.UrlLink;
34 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.Comparator;
39 import java.util.LinkedHashMap;
40 import java.util.List;
44 * generate HTML reports for a sequence
48 public class SequenceAnnotationReport
50 private static final String COMMA = ",";
52 private static final String ELLIPSIS = "...";
54 private static final int MAX_REFS_PER_SOURCE = 4;
56 private static final int MAX_SOURCES = 40;
58 private static final String[][] PRIMARY_SOURCES = new String[][] {
59 DBRefSource.CODINGDBS, DBRefSource.DNACODINGDBS,
60 DBRefSource.PROTEINDBS };
62 final String linkImageURL;
65 * Comparator to order DBRefEntry by Source + accession id (case-insensitive),
66 * with 'Primary' sources placed before others, and 'chromosome' first of all
68 private static Comparator<DBRefEntry> comparator = new Comparator<DBRefEntry>()
72 public int compare(DBRefEntry ref1, DBRefEntry ref2)
74 if (ref1 instanceof GeneLociI)
78 if (ref2 instanceof GeneLociI)
82 String s1 = ref1.getSource();
83 String s2 = ref2.getSource();
84 boolean s1Primary = isPrimarySource(s1);
85 boolean s2Primary = isPrimarySource(s2);
86 if (s1Primary && !s2Primary)
90 if (!s1Primary && s2Primary)
94 int comp = s1 == null ? -1 : (s2 == null ? 1 : s1
95 .compareToIgnoreCase(s2));
98 String a1 = ref1.getAccessionId();
99 String a2 = ref2.getAccessionId();
100 comp = a1 == null ? -1 : (a2 == null ? 1 : a1
101 .compareToIgnoreCase(a2));
106 private boolean isPrimarySource(String source)
108 for (String[] primary : PRIMARY_SOURCES)
110 for (String s : primary)
112 if (source.equals(s))
122 public SequenceAnnotationReport(String linkURL)
124 this.linkImageURL = linkURL;
128 * Append text for the list of features to the tooltip
135 public void appendFeatures(final StringBuilder sb, int residuePos,
136 List<SequenceFeature> features, FeatureRendererModel fr)
138 for (SequenceFeature feature : features)
140 appendFeature(sb, residuePos, fr, feature, null);
145 * Appends text for mapped features (e.g. CDS feature for peptide or vice versa)
152 public void appendFeatures(StringBuilder sb, int residuePos,
153 MappedFeatures mf, FeatureRendererModel fr)
155 for (SequenceFeature feature : mf.features)
157 appendFeature(sb, residuePos, fr, feature, mf);
162 * Appends the feature at rpos to the given buffer
169 void appendFeature(final StringBuilder sb, int rpos,
170 FeatureRendererModel fr, SequenceFeature feature,
173 if (feature.isContactFeature())
175 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
181 sb.append(feature.getType()).append(" ").append(feature.getBegin())
182 .append(":").append(feature.getEnd());
191 // TODO: remove this hack to display link only features
192 boolean linkOnly = feature.getValue("linkonly") != null;
195 sb.append(feature.getType()).append(" ");
198 // we are marking a positional feature
199 sb.append(feature.begin);
201 if (feature.begin != feature.end)
203 sb.append(" ").append(feature.end);
206 String description = feature.getDescription();
207 if (description != null && !description.equals(feature.getType()))
209 description = StringUtils.stripHtmlTags(description);
210 sb.append("; ").append(description);
213 if (showScore(feature, fr))
215 sb.append(" Score=").append(String.valueOf(feature.getScore()));
217 String status = (String) feature.getValue("status");
218 if (status != null && status.length() > 0)
220 sb.append("; (").append(status).append(")");
224 * add attribute value if coloured by attribute
228 FeatureColourI fc = fr.getFeatureColours().get(feature.getType());
229 if (fc != null && fc.isColourByAttribute())
231 String[] attName = fc.getAttributeName();
232 String attVal = feature.getValueAsString(attName);
235 sb.append("; ").append(String.join(":", attName)).append("=")
243 String variants = mf.findProteinVariants(feature);
244 if (!variants.isEmpty())
246 sb.append(" ").append(variants);
253 * Answers true if score should be shown, else false. Score is shown if it is
254 * not NaN, and the feature type has a non-trivial min-max score range
256 boolean showScore(SequenceFeature feature, FeatureRendererModel fr)
258 if (Float.isNaN(feature.getScore()))
266 float[][] minMax = fr.getMinMax().get(feature.getType());
269 * minMax[0] is the [min, max] score range for positional features
271 if (minMax == null || minMax[0] == null || minMax[0][0] == minMax[0][1])
279 * Format and appends any hyperlinks for the sequence feature to the string
285 void appendLinks(final StringBuffer sb, SequenceFeature feature)
287 if (feature.links != null)
289 if (linkImageURL != null)
291 sb.append(" <img src=\"" + linkImageURL + "\">");
295 for (String urlstring : feature.links)
299 for (List<String> urllink : createLinksFrom(null, urlstring))
301 sb.append("<br/> <a href=\""
306 + (urllink.get(0).toLowerCase()
307 .equals(urllink.get(1).toLowerCase()) ? urllink
308 .get(0) : (urllink.get(0) + ":" + urllink
309 .get(1))) + "</a></br>");
311 } catch (Exception x)
313 System.err.println("problem when creating links from "
327 * @return Collection< List<String> > { List<String> { link target, link
328 * label, dynamic component inserted (if any), url }}
330 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
332 Map<String, List<String>> urlSets = new LinkedHashMap<>();
333 UrlLink urlLink = new UrlLink(link);
334 if (!urlLink.isValid())
336 System.err.println(urlLink.getInvalidMessage());
340 urlLink.createLinksFromSeq(seq, urlSets);
342 return urlSets.values();
345 public void createSequenceAnnotationReport(final StringBuilder tip,
346 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
347 FeatureRendererModel fr)
349 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
354 * Builds an html formatted report of sequence details and appends it to the
358 * buffer to append report to
360 * the sequence the report is for
362 * whether to include database references for the sequence
364 * whether to include non-positional sequence features
369 int createSequenceAnnotationReport(final StringBuilder sb,
370 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
371 FeatureRendererModel fr, boolean summary)
377 if (sequence.getDescription() != null)
379 tmp = sequence.getDescription();
380 sb.append("<br>").append(tmp);
381 maxWidth = Math.max(maxWidth, tmp.length());
384 SequenceI ds = sequence;
385 while (ds.getDatasetSequence() != null)
387 ds = ds.getDatasetSequence();
391 * add any annotation scores
393 AlignmentAnnotation[] anns = ds.getAnnotation();
394 for (int i = 0; anns != null && i < anns.length; i++)
396 AlignmentAnnotation aa = anns[i];
397 if (aa != null && aa.hasScore() && aa.sequenceRef != null)
399 sb.append("<br>").append(aa.label).append(": ")
400 .append(aa.getScore());
406 maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
410 * add non-positional features if wanted
414 for (SequenceFeature sf : sequence.getFeatures()
415 .getNonPositionalFeatures())
417 int sz = -sb.length();
418 appendFeature(sb, 0, fr, sf, null);
420 maxWidth = Math.max(maxWidth, sz);
425 if (sequence.getAnnotation("Search Scores") != null)
428 String eValue = " E-Value: "
429 + sequence.getAnnotation("Search Scores")[0].getEValue();
430 String bitScore = " Bit Score: "
431 + sequence.getAnnotation("Search Scores")[0].getBitScore();
435 maxWidth = Math.max(maxWidth, eValue.length());
436 maxWidth = Math.max(maxWidth, bitScore.length());
445 * A helper method that appends any DBRefs, returning the maximum line length
453 protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
456 DBRefEntry[] dbrefs = ds.getDBRefs();
462 // note this sorts the refs held on the sequence!
463 Arrays.sort(dbrefs, comparator);
464 boolean ellipsis = false;
465 String source = null;
466 String lastSource = null;
467 int countForSource = 0;
469 boolean moreSources = false;
470 int maxLineLength = 0;
473 for (DBRefEntry ref : dbrefs)
475 source = ref.getSource();
481 boolean sourceChanged = !source.equals(lastSource);
488 if (sourceCount > MAX_SOURCES && summary)
496 if (countForSource == 1 || !summary)
500 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
502 String accessionId = ref.getAccessionId();
503 lineLength += accessionId.length() + 1;
504 if (countForSource > 1 && summary)
506 sb.append(", ").append(accessionId);
511 sb.append(source).append(" ").append(accessionId);
512 lineLength += source.length();
514 maxLineLength = Math.max(maxLineLength, lineLength);
516 if (countForSource == MAX_REFS_PER_SOURCE && summary)
518 sb.append(COMMA).append(ELLIPSIS);
524 sb.append("<br>").append(source).append(COMMA).append(ELLIPSIS);
529 sb.append(MessageManager.getString("label.output_seq_details"));
533 return maxLineLength;
536 public void createTooltipAnnotationReport(final StringBuilder tip,
537 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
538 FeatureRendererModel fr)
540 int maxWidth = createSequenceAnnotationReport(tip, sequence,
541 showDbRefs, showNpFeats, fr, true);
545 // ? not sure this serves any useful purpose
546 // tip.insert(0, "<table width=350 border=0><tr><td>");
547 // tip.append("</td></tr></table>");