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