3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
27 package org.forester.util;
29 public class AsciiHistogram {
31 final private DescriptiveStatistics _stats;
32 final private String _title;
34 public AsciiHistogram( final DescriptiveStatistics stats ) {
39 public AsciiHistogram( final DescriptiveStatistics stats, final String title ) {
44 private void drawToStringBuffer( final double min,
48 final StringBuffer sb,
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 );
62 sb.append( "[" + under + "] " );
63 sb.append( ForesterUtil.LINE_SEPARATOR );
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 ) {
74 sb.append( ForesterUtil.LINE_SEPARATOR );
77 sb.append( "[" + over + "] " );
78 sb.append( ForesterUtil.LINE_SEPARATOR );
82 private DescriptiveStatistics getDescriptiveStatistics() {
86 private String getTitle() {
90 public StringBuffer toStringBuffer( final double min,
92 final int number_of_bins,
97 throw new IllegalArgumentException( "min [" + min + "] is larger than or equal to max [" + max + "]" );
99 if ( number_of_bins < 3 ) {
100 throw new IllegalArgumentException( "number of bins is smaller than 3" );
103 throw new IllegalArgumentException( "size is smaller than 2" );
105 final StringBuffer sb = new StringBuffer();
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 ) {
115 drawToStringBuffer( min, symbol, size, digits, sb, bins, max_count, 0, 0, binning_factor );
119 public StringBuffer toStringBuffer( final int bins, final char symbol, final int size, final int digits ) {
120 return toStringBuffer( getDescriptiveStatistics().getMin(),
121 getDescriptiveStatistics().getMax(),