fixing vector size issue...
[jalview.git] / forester / java / src / org / forester / archaeopteryx / phylogeny / data / RenderableVector.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.archaeopteryx.phylogeny.data;
28
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.Graphics2D;
32 import java.awt.geom.Rectangle2D;
33 import java.io.IOException;
34 import java.io.Writer;
35 import java.util.List;
36
37 import org.forester.archaeopteryx.AptxUtil;
38 import org.forester.archaeopteryx.Configuration;
39 import org.forester.archaeopteryx.TreePanel;
40 import org.forester.phylogeny.data.PhylogenyData;
41 import org.forester.util.DescriptiveStatistics;
42 import org.forester.util.ForesterUtil;
43
44 public final class RenderableVector implements RenderablePhylogenyData {
45
46     final static int         VECTOR_DEFAULT_HEIGHT          = 12;
47     public final static  int         VECTOR_DEFAULT_WIDTH           = 120;
48     private double                  _rendering_factor_width = 1.0;
49     private List<Double>            _values;
50     private final Rectangle2D       _rectangle              = new Rectangle2D.Float();
51    
52     private double                  _height =  VECTOR_DEFAULT_HEIGHT;
53     private double                  _min;
54     private double                  _max;
55     private double                  _mean;
56     private  Color _min_color = Color.BLUE;
57     private  Color _max_color = Color.YELLOW;
58     private  Color _mean_color = Color.WHITE;
59     private  int _width = VECTOR_DEFAULT_WIDTH;
60     
61     
62     private static RenderableVector _instance               = null;
63
64     private RenderableVector() {
65         _values = null;
66     }
67
68     @Override
69     public StringBuffer asSimpleText() {
70         return new StringBuffer( _values.toString() );
71     }
72
73     @Override
74     public StringBuffer asText() {
75         return asSimpleText();
76     }
77
78     @Override
79     public Object clone() {
80         throw new NoSuchMethodError();
81     }
82
83     @Override
84     public PhylogenyData copy() {
85         throw new NoSuchMethodError();
86     }
87
88     @Override
89     public Dimension getOriginalSize() {
90         return new Dimension( getTotalLength(), ( int ) getRenderingHeight() );
91     }
92
93     @Override
94     public Object getParameter() {
95         return null;
96     }
97
98     public double getRenderingFactorWidth() {
99         return _rendering_factor_width;
100     }
101
102     @Override
103     public Dimension getRenderingSize() {
104         return getOriginalSize();
105     }
106
107     public int getTotalLength() {
108         return ( int ) ( _values.size() * getRenderingHeight() );
109     }
110
111     @Override
112     public boolean isEqual( final PhylogenyData data ) {
113         throw new NoSuchMethodError();
114     }
115
116     @Override
117     public void render( final double x1,
118                         final double y1,
119                         final Graphics2D g,
120                         final TreePanel tree_panel,
121                         final boolean to_pdf ) {
122         final double y = y1;
123         final double start = x1 + 20.0;
124         final double width = ( double ) _width / _values.size();
125         for( int i = 0; i < _values.size(); ++i ) {
126             g.setColor( calculateColor( _values.get( i ) ) );
127             _rectangle.setFrame( start + ( i * width ), y - 0.5, width, getRenderingHeight() );
128             g.fill( _rectangle );
129         }
130     }
131
132     @Override
133     public void setParameter( final double parameter ) {
134         throw new NoSuchMethodError();
135     }
136
137     public void setRenderingFactorWidth( final double rendering_factor_width ) {
138         _rendering_factor_width = rendering_factor_width;
139     }
140
141     @Override
142     public void setRenderingHeight( final double height ) {
143         _height = height;
144     }
145
146     @Override
147     public StringBuffer toNHX() {
148         throw new NoSuchMethodError();
149     }
150
151     @Override
152     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
153         throw new NoSuchMethodError();
154     }
155
156     private Color calculateColor( final double v ) {
157         return ForesterUtil.calcColor( v, _min, _max, _mean, _min_color, _max_color, _mean_color );
158     }
159
160     private double getRenderingHeight() {
161         return _height;
162     }
163
164     public static RenderableVector createInstance( final List<Double> values,
165                                                    final DescriptiveStatistics stats,
166                                                    final Configuration configuration ) {
167         if ( _instance == null ) {
168             _instance = new RenderableVector();
169         }
170        
171         _instance._values = values;
172         
173         if ( configuration != null ) {
174             _instance._min_color =configuration.getVectorDataMinColor();
175             _instance._max_color = configuration.getVectorDataMaxColor();
176             _instance._mean_color = configuration.getVectorDataMeanColor();
177             _instance._width = configuration.getVectorDataWidth();
178             _instance._height = configuration.getVectorDataHeight();
179         }
180         
181         
182         if ( stats.getN() > 0 ) {
183             _instance._min = stats.getMin();
184             _instance._max = stats.getMax();
185             _instance._mean = stats.arithmeticMean();
186         }
187         else {
188             _instance._min = 0;
189             _instance._max = 0;
190             _instance._mean = 0;
191             AptxUtil.printWarningMessage( "Archaeopteryx", "creating renderable vector with empty statistics" );
192         }
193         return _instance;
194     }
195 }