2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import java.io.StringWriter;
21 import java.io.PrintWriter;
22 import java.util.Enumeration;
23 import java.util.Hashtable;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
29 * Render associated attributes of an alignment. The heart of this code was
30 * refactored from jalview.gui.AlignFrame and jalview.appletgui.AlignFrame TODO:
31 * consider extending the html renderer to annotate elements with CSS ids
32 * enabling finer output format control.
35 public class AlignmentProperties
39 public AlignmentProperties(AlignmentI alignment)
41 this.alignment = alignment;
45 * render the alignment's properties report as text or an HTML fragment
50 public void writeProperties(PrintWriter pw, boolean html)
52 final String nl = html ? "<br>" : System.getProperty("line.separator");
54 int min = Integer.MAX_VALUE, max = 0;
55 for (int i = 0; i < alignment.getHeight(); i++)
57 int size = 1 + alignment.getSequenceAt(i).getEnd()
58 - alignment.getSequenceAt(i).getStart();
65 avg = avg / (float) alignment.getHeight();
67 pw.print("Sequences: " + alignment.getHeight());
69 pw.print("Minimum Sequence Length: " + min);
71 pw.print("Maximum Sequence Length: " + max);
73 pw.print("Average Length: " + (int) avg);
75 if (((Alignment) alignment).alignmentProperties != null)
81 pw.print("<table border=\"1\">");
83 Hashtable props = ((Alignment) alignment).alignmentProperties;
84 Enumeration en = props.keys();
85 while (en.hasMoreElements())
87 String key = en.nextElement().toString();
88 String vals = props.get(key).toString();
91 // wrap the text in the table
92 StringBuffer val = new StringBuffer();
96 npos = vals.indexOf("\n", pos);
99 val.append(vals.substring(pos));
103 val.append(vals.substring(pos, npos));
107 } while (npos != -1);
108 pw.print("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
112 pw.print(nl + key + "\t" + vals);
117 pw.print("</table>");
123 * generate a report as plain text
127 public StringBuffer formatAsString()
129 return formatReport(false);
132 protected StringBuffer formatReport(boolean html)
134 StringWriter content = new StringWriter();
135 writeProperties(new PrintWriter(content), html);
136 return content.getBuffer();
140 * generate a report as a fragment of html
144 public StringBuffer formatAsHtml()
146 return formatReport(true);