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.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.DBRefUtils;
27 import jalview.util.UrlLink;
29 import java.util.ArrayList;
30 import java.util.List;
34 * generate HTML reports for a sequence
38 public class SequenceAnnotationReport
40 final String linkImageURL;
42 public SequenceAnnotationReport(String linkImageURL)
44 this.linkImageURL = linkImageURL;
48 * Append text for the list of features to the tooltip
55 public void appendFeatures(final StringBuffer tooltipText2, int rpos,
56 List<SequenceFeature> features, Map<String, float[][]> minmax)
60 for (SequenceFeature feature : features)
62 appendFeature(tooltipText2, rpos, minmax, feature);
68 * Appends text for one sequence feature to the string buffer
73 * {{min, max}, {min, max}} positional and non-positional feature
74 * scores for this type
77 void appendFeature(final StringBuffer sb, int rpos,
78 Map<String, float[][]> minmax, SequenceFeature feature)
80 if ("disulfide bond".equals(feature.getType()))
82 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
88 sb.append("disulfide bond ").append(feature.getBegin()).append(":")
89 .append(feature.getEnd());
98 // TODO: remove this hack to display link only features
99 boolean linkOnly = feature.getValue("linkonly") != null;
102 sb.append(feature.getType()).append(" ");
105 // we are marking a positional feature
106 sb.append(feature.begin);
108 if (feature.begin != feature.end)
110 sb.append(" " + feature.end);
113 if (feature.getDescription() != null
114 && !feature.description.equals(feature.getType()))
116 String tmpString = feature.getDescription();
117 String tmp2up = tmpString.toUpperCase();
118 final int startTag = tmp2up.indexOf("<HTML>");
121 tmpString = tmpString.substring(startTag + 6);
122 tmp2up = tmp2up.substring(startTag + 6);
124 // TODO strips off </body> but not <body> - is that intended?
125 int endTag = tmp2up.indexOf("</BODY>");
128 tmpString = tmpString.substring(0, endTag);
129 tmp2up = tmp2up.substring(0, endTag);
131 endTag = tmp2up.indexOf("</HTML>");
134 tmpString = tmpString.substring(0, endTag);
139 sb.append("; ").append(tmpString);
143 if (tmpString.indexOf("<") > -1
144 || tmpString.indexOf(">") > -1)
146 // The description does not specify html is to
147 // be used, so we must remove < > symbols
148 tmpString = tmpString.replaceAll("<", "<");
149 tmpString = tmpString.replaceAll(">", ">");
150 sb.append("; ").append(tmpString);
154 sb.append("; ").append(tmpString);
160 * score should be shown if there is one, and min != max
161 * for this feature type (e.g. not all 0)
163 if (!Float.isNaN(feature.getScore()))
165 float[][] rng = (minmax == null) ? null : minmax.get(feature
167 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
169 sb.append(" Score=").append(
170 String.valueOf(feature.getScore()));
173 String status = (String) feature.getValue("status");
174 if (status != null && status.length() > 0)
176 sb.append("; (").append(status).append(")");
178 String clinSig = (String) feature.getValue("clinical_significance");
181 sb.append("; ").append(clinSig);
185 appendLinks(sb, feature);
189 * Format and appends any hyperlinks for the sequence feature to the string
195 void appendLinks(final StringBuffer sb, SequenceFeature feature)
197 if (feature.links != null)
199 if (linkImageURL != null)
201 sb.append(" <img src=\"" + linkImageURL + "\">");
205 for (String urlstring : feature.links)
209 for (String[] urllink : createLinksFrom(null, urlstring))
211 sb.append("<br/> <a href=\""
216 + (urllink[0].toLowerCase().equals(
217 urllink[1].toLowerCase()) ? urllink[0]
218 : (urllink[0] + ":" + urllink[1]))
221 } catch (Exception x)
223 System.err.println("problem when creating links from "
237 * @return String[][] { String[] { link target, link label, dynamic component
238 * inserted (if any), url }}
240 String[][] createLinksFrom(SequenceI seq, String link)
242 List<String[]> urlSets = new ArrayList<String[]>();
243 List<String> uniques = new ArrayList<String>();
244 UrlLink urlLink = new UrlLink(link);
245 if (!urlLink.isValid())
247 System.err.println(urlLink.getInvalidMessage());
250 if (seq != null && urlLink.isDynamic())
252 urlSets.addAll(createDynamicLinks(seq, urlLink, uniques));
256 String target = urlLink.getTarget();
257 String label = urlLink.getLabel();
258 String unq = label + "|" + urlLink.getUrl_prefix();
259 if (!uniques.contains(unq))
262 urlSets.add(new String[] { target, label, null,
263 urlLink.getUrl_prefix() });
267 return urlSets.toArray(new String[][] {});
271 * Formats and returns a list of dynamic href links
277 List<String[]> createDynamicLinks(SequenceI seq, UrlLink urlLink,
278 List<String> uniques)
280 List<String[]> result = new ArrayList<String[]>();
281 final String target = urlLink.getTarget();
282 final String label = urlLink.getLabel();
284 // collect matching db-refs
285 DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(),
286 new String[] { target });
287 // collect id string too
288 String id = seq.getName();
289 String descr = seq.getDescription();
290 if (descr != null && descr.length() < 1)
296 for (int r = 0; r < dbr.length; r++)
298 if (id != null && dbr[r].getAccessionId().equals(id))
300 // suppress duplicate link creation for the bare sequence ID
301 // string with this link
304 // create Bare ID link for this URL
305 String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
308 for (int u = 0; u < urls.length; u += 2)
310 String unq = urls[u] + "|" + urls[u + 1];
311 if (!uniques.contains(unq))
313 result.add(new String[] { target, label, urls[u],
323 // create Bare ID link for this URL
324 String[] urls = urlLink.makeUrls(id, true);
327 for (int u = 0; u < urls.length; u += 2)
329 String unq = urls[u] + "|" + urls[u + 1];
330 if (!uniques.contains(unq))
332 result.add(new String[] { target, label, urls[u],
339 if (descr != null && urlLink.getRegexReplace() != null)
341 // create link for this URL from description only if regex matches
342 String[] urls = urlLink.makeUrls(descr, true);
345 for (int u = 0; u < urls.length; u += 2)
347 String unq = urls[u] + "|" + urls[u + 1];
348 if (!uniques.contains(unq))
350 result.add(new String[] { target, label, urls[u],
360 public void createSequenceAnnotationReport(final StringBuffer tip,
361 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
362 Map<String, float[][]> minmax)
364 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
368 public void createSequenceAnnotationReport(final StringBuffer tip,
369 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
370 boolean tableWrap, Map<String, float[][]> minmax)
376 if (sequence.getDescription() != null)
378 tmp = sequence.getDescription();
379 tip.append("<br>" + tmp);
380 maxWidth = Math.max(maxWidth, tmp.length());
382 SequenceI ds = sequence;
383 while (ds.getDatasetSequence() != null)
385 ds = ds.getDatasetSequence();
387 DBRefEntry[] dbrefs = ds.getDBRefs();
388 if (showDbRefs && dbrefs != null)
390 for (int i = 0; i < dbrefs.length; i++)
393 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
395 maxWidth = Math.max(maxWidth, tmp.length());
399 // ADD NON POSITIONAL SEQUENCE INFO
400 SequenceFeature[] features = sequence.getSequenceFeatures();
401 if (showNpFeats && features != null)
403 for (int i = 0; i < features.length; i++)
405 if (features[i].begin == 0 && features[i].end == 0)
407 int sz = -tip.length();
408 List<SequenceFeature> tfeat = new ArrayList<SequenceFeature>();
409 tfeat.add(features[i]);
410 appendFeatures(tip, 0, tfeat, minmax);
412 maxWidth = Math.max(maxWidth, sz);
417 if (tableWrap && maxWidth > 60)
419 tip.insert(0, "<table width=350 border=0><tr><td><i>");
420 tip.append("</i></td></tr></table>");