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.datamodel.DBRefEntry;
24 import jalview.datamodel.DBRefSource;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.gff.GffConstants;
28 import jalview.util.MessageManager;
29 import jalview.util.UrlLink;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Comparator;
34 import java.util.LinkedHashMap;
35 import java.util.List;
39 * generate HTML reports for a sequence
43 public class SequenceAnnotationReport
45 private static final String COMMA = ",";
47 private static final String ELLIPSIS = "...";
49 private static final int MAX_REFS_PER_SOURCE = 4;
51 private static final int MAX_SOURCES = 40;
53 private static final String[][] PRIMARY_SOURCES = new String[][] {
54 DBRefSource.CODINGDBS, DBRefSource.DNACODINGDBS,
55 DBRefSource.PROTEINDBS };
57 final String linkImageURL;
60 * Comparator to order DBRefEntry by Source + accession id (case-insensitive),
61 * with 'Primary' sources placed before others
63 private static Comparator<DBRefEntry> comparator = new Comparator<DBRefEntry>()
67 public int compare(DBRefEntry ref1, DBRefEntry ref2)
69 String s1 = ref1.getSource();
70 String s2 = ref2.getSource();
71 boolean s1Primary = isPrimarySource(s1);
72 boolean s2Primary = isPrimarySource(s2);
73 if (s1Primary && !s2Primary)
77 if (!s1Primary && s2Primary)
81 int comp = s1 == null ? -1
82 : (s2 == null ? 1 : s1.compareToIgnoreCase(s2));
85 String a1 = ref1.getAccessionId();
86 String a2 = ref2.getAccessionId();
87 comp = a1 == null ? -1
88 : (a2 == null ? 1 : a1.compareToIgnoreCase(a2));
93 private boolean isPrimarySource(String source)
95 for (String[] primary : PRIMARY_SOURCES)
97 for (String s : primary)
109 public SequenceAnnotationReport(String linkImageURL)
111 this.linkImageURL = linkImageURL;
115 * Append text for the list of features to the tooltip
122 public void appendFeatures(final StringBuilder sb, int rpos,
123 List<SequenceFeature> features, Map<String, float[][]> minmax)
125 if (features != null)
127 for (SequenceFeature feature : features)
129 appendFeature(sb, rpos, minmax, feature);
135 * Appends the feature at rpos to the given buffer
142 void appendFeature(final StringBuilder sb, int rpos,
143 Map<String, float[][]> minmax, SequenceFeature feature)
145 if (feature.isContactFeature())
147 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
153 sb.append(feature.getType()).append(" ").append(feature.getBegin())
154 .append(":").append(feature.getEnd());
163 // TODO: remove this hack to display link only features
164 boolean linkOnly = feature.getValue("linkonly") != null;
167 sb.append(feature.getType()).append(" ");
170 // we are marking a positional feature
171 sb.append(feature.begin);
173 if (feature.begin != feature.end)
175 sb.append(" ").append(feature.end);
178 if (feature.getDescription() != null
179 && !feature.description.equals(feature.getType()))
181 String tmpString = feature.getDescription();
182 String tmp2up = tmpString.toUpperCase();
183 int startTag = tmp2up.indexOf("<HTML>");
186 tmpString = tmpString.substring(startTag + 6);
187 tmp2up = tmp2up.substring(startTag + 6);
189 int endTag = tmp2up.indexOf("</BODY>");
192 tmpString = tmpString.substring(0, endTag);
193 tmp2up = tmp2up.substring(0, endTag);
195 endTag = tmp2up.indexOf("</HTML>");
198 tmpString = tmpString.substring(0, endTag);
203 sb.append("; ").append(tmpString);
207 if (tmpString.indexOf("<") > -1 || tmpString.indexOf(">") > -1)
209 // The description does not specify html is to
210 // be used, so we must remove < > symbols
211 tmpString = tmpString.replaceAll("<", "<");
212 tmpString = tmpString.replaceAll(">", ">");
215 sb.append(tmpString);
219 sb.append("; ").append(tmpString);
223 // check score should be shown
224 if (!Float.isNaN(feature.getScore()))
226 float[][] rng = (minmax == null) ? null
227 : minmax.get(feature.getType());
228 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
230 sb.append(" Score=").append(String.valueOf(feature.getScore()));
233 String status = (String) feature.getValue("status");
234 if (status != null && status.length() > 0)
236 sb.append("; (").append(status).append(")");
238 String clinSig = (String) feature
239 .getValue(GffConstants.CLINICAL_SIGNIFICANCE);
242 sb.append("; ").append(clinSig);
249 * Format and appends any hyperlinks for the sequence feature to the string
255 void appendLinks(final StringBuffer sb, SequenceFeature feature)
257 if (feature.links != null)
259 if (linkImageURL != null)
261 sb.append(" <img src=\"" + linkImageURL + "\">");
265 for (String urlstring : feature.links)
269 for (List<String> urllink : createLinksFrom(null, urlstring))
271 sb.append("<br/> <a href=\"" + urllink.get(3) + "\" target=\""
272 + urllink.get(0) + "\">"
273 + (urllink.get(0).toLowerCase()
274 .equals(urllink.get(1).toLowerCase())
276 : (urllink.get(0) + ":"
280 } catch (Exception x)
283 "problem when creating links from " + urlstring);
296 * @return Collection< List<String> > { List<String> { link target, link
297 * label, dynamic component inserted (if any), url }}
299 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
301 Map<String, List<String>> urlSets = new LinkedHashMap<String, List<String>>();
302 UrlLink urlLink = new UrlLink(link);
303 if (!urlLink.isValid())
305 System.err.println(urlLink.getInvalidMessage());
309 urlLink.createLinksFromSeq(seq, urlSets);
311 return urlSets.values();
314 public void createSequenceAnnotationReport(final StringBuilder tip,
315 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
316 Map<String, float[][]> minmax)
318 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
323 * Builds an html formatted report of sequence details and appends it to the
327 * buffer to append report to
329 * the sequence the report is for
331 * whether to include database references for the sequence
333 * whether to include non-positional sequence features
338 int createSequenceAnnotationReport(final StringBuilder sb,
339 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
340 Map<String, float[][]> minmax, boolean summary)
346 if (sequence.getDescription() != null)
348 tmp = sequence.getDescription();
349 sb.append("<br>").append(tmp);
350 maxWidth = Math.max(maxWidth, tmp.length());
352 SequenceI ds = sequence;
353 while (ds.getDatasetSequence() != null)
355 ds = ds.getDatasetSequence();
360 maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
364 * add non-positional features if wanted
368 for (SequenceFeature sf : sequence.getFeatures()
369 .getNonPositionalFeatures())
371 int sz = -sb.length();
372 appendFeature(sb, 0, minmax, sf);
374 maxWidth = Math.max(maxWidth, sz);
382 * A helper method that appends any DBRefs, returning the maximum line length
390 protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
393 DBRefEntry[] dbrefs = ds.getDBRefs();
399 // note this sorts the refs held on the sequence!
400 Arrays.sort(dbrefs, comparator);
401 boolean ellipsis = false;
402 String source = null;
403 String lastSource = null;
404 int countForSource = 0;
406 boolean moreSources = false;
407 int maxLineLength = 0;
410 for (DBRefEntry ref : dbrefs)
412 source = ref.getSource();
418 boolean sourceChanged = !source.equals(lastSource);
425 if (sourceCount > MAX_SOURCES && summary)
433 if (countForSource == 1 || !summary)
437 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
439 String accessionId = ref.getAccessionId();
440 lineLength += accessionId.length() + 1;
441 if (countForSource > 1 && summary)
443 sb.append(", ").append(accessionId);
448 sb.append(source).append(" ").append(accessionId);
449 lineLength += source.length();
451 maxLineLength = Math.max(maxLineLength, lineLength);
453 if (countForSource == MAX_REFS_PER_SOURCE && summary)
455 sb.append(COMMA).append(ELLIPSIS);
461 sb.append("<br>").append(source)
462 .append(COMMA).append(ELLIPSIS);
467 sb.append(MessageManager.getString("label.output_seq_details"));
471 return maxLineLength;
474 public void createTooltipAnnotationReport(final StringBuilder tip,
475 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
476 Map<String, float[][]> minmax)
478 int maxWidth = createSequenceAnnotationReport(tip, sequence, showDbRefs,
479 showNpFeats, minmax, true);
483 // ? not sure this serves any useful purpose
484 // tip.insert(0, "<table width=350 border=0><tr><td>");
485 // tip.append("</td></tr></table>");