2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import java.io.StringWriter;
22 import java.io.PrintWriter;
23 import java.util.Enumeration;
24 import java.util.Hashtable;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentI;
30 * Render associated attributes of an alignment. The heart of this code was
31 * refactored from jalview.gui.AlignFrame and jalview.appletgui.AlignFrame TODO:
32 * consider extending the html renderer to annotate elements with CSS ids
33 * enabling finer output format control.
36 public class AlignmentProperties
40 public AlignmentProperties(AlignmentI alignment)
42 this.alignment = alignment;
46 * render the alignment's properties report as text or an HTML fragment
51 public void writeProperties(PrintWriter pw, boolean html)
53 final String nl = html ? "<br>" : System.getProperty("line.separator");
55 int min = Integer.MAX_VALUE, max = 0;
56 for (int i = 0; i < alignment.getHeight(); i++)
58 int size = 1 + alignment.getSequenceAt(i).getEnd()
59 - alignment.getSequenceAt(i).getStart();
66 avg = avg / (float) alignment.getHeight();
68 pw.print("Sequences: " + alignment.getHeight());
70 pw.print("Minimum Sequence Length: " + min);
72 pw.print("Maximum Sequence Length: " + max);
74 pw.print("Average Length: " + (int) avg);
76 if (((Alignment) alignment).alignmentProperties != null)
82 pw.print("<table border=\"1\">");
84 Hashtable props = ((Alignment) alignment).alignmentProperties;
85 Enumeration en = props.keys();
86 while (en.hasMoreElements())
88 String key = en.nextElement().toString();
89 String vals = props.get(key).toString();
92 // wrap the text in the table
93 StringBuffer val = new StringBuffer();
97 npos = vals.indexOf("\n", pos);
100 val.append(vals.substring(pos));
104 val.append(vals.substring(pos, npos));
108 } while (npos != -1);
109 pw.print("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
113 pw.print(nl + key + "\t" + vals);
118 pw.print("</table>");
124 * generate a report as plain text
128 public StringBuffer formatAsString()
130 return formatReport(false);
133 protected StringBuffer formatReport(boolean html)
135 StringWriter content = new StringWriter();
136 writeProperties(new PrintWriter(content), html);
137 return content.getBuffer();
141 * generate a report as a fragment of html
145 public StringBuffer formatAsHtml()
147 return formatReport(true);