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