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.io.gff.GffConstants;
27 import jalview.util.UrlLink;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.LinkedHashMap;
32 import java.util.List;
36 * generate HTML reports for a sequence
40 public class SequenceAnnotationReport
42 final String linkImageURL;
44 public SequenceAnnotationReport(String linkImageURL)
46 this.linkImageURL = linkImageURL;
50 * Append text for the list of features to the tooltip
57 public void appendFeatures(final StringBuffer tooltipText2, int rpos,
58 List<SequenceFeature> features, Map<String, float[][]> minmax)
62 for (SequenceFeature feature : features)
64 appendFeature(tooltipText2, rpos, minmax, feature);
70 * Appends text for one sequence feature to the string buffer
75 * {{min, max}, {min, max}} positional and non-positional feature
76 * scores for this type
79 void appendFeature(final StringBuffer sb, int rpos,
80 Map<String, float[][]> minmax, SequenceFeature feature)
82 if ("disulfide bond".equals(feature.getType()))
84 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
90 sb.append("disulfide bond ").append(feature.getBegin()).append(":")
91 .append(feature.getEnd());
100 // TODO: remove this hack to display link only features
101 boolean linkOnly = feature.getValue("linkonly") != null;
104 sb.append(feature.getType()).append(" ");
107 // we are marking a positional feature
108 sb.append(feature.begin);
110 if (feature.begin != feature.end)
112 sb.append(" " + feature.end);
115 if (feature.getDescription() != null
116 && !feature.description.equals(feature.getType()))
118 String tmpString = feature.getDescription();
119 String tmp2up = tmpString.toUpperCase();
120 final int startTag = tmp2up.indexOf("<HTML>");
123 tmpString = tmpString.substring(startTag + 6);
124 tmp2up = tmp2up.substring(startTag + 6);
126 // TODO strips off </body> but not <body> - is that intended?
127 int endTag = tmp2up.indexOf("</BODY>");
130 tmpString = tmpString.substring(0, endTag);
131 tmp2up = tmp2up.substring(0, endTag);
133 endTag = tmp2up.indexOf("</HTML>");
136 tmpString = tmpString.substring(0, endTag);
141 sb.append("; ").append(tmpString);
145 if (tmpString.indexOf("<") > -1 || tmpString.indexOf(">") > -1)
147 // The description does not specify html is to
148 // be used, so we must remove < > symbols
149 tmpString = tmpString.replaceAll("<", "<");
150 tmpString = tmpString.replaceAll(">", ">");
151 sb.append("; ").append(tmpString);
155 sb.append("; ").append(tmpString);
161 * score should be shown if there is one, and min != max
162 * for this feature type (e.g. not all 0)
164 if (!Float.isNaN(feature.getScore()))
166 float[][] rng = (minmax == null) ? null : minmax.get(feature
168 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
170 sb.append(" Score=").append(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
179 .getValue(GffConstants.CLINICAL_SIGNIFICANCE);
182 sb.append("; ").append(clinSig);
186 appendLinks(sb, feature);
190 * Format and appends any hyperlinks for the sequence feature to the string
196 void appendLinks(final StringBuffer sb, SequenceFeature feature)
198 if (feature.links != null)
200 if (linkImageURL != null)
202 sb.append(" <img src=\"" + linkImageURL + "\">");
206 for (String urlstring : feature.links)
210 for (List<String> urllink : createLinksFrom(null, urlstring))
212 sb.append("<br/> <a href=\""
217 + (urllink.get(0).toLowerCase()
218 .equals(urllink.get(1).toLowerCase()) ? urllink
219 .get(0) : (urllink.get(0) + ":" + urllink
223 } catch (Exception x)
225 System.err.println("problem when creating links from "
239 * @return Collection< List<String> > { List<String> { link target, link
240 * label, dynamic component inserted (if any), url }}
242 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
244 Map<String, List<String>> urlSets = new LinkedHashMap<String, List<String>>();
245 UrlLink urlLink = new UrlLink(link);
246 if (!urlLink.isValid())
248 System.err.println(urlLink.getInvalidMessage());
252 urlLink.createLinksFromSeq(seq, urlSets);
254 return urlSets.values();
257 public void createSequenceAnnotationReport(final StringBuffer tip,
258 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
259 Map<String, float[][]> minmax)
261 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
265 public void createSequenceAnnotationReport(final StringBuffer tip,
266 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
267 boolean tableWrap, Map<String, float[][]> minmax)
273 if (sequence.getDescription() != null)
275 tmp = sequence.getDescription();
276 tip.append("<br>" + tmp);
277 maxWidth = Math.max(maxWidth, tmp.length());
279 SequenceI ds = sequence;
280 while (ds.getDatasetSequence() != null)
282 ds = ds.getDatasetSequence();
284 DBRefEntry[] dbrefs = ds.getDBRefs();
285 if (showDbRefs && dbrefs != null)
287 for (int i = 0; i < dbrefs.length; i++)
290 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
292 maxWidth = Math.max(maxWidth, tmp.length());
296 // ADD NON POSITIONAL SEQUENCE INFO
297 SequenceFeature[] features = sequence.getSequenceFeatures();
298 if (showNpFeats && features != null)
300 for (int i = 0; i < features.length; i++)
302 if (features[i].begin == 0 && features[i].end == 0)
304 int sz = -tip.length();
305 List<SequenceFeature> tfeat = new ArrayList<SequenceFeature>();
306 tfeat.add(features[i]);
307 appendFeatures(tip, 0, tfeat, minmax);
309 maxWidth = Math.max(maxWidth, sz);
314 if (tableWrap && maxWidth > 60)
316 tip.insert(0, "<table width=350 border=0><tr><td><i>");
317 tip.append("</i></td></tr></table>");