2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.StringWriter;
24 import java.io.PrintWriter;
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();
68 avg = avg / (float) alignment.getHeight();
70 pw.print("Sequences: " + alignment.getHeight());
72 pw.print("Minimum Sequence Length: " + min);
74 pw.print("Maximum Sequence Length: " + max);
76 pw.print("Average Length: " + (int) avg);
78 if (((Alignment) alignment).alignmentProperties != null)
84 pw.print("<table border=\"1\">");
86 Hashtable props = ((Alignment) alignment).alignmentProperties;
87 Enumeration en = props.keys();
88 while (en.hasMoreElements())
90 String key = en.nextElement().toString();
91 String vals = props.get(key).toString();
94 // wrap the text in the table
95 StringBuffer val = new StringBuffer();
99 npos = vals.indexOf("\n", pos);
102 val.append(vals.substring(pos));
106 val.append(vals.substring(pos, npos));
110 } while (npos != -1);
111 pw.print("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
115 pw.print(nl + key + "\t" + vals);
120 pw.print("</table>");
126 * generate a report as plain text
130 public StringBuffer formatAsString()
132 return formatReport(false);
135 protected StringBuffer formatReport(boolean html)
137 StringWriter content = new StringWriter();
138 writeProperties(new PrintWriter(content), html);
139 return content.getBuffer();
143 * generate a report as a fragment of html
147 public StringBuffer formatAsHtml()
149 return formatReport(true);