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.DBRefUtils;
29 import jalview.util.MessageManager;
30 import jalview.util.UrlLink;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Comparator;
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)
62 private static Comparator<DBRefEntry> comparator = new Comparator<DBRefEntry>()
66 public int compare(DBRefEntry ref1, DBRefEntry ref2)
68 String s1 = ref1.getSource();
69 String s2 = ref2.getSource();
70 boolean s1Primary = isPrimarySource(s1);
71 boolean s2Primary = isPrimarySource(s2);
72 if (s1Primary && !s2Primary)
76 if (!s1Primary && s2Primary)
80 int comp = s1 == null ? -1 : (s2 == null ? 1 : s1
81 .compareToIgnoreCase(s2));
84 String a1 = ref1.getAccessionId();
85 String a2 = ref2.getAccessionId();
86 comp = a1 == null ? -1 : (a2 == null ? 1 : a1
87 .compareToIgnoreCase(a2));
92 private boolean isPrimarySource(String source)
94 for (String[] primary : PRIMARY_SOURCES)
96 for (String s : primary)
108 public SequenceAnnotationReport(String linkImageURL)
110 this.linkImageURL = linkImageURL;
114 * Append text for the list of features to the tooltip
121 public void appendFeatures(final StringBuilder sb, int rpos,
122 List<SequenceFeature> features, Map<String, float[][]> minmax)
124 if (features != null)
126 for (SequenceFeature feature : features)
128 appendFeature(sb, rpos, minmax, feature);
134 * Appends the feature at rpos to the given buffer
141 void appendFeature(final StringBuilder sb, int rpos,
142 Map<String, float[][]> minmax, SequenceFeature feature)
145 if (feature.getType().equals("disulfide bond"))
147 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
153 sb.append("disulfide bond ").append(feature.getBegin()).append(":")
154 .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 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 : ((float[][]) minmax
227 .get(feature.getType()));
228 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
230 sb.append(" Score=" + 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 (String[] urllink : createLinksFrom(null, urlstring))
271 sb.append("<br/> <a href=\""
276 + (urllink[0].toLowerCase().equals(
277 urllink[1].toLowerCase()) ? urllink[0]
278 : (urllink[0] + ":" + urllink[1]))
281 } catch (Exception x)
283 System.err.println("problem when creating links from "
297 * @return String[][] { String[] { link target, link label, dynamic component
298 * inserted (if any), url }}
300 String[][] createLinksFrom(SequenceI seq, String link)
302 List<String[]> urlSets = new ArrayList<String[]>();
303 List<String> uniques = new ArrayList<String>();
304 UrlLink urlLink = new UrlLink(link);
305 if (!urlLink.isValid())
307 System.err.println(urlLink.getInvalidMessage());
310 final String target = urlLink.getTarget(); // link.substring(0,
311 // link.indexOf("|"));
312 final String label = urlLink.getLabel();
313 if (seq != null && urlLink.isDynamic())
315 urlSets.addAll(createDynamicLinks(seq, urlLink, uniques));
319 String unq = label + "|" + urlLink.getUrl_prefix();
320 if (!uniques.contains(unq))
323 urlSets.add(new String[] { target, label, null,
324 urlLink.getUrl_prefix() });
328 return urlSets.toArray(new String[][] {});
332 * Formats and returns a list of dynamic href links
338 List<String[]> createDynamicLinks(SequenceI seq, UrlLink urlLink,
339 List<String> uniques)
341 List<String[]> result = new ArrayList<String[]>();
342 final String target = urlLink.getTarget();
343 final String label = urlLink.getLabel();
345 // collect matching db-refs
346 DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(),
347 new String[] { target });
348 // collect id string too
349 String id = seq.getName();
350 String descr = seq.getDescription();
351 if (descr != null && descr.length() < 1)
357 for (int r = 0; r < dbr.length; r++)
359 if (id != null && dbr[r].getAccessionId().equals(id))
361 // suppress duplicate link creation for the bare sequence ID
362 // string with this link
365 // create Bare ID link for this URL
366 String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
369 for (int u = 0; u < urls.length; u += 2)
371 String unq = urls[u] + "|" + urls[u + 1];
372 if (!uniques.contains(unq))
374 result.add(new String[] { target, label, urls[u], urls[u + 1] });
383 // create Bare ID link for this URL
384 String[] urls = urlLink.makeUrls(id, true);
387 for (int u = 0; u < urls.length; u += 2)
389 String unq = urls[u] + "|" + urls[u + 1];
390 if (!uniques.contains(unq))
392 result.add(new String[] { target, label, urls[u], urls[u + 1] });
398 if (descr != null && urlLink.getRegexReplace() != null)
400 // create link for this URL from description only if regex matches
401 String[] urls = urlLink.makeUrls(descr, true);
404 for (int u = 0; u < urls.length; u += 2)
406 String unq = urls[u] + "|" + urls[u + 1];
407 if (!uniques.contains(unq))
409 result.add(new String[] { target, label, urls[u], urls[u + 1] });
418 public void createSequenceAnnotationReport(final StringBuilder tip,
419 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
420 Map<String, float[][]> minmax)
422 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
427 * Builds an html formatted report of sequence details and appends it to the
431 * buffer to append report to
433 * the sequence the report is for
435 * whether to include database references for the sequence
437 * whether to include non-positional sequence features
442 int createSequenceAnnotationReport(final StringBuilder sb,
443 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
444 Map<String, float[][]> minmax, boolean summary)
450 if (sequence.getDescription() != null)
452 tmp = sequence.getDescription();
453 sb.append("<br>").append(tmp);
454 maxWidth = Math.max(maxWidth, tmp.length());
456 SequenceI ds = sequence;
457 while (ds.getDatasetSequence() != null)
459 ds = ds.getDatasetSequence();
461 DBRefEntry[] dbrefs = ds.getDBRefs();
462 if (showDbRefs && dbrefs != null)
464 // note this sorts the refs held on the sequence!
465 Arrays.sort(dbrefs, comparator);
466 boolean ellipsis = false;
467 String source = null;
468 String lastSource = null;
469 int countForSource = 0;
471 boolean moreSources = false;
474 for (DBRefEntry ref : dbrefs)
476 source = ref.getSource();
482 boolean sourceChanged = !source.equals(lastSource);
489 if (sourceCount > MAX_SOURCES && summary)
497 if (countForSource == 1 || !summary)
501 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
503 String accessionId = ref.getAccessionId();
504 lineLength += accessionId.length() + 1;
505 if (countForSource > 1 && summary)
507 sb.append(", ").append(accessionId);
512 sb.append(source).append(" ").append(accessionId);
513 lineLength += source.length();
515 maxWidth = Math.max(maxWidth, lineLength);
517 if (countForSource == MAX_REFS_PER_SOURCE && summary)
519 sb.append(COMMA).append(ELLIPSIS);
525 sb.append("<br>").append(ELLIPSIS).append(COMMA).append(source)
526 .append(COMMA).append(ELLIPSIS);
531 sb.append(MessageManager.getString("label.output_seq_details"));
537 * add non-positional features if wanted
539 SequenceFeature[] features = sequence.getSequenceFeatures();
540 if (showNpFeats && features != null)
542 for (int i = 0; i < features.length; i++)
544 if (features[i].begin == 0 && features[i].end == 0)
546 int sz = -sb.length();
547 appendFeature(sb, 0, minmax, features[i]);
549 maxWidth = Math.max(maxWidth, sz);
557 public void createTooltipAnnotationReport(final StringBuilder tip,
558 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
559 Map<String, float[][]> minmax)
561 int maxWidth = createSequenceAnnotationReport(tip, sequence,
562 showDbRefs, showNpFeats, minmax, true);
566 // ? not sure this serves any useful purpose
567 // tip.insert(0, "<table width=350 border=0><tr><td>");
568 // tip.append("</td></tr></table>");