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 java.util.Collection;
24 import java.util.Comparator;
25 import java.util.LinkedHashMap;
26 import java.util.List;
29 import jalview.api.FeatureColourI;
30 import jalview.datamodel.DBRefEntry;
31 import jalview.datamodel.DBRefSource;
32 import jalview.datamodel.GeneLociI;
33 import jalview.datamodel.MappedFeatures;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceI;
36 import jalview.util.MessageManager;
37 import jalview.util.StringUtils;
38 import jalview.util.UrlLink;
39 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
42 * generate HTML reports for a sequence
46 public class SequenceAnnotationReport
48 private static final int MAX_DESCRIPTION_LENGTH = 40;
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 // public static final String[][] PRIMARY_SOURCES moved to DBRefSource.java
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 instanceof GeneLociI)
76 if (ref2 instanceof GeneLociI)
80 String s1 = ref1.getSource();
81 String s2 = ref2.getSource();
82 boolean s1Primary = DBRefSource.isPrimarySource(s1);
83 boolean s2Primary = DBRefSource.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 : DBRefSource.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 Returns number of
127 * features left if maxlength limit is (or would have been) reached
135 public int appendFeaturesLengthLimit(final StringBuilder sb,
136 int residuePos, List<SequenceFeature> features,
137 FeatureRendererModel fr, int maxlength)
139 for (int i = 0; i < features.size(); i++)
141 SequenceFeature feature = features.get(i);
142 if (appendFeature(sb, residuePos, fr, feature, null, maxlength))
144 return features.size() - i;
150 public void appendFeatures(final StringBuilder sb, int residuePos,
151 List<SequenceFeature> features, FeatureRendererModel fr)
153 appendFeaturesLengthLimit(sb, residuePos, features, fr, 0);
157 * Appends text for mapped features (e.g. CDS feature for peptide or vice versa)
158 * Returns number of features left if maxlength limit is (or would have been)
167 public int appendFeaturesLengthLimit(StringBuilder sb, int residuePos,
168 MappedFeatures mf, FeatureRendererModel fr, int maxlength)
170 for (int i = 0; i < mf.features.size(); i++)
172 SequenceFeature feature = mf.features.get(i);
173 if (appendFeature(sb, residuePos, fr, feature, mf, maxlength))
175 return mf.features.size() - i;
181 public void appendFeatures(StringBuilder sb, int residuePos,
182 MappedFeatures mf, FeatureRendererModel fr)
184 appendFeaturesLengthLimit(sb, residuePos, mf, fr, 0);
188 * Appends the feature at rpos to the given buffer
195 boolean appendFeature(final StringBuilder sb0, int rpos,
196 FeatureRendererModel fr, SequenceFeature feature,
197 MappedFeatures mf, int maxlength)
199 StringBuilder sb = new StringBuilder();
200 if (feature.isContactFeature())
202 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
204 if (sb0.length() > 6)
208 sb.append(feature.getType()).append(" ").append(feature.getBegin())
209 .append(":").append(feature.getEnd());
211 return appendTextMaxLengthReached(sb0, sb, maxlength);
214 if (sb0.length() > 6)
218 // TODO: remove this hack to display link only features
219 boolean linkOnly = feature.getValue("linkonly") != null;
222 sb.append(feature.getType()).append(" ");
225 // we are marking a positional feature
226 sb.append(feature.begin);
228 if (feature.begin != feature.end)
230 sb.append(" ").append(feature.end);
233 String description = feature.getDescription();
234 if (description != null && !description.equals(feature.getType()))
236 description = StringUtils.stripHtmlTags(description);
239 * truncate overlong descriptions unless they contain an href
240 * before the truncation point (as truncation could leave corrupted html)
242 int linkindex = description.toLowerCase().indexOf("<a ");
243 boolean hasLink = linkindex > -1
244 && linkindex < MAX_DESCRIPTION_LENGTH;
245 if (description.length() > MAX_DESCRIPTION_LENGTH && !hasLink)
247 description = description.substring(0, MAX_DESCRIPTION_LENGTH)
251 sb.append("; ").append(description);
254 if (showScore(feature, fr))
256 sb.append(" Score=").append(String.valueOf(feature.getScore()));
258 String status = (String) feature.getValue("status");
259 if (status != null && status.length() > 0)
261 sb.append("; (").append(status).append(")");
265 * add attribute value if coloured by attribute
269 FeatureColourI fc = fr.getFeatureColours().get(feature.getType());
270 if (fc != null && fc.isColourByAttribute())
272 String[] attName = fc.getAttributeName();
273 String attVal = feature.getValueAsString(attName);
276 sb.append("; ").append(String.join(":", attName)).append("=")
284 String variants = mf.findProteinVariants(feature);
285 if (!variants.isEmpty())
287 sb.append(" ").append(variants);
291 return appendTextMaxLengthReached(sb0, sb, maxlength);
294 void appendFeature(final StringBuilder sb, int rpos,
295 FeatureRendererModel fr, SequenceFeature feature,
298 appendFeature(sb, rpos, fr, feature, mf, 0);
302 * Appends {@code sb} to {@code sb0}, and returns false, unless
303 * {@code maxlength} is not zero and appending would make the total length
304 * greater than {@code maxlength}, in which case the text is not appended, and
305 * the method returns true.
312 private static boolean appendTextMaxLengthReached(StringBuilder sb0,
313 StringBuilder sb, int maxlength)
315 if (maxlength == 0 || sb0.length() + sb.length() < maxlength)
324 * Answers true if score should be shown, else false. Score is shown if it is
325 * not NaN, and the feature type has a non-trivial min-max score range
327 boolean showScore(SequenceFeature feature, FeatureRendererModel fr)
329 if (Float.isNaN(feature.getScore()))
337 float[][] minMax = fr.getMinMax().get(feature.getType());
340 * minMax[0] is the [min, max] score range for positional features
342 if (minMax == null || minMax[0] == null || minMax[0][0] == minMax[0][1])
350 * Format and appends any hyperlinks for the sequence feature to the string
356 void appendLinks(final StringBuffer sb, SequenceFeature feature)
358 if (feature.links != null)
360 if (linkImageURL != null)
362 sb.append(" <img src=\"" + linkImageURL + "\">");
366 for (String urlstring : feature.links)
370 for (List<String> urllink : createLinksFrom(null, urlstring))
372 sb.append("<br/> <a href=\""
377 + (urllink.get(0).toLowerCase()
378 .equals(urllink.get(1).toLowerCase()) ? urllink
379 .get(0) : (urllink.get(0) + ":" + urllink
383 } catch (Exception x)
385 System.err.println("problem when creating links from "
399 * @return Collection< List<String> > { List<String> { link target, link
400 * label, dynamic component inserted (if any), url }}
402 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
404 Map<String, List<String>> urlSets = new LinkedHashMap<>();
405 UrlLink urlLink = new UrlLink(link);
406 if (!urlLink.isValid())
408 System.err.println(urlLink.getInvalidMessage());
412 urlLink.createLinksFromSeq(seq, urlSets);
414 return urlSets.values();
417 public void createSequenceAnnotationReport(final StringBuilder tip,
418 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
419 FeatureRendererModel fr)
421 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
426 * Builds an html formatted report of sequence details and appends it to the
430 * buffer to append report to
432 * the sequence the report is for
434 * whether to include database references for the sequence
436 * whether to include non-positional sequence features
441 int createSequenceAnnotationReport(final StringBuilder sb,
442 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
443 FeatureRendererModel fr, boolean summary)
449 if (sequence.getDescription() != null)
451 tmp = sequence.getDescription();
453 maxWidth = Math.max(maxWidth, tmp.length());
455 SequenceI ds = sequence;
456 while (ds.getDatasetSequence() != null)
458 ds = ds.getDatasetSequence();
463 maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
467 * add non-positional features if wanted
471 for (SequenceFeature sf : sequence.getFeatures()
472 .getNonPositionalFeatures())
474 int sz = -sb.length();
475 appendFeature(sb, 0, fr, sf, null);
477 maxWidth = Math.max(maxWidth, sz);
485 * A helper method that appends any DBRefs, returning the maximum line length
493 protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
496 List<DBRefEntry> dbrefs = ds.getDBRefs();
502 // note this sorts the refs held on the sequence!
503 dbrefs.sort(comparator);
504 boolean ellipsis = false;
505 String source = null;
506 String lastSource = null;
507 int countForSource = 0;
509 boolean moreSources = false;
510 int maxLineLength = 0;
513 for (DBRefEntry ref : dbrefs)
515 source = ref.getSource();
521 boolean sourceChanged = !source.equals(lastSource);
528 if (sourceCount > MAX_SOURCES && summary)
536 if (countForSource == 1 || !summary)
540 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
542 String accessionId = ref.getAccessionId();
543 lineLength += accessionId.length() + 1;
544 if (countForSource > 1 && summary)
546 sb.append(", ").append(accessionId);
551 sb.append(source).append(" ").append(accessionId);
552 lineLength += source.length();
554 maxLineLength = Math.max(maxLineLength, lineLength);
556 if (countForSource == MAX_REFS_PER_SOURCE && summary)
558 sb.append(COMMA).append(ELLIPSIS);
564 sb.append("<br/>").append(source).append(COMMA).append(ELLIPSIS);
569 sb.append(MessageManager.getString("label.output_seq_details"));
573 return maxLineLength;
576 public void createTooltipAnnotationReport(final StringBuilder tip,
577 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
578 FeatureRendererModel fr)
580 int maxWidth = createSequenceAnnotationReport(tip, sequence,
581 showDbRefs, showNpFeats, fr, true);
585 // ? not sure this serves any useful purpose
586 // tip.insert(0, "<table width=350 border=0><tr><td>");
587 // tip.append("</td></tr></table>");