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=\"" + urllink[3]
168 + "\" target=\"" + urllink[0] + "\">"
169 + (urllink[0].toLowerCase().equals(urllink[1].toLowerCase()) ? urllink[0] : (urllink[0]+ ":" + urllink[1])) + "</a></br>");
171 } catch (Exception x)
173 System.err.println("problem when creating links from "
189 * @return String[][] { String[] { link target, link label, dynamic component
190 * inserted (if any), url }}
192 public String[][] createLinksFrom(SequenceI seq, String link)
194 ArrayList<String[]> urlSets = new ArrayList<String[]>();
195 ArrayList<String> uniques=new ArrayList<String>();
196 UrlLink urlLink = new UrlLink(link);
197 if (!urlLink.isValid())
199 System.err.println(urlLink.getInvalidMessage());
202 final String target = urlLink.getTarget(); // link.substring(0,
203 // link.indexOf("|"));
204 final String label = urlLink.getLabel();
205 if (seq != null && urlLink.isDynamic())
208 // collect matching db-refs
209 DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(),
212 // collect id string too
213 String id = seq.getName();
214 String descr = seq.getDescription();
215 if (descr != null && descr.length() < 1)
221 for (int r = 0; r < dbr.length; r++)
223 if (id != null && dbr[r].getAccessionId().equals(id))
225 // suppress duplicate link creation for the bare sequence ID
226 // string with this link
229 // create Bare ID link for this RUL
230 String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
233 for (int u = 0; u < urls.length; u += 2)
235 String unq=urls[u]+"|"+urls[u+1];
236 if (!uniques.contains(unq))
238 urlSets.add(new String[]
239 { target, label, urls[u], urls[u + 1] });
248 // create Bare ID link for this RUL
249 String[] urls = urlLink.makeUrls(id, true);
252 for (int u = 0; u < urls.length; u += 2)
254 String unq=urls[u]+"|"+urls[u+1];
255 if (!uniques.contains(unq))
257 urlSets.add(new String[]
258 { target, label, urls[u], urls[u + 1] });
264 if (descr != null && urlLink.getRegexReplace() != null)
266 // create link for this URL from description only if regex matches
267 String[] urls = urlLink.makeUrls(descr, true);
270 for (int u = 0; u < urls.length; u += 2)
272 String unq=urls[u]+"|"+urls[u+1];
273 if (!uniques.contains(unq))
275 urlSets.add(new String[]
276 { target, label, urls[u], urls[u + 1] });
284 String unq=label + "|" + urlLink.getUrl_prefix();
285 if (!uniques.contains(unq)){
287 // Add a non-dynamic link
288 urlSets.add(new String[] {target, label, null, urlLink.getUrl_prefix()});
292 return urlSets.toArray(new String[][]
297 public void createSequenceAnnotationReport(final StringBuffer tip,
298 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
301 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats, true, minmax);
304 public void createSequenceAnnotationReport(final StringBuffer tip,
305 SequenceI sequence, boolean showDbRefs, boolean showNpFeats, boolean tableWrap,
312 if (sequence.getDescription() != null)
314 tmp = sequence.getDescription();
315 tip.append("<br>" + tmp);
316 maxWidth = Math.max(maxWidth, tmp.length());
318 SequenceI ds=sequence;
319 while (ds.getDatasetSequence()!=null)
321 ds=ds.getDatasetSequence();
323 DBRefEntry[] dbrefs = ds.getDBRef();
324 if (showDbRefs && dbrefs != null)
326 for (int i = 0; i < dbrefs.length; i++)
329 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
331 maxWidth = Math.max(maxWidth, tmp.length());
335 // ADD NON POSITIONAL SEQUENCE INFO
336 SequenceFeature[] features = ds.getSequenceFeatures();
337 SequenceFeature[] tfeat = new SequenceFeature[1];
338 if (showNpFeats && features != null)
340 for (int i = 0; i < features.length; i++)
342 if (features[i].begin == 0 && features[i].end == 0)
344 int sz = -tip.length();
345 tfeat[0] = features[i];
346 appendFeatures(tip, 0, tfeat, minmax);
348 maxWidth = Math.max(maxWidth, sz);
353 if (tableWrap && maxWidth > 60)
355 tip.insert(0, "<table width=350 border=0><tr><td><i>");
356 tip.append("</i></td></tr></table>");