6 import java.io.IOException;
7 import java.io.StringWriter;
8 import java.io.PrintWriter;
9 import java.util.Enumeration;
10 import java.util.Hashtable;
12 import jalview.datamodel.Alignment;
13 import jalview.datamodel.AlignmentI;
16 * Render associated attributes of an alignment. The heart of this code was refactored from jalview.gui.AlignFrame and jalview.appletgui.AlignFrame
17 * TODO: consider extending the html renderer to annotate elements with CSS ids enabling finer output format control.
20 public class AlignmentProperties
24 public AlignmentProperties(AlignmentI alignment)
26 this.alignment = alignment;
30 * render the alignment's properties report as text or an HTML fragment
34 public void writeProperties(PrintWriter pw, boolean html)
36 final String nl = html ? "<br>" : "\n";
38 int min = Integer.MAX_VALUE, max = 0;
39 for (int i = 0; i < alignment.getHeight(); i++)
41 int size = 1 + alignment.getSequenceAt(i).getEnd()
42 - alignment.getSequenceAt(i).getStart();
49 avg = avg / (float) alignment.getHeight();
51 pw.append("Sequences: " + alignment.getHeight());
53 pw.append("Minimum Sequence Length: " + min);
55 pw.append("Maximum Sequence Length: " + max);
57 pw.append("Average Length: " + (int) avg);
59 if (((Alignment) alignment).alignmentProperties != null)
65 pw.append("<table border=\"1\">");
67 Hashtable props = ((Alignment) alignment).alignmentProperties;
68 Enumeration en = props.keys();
69 while (en.hasMoreElements())
71 String key = en.nextElement().toString();
72 String vals = props.get(key).toString();
75 // wrap the text in the table
76 StringBuffer val = new StringBuffer();
80 npos = vals.indexOf("\n", pos);
83 val.append(vals.substring(pos));
87 val.append(vals.substring(pos, npos));
92 pw.append("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
96 pw.append("\n" + key + "\t" + vals);
101 pw.append("</table>");
107 * generate a report as plain text
111 public StringBuffer formatAsString()
113 return formatReport(false);
116 protected StringBuffer formatReport(boolean html)
118 StringWriter content = new StringWriter();
119 writeProperties(new PrintWriter(content), html);
120 return content.getBuffer();
124 * generate a report as a fragment of html
128 public StringBuffer formatAsHtml()
130 return formatReport(true);