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 java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.util.Enumeration;
26 import java.util.Hashtable;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
32 * Render associated attributes of an alignment. The heart of this code was
33 * refactored from jalview.gui.AlignFrame and jalview.appletgui.AlignFrame TODO:
34 * consider extending the html renderer to annotate elements with CSS ids
35 * enabling finer output format control.
38 public class AlignmentProperties
42 public AlignmentProperties(AlignmentI alignment)
44 this.alignment = alignment;
48 * render the alignment's properties report as text or an HTML fragment
53 public void writeProperties(PrintWriter pw, boolean html)
55 final String nl = html ? "<br>" : System.getProperty("line.separator");
57 int min = Integer.MAX_VALUE, max = 0;
58 for (int i = 0; i < alignment.getHeight(); i++)
60 int size = 1 + alignment.getSequenceAt(i).getEnd()
61 - alignment.getSequenceAt(i).getStart();
72 avg = avg / alignment.getHeight();
74 pw.print("Sequences: " + alignment.getHeight());
76 pw.print("Minimum Sequence Length: " + min);
78 pw.print("Maximum Sequence Length: " + max);
80 pw.print("Average Length: " + (int) avg);
82 if (((Alignment) alignment).alignmentProperties != null)
88 pw.print("<table border=\"1\">");
90 Hashtable props = ((Alignment) alignment).alignmentProperties;
91 Enumeration en = props.keys();
92 while (en.hasMoreElements())
94 String key = en.nextElement().toString();
95 String vals = props.get(key).toString();
98 // wrap the text in the table
99 StringBuffer val = new StringBuffer();
103 npos = vals.indexOf("\n", pos);
106 val.append(vals.substring(pos));
110 val.append(vals.substring(pos, npos));
114 } while (npos != -1);
115 pw.print("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
119 pw.print(nl + key + "\t" + vals);
124 pw.print("</table>");
130 * generate a report as plain text
134 public StringBuffer formatAsString()
136 return formatReport(false);
139 protected StringBuffer formatReport(boolean html)
141 StringWriter content = new StringWriter();
142 writeProperties(new PrintWriter(content), html);
143 return content.getBuffer();
147 * generate a report as a fragment of html
151 public StringBuffer formatAsHtml()
153 return formatReport(true);