in progress...
[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: www.phylosoft.org/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 double draw_factor = ( double ) max_count / size;
55         final int counts_size = ForesterUtil.roundToInt( Math.log10( max_count ) ) + 1;
56         if ( !ForesterUtil.isEmpty( getTitle() ) ) {
57             sb.append( getTitle() );
58             sb.append( ForesterUtil.LINE_SEPARATOR );
59             sb.append( ForesterUtil.LINE_SEPARATOR );
60         }
61         if ( under > 0 ) {
62             sb.append( "[" + under + "] " );
63             sb.append( ForesterUtil.LINE_SEPARATOR );
64         }
65         for( int i = 0; i < bins.length; ++i ) {
66             final int count = bins[ i ];
67             final double label = ForesterUtil.round( ( min + i * ( 1.0 / binning_factor ) ), digits );
68             sb.append( ForesterUtil.pad( label + "", digits, '0', false ) );
69             sb.append( " [" + ForesterUtil.pad( count + "", counts_size, ' ', true ) + "] " );
70             final int s = ForesterUtil.roundToInt( count / draw_factor );
71             for( int j = 0; j < s; ++j ) {
72                 sb.append( symbol );
73             }
74             sb.append( ForesterUtil.LINE_SEPARATOR );
75         }
76         if ( over > 0 ) {
77             sb.append( "[" + over + "] " );
78             sb.append( ForesterUtil.LINE_SEPARATOR );
79         }
80     }
81
82     private DescriptiveStatistics getDescriptiveStatistics() {
83         return _stats;
84     }
85
86     private String getTitle() {
87         return _title;
88     }
89
90     public StringBuffer toStringBuffer( final double min,
91                                         final double max,
92                                         final int number_of_bins,
93                                         final char symbol,
94                                         final int size,
95                                         final int digits ) {
96         if ( min >= max ) {
97             throw new IllegalArgumentException( "min [" + min + "] is larger than or equal to max [" + max + "]" );
98         }
99         if ( number_of_bins < 3 ) {
100             throw new IllegalArgumentException( "number of bins is smaller than 3" );
101         }
102         if ( size < 2 ) {
103             throw new IllegalArgumentException( "size is smaller than 2" );
104         }
105         final StringBuffer sb = new StringBuffer();
106         int max_count = 0;
107         final double binning_factor = number_of_bins / ( max - min );
108         final int[] bins = BasicDescriptiveStatistics
109                 .performBinning( getDescriptiveStatistics().getDataAsDoubleArray(), min, max, number_of_bins );
110         for( final int bin : bins ) {
111             if ( bin > max_count ) {
112                 max_count = bin;
113             }
114         }
115         drawToStringBuffer( min, symbol, size, digits, sb, bins, max_count, 0, 0, binning_factor );
116         return sb;
117     }
118
119     public StringBuffer toStringBuffer( final int bins, final char symbol, final int size, final int digits ) {
120         return toStringBuffer( getDescriptiveStatistics().getMin(),
121                                getDescriptiveStatistics().getMax(),
122                                bins,
123                                symbol,
124                                size,
125                                digits );
126     }
127 }