8d3e56a42647c68fe983f2966ad57cd068b30ee4
[jalview.git] / src / jalview / io / AlignmentProperties.java
1 /**
2  * 
3  */
4 package jalview.io;
5
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;
11
12 import jalview.datamodel.Alignment;
13 import jalview.datamodel.AlignmentI;
14
15 /**
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.
18  * 
19  */
20 public class AlignmentProperties
21 {
22   AlignmentI alignment;
23
24   public AlignmentProperties(AlignmentI alignment)
25   {
26     this.alignment = alignment;
27   }
28
29   /**
30    * render the alignment's properties report as text or an HTML fragment
31    * @param pw
32    * @param html
33    */
34   public void writeProperties(PrintWriter pw, boolean html)
35   {
36     final String nl = html ? "<br>" : System.getProperty("line.separator");
37     float avg = 0;
38     int min = Integer.MAX_VALUE, max = 0;
39     for (int i = 0; i < alignment.getHeight(); i++)
40     {
41       int size = 1 + alignment.getSequenceAt(i).getEnd()
42               - alignment.getSequenceAt(i).getStart();
43       avg += size;
44       if (size > max)
45         max = size;
46       if (size < min)
47         min = size;
48     }
49     avg = avg / (float) alignment.getHeight();
50     pw.print(nl);
51     pw.print("Sequences: " + alignment.getHeight());
52     pw.print(nl);
53     pw.print("Minimum Sequence Length: " + min);
54     pw.print(nl);
55     pw.print("Maximum Sequence Length: " + max);
56     pw.print(nl);
57     pw.print("Average Length: " + (int) avg);
58
59     if (((Alignment) alignment).alignmentProperties != null)
60     {
61       pw.print(nl);
62       pw.print(nl);
63       if (html)
64       {
65         pw.print("<table border=\"1\">");
66       }
67       Hashtable props = ((Alignment) alignment).alignmentProperties;
68       Enumeration en = props.keys();
69       while (en.hasMoreElements())
70       {
71         String key = en.nextElement().toString();
72         String vals = props.get(key).toString();
73         if (html)
74         {
75           // wrap the text in the table
76           StringBuffer val = new StringBuffer();
77           int pos = 0, npos;
78           do
79           {
80             npos = vals.indexOf("\n", pos);
81             if (npos == -1)
82             {
83               val.append(vals.substring(pos));
84             }
85             else
86             {
87               val.append(vals.substring(pos, npos));
88               val.append("<br>");
89             }
90             pos = npos + 1;
91           } while (npos != -1);
92           pw.print("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
93         }
94         else
95         {
96           pw.print(nl + key + "\t" + vals);
97         }
98       }
99       if (html)
100       {
101         pw.print("</table>");
102       }
103     }
104   }
105
106   /**
107    * generate a report as plain text
108    * 
109    * @return
110    */
111   public StringBuffer formatAsString()
112   {
113     return formatReport(false);
114   }
115
116   protected StringBuffer formatReport(boolean html)
117   {
118     StringWriter content = new StringWriter();
119     writeProperties(new PrintWriter(content), html);
120     return content.getBuffer();
121   }
122
123   /**
124    * generate a report as a fragment of html
125    * 
126    * @return
127    */
128   public StringBuffer formatAsHtml()
129   {
130     return formatReport(true);
131   }
132
133 }