9f33fb438405cb342f2cf5f41287dc087dd05ece
[jalview.git] / src / jalview / io / AlignmentProperties.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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 java.io.StringWriter;
24 import java.io.PrintWriter;
25 import java.util.Enumeration;
26 import java.util.Hashtable;
27
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
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         max = size;
65       if (size < min)
66         min = size;
67     }
68     avg = avg / (float) alignment.getHeight();
69     pw.print(nl);
70     pw.print("Sequences: " + alignment.getHeight());
71     pw.print(nl);
72     pw.print("Minimum Sequence Length: " + min);
73     pw.print(nl);
74     pw.print("Maximum Sequence Length: " + max);
75     pw.print(nl);
76     pw.print("Average Length: " + (int) avg);
77
78     if (((Alignment) alignment).alignmentProperties != null)
79     {
80       pw.print(nl);
81       pw.print(nl);
82       if (html)
83       {
84         pw.print("<table border=\"1\">");
85       }
86       Hashtable props = ((Alignment) alignment).alignmentProperties;
87       Enumeration en = props.keys();
88       while (en.hasMoreElements())
89       {
90         String key = en.nextElement().toString();
91         String vals = props.get(key).toString();
92         if (html)
93         {
94           // wrap the text in the table
95           StringBuffer val = new StringBuffer();
96           int pos = 0, npos;
97           do
98           {
99             npos = vals.indexOf("\n", pos);
100             if (npos == -1)
101             {
102               val.append(vals.substring(pos));
103             }
104             else
105             {
106               val.append(vals.substring(pos, npos));
107               val.append("<br>");
108             }
109             pos = npos + 1;
110           } while (npos != -1);
111           pw.print("<tr><td>" + key + "</td><td>" + val + "</td></tr>");
112         }
113         else
114         {
115           pw.print(nl + key + "\t" + vals);
116         }
117       }
118       if (html)
119       {
120         pw.print("</table>");
121       }
122     }
123   }
124
125   /**
126    * generate a report as plain text
127    * 
128    * @return
129    */
130   public StringBuffer formatAsString()
131   {
132     return formatReport(false);
133   }
134
135   protected StringBuffer formatReport(boolean html)
136   {
137     StringWriter content = new StringWriter();
138     writeProperties(new PrintWriter(content), html);
139     return content.getBuffer();
140   }
141
142   /**
143    * generate a report as a fragment of html
144    * 
145    * @return
146    */
147   public StringBuffer formatAsHtml()
148   {
149     return formatReport(true);
150   }
151
152 }