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