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: https://sites.google.com/site/cmzmasek/home/software/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 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 );
63 if ( !ForesterUtil.isEmpty( indent ) ) {
66 sb.append( "[" + under + "] " );
67 sb.append( ForesterUtil.LINE_SEPARATOR );
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 ) ) {
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 ) {
81 sb.append( ForesterUtil.LINE_SEPARATOR );
84 if ( !ForesterUtil.isEmpty( indent ) ) {
87 sb.append( "[" + over + "] " );
88 sb.append( ForesterUtil.LINE_SEPARATOR );
92 private DescriptiveStatistics getDescriptiveStatistics() {
96 private String getTitle() {
100 public StringBuffer toStringBuffer( final double min,
102 final int number_of_bins,
106 final String indent ) {
108 throw new IllegalArgumentException( "min [" + min + "] is larger than or equal to max [" + max + "]" );
110 if ( number_of_bins < 3 ) {
111 throw new IllegalArgumentException( "number of bins is smaller than 3" );
114 throw new IllegalArgumentException( "size is smaller than 2" );
116 final StringBuffer sb = new StringBuffer();
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 ) {
126 drawToStringBuffer( min, symbol, size, digits, sb, bins, max_count, 0, 0, binning_factor, indent );
130 public StringBuffer toStringBuffer( final int bins,
134 final String indent ) {
135 return toStringBuffer( getDescriptiveStatistics().getMin(),
136 getDescriptiveStatistics().getMax(),
144 public StringBuffer toStringBuffer( final int bins, final char symbol, final int size, final int digits ) {
145 return toStringBuffer( getDescriptiveStatistics().getMin(),
146 getDescriptiveStatistics().getMax(),