clean up
[jalview.git] / forester / java / src / org / forester / archaeopteryx / phylogeny / data / RenderableDomainArchitecture.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.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.Map;
36 import java.util.SortedMap;
37
38 import org.forester.archaeopteryx.Configuration;
39 import org.forester.archaeopteryx.TreePanel;
40 import org.forester.phylogeny.data.DomainArchitecture;
41 import org.forester.phylogeny.data.PhylogenyData;
42 import org.forester.phylogeny.data.PhylogenyDataUtil;
43 import org.forester.phylogeny.data.ProteinDomain;
44 import org.forester.util.ForesterUtil;
45
46 public final class RenderableDomainArchitecture extends DomainArchitecture implements RenderablePhylogenyData {
47
48     static private Map<String, Color> Domain_colors;
49     final static private int          BRIGHTEN_COLOR_BY             = 200;
50     final static private int          E_VALUE_THRESHOLD_EXP_DEFAULT = 0;
51     private static int                _Next_default_domain_color    = 0;
52     private final static String[]     DEFAULT_DOMAINS_COLORS        = { "0xFF0000", "0x0000FF", "0xAAAA00", "0xFF00FF",
53             "0x00FFFF", "0x800000", "0x000080", "0x808000", "0x800080", "0x008080", "0xE1B694" };
54     private int                       _e_value_threshold_exp        = RenderableDomainArchitecture.E_VALUE_THRESHOLD_EXP_DEFAULT;
55     private double                    _rendering_factor_width       = 1.0;
56     private double                    _rendering_height             = 0;
57     private final DomainArchitecture  _domain_structure;
58     private final Rectangle2D         _rectangle                    = new Rectangle2D.Float();
59     private final Configuration       _configuration;
60
61     public RenderableDomainArchitecture( final DomainArchitecture domain_structure, final Configuration configuration ) {
62         _domain_structure = domain_structure;
63         _configuration = configuration;
64     }
65
66     private Configuration getConfiguration() {
67         return _configuration;
68     }
69
70     @Override
71     public StringBuffer asSimpleText() {
72         return _domain_structure.asSimpleText();
73     }
74
75     @Override
76     public StringBuffer asText() {
77         return _domain_structure.asText();
78     }
79
80     @Override
81     public PhylogenyData copy() {
82         return _domain_structure.copy();
83     }
84
85     private final void drawDomain( final double x,
86                                    final double y,
87                                    final double width,
88                                    final double heigth,
89                                    final String name,
90                                    final Graphics2D g,
91                                    final boolean to_pdf ) {
92         final double h2 = heigth / 2.0;
93         final Color color_one = getColorOne( name );
94         final Color color_two = getColorTwo( color_one );
95         double step = 1;
96         if ( to_pdf ) {
97             step = 0.1;
98         }
99         for( double i = 0; i < heigth; i += step ) {
100             g.setColor( org.forester.util.ForesterUtil
101                     .calcColor( i >= h2 ? heigth - i : i, 0, h2, color_one, color_two ) );
102             _rectangle.setFrame( x, i + y, width, step );
103             g.fill( _rectangle );
104         }
105     }
106
107     private Color getColorOne( final String name ) {
108         Color c = getConfiguration().getDomainStructureBaseColor();
109         if ( RenderableDomainArchitecture.Domain_colors != null ) {
110             c = RenderableDomainArchitecture.Domain_colors.get( name );
111             if ( c == null ) {
112                 if ( RenderableDomainArchitecture._Next_default_domain_color < RenderableDomainArchitecture.DEFAULT_DOMAINS_COLORS.length ) {
113                     c = Color
114                             .decode( RenderableDomainArchitecture.DEFAULT_DOMAINS_COLORS[ RenderableDomainArchitecture._Next_default_domain_color++ ] );
115                     RenderableDomainArchitecture.Domain_colors.put( name, c );
116                 }
117                 else {
118                     c = getConfiguration().getDomainStructureBaseColor();
119                 }
120             }
121         }
122         return c;
123     }
124
125     private Color getColorTwo( final Color color_one ) {
126         final int red = color_one.getRed() + RenderableDomainArchitecture.BRIGHTEN_COLOR_BY;
127         final int green = color_one.getGreen() + RenderableDomainArchitecture.BRIGHTEN_COLOR_BY;
128         final int blue = color_one.getBlue() + RenderableDomainArchitecture.BRIGHTEN_COLOR_BY;
129         return new Color( red > 255 ? 255 : red, green > 255 ? 255 : green, blue > 255 ? 255 : blue );
130     }
131
132     @Override
133     public ProteinDomain getDomain( final int i ) {
134         return _domain_structure.getDomain( i );
135     }
136
137     @Override
138     public SortedMap<Double, ProteinDomain> getDomains() {
139         return _domain_structure.getDomains();
140     }
141
142     @Override
143     public int getNumberOfDomains() {
144         return _domain_structure.getNumberOfDomains();
145     }
146
147     @Override
148     public Dimension getOriginalSize() {
149         return new Dimension( _domain_structure.getTotalLength(), ForesterUtil.roundToInt( _rendering_height ) );
150     }
151
152     @Override
153     public Object getParameter() {
154         return new Integer( _e_value_threshold_exp );
155     }
156
157     public double getRenderingFactorWidth() {
158         return _rendering_factor_width;
159     }
160
161     @Override
162     public Dimension getRenderingSize() {
163         return new Dimension( ForesterUtil.roundToInt( _domain_structure.getTotalLength() * _rendering_factor_width ),
164                               ForesterUtil.roundToInt( _rendering_height ) );
165     }
166
167     @Override
168     public int getTotalLength() {
169         return _domain_structure.getTotalLength();
170     }
171
172     @Override
173     public boolean isEqual( final PhylogenyData data ) {
174         return _domain_structure.isEqual( data );
175     }
176
177     @Override
178     public void render( final double x1,
179                         final double y1,
180                         final Graphics2D g,
181                         final TreePanel tree_panel,
182                         final boolean to_pdf ) {
183         final double f = getRenderingFactorWidth();
184         final double y = y1 + ( _rendering_height / 2 );
185         final double start = x1 + 20.0;
186         g.setColor( getConfiguration().getDomainStructureFontColor() );
187         _rectangle.setFrame( start, y - 0.5, _domain_structure.getTotalLength() * f, 1 );
188         g.fill( _rectangle );
189         for( int i = 0; i < _domain_structure.getDomains().size(); ++i ) {
190             final ProteinDomain d = _domain_structure.getDomain( i );
191             if ( d.getConfidence() <= Math.pow( 10, _e_value_threshold_exp ) ) {
192                 final double xa = start + d.getFrom() * f;
193                 final double xb = xa + d.getLength() * f;
194                 if ( tree_panel.getMainPanel().getOptions().isShowDomainLabels() ) {
195                     g.setFont( tree_panel.getMainPanel().getTreeFontSet().getSmallFont() );
196                     g.setColor( getConfiguration().getDomainStructureFontColor() );
197                     PhylogenyDataUtil.drawString( d.getName(), xa, y1
198                             + tree_panel.getMainPanel().getTreeFontSet()._fm_small.getAscent() + 6, g );
199                 }
200                 drawDomain( xa, y1, xb - xa, _rendering_height, d.getName(), g, to_pdf );
201             }
202         }
203     }
204
205     @Override
206     public void setParameter( final double e_value_threshold_exp ) {
207         _e_value_threshold_exp = ( int ) e_value_threshold_exp;
208     }
209
210     public void setRenderingFactorWidth( final double rendering_factor_width ) {
211         _rendering_factor_width = rendering_factor_width;
212     }
213
214     @Override
215     public void setRenderingHeight( final double rendering_height ) {
216         _rendering_height = rendering_height;
217     }
218
219     @Override
220     public StringBuffer toNHX() {
221         return _domain_structure.toNHX();
222     }
223
224     @Override
225     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
226         _domain_structure.toPhyloXML( writer, level, indentation );
227     }
228
229     public static void setColorMap( final Map<String, Color> domain_colors ) {
230         RenderableDomainArchitecture.Domain_colors = domain_colors;
231     }
232 }