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.
24 import jalview.datamodel.DBRefEntry;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.UrlLink;
29 import java.util.ArrayList;
30 import java.util.Hashtable;
31 import java.util.List;
35 * generate HTML reports for a sequence
39 public class SequenceAnnotationReport
41 final String linkImageURL;
43 public SequenceAnnotationReport(String linkImageURL)
45 this.linkImageURL = linkImageURL;
49 * appends the features at rpos to the given stringbuffer ready for display in
56 * TODO refactor to Jalview 'utilities' somehow.
58 public void appendFeatures(final StringBuffer tooltipText2, int rpos,
59 List<SequenceFeature> features)
61 appendFeatures(tooltipText2, rpos, features, null);
64 public void appendFeatures(final StringBuffer tooltipText2, int rpos,
65 List<SequenceFeature> features, Hashtable minmax)
70 for (SequenceFeature feature:features)
72 if (feature.getType().equals("disulfide bond"))
74 if (feature.getBegin() == rpos
75 || feature.getEnd() == rpos)
77 if (tooltipText2.length() > 6)
79 tooltipText2.append("<br>");
81 tooltipText2.append("disulfide bond " + feature.getBegin()
82 + ":" + feature.getEnd());
87 if (tooltipText2.length() > 6)
89 tooltipText2.append("<br>");
91 // TODO: remove this hack to display link only features
92 boolean linkOnly = feature.getValue("linkonly") != null;
95 tooltipText2.append(feature.getType() + " ");
98 // we are marking a positional feature
99 tooltipText2.append(feature.begin);
101 if (feature.begin != feature.end)
103 tooltipText2.append(" " + feature.end);
106 if (feature.getDescription() != null
107 && !feature.description.equals(feature
110 tmpString = feature.getDescription();
111 String tmp2up = tmpString.toUpperCase();
112 int startTag = tmp2up.indexOf("<HTML>");
115 tmpString = tmpString.substring(startTag + 6);
116 tmp2up = tmp2up.substring(startTag + 6);
118 int endTag = tmp2up.indexOf("</BODY>");
121 tmpString = tmpString.substring(0, endTag);
122 tmp2up = tmp2up.substring(0, endTag);
124 endTag = tmp2up.indexOf("</HTML>");
127 tmpString = tmpString.substring(0, endTag);
132 tooltipText2.append("; " + tmpString);
136 if (tmpString.indexOf("<") > -1
137 || tmpString.indexOf(">") > -1)
139 // The description does not specify html is to
140 // be used, so we must remove < > symbols
141 tmpString = tmpString.replaceAll("<", "<");
142 tmpString = tmpString.replaceAll(">", ">");
144 tooltipText2.append("; ");
145 tooltipText2.append(tmpString);
150 tooltipText2.append("; " + tmpString);
154 // check score should be shown
155 if (!Float.isNaN(feature.getScore()))
157 float[][] rng = (minmax == null) ? null : ((float[][]) minmax
158 .get(feature.getType()));
159 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
161 tooltipText2.append(" Score=" + feature.getScore());
164 if (feature.getValue("status") != null)
166 String status = feature.getValue("status").toString();
167 if (status.length() > 0)
169 tooltipText2.append("; (" + feature.getValue("status")
175 if (feature.links != null)
177 if (linkImageURL != null)
179 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
183 for (String urlstring : feature.links)
187 for (String[] urllink : createLinksFrom(null, urlstring))
189 tooltipText2.append("<br/> <a href=\""
194 + (urllink[0].toLowerCase().equals(
195 urllink[1].toLowerCase()) ? urllink[0]
196 : (urllink[0] + ":" + urllink[1]))
199 } catch (Exception x)
201 System.err.println("problem when creating links from "
217 * @return String[][] { String[] { link target, link label, dynamic component
218 * inserted (if any), url }}
220 public String[][] createLinksFrom(SequenceI seq, String link)
222 ArrayList<String[]> urlSets = new ArrayList<String[]>();
223 ArrayList<String> uniques = new ArrayList<String>();
224 UrlLink urlLink = new UrlLink(link);
225 if (!urlLink.isValid())
227 System.err.println(urlLink.getInvalidMessage());
230 final String target = urlLink.getTarget(); // link.substring(0,
231 // link.indexOf("|"));
232 final String label = urlLink.getLabel();
233 if (seq != null && urlLink.isDynamic())
236 // collect matching db-refs
237 DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(),
240 // collect id string too
241 String id = seq.getName();
242 String descr = seq.getDescription();
243 if (descr != null && descr.length() < 1)
249 for (int r = 0; r < dbr.length; r++)
251 if (id != null && dbr[r].getAccessionId().equals(id))
253 // suppress duplicate link creation for the bare sequence ID
254 // string with this link
257 // create Bare ID link for this RUL
258 String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
261 for (int u = 0; u < urls.length; u += 2)
263 String unq = urls[u] + "|" + urls[u + 1];
264 if (!uniques.contains(unq))
266 urlSets.add(new String[]
267 { target, label, urls[u], urls[u + 1] });
276 // create Bare ID link for this RUL
277 String[] urls = urlLink.makeUrls(id, true);
280 for (int u = 0; u < urls.length; u += 2)
282 String unq = urls[u] + "|" + urls[u + 1];
283 if (!uniques.contains(unq))
285 urlSets.add(new String[]
286 { target, label, urls[u], urls[u + 1] });
292 if (descr != null && urlLink.getRegexReplace() != null)
294 // create link for this URL from description only if regex matches
295 String[] urls = urlLink.makeUrls(descr, true);
298 for (int u = 0; u < urls.length; u += 2)
300 String unq = urls[u] + "|" + urls[u + 1];
301 if (!uniques.contains(unq))
303 urlSets.add(new String[]
304 { target, label, urls[u], urls[u + 1] });
314 String unq = label + "|" + urlLink.getUrl_prefix();
315 if (!uniques.contains(unq))
318 // Add a non-dynamic link
319 urlSets.add(new String[]
320 { target, label, null, urlLink.getUrl_prefix() });
324 return urlSets.toArray(new String[][]
328 public void createSequenceAnnotationReport(final StringBuffer tip,
329 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
332 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
336 public void createSequenceAnnotationReport(final StringBuffer tip,
337 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
338 boolean tableWrap, Hashtable minmax)
344 if (sequence.getDescription() != null)
346 tmp = sequence.getDescription();
347 tip.append("<br>" + tmp);
348 maxWidth = Math.max(maxWidth, tmp.length());
350 SequenceI ds = sequence;
351 while (ds.getDatasetSequence() != null)
353 ds = ds.getDatasetSequence();
355 DBRefEntry[] dbrefs = ds.getDBRef();
356 if (showDbRefs && dbrefs != null)
358 for (int i = 0; i < dbrefs.length; i++)
361 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
363 maxWidth = Math.max(maxWidth, tmp.length());
367 // ADD NON POSITIONAL SEQUENCE INFO
368 SequenceFeature[] features = sequence.getSequenceFeatures();
369 if (showNpFeats && features != null)
371 for (int i = 0; i < features.length; i++)
373 if (features[i].begin == 0 && features[i].end == 0)
375 int sz = -tip.length();
376 List<SequenceFeature> tfeat = new ArrayList<SequenceFeature>();
377 tfeat.add(features[i]);
378 appendFeatures(tip, 0, tfeat, minmax);
380 maxWidth = Math.max(maxWidth, sz);
385 if (tableWrap && maxWidth > 60)
387 tip.insert(0, "<table width=350 border=0><tr><td><i>");
388 tip.append("</i></td></tr></table>");