2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import java.util.ArrayList;
22 import java.util.Hashtable;
23 import java.util.Vector;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.UrlLink;
31 * generate HTML reports for a sequence
35 public class SequenceAnnotationReport
37 final String linkImageURL;
39 public SequenceAnnotationReport(String linkImageURL)
41 this.linkImageURL = linkImageURL;
45 * appends the features at rpos to the given stringbuffer ready for display in
52 * TODO refactor to Jalview 'utilities' somehow.
54 public void appendFeatures(final StringBuffer tooltipText2, int rpos,
55 SequenceFeature[] features)
57 appendFeatures(tooltipText2, rpos, features, null);
60 public void appendFeatures(final StringBuffer tooltipText2, int rpos,
61 SequenceFeature[] features, Hashtable minmax)
66 for (int i = 0; i < features.length; i++)
68 if (features[i].getType().equals("disulfide bond"))
70 if (features[i].getBegin() == rpos
71 || features[i].getEnd() == rpos)
73 if (tooltipText2.length() > 6)
75 tooltipText2.append("<br>");
77 tooltipText2.append("disulfide bond " + features[i].getBegin()
78 + ":" + features[i].getEnd());
83 if (tooltipText2.length() > 6)
85 tooltipText2.append("<br>");
87 // TODO: remove this hack to display link only features
88 boolean linkOnly = features[i].getValue("linkonly") != null;
91 tooltipText2.append(features[i].getType() + " ");
94 // we are marking a positional feature
95 tooltipText2.append(features[i].begin);
97 if (features[i].begin != features[i].end)
99 tooltipText2.append(" " + features[i].end);
102 if (features[i].getDescription() != null
103 && !features[i].description.equals(features[i]
106 tmpString = features[i].getDescription();
107 String tmp2up = tmpString.toUpperCase();
108 int startTag = tmp2up.indexOf("<HTML>");
111 tmpString = tmpString.substring(startTag + 6);
112 tmp2up = tmp2up.substring(startTag + 6);
114 int endTag = tmp2up.indexOf("</BODY>");
117 tmpString = tmpString.substring(0, endTag);
118 tmp2up = tmp2up.substring(0, endTag);
120 endTag = tmp2up.indexOf("</HTML>");
123 tmpString = tmpString.substring(0, endTag);
128 tooltipText2.append("; " + tmpString);
132 if (tmpString.indexOf("<") > -1
133 || tmpString.indexOf(">") > -1)
135 // The description does not specify html is to
136 // be used, so we must remove < > symbols
137 tmpString = tmpString.replaceAll("<", "<");
138 tmpString = tmpString.replaceAll(">", ">");
140 tooltipText2.append("; ");
141 tooltipText2.append(tmpString);
146 tooltipText2.append("; " + tmpString);
150 // check score should be shown
151 if (features[i].getScore() != Float.NaN)
153 float[][] rng = (minmax == null) ? null : ((float[][]) minmax
154 .get(features[i].getType()));
155 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
157 tooltipText2.append(" Score=" + features[i].getScore());
160 if (features[i].getValue("status") != null)
162 String status = features[i].getValue("status").toString();
163 if (status.length() > 0)
165 tooltipText2.append("; (" + features[i].getValue("status")
171 if (features[i].links != null)
173 if (linkImageURL != null)
175 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
179 for (String urlstring : (Vector<String>) features[i].links)
183 for (String[] urllink : createLinksFrom(null, urlstring))
185 tooltipText2.append("<br/> <a href=\""
190 + (urllink[0].toLowerCase().equals(
191 urllink[1].toLowerCase()) ? urllink[0]
192 : (urllink[0] + ":" + urllink[1]))
195 } catch (Exception x)
197 System.err.println("problem when creating links from "
213 * @return String[][] { String[] { link target, link label, dynamic component
214 * inserted (if any), url }}
216 public String[][] createLinksFrom(SequenceI seq, String link)
218 ArrayList<String[]> urlSets = new ArrayList<String[]>();
219 ArrayList<String> uniques = new ArrayList<String>();
220 UrlLink urlLink = new UrlLink(link);
221 if (!urlLink.isValid())
223 System.err.println(urlLink.getInvalidMessage());
226 final String target = urlLink.getTarget(); // link.substring(0,
227 // link.indexOf("|"));
228 final String label = urlLink.getLabel();
229 if (seq != null && urlLink.isDynamic())
232 // collect matching db-refs
233 DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(),
236 // collect id string too
237 String id = seq.getName();
238 String descr = seq.getDescription();
239 if (descr != null && descr.length() < 1)
245 for (int r = 0; r < dbr.length; r++)
247 if (id != null && dbr[r].getAccessionId().equals(id))
249 // suppress duplicate link creation for the bare sequence ID
250 // string with this link
253 // create Bare ID link for this RUL
254 String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
257 for (int u = 0; u < urls.length; u += 2)
259 String unq = urls[u] + "|" + urls[u + 1];
260 if (!uniques.contains(unq))
262 urlSets.add(new String[]
263 { target, label, urls[u], urls[u + 1] });
272 // create Bare ID link for this RUL
273 String[] urls = urlLink.makeUrls(id, true);
276 for (int u = 0; u < urls.length; u += 2)
278 String unq = urls[u] + "|" + urls[u + 1];
279 if (!uniques.contains(unq))
281 urlSets.add(new String[]
282 { target, label, urls[u], urls[u + 1] });
288 if (descr != null && urlLink.getRegexReplace() != null)
290 // create link for this URL from description only if regex matches
291 String[] urls = urlLink.makeUrls(descr, true);
294 for (int u = 0; u < urls.length; u += 2)
296 String unq = urls[u] + "|" + urls[u + 1];
297 if (!uniques.contains(unq))
299 urlSets.add(new String[]
300 { target, label, urls[u], urls[u + 1] });
310 String unq = label + "|" + urlLink.getUrl_prefix();
311 if (!uniques.contains(unq))
314 // Add a non-dynamic link
315 urlSets.add(new String[]
316 { target, label, null, urlLink.getUrl_prefix() });
320 return urlSets.toArray(new String[][]
324 public void createSequenceAnnotationReport(final StringBuffer tip,
325 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
328 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
332 public void createSequenceAnnotationReport(final StringBuffer tip,
333 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
334 boolean tableWrap, Hashtable minmax)
340 if (sequence.getDescription() != null)
342 tmp = sequence.getDescription();
343 tip.append("<br>" + tmp);
344 maxWidth = Math.max(maxWidth, tmp.length());
346 SequenceI ds = sequence;
347 while (ds.getDatasetSequence() != null)
349 ds = ds.getDatasetSequence();
351 DBRefEntry[] dbrefs = ds.getDBRef();
352 if (showDbRefs && dbrefs != null)
354 for (int i = 0; i < dbrefs.length; i++)
357 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
359 maxWidth = Math.max(maxWidth, tmp.length());
363 // ADD NON POSITIONAL SEQUENCE INFO
364 SequenceFeature[] features = ds.getSequenceFeatures();
365 SequenceFeature[] tfeat = new SequenceFeature[1];
366 if (showNpFeats && features != null)
368 for (int i = 0; i < features.length; i++)
370 if (features[i].begin == 0 && features[i].end == 0)
372 int sz = -tip.length();
373 tfeat[0] = features[i];
374 appendFeatures(tip, 0, tfeat, minmax);
376 maxWidth = Math.max(maxWidth, sz);
381 if (tableWrap && maxWidth > 60)
383 tip.insert(0, "<table width=350 border=0><tr><td><i>");
384 tip.append("</i></td></tr></table>");