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