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.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.MessageManager;
29 import jalview.util.StringUtils;
30 import jalview.util.UrlLink;
31 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Comparator;
36 import java.util.LinkedHashMap;
37 import java.util.List;
41 * generate HTML reports for a sequence
45 public class SequenceAnnotationReport
47 private static final String COMMA = ",";
49 private static final String ELLIPSIS = "...";
51 private static final int MAX_REFS_PER_SOURCE = 4;
53 private static final int MAX_SOURCES = 40;
55 // public static final String[][] PRIMARY_SOURCES moved to DBRefSource.java
57 final String linkImageURL;
60 * Comparator to order DBRefEntry by Source + accession id (case-insensitive),
61 * with 'Primary' sources placed before others, and 'chromosome' first of all
63 private static Comparator<DBRefEntry> comparator = new Comparator<DBRefEntry>()
67 public int compare(DBRefEntry ref1, DBRefEntry ref2)
69 if (ref1.isChromosome())
73 if (ref2.isChromosome())
77 String s1 = ref1.getSource();
78 String s2 = ref2.getSource();
79 boolean s1Primary = DBRefSource.isPrimarySource(s1);
80 boolean s2Primary = DBRefSource.isPrimarySource(s2);
81 if (s1Primary && !s2Primary)
85 if (!s1Primary && s2Primary)
89 int comp = s1 == null ? -1 : (s2 == null ? 1 : s1
90 .compareToIgnoreCase(s2));
93 String a1 = ref1.getAccessionId();
94 String a2 = ref2.getAccessionId();
95 comp = a1 == null ? -1 : (a2 == null ? 1 : a1
96 .compareToIgnoreCase(a2));
101 // private boolean isPrimarySource(String source)
103 // for (String[] primary : DBRefSource.PRIMARY_SOURCES)
105 // for (String s : primary)
107 // if (source.equals(s))
117 public SequenceAnnotationReport(String linkURL)
119 this.linkImageURL = linkURL;
123 * Append text for the list of features to the tooltip
130 public void appendFeatures(final StringBuilder sb, int rpos,
131 List<SequenceFeature> features, FeatureRendererModel fr)
133 if (features != null)
135 for (SequenceFeature feature : features)
137 appendFeature(sb, rpos, fr, feature);
143 * Appends the feature at rpos to the given buffer
150 void appendFeature(final StringBuilder sb, int rpos,
151 FeatureRendererModel fr, SequenceFeature feature)
153 if (feature.isContactFeature())
155 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
161 sb.append(feature.getType()).append(" ").append(feature.getBegin())
162 .append(":").append(feature.getEnd());
171 // TODO: remove this hack to display link only features
172 boolean linkOnly = feature.getValue("linkonly") != null;
175 sb.append(feature.getType()).append(" ");
178 // we are marking a positional feature
179 sb.append(feature.begin);
181 if (feature.begin != feature.end)
183 sb.append(" ").append(feature.end);
186 String description = feature.getDescription();
187 if (description != null && !description.equals(feature.getType()))
189 description = StringUtils.stripHtmlTags(description);
190 sb.append("; ").append(description);
193 if (showScore(feature, fr))
195 sb.append(" Score=").append(String.valueOf(feature.getScore()));
197 String status = (String) feature.getValue("status");
198 if (status != null && status.length() > 0)
200 sb.append("; (").append(status).append(")");
204 * add attribute value if coloured by attribute
208 FeatureColourI fc = fr.getFeatureColours().get(feature.getType());
209 if (fc != null && fc.isColourByAttribute())
211 String[] attName = fc.getAttributeName();
212 String attVal = feature.getValueAsString(attName);
215 sb.append("; ").append(String.join(":", attName)).append("=")
224 * Answers true if score should be shown, else false. Score is shown if it is
225 * not NaN, and the feature type has a non-trivial min-max score range
227 boolean showScore(SequenceFeature feature, FeatureRendererModel fr)
229 if (Float.isNaN(feature.getScore()))
237 float[][] minMax = fr.getMinMax().get(feature.getType());
240 * minMax[0] is the [min, max] score range for positional features
242 if (minMax == null || minMax[0] == null || minMax[0][0] == minMax[0][1])
250 * Format and appends any hyperlinks for the sequence feature to the string
256 void appendLinks(final StringBuffer sb, SequenceFeature feature)
258 if (feature.links != null)
260 if (linkImageURL != null)
262 sb.append(" <img src=\"" + linkImageURL + "\">");
266 for (String urlstring : feature.links)
270 for (List<String> urllink : createLinksFrom(null, urlstring))
272 sb.append("<br/> <a href=\""
277 + (urllink.get(0).toLowerCase()
278 .equals(urllink.get(1).toLowerCase()) ? urllink
279 .get(0) : (urllink.get(0) + ":" + urllink
280 .get(1))) + "</a></br>");
282 } catch (Exception x)
284 System.err.println("problem when creating links from "
298 * @return Collection< List<String> > { List<String> { link target, link
299 * label, dynamic component inserted (if any), url }}
301 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
303 Map<String, List<String>> urlSets = new LinkedHashMap<>();
304 UrlLink urlLink = new UrlLink(link);
305 if (!urlLink.isValid())
307 System.err.println(urlLink.getInvalidMessage());
311 urlLink.createLinksFromSeq(seq, urlSets);
313 return urlSets.values();
316 public void createSequenceAnnotationReport(final StringBuilder tip,
317 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
318 FeatureRendererModel fr)
320 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
325 * Builds an html formatted report of sequence details and appends it to the
329 * buffer to append report to
331 * the sequence the report is for
333 * whether to include database references for the sequence
335 * whether to include non-positional sequence features
340 int createSequenceAnnotationReport(final StringBuilder sb,
341 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
342 FeatureRendererModel fr, boolean summary)
348 if (sequence.getDescription() != null)
350 tmp = sequence.getDescription();
351 sb.append("<br>").append(tmp);
352 maxWidth = Math.max(maxWidth, tmp.length());
354 SequenceI ds = sequence;
355 while (ds.getDatasetSequence() != null)
357 ds = ds.getDatasetSequence();
362 maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
366 * add non-positional features if wanted
370 for (SequenceFeature sf : sequence.getFeatures()
371 .getNonPositionalFeatures())
373 int sz = -sb.length();
374 appendFeature(sb, 0, fr, sf);
376 maxWidth = Math.max(maxWidth, sz);
384 * A helper method that appends any DBRefs, returning the maximum line length
392 protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
395 List<DBRefEntry> dbrefs = ds.getDBRefs();
401 // note this sorts the refs held on the sequence!
402 dbrefs.sort(comparator);
403 boolean ellipsis = false;
404 String source = null;
405 String lastSource = null;
406 int countForSource = 0;
408 boolean moreSources = false;
409 int maxLineLength = 0;
412 for (DBRefEntry ref : dbrefs)
414 source = ref.getSource();
420 boolean sourceChanged = !source.equals(lastSource);
427 if (sourceCount > MAX_SOURCES && summary)
435 if (countForSource == 1 || !summary)
439 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
441 String accessionId = ref.getAccessionId();
442 lineLength += accessionId.length() + 1;
443 if (countForSource > 1 && summary)
445 sb.append(", ").append(accessionId);
450 sb.append(source).append(" ").append(accessionId);
451 lineLength += source.length();
453 maxLineLength = Math.max(maxLineLength, lineLength);
455 if (countForSource == MAX_REFS_PER_SOURCE && summary)
457 sb.append(COMMA).append(ELLIPSIS);
463 sb.append("<br>").append(source).append(COMMA).append(ELLIPSIS);
468 sb.append(MessageManager.getString("label.output_seq_details"));
472 return maxLineLength;
475 public void createTooltipAnnotationReport(final StringBuilder tip,
476 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
477 FeatureRendererModel fr)
479 int maxWidth = createSequenceAnnotationReport(tip, sequence,
480 showDbRefs, showNpFeats, fr, true);
484 // ? not sure this serves any useful purpose
485 // tip.insert(0, "<table width=350 border=0><tr><td>");
486 // tip.append("</td></tr></table>");