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<DBRefEntry>()
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 Returns number of
130 * features left if maxlength limit is (or would have been) reached
138 public int appendFeaturesLengthLimit(final StringBuilder sb,
139 int residuePos, List<SequenceFeature> features,
140 FeatureRendererModel fr, int maxlength)
142 for (int i = 0; i < features.size(); i++)
144 SequenceFeature feature = features.get(i);
145 if (appendFeature(sb, residuePos, fr, feature, null, maxlength))
147 return features.size() - i;
153 public void appendFeatures(final StringBuilder sb, int residuePos,
154 List<SequenceFeature> features, FeatureRendererModel fr)
156 appendFeaturesLengthLimit(sb, residuePos, features, fr, 0);
160 * Appends text for mapped features (e.g. CDS feature for peptide or vice versa)
161 * Returns number of features left if maxlength limit is (or would have been)
170 public int appendFeaturesLengthLimit(StringBuilder sb, int residuePos,
171 MappedFeatures mf, FeatureRendererModel fr, int maxlength)
173 for (int i = 0; i < mf.features.size(); i++)
175 SequenceFeature feature = mf.features.get(i);
176 if (appendFeature(sb, residuePos, fr, feature, mf, maxlength))
178 return mf.features.size() - i;
184 public void appendFeatures(StringBuilder sb, int residuePos,
185 MappedFeatures mf, FeatureRendererModel fr)
187 appendFeaturesLengthLimit(sb, residuePos, mf, fr, 0);
191 * Appends the feature at rpos to the given buffer
198 boolean appendFeature(final StringBuilder sb0, int rpos,
199 FeatureRendererModel fr, SequenceFeature feature,
200 MappedFeatures mf, int maxlength)
202 StringBuilder sb = new StringBuilder();
203 if (feature.isContactFeature())
205 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
207 if (sb0.length() > 6)
211 sb.append(feature.getType()).append(" ").append(feature.getBegin())
212 .append(":").append(feature.getEnd());
214 return appendTextMaxLengthReached(sb0, sb, maxlength);
217 if (sb0.length() > 6)
221 // TODO: remove this hack to display link only features
222 boolean linkOnly = feature.getValue("linkonly") != null;
225 sb.append(feature.getType()).append(" ");
228 // we are marking a positional feature
229 sb.append(feature.begin);
231 if (feature.begin != feature.end)
233 sb.append(" ").append(feature.end);
236 String description = feature.getDescription();
237 if (description != null && !description.equals(feature.getType()))
239 description = StringUtils.stripHtmlTags(description);
242 * truncate overlong descriptions unless they contain an href
243 * before the truncation point (as truncation could leave corrupted html)
245 int linkindex = description.toLowerCase().indexOf("<a ");
246 boolean hasLink = linkindex > -1
247 && linkindex < MAX_DESCRIPTION_LENGTH;
248 if (description.length() > MAX_DESCRIPTION_LENGTH && !hasLink)
250 description = description.substring(0, MAX_DESCRIPTION_LENGTH)
254 sb.append("; ").append(description);
257 if (showScore(feature, fr))
259 sb.append(" Score=").append(String.valueOf(feature.getScore()));
261 String status = (String) feature.getValue("status");
262 if (status != null && status.length() > 0)
264 sb.append("; (").append(status).append(")");
268 * add attribute value if coloured by attribute
272 FeatureColourI fc = fr.getFeatureColours().get(feature.getType());
273 if (fc != null && fc.isColourByAttribute())
275 String[] attName = fc.getAttributeName();
276 String attVal = feature.getValueAsString(attName);
279 sb.append("; ").append(String.join(":", attName)).append("=")
287 String variants = mf.findProteinVariants(feature);
288 if (!variants.isEmpty())
290 sb.append(" ").append(variants);
294 return appendTextMaxLengthReached(sb0, sb, maxlength);
297 void appendFeature(final StringBuilder sb, int rpos,
298 FeatureRendererModel fr, SequenceFeature feature,
301 appendFeature(sb, rpos, fr, feature, mf, 0);
304 private static boolean appendTextMaxLengthReached(StringBuilder sb0,
305 StringBuilder sb, int maxlength)
308 if (maxlength == 0 || sb0.length() + sb.length() < maxlength)
318 * Answers true if score should be shown, else false. Score is shown if it is
319 * not NaN, and the feature type has a non-trivial min-max score range
321 boolean showScore(SequenceFeature feature, FeatureRendererModel fr)
323 if (Float.isNaN(feature.getScore()))
331 float[][] minMax = fr.getMinMax().get(feature.getType());
334 * minMax[0] is the [min, max] score range for positional features
336 if (minMax == null || minMax[0] == null || minMax[0][0] == minMax[0][1])
344 * Format and appends any hyperlinks for the sequence feature to the string
350 void appendLinks(final StringBuffer sb, SequenceFeature feature)
352 if (feature.links != null)
354 if (linkImageURL != null)
356 sb.append(" <img src=\"" + linkImageURL + "\">");
360 for (String urlstring : feature.links)
364 for (List<String> urllink : createLinksFrom(null, urlstring))
366 sb.append("<br/> <a href=\""
371 + (urllink.get(0).toLowerCase()
372 .equals(urllink.get(1).toLowerCase()) ? urllink
373 .get(0) : (urllink.get(0) + ":" + urllink
377 } catch (Exception x)
379 System.err.println("problem when creating links from "
393 * @return Collection< List<String> > { List<String> { link target, link
394 * label, dynamic component inserted (if any), url }}
396 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
398 Map<String, List<String>> urlSets = new LinkedHashMap<>();
399 UrlLink urlLink = new UrlLink(link);
400 if (!urlLink.isValid())
402 System.err.println(urlLink.getInvalidMessage());
406 urlLink.createLinksFromSeq(seq, urlSets);
408 return urlSets.values();
411 public void createSequenceAnnotationReport(final StringBuilder tip,
412 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
413 FeatureRendererModel fr)
415 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
420 * Builds an html formatted report of sequence details and appends it to the
424 * buffer to append report to
426 * the sequence the report is for
428 * whether to include database references for the sequence
430 * whether to include non-positional sequence features
435 int createSequenceAnnotationReport(final StringBuilder sb,
436 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
437 FeatureRendererModel fr, boolean summary)
443 if (sequence.getDescription() != null)
445 tmp = sequence.getDescription();
447 maxWidth = Math.max(maxWidth, tmp.length());
449 SequenceI ds = sequence;
450 while (ds.getDatasetSequence() != null)
452 ds = ds.getDatasetSequence();
457 maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
461 * add non-positional features if wanted
465 for (SequenceFeature sf : sequence.getFeatures()
466 .getNonPositionalFeatures())
468 int sz = -sb.length();
469 appendFeature(sb, 0, fr, sf, null);
471 maxWidth = Math.max(maxWidth, sz);
479 * A helper method that appends any DBRefs, returning the maximum line length
487 protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
490 DBRefEntry[] dbrefs = ds.getDBRefs();
496 // note this sorts the refs held on the sequence!
497 Arrays.sort(dbrefs, comparator);
498 boolean ellipsis = false;
499 String source = null;
500 String lastSource = null;
501 int countForSource = 0;
503 boolean moreSources = false;
504 int maxLineLength = 0;
507 for (DBRefEntry ref : dbrefs)
509 source = ref.getSource();
515 boolean sourceChanged = !source.equals(lastSource);
522 if (sourceCount > MAX_SOURCES && summary)
530 if (countForSource == 1 || !summary)
534 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
536 String accessionId = ref.getAccessionId();
537 lineLength += accessionId.length() + 1;
538 if (countForSource > 1 && summary)
540 sb.append(", ").append(accessionId);
545 sb.append(source).append(" ").append(accessionId);
546 lineLength += source.length();
548 maxLineLength = Math.max(maxLineLength, lineLength);
550 if (countForSource == MAX_REFS_PER_SOURCE && summary)
552 sb.append(COMMA).append(ELLIPSIS);
558 sb.append("<br/>").append(source).append(COMMA).append(ELLIPSIS);
563 sb.append(MessageManager.getString("label.output_seq_details"));
567 return maxLineLength;
570 public void createTooltipAnnotationReport(final StringBuilder tip,
571 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
572 FeatureRendererModel fr)
574 int maxWidth = createSequenceAnnotationReport(tip, sequence,
575 showDbRefs, showNpFeats, fr, true);
579 // ? not sure this serves any useful purpose
580 // tip.insert(0, "<table width=350 border=0><tr><td>");
581 // tip.append("</td></tr></table>");