JAL-2872 Undid inference disabling (done in Jalview instead)
[jalview.git] / forester / java / src / org / forester / util / AsciiHistogram.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.util;
28
29 public class AsciiHistogram {
30
31     final private DescriptiveStatistics _stats;
32     final private String                _title;
33
34     public AsciiHistogram( final DescriptiveStatistics stats ) {
35         _stats = stats;
36         _title = "";
37     }
38
39     public AsciiHistogram( final DescriptiveStatistics stats, final String title ) {
40         _stats = stats;
41         _title = title;
42     }
43
44     private void drawToStringBuffer( final double min,
45                                      final char symbol,
46                                      final int size,
47                                      final int digits,
48                                      final StringBuffer sb,
49                                      final int[] bins,
50                                      final int max_count,
51                                      final int under,
52                                      final int over,
53                                      final double binning_factor,
54                                      final String indent ) {
55         final double draw_factor = ( double ) max_count / size;
56         final int counts_size = ForesterUtil.roundToInt( Math.log10( max_count ) ) + 1;
57         if ( !ForesterUtil.isEmpty( getTitle() ) ) {
58             sb.append( getTitle() );
59             sb.append( ForesterUtil.LINE_SEPARATOR );
60             sb.append( ForesterUtil.LINE_SEPARATOR );
61         }
62         if ( under > 0 ) {
63             if ( !ForesterUtil.isEmpty( indent ) ) {
64                 sb.append( indent );
65             }
66             sb.append( "[" + under + "] " );
67             sb.append( ForesterUtil.LINE_SEPARATOR );
68         }
69         for( int i = 0; i < bins.length; ++i ) {
70             final int count = bins[ i ];
71             final double label = ForesterUtil.round( ( min + ( i * ( 1.0 / binning_factor ) ) ), digits );
72             if ( !ForesterUtil.isEmpty( indent ) ) {
73                 sb.append( indent );
74             }
75             sb.append( ForesterUtil.pad( label + "", digits, '0', false ) );
76             sb.append( " [" + ForesterUtil.pad( count + "", counts_size, ' ', true ) + "] " );
77             final int s = ForesterUtil.roundToInt( count / draw_factor );
78             for( int j = 0; j < s; ++j ) {
79                 sb.append( symbol );
80             }
81             sb.append( ForesterUtil.LINE_SEPARATOR );
82         }
83         if ( over > 0 ) {
84             if ( !ForesterUtil.isEmpty( indent ) ) {
85                 sb.append( indent );
86             }
87             sb.append( "[" + over + "] " );
88             sb.append( ForesterUtil.LINE_SEPARATOR );
89         }
90     }
91
92     private DescriptiveStatistics getDescriptiveStatistics() {
93         return _stats;
94     }
95
96     private String getTitle() {
97         return _title;
98     }
99
100     public StringBuffer toStringBuffer( final double min,
101                                         final double max,
102                                         final int number_of_bins,
103                                         final char symbol,
104                                         final int size,
105                                         final int digits,
106                                         final String indent ) {
107         if ( min >= max ) {
108             throw new IllegalArgumentException( "min [" + min + "] is larger than or equal to max [" + max + "]" );
109         }
110         if ( number_of_bins < 3 ) {
111             throw new IllegalArgumentException( "number of bins is smaller than 3" );
112         }
113         if ( size < 2 ) {
114             throw new IllegalArgumentException( "size is smaller than 2" );
115         }
116         final StringBuffer sb = new StringBuffer();
117         int max_count = 0;
118         final double binning_factor = number_of_bins / ( max - min );
119         final int[] bins = BasicDescriptiveStatistics
120                 .performBinning( getDescriptiveStatistics().getDataAsDoubleArray(), min, max, number_of_bins );
121         for( final int bin : bins ) {
122             if ( bin > max_count ) {
123                 max_count = bin;
124             }
125         }
126         drawToStringBuffer( min, symbol, size, digits, sb, bins, max_count, 0, 0, binning_factor, indent );
127         return sb;
128     }
129
130     public StringBuffer toStringBuffer( final int bins,
131                                         final char symbol,
132                                         final int size,
133                                         final int digits,
134                                         final String indent ) {
135         return toStringBuffer( getDescriptiveStatistics().getMin(),
136                                getDescriptiveStatistics().getMax(),
137                                bins,
138                                symbol,
139                                size,
140                                digits,
141                                indent );
142     }
143
144     public StringBuffer toStringBuffer( final int bins, final char symbol, final int size, final int digits ) {
145         return toStringBuffer( getDescriptiveStatistics().getMin(),
146                                getDescriptiveStatistics().getMax(),
147                                bins,
148                                symbol,
149                                size,
150                                digits,
151                                null );
152     }
153 }