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, 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 = isPrimarySource(s1);
80 boolean s2Primary = isPrimarySource(s2);
81 if (s1Primary && !s2Primary)
85 if (!s1Primary && s2Primary)
89 int comp = s1 == null ? -1
90 : (s2 == null ? 1 : s1.compareToIgnoreCase(s2));
93 String a1 = ref1.getAccessionId();
94 String a2 = ref2.getAccessionId();
95 comp = a1 == null ? -1
96 : (a2 == null ? 1 : a1.compareToIgnoreCase(a2));
101 private boolean isPrimarySource(String source)
103 for (String[] primary : PRIMARY_SOURCES)
105 for (String s : primary)
107 if (source.equals(s))
117 public SequenceAnnotationReport(String linkImageURL)
119 this.linkImageURL = linkImageURL;
123 * Append text for the list of features to the tooltip
130 public void appendFeatures(final StringBuilder sb, int rpos,
131 List<SequenceFeature> features, Map<String, float[][]> minmax)
133 if (features != null)
135 for (SequenceFeature feature : features)
137 appendFeature(sb, rpos, minmax, feature);
143 * Appends the feature at rpos to the given buffer
150 void appendFeature(final StringBuilder sb, int rpos,
151 Map<String, float[][]> minmax, 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 if (feature.getDescription() != null
187 && !feature.description.equals(feature.getType()))
189 String tmpString = feature.getDescription();
190 String tmp2up = tmpString.toUpperCase();
191 int startTag = tmp2up.indexOf("<HTML>");
194 tmpString = tmpString.substring(startTag + 6);
195 tmp2up = tmp2up.substring(startTag + 6);
197 int endTag = tmp2up.indexOf("</BODY>");
200 tmpString = tmpString.substring(0, endTag);
201 tmp2up = tmp2up.substring(0, endTag);
203 endTag = tmp2up.indexOf("</HTML>");
206 tmpString = tmpString.substring(0, endTag);
211 sb.append("; ").append(tmpString);
215 if (tmpString.indexOf("<") > -1 || tmpString.indexOf(">") > -1)
217 // The description does not specify html is to
218 // be used, so we must remove < > symbols
219 tmpString = tmpString.replaceAll("<", "<");
220 tmpString = tmpString.replaceAll(">", ">");
223 sb.append(tmpString);
227 sb.append("; ").append(tmpString);
231 // check score should be shown
232 if (!Float.isNaN(feature.getScore()))
234 float[][] rng = (minmax == null) ? null
235 : minmax.get(feature.getType());
236 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
238 sb.append(" Score=").append(String.valueOf(feature.getScore()));
241 String status = (String) feature.getValue("status");
242 if (status != null && status.length() > 0)
244 sb.append("; (").append(status).append(")");
246 String clinSig = (String) feature
247 .getValue(GffConstants.CLINICAL_SIGNIFICANCE);
250 sb.append("; ").append(clinSig);
257 * Format and appends any hyperlinks for the sequence feature to the string
263 void appendLinks(final StringBuffer sb, SequenceFeature feature)
265 if (feature.links != null)
267 if (linkImageURL != null)
269 sb.append(" <img src=\"" + linkImageURL + "\">");
273 for (String urlstring : feature.links)
277 for (List<String> urllink : createLinksFrom(null, urlstring))
279 sb.append("<br/> <a href=\"" + urllink.get(3) + "\" target=\""
280 + urllink.get(0) + "\">"
281 + (urllink.get(0).toLowerCase()
282 .equals(urllink.get(1).toLowerCase())
284 : (urllink.get(0) + ":"
288 } catch (Exception x)
291 "problem when creating links from " + urlstring);
304 * @return Collection< List<String> > { List<String> { link target, link
305 * label, dynamic component inserted (if any), url }}
307 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
309 Map<String, List<String>> urlSets = new LinkedHashMap<String, List<String>>();
310 UrlLink urlLink = new UrlLink(link);
311 if (!urlLink.isValid())
313 System.err.println(urlLink.getInvalidMessage());
317 urlLink.createLinksFromSeq(seq, urlSets);
319 return urlSets.values();
322 public void createSequenceAnnotationReport(final StringBuilder tip,
323 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
324 Map<String, float[][]> minmax)
326 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
331 * Builds an html formatted report of sequence details and appends it to the
335 * buffer to append report to
337 * the sequence the report is for
339 * whether to include database references for the sequence
341 * whether to include non-positional sequence features
346 int createSequenceAnnotationReport(final StringBuilder sb,
347 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
348 Map<String, float[][]> minmax, boolean summary)
354 if (sequence.getDescription() != null)
356 tmp = sequence.getDescription();
357 sb.append("<br>").append(tmp);
358 maxWidth = Math.max(maxWidth, tmp.length());
360 SequenceI ds = sequence;
361 while (ds.getDatasetSequence() != null)
363 ds = ds.getDatasetSequence();
368 maxWidth = Math.max(maxWidth, appendDbRefs(sb, ds, summary));
372 * add non-positional features if wanted
376 for (SequenceFeature sf : sequence.getFeatures()
377 .getNonPositionalFeatures())
379 int sz = -sb.length();
380 appendFeature(sb, 0, minmax, sf);
382 maxWidth = Math.max(maxWidth, sz);
390 * A helper method that appends any DBRefs, returning the maximum line length
398 protected int appendDbRefs(final StringBuilder sb, SequenceI ds,
401 DBRefEntry[] dbrefs = ds.getDBRefs();
407 // note this sorts the refs held on the sequence!
408 Arrays.sort(dbrefs, comparator);
409 boolean ellipsis = false;
410 String source = null;
411 String lastSource = null;
412 int countForSource = 0;
414 boolean moreSources = false;
415 int maxLineLength = 0;
418 for (DBRefEntry ref : dbrefs)
420 source = ref.getSource();
426 boolean sourceChanged = !source.equals(lastSource);
433 if (sourceCount > MAX_SOURCES && summary)
441 if (countForSource == 1 || !summary)
445 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
447 String accessionId = ref.getAccessionId();
448 lineLength += accessionId.length() + 1;
449 if (countForSource > 1 && summary)
451 sb.append(", ").append(accessionId);
456 sb.append(source).append(" ").append(accessionId);
457 lineLength += source.length();
459 maxLineLength = Math.max(maxLineLength, lineLength);
461 if (countForSource == MAX_REFS_PER_SOURCE && summary)
463 sb.append(COMMA).append(ELLIPSIS);
469 sb.append("<br>").append(source)
470 .append(COMMA).append(ELLIPSIS);
475 sb.append(MessageManager.getString("label.output_seq_details"));
479 return maxLineLength;
482 public void createTooltipAnnotationReport(final StringBuilder tip,
483 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
484 Map<String, float[][]> minmax)
486 int maxWidth = createSequenceAnnotationReport(tip, sequence, showDbRefs,
487 showNpFeats, minmax, true);
491 // ? not sure this serves any useful purpose
492 // tip.insert(0, "<table width=350 border=0><tr><td>");
493 // tip.append("</td></tr></table>");