2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.IOException;
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 refactored from jalview.gui.AlignFrame and jalview.appletgui.AlignFrame
31 * TODO: consider extending the html renderer to annotate elements with CSS ids enabling finer output format control.
34 public class AlignmentProperties
38 public AlignmentProperties(AlignmentI alignment)
40 this.alignment = alignment;
44 * render the alignment's properties report as text or an HTML fragment
48 public void writeProperties(PrintWriter pw, boolean html)
50 final String nl = html ? "<br>" : System.getProperty("line.separator");
52 int min = Integer.MAX_VALUE, max = 0;
53 for (int i = 0; i < alignment.getHeight(); i++)
55 int size = 1 + alignment.getSequenceAt(i).getEnd()
56 - alignment.getSequenceAt(i).getStart();
63 avg = avg / (float) alignment.getHeight();
65 pw.print("Sequences: " + alignment.getHeight());
67 pw.print("Minimum Sequence Length: " + min);
69 pw.print("Maximum Sequence Length: " + max);
71 pw.print("Average Length: " + (int) avg);
73 if (((Alignment) alignment).alignmentProperties != null)
79 pw.print("<table border=\"1\">");
81 Hashtable props = ((Alignment) alignment).alignmentProperties;
82 Enumeration en = props.keys();
83 while (en.hasMoreElements())
85 String key = en.nextElement().toString();
86 String vals = props.get(key).toString();
89 // wrap the text in the table
90 StringBuffer val = new StringBuffer();
94 npos = vals.indexOf("\n", pos);
97 val.append(vals.substring(pos));
101 val.append(vals.substring(pos, npos));
105 } while (npos != -1);
106 pw.print("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
110 pw.print(nl + key + "\t" + vals);
115 pw.print("</table>");
121 * generate a report as plain text
125 public StringBuffer formatAsString()
127 return formatReport(false);
130 protected StringBuffer formatReport(boolean html)
132 StringWriter content = new StringWriter();
133 writeProperties(new PrintWriter(content), html);
134 return content.getBuffer();
138 * generate a report as a fragment of html
142 public StringBuffer formatAsHtml()
144 return formatReport(true);