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