38f61cf5223bb525c0c133c658231eb098246b81
[jalview.git] / src / jalview / io / AlignmentProperties.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  *  
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.
15  * 
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  */
18 package jalview.io;
19
20 import java.io.StringWriter;
21 import java.io.PrintWriter;
22 import java.util.Enumeration;
23 import java.util.Hashtable;
24
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
27
28 /**
29  * Render associated attributes of an alignment. The heart of this code was
30  * refactored from jalview.gui.AlignFrame and jalview.appletgui.AlignFrame TODO:
31  * consider extending the html renderer to annotate elements with CSS ids
32  * enabling finer output format control.
33  * 
34  */
35 public class AlignmentProperties
36 {
37   AlignmentI alignment;
38
39   public AlignmentProperties(AlignmentI alignment)
40   {
41     this.alignment = alignment;
42   }
43
44   /**
45    * render the alignment's properties report as text or an HTML fragment
46    * 
47    * @param pw
48    * @param html
49    */
50   public void writeProperties(PrintWriter pw, boolean html)
51   {
52     final String nl = html ? "<br>" : System.getProperty("line.separator");
53     float avg = 0;
54     int min = Integer.MAX_VALUE, max = 0;
55     for (int i = 0; i < alignment.getHeight(); i++)
56     {
57       int size = 1 + alignment.getSequenceAt(i).getEnd()
58               - alignment.getSequenceAt(i).getStart();
59       avg += size;
60       if (size > max)
61         max = size;
62       if (size < min)
63         min = size;
64     }
65     avg = avg / (float) alignment.getHeight();
66     pw.print(nl);
67     pw.print("Sequences: " + alignment.getHeight());
68     pw.print(nl);
69     pw.print("Minimum Sequence Length: " + min);
70     pw.print(nl);
71     pw.print("Maximum Sequence Length: " + max);
72     pw.print(nl);
73     pw.print("Average Length: " + (int) avg);
74
75     if (((Alignment) alignment).alignmentProperties != null)
76     {
77       pw.print(nl);
78       pw.print(nl);
79       if (html)
80       {
81         pw.print("<table border=\"1\">");
82       }
83       Hashtable props = ((Alignment) alignment).alignmentProperties;
84       Enumeration en = props.keys();
85       while (en.hasMoreElements())
86       {
87         String key = en.nextElement().toString();
88         String vals = props.get(key).toString();
89         if (html)
90         {
91           // wrap the text in the table
92           StringBuffer val = new StringBuffer();
93           int pos = 0, npos;
94           do
95           {
96             npos = vals.indexOf("\n", pos);
97             if (npos == -1)
98             {
99               val.append(vals.substring(pos));
100             }
101             else
102             {
103               val.append(vals.substring(pos, npos));
104               val.append("<br>");
105             }
106             pos = npos + 1;
107           } while (npos != -1);
108           pw.print("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
109         }
110         else
111         {
112           pw.print(nl + key + "\t" + vals);
113         }
114       }
115       if (html)
116       {
117         pw.print("</table>");
118       }
119     }
120   }
121
122   /**
123    * generate a report as plain text
124    * 
125    * @return
126    */
127   public StringBuffer formatAsString()
128   {
129     return formatReport(false);
130   }
131
132   protected StringBuffer formatReport(boolean html)
133   {
134     StringWriter content = new StringWriter();
135     writeProperties(new PrintWriter(content), html);
136     return content.getBuffer();
137   }
138
139   /**
140    * generate a report as a fragment of html
141    * 
142    * @return
143    */
144   public StringBuffer formatAsHtml()
145   {
146     return formatReport(true);
147   }
148
149 }