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.PDBEntry;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.io.gff.GffConstants;
29 import jalview.util.MessageManager;
30 import jalview.util.UrlLink;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.Comparator;
35 import java.util.LinkedHashMap;
36 import java.util.List;
40 * generate HTML reports for a sequence
44 public class SequenceAnnotationReport
46 private static final String COMMA = ",";
48 private static final String ELLIPSIS = "...";
50 private static final int MAX_REFS_PER_SOURCE = 4;
52 private static final int MAX_SOURCES = 40;
54 private static final String[][] PRIMARY_SOURCES = new String[][] {
55 DBRefSource.CODINGDBS, DBRefSource.DNACODINGDBS,
56 DBRefSource.PROTEINDBS };
58 final String linkImageURL;
61 * Comparator to order DBRefEntry by Source + accession id (case-insensitive)
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 : (s2 == null ? 1 : s1
82 .compareToIgnoreCase(s2));
85 String a1 = ref1.getAccessionId();
86 String a2 = ref2.getAccessionId();
87 comp = a1 == null ? -1 : (a2 == null ? 1 : a1
88 .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())
155 .append(feature.getEnd());
164 // TODO: remove this hack to display link only features
165 boolean linkOnly = feature.getValue("linkonly") != null;
168 sb.append(feature.getType()).append(" ");
171 // we are marking a positional feature
172 sb.append(feature.begin);
174 if (feature.begin != feature.end)
176 sb.append(" ").append(feature.end);
179 if (feature.getDescription() != null
180 && !feature.description.equals(feature.getType()))
182 String tmpString = feature.getDescription();
183 String tmp2up = tmpString.toUpperCase();
184 int startTag = tmp2up.indexOf("<HTML>");
187 tmpString = tmpString.substring(startTag + 6);
188 tmp2up = tmp2up.substring(startTag + 6);
190 int endTag = tmp2up.indexOf("</BODY>");
193 tmpString = tmpString.substring(0, endTag);
194 tmp2up = tmp2up.substring(0, endTag);
196 endTag = tmp2up.indexOf("</HTML>");
199 tmpString = tmpString.substring(0, endTag);
204 sb.append("; ").append(tmpString);
208 if (tmpString.indexOf("<") > -1 || tmpString.indexOf(">") > -1)
210 // The description does not specify html is to
211 // be used, so we must remove < > symbols
212 tmpString = tmpString.replaceAll("<", "<");
213 tmpString = tmpString.replaceAll(">", ">");
216 sb.append(tmpString);
220 sb.append("; ").append(tmpString);
224 // check score should be shown
225 if (!Float.isNaN(feature.getScore()))
227 float[][] rng = (minmax == null) ? null : minmax.get(feature
229 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
231 sb.append(" Score=").append(String.valueOf(feature.getScore()));
234 String status = (String) feature.getValue("status");
235 if (status != null && status.length() > 0)
237 sb.append("; (").append(status).append(")");
239 String clinSig = (String) feature
240 .getValue(GffConstants.CLINICAL_SIGNIFICANCE);
243 sb.append("; ").append(clinSig);
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
283 } catch (Exception x)
285 System.err.println("problem when creating links from "
299 * @return Collection< List<String> > { List<String> { link target, link
300 * label, dynamic component inserted (if any), url }}
302 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
304 Map<String, List<String>> urlSets = new LinkedHashMap<String, List<String>>();
305 UrlLink urlLink = new UrlLink(link);
306 if (!urlLink.isValid())
308 System.err.println(urlLink.getInvalidMessage());
312 urlLink.createLinksFromSeq(seq, urlSets);
314 return urlSets.values();
317 public void createSequenceAnnotationReport(final StringBuilder tip,
318 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
319 Map<String, float[][]> minmax)
321 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
326 * Builds an html formatted report of sequence details and appends it to the
330 * buffer to append report to
332 * the sequence the report is for
334 * whether to include database references for the sequence
336 * whether to include non-positional sequence features
341 int createSequenceAnnotationReport(final StringBuilder sb,
342 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
343 Map<String, float[][]> minmax, boolean summary)
349 if (sequence.getDescription() != null)
351 tmp = sequence.getDescription();
352 sb.append("<br>").append(tmp);
353 maxWidth = Math.max(maxWidth, tmp.length());
355 SequenceI ds = sequence;
356 while (ds.getDatasetSequence() != null)
358 ds = ds.getDatasetSequence();
360 DBRefEntry[] dbrefs = ds.getDBRefs();
361 if (showDbRefs && dbrefs != null)
363 // note this sorts the refs held on the sequence!
364 Arrays.sort(dbrefs, comparator);
365 boolean ellipsis = false;
366 String source = null;
367 String lastSource = null;
368 int countForSource = 0;
370 boolean moreSources = false;
373 for (DBRefEntry ref : dbrefs)
375 source = ref.getSource();
381 boolean sourceChanged = !source.equals(lastSource);
388 if (sourceCount > MAX_SOURCES && summary)
396 if (countForSource == 1 || !summary)
400 if (countForSource <= MAX_REFS_PER_SOURCE || !summary)
402 String accessionId = ref.getAccessionId();
403 lineLength += accessionId.length() + 1;
404 if (countForSource > 1 && summary)
406 sb.append(", ").append(accessionId);
411 sb.append(source).append(" ").append(accessionId);
412 lineLength += source.length();
414 maxWidth = Math.max(maxWidth, lineLength);
416 if (countForSource == MAX_REFS_PER_SOURCE && summary)
418 sb.append(COMMA).append(ELLIPSIS);
424 sb.append("<br>").append(ELLIPSIS).append(COMMA).append(source)
425 .append(COMMA).append(ELLIPSIS);
430 sb.append(MessageManager.getString("label.output_seq_details"));
436 * add non-positional features if wanted
438 SequenceFeature[] features = sequence.getSequenceFeatures();
439 if (showNpFeats && features != null)
441 for (int i = 0; i < features.length; i++)
443 if (features[i].begin == 0 && features[i].end == 0)
445 int sz = -sb.length();
446 appendFeature(sb, 0, minmax, features[i]);
448 maxWidth = Math.max(maxWidth, sz);
454 for (PDBEntry pdb : ds.getAllPDBEntries())
456 if (pdb != null && pdb.getProperty("PHYRE2_MODEL_INFO") != null)
458 sb.append(pdb.getProperty("PHYRE2_MODEL_INFO"));
464 public void createTooltipAnnotationReport(final StringBuilder tip,
465 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
466 Map<String, float[][]> minmax)
468 int maxWidth = createSequenceAnnotationReport(tip, sequence,
469 showDbRefs, showNpFeats, minmax, true);
473 // ? not sure this serves any useful purpose
474 // tip.insert(0, "<table width=350 border=0><tr><td>");
475 // tip.append("</td></tr></table>");