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.
24 import java.util.Enumeration;
25 import java.util.Hashtable;
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentI;
31 * Render associated attributes of an alignment. The heart of this code was
32 * refactored from jalview.gui.AlignFrame and jalview.appletgui.AlignFrame TODO:
33 * consider extending the html renderer to annotate elements with CSS ids
34 * enabling finer output format control.
37 public class AlignmentProperties
41 public AlignmentProperties(AlignmentI alignment)
43 this.alignment = alignment;
47 * render the alignment's properties report as text or an HTML fragment
52 public void writeProperties(StringBuffer sb, boolean html)
54 final String nl = html ? "<br>" : System.getProperty("line.separator");
56 int min = Integer.MAX_VALUE, max = 0;
57 for (int i = 0; i < alignment.getHeight(); i++)
59 int size = 1 + alignment.getSequenceAt(i).getEnd()
60 - alignment.getSequenceAt(i).getStart();
71 avg = avg / alignment.getHeight();
73 sb.append("Sequences: " + alignment.getHeight());
75 sb.append("Minimum Sequence Length: " + min);
77 sb.append("Maximum Sequence Length: " + max);
79 sb.append("Average Length: " + (int) avg);
81 if (((Alignment) alignment).alignmentProperties != null)
87 sb.append("<table border=\"1\">");
89 Hashtable props = ((Alignment) alignment).alignmentProperties;
90 Enumeration en = props.keys();
91 while (en.hasMoreElements())
93 String key = en.nextElement().toString();
94 String vals = props.get(key).toString();
97 // wrap the text in the table
98 StringBuffer val = new StringBuffer();
102 npos = vals.indexOf("\n", pos);
105 val.append(vals.substring(pos));
109 val.append(vals.substring(pos, npos));
113 } while (npos != -1);
114 sb.append("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
118 sb.append(nl + key + "\t" + vals);
123 sb.append("</table>");
129 * generate a report as plain text
133 public StringBuffer formatAsString()
135 return formatReport(false);
138 protected StringBuffer formatReport(boolean html)
140 StringBuffer sb = new StringBuffer(); // BH was PrintBuffer
141 writeProperties(sb, html);
146 * generate a report as a fragment of html
150 public StringBuffer formatAsHtml()
152 return formatReport(true);