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 (feature.isContactFeature())
84 if (feature.getBegin() == rpos || feature.getEnd() == rpos)
90 sb.append(feature.getType()).append(" ").append(feature.getBegin())
92 .append(feature.getEnd());
101 // TODO: remove this hack to display link only features
102 boolean linkOnly = feature.getValue("linkonly") != null;
105 sb.append(feature.getType()).append(" ");
108 // we are marking a positional feature
109 sb.append(feature.begin);
111 if (feature.begin != feature.end)
113 sb.append(" " + feature.end);
116 if (feature.getDescription() != null
117 && !feature.description.equals(feature.getType()))
119 String tmpString = feature.getDescription();
120 String tmp2up = tmpString.toUpperCase();
121 final int startTag = tmp2up.indexOf("<HTML>");
124 tmpString = tmpString.substring(startTag + 6);
125 tmp2up = tmp2up.substring(startTag + 6);
127 // TODO strips off </body> but not <body> - is that intended?
128 int endTag = tmp2up.indexOf("</BODY>");
131 tmpString = tmpString.substring(0, endTag);
132 tmp2up = tmp2up.substring(0, endTag);
134 endTag = tmp2up.indexOf("</HTML>");
137 tmpString = tmpString.substring(0, endTag);
142 sb.append("; ").append(tmpString);
146 if (tmpString.indexOf("<") > -1 || tmpString.indexOf(">") > -1)
148 // The description does not specify html is to
149 // be used, so we must remove < > symbols
150 tmpString = tmpString.replaceAll("<", "<");
151 tmpString = tmpString.replaceAll(">", ">");
152 sb.append("; ").append(tmpString);
156 sb.append("; ").append(tmpString);
162 * score should be shown if there is one, and min != max
163 * for this feature type (e.g. not all 0)
165 if (!Float.isNaN(feature.getScore()))
167 float[][] rng = (minmax == null) ? null : minmax.get(feature
169 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
171 sb.append(" Score=").append(String.valueOf(feature.getScore()));
174 String status = (String) feature.getValue("status");
175 if (status != null && status.length() > 0)
177 sb.append("; (").append(status).append(")");
179 String clinSig = (String) feature
180 .getValue(GffConstants.CLINICAL_SIGNIFICANCE);
183 sb.append("; ").append(clinSig);
187 appendLinks(sb, feature);
191 * Format and appends any hyperlinks for the sequence feature to the string
197 void appendLinks(final StringBuffer sb, SequenceFeature feature)
199 if (feature.links != null)
201 if (linkImageURL != null)
203 sb.append(" <img src=\"" + linkImageURL + "\">");
207 for (String urlstring : feature.links)
211 for (List<String> urllink : createLinksFrom(null, urlstring))
213 sb.append("<br/> <a href=\""
218 + (urllink.get(0).toLowerCase()
219 .equals(urllink.get(1).toLowerCase()) ? urllink
220 .get(0) : (urllink.get(0) + ":" + urllink
224 } catch (Exception x)
226 System.err.println("problem when creating links from "
240 * @return Collection< List<String> > { List<String> { link target, link
241 * label, dynamic component inserted (if any), url }}
243 Collection<List<String>> createLinksFrom(SequenceI seq, String link)
245 Map<String, List<String>> urlSets = new LinkedHashMap<String, List<String>>();
246 UrlLink urlLink = new UrlLink(link);
247 if (!urlLink.isValid())
249 System.err.println(urlLink.getInvalidMessage());
253 urlLink.createLinksFromSeq(seq, urlSets);
255 return urlSets.values();
258 public void createSequenceAnnotationReport(final StringBuffer tip,
259 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
260 Map<String, float[][]> minmax)
262 createSequenceAnnotationReport(tip, sequence, showDbRefs, showNpFeats,
266 public void createSequenceAnnotationReport(final StringBuffer tip,
267 SequenceI sequence, boolean showDbRefs, boolean showNpFeats,
268 boolean tableWrap, Map<String, float[][]> minmax)
274 if (sequence.getDescription() != null)
276 tmp = sequence.getDescription();
277 tip.append("<br>" + tmp);
278 maxWidth = Math.max(maxWidth, tmp.length());
280 SequenceI ds = sequence;
281 while (ds.getDatasetSequence() != null)
283 ds = ds.getDatasetSequence();
285 DBRefEntry[] dbrefs = ds.getDBRefs();
286 if (showDbRefs && dbrefs != null)
288 for (int i = 0; i < dbrefs.length; i++)
291 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
293 maxWidth = Math.max(maxWidth, tmp.length());
297 // ADD NON POSITIONAL SEQUENCE INFO
298 SequenceFeature[] features = sequence.getSequenceFeatures();
299 if (showNpFeats && features != null)
301 for (int i = 0; i < features.length; i++)
303 if (features[i].begin == 0 && features[i].end == 0)
305 int sz = -tip.length();
306 List<SequenceFeature> tfeat = new ArrayList<SequenceFeature>();
307 tfeat.add(features[i]);
308 appendFeatures(tip, 0, tfeat, minmax);
310 maxWidth = Math.max(maxWidth, sz);
315 if (tableWrap && maxWidth > 60)
317 tip.insert(0, "<table width=350 border=0><tr><td><i>");
318 tip.append("</i></td></tr></table>");