3 import java.util.ArrayList;
4 import java.util.Hashtable;
5 import java.util.Vector;
7 import jalview.datamodel.DBRefEntry;
8 import jalview.datamodel.SequenceFeature;
9 import jalview.datamodel.SequenceI;
10 import jalview.util.UrlLink;
13 * generate HTML reports for a sequence
17 public class SequenceAnnotationReport
19 final String linkImageURL;
21 public SequenceAnnotationReport(String linkImageURL)
23 this.linkImageURL = linkImageURL;
27 * appends the features at rpos to the given stringbuffer ready for display in
34 * TODO refactor to Jalview 'utilities' somehow.
36 public void appendFeatures(final StringBuffer tooltipText2, int rpos,
37 SequenceFeature[] features)
39 appendFeatures(tooltipText2, rpos, features, null);
42 public void appendFeatures(final StringBuffer tooltipText2, int rpos,
43 SequenceFeature[] features, Hashtable minmax)
48 for (int i = 0; i < features.length; i++)
50 if (features[i].getType().equals("disulfide bond"))
52 if (features[i].getBegin() == rpos
53 || features[i].getEnd() == rpos)
55 if (tooltipText2.length() > 6)
57 tooltipText2.append("<br>");
59 tooltipText2.append("disulfide bond " + features[i].getBegin()
60 + ":" + features[i].getEnd());
65 if (tooltipText2.length() > 6)
67 tooltipText2.append("<br>");
69 // TODO: remove this hack to display link only features
70 boolean linkOnly = features[i].getValue("linkonly") != null;
73 tooltipText2.append(features[i].getType() + " ");
76 // we are marking a positional feature
77 tooltipText2.append(features[i].begin);
79 if (features[i].begin != features[i].end)
81 tooltipText2.append(" " + features[i].end);
84 if (features[i].getDescription() != null
85 && !features[i].description.equals(features[i]
88 tmpString = features[i].getDescription();
89 String tmp2up = tmpString.toUpperCase();
90 int startTag = tmp2up.indexOf("<HTML>");
93 tmpString = tmpString.substring(startTag + 6);
94 tmp2up = tmp2up.substring(startTag + 6);
96 int endTag = tmp2up.indexOf("</BODY>");
99 tmpString = tmpString.substring(0, endTag);
100 tmp2up = tmp2up.substring(0, endTag);
102 endTag = tmp2up.indexOf("</HTML>");
105 tmpString = tmpString.substring(0, endTag);
110 tooltipText2.append("; " + tmpString);
114 if (tmpString.indexOf("<") > -1
115 || tmpString.indexOf(">") > -1)
117 // The description does not specify html is to
118 // be used, so we must remove < > symbols
119 tmpString = tmpString.replaceAll("<", "<");
120 tmpString = tmpString.replaceAll(">", ">");
122 tooltipText2.append("; ");
123 tooltipText2.append(tmpString);
128 tooltipText2.append("; " + tmpString);
132 // check score should be shown
133 if (features[i].getScore() != Float.NaN)
135 float[][] rng = (minmax == null) ? null : ((float[][]) minmax
136 .get(features[i].getType()));
137 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
139 tooltipText2.append(" Score=" + features[i].getScore());
142 if (features[i].getValue("status") != null)
144 String status = features[i].getValue("status").toString();
145 if (status.length() > 0)
147 tooltipText2.append("; (" + features[i].getValue("status")
153 if (features[i].links != null)
155 if (linkImageURL != null)
157 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
161 for (String urlstring : (Vector<String>) features[i].links)
165 for (String[] urllink : createLinksFrom(null, urlstring))
167 tooltipText2.append("<br/> <a href=\""
172 + (urllink[0].toLowerCase().equals(
173 urllink[1].toLowerCase()) ? urllink[0]
174 : (urllink[0] + ":" + urllink[1]))
177 } catch (Exception x)
179 System.err.println("problem when creating links from "
195 * @return String[][] { String[] { link target, link label, dynamic component
196 * inserted (if any), url }}
198 public String[][] createLinksFrom(SequenceI seq, String link)
200 ArrayList<String[]> urlSets = new ArrayList<String[]>();
201 ArrayList<String> uniques = new ArrayList<String>();
202 UrlLink urlLink = new UrlLink(link);
203 if (!urlLink.isValid())
205 System.err.println(urlLink.getInvalidMessage());
208 final String target = urlLink.getTarget(); // link.substring(0,
209 // link.indexOf("|"));
210 final String label = urlLink.getLabel();
211 if (seq != null && urlLink.isDynamic())
214 // collect matching db-refs
215 DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(),
218 // collect id string too
219 String id = seq.getName();
220 String descr = seq.getDescription();
221 if (descr != null && descr.length() < 1)
227 for (int r = 0; r < dbr.length; r++)
229 if (id != null && dbr[r].getAccessionId().equals(id))
231 // suppress duplicate link creation for the bare sequence ID
232 // string with this link
235 // create Bare ID link for this RUL
236 String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
239 for (int u = 0; u < urls.length; u += 2)
241 String unq = urls[u] + "|" + urls[u + 1];
242 if (!uniques.contains(unq))
244 urlSets.add(new String[]
245 { target, label, urls[u], urls[u + 1] });
254 // create Bare ID link for this RUL
255 String[] urls = urlLink.makeUrls(id, true);
258 for (int u = 0; u < urls.length; u += 2)
260 String unq = urls[u] + "|" + urls[u + 1];
261 if (!uniques.contains(unq))
263 urlSets.add(new String[]
264 { target, label, urls[u], urls[u + 1] });
270 if (descr != null && urlLink.getRegexReplace() != null)
272 // create link for this URL from description only if regex matches
273 String[] urls = urlLink.makeUrls(descr, 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] });
292 String unq = label + "|" + urlLink.getUrl_prefix();
293 if (!uniques.contains(unq))
296 // Add a non-dynamic link
297 urlSets.add(new String[]
298 { target, label, null, urlLink.getUrl_prefix() });
302 return urlSets.toArray(new String[][]
306 public void createSequenceAnnotationReport(final StringBuffer tip,
307 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
310 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
314 public void createSequenceAnnotationReport(final StringBuffer tip,
315 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
316 boolean tableWrap, Hashtable minmax)
322 if (sequence.getDescription() != null)
324 tmp = sequence.getDescription();
325 tip.append("<br>" + tmp);
326 maxWidth = Math.max(maxWidth, tmp.length());
328 SequenceI ds = sequence;
329 while (ds.getDatasetSequence() != null)
331 ds = ds.getDatasetSequence();
333 DBRefEntry[] dbrefs = ds.getDBRef();
334 if (showDbRefs && dbrefs != null)
336 for (int i = 0; i < dbrefs.length; i++)
339 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
341 maxWidth = Math.max(maxWidth, tmp.length());
345 // ADD NON POSITIONAL SEQUENCE INFO
346 SequenceFeature[] features = ds.getSequenceFeatures();
347 SequenceFeature[] tfeat = new SequenceFeature[1];
348 if (showNpFeats && features != null)
350 for (int i = 0; i < features.length; i++)
352 if (features[i].begin == 0 && features[i].end == 0)
354 int sz = -tip.length();
355 tfeat[0] = features[i];
356 appendFeatures(tip, 0, tfeat, minmax);
358 maxWidth = Math.max(maxWidth, sz);
363 if (tableWrap && maxWidth > 60)
365 tip.insert(0, "<table width=350 border=0><tr><td><i>");
366 tip.append("</i></td></tr></table>");