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.archaeopteryx.phylogeny.data;
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.math.BigDecimal;
37 import java.util.SortedMap;
39 import org.forester.archaeopteryx.Configuration;
40 import org.forester.archaeopteryx.TreePanel;
41 import org.forester.phylogeny.data.DomainArchitecture;
42 import org.forester.phylogeny.data.PhylogenyData;
43 import org.forester.phylogeny.data.PhylogenyDataUtil;
44 import org.forester.phylogeny.data.ProteinDomain;
45 import org.forester.util.ForesterUtil;
47 public final class RenderableDomainArchitecture extends DomainArchitecture implements RenderablePhylogenyData {
49 static private Map<String, Color> Domain_colors;
50 final static private int BRIGHTEN_COLOR_BY = 200;
51 final static private int E_VALUE_THRESHOLD_EXP_DEFAULT = 0;
52 private static int _Next_default_domain_color = 0;
53 private final static String[] DEFAULT_DOMAINS_COLORS = { "0xFF0000", "0x0000FF", "0xAAAA00", "0xFF00FF",
54 "0x00FFFF", "0x800000", "0x000080", "0x808000", "0x800080", "0x008080", "0xE1B694" };
55 private int _e_value_threshold_exp = RenderableDomainArchitecture.E_VALUE_THRESHOLD_EXP_DEFAULT;
56 private double _rendering_factor_width = 1.0;
57 private double _rendering_height = 0;
58 private final DomainArchitecture _domain_structure;
59 private final Rectangle2D _rectangle = new Rectangle2D.Float();
60 private final Configuration _configuration;
62 public RenderableDomainArchitecture( final DomainArchitecture domain_structure, final Configuration configuration ) {
63 _domain_structure = domain_structure;
64 _configuration = configuration;
67 private Configuration getConfiguration() {
68 return _configuration;
72 public StringBuffer asSimpleText() {
73 return _domain_structure.asSimpleText();
77 public StringBuffer asText() {
78 return _domain_structure.asText();
82 public PhylogenyData copy() {
83 return _domain_structure.copy();
86 private final void drawDomain( final double x,
92 final boolean to_pdf ) {
93 final double h2 = heigth / 2.0;
94 final Color color_one = getColorOne( name );
95 final Color color_two = getColorTwo( color_one );
100 for( double i = 0; i < heigth; i += step ) {
101 g.setColor( org.forester.util.ForesterUtil
102 .calcColor( i >= h2 ? heigth - i : i, 0, h2, color_one, color_two ) );
103 _rectangle.setFrame( x, i + y, width, step );
104 g.fill( _rectangle );
108 private Color getColorOne( final String name ) {
109 Color c = getConfiguration().getDomainStructureBaseColor();
110 if ( RenderableDomainArchitecture.Domain_colors != null ) {
111 c = RenderableDomainArchitecture.Domain_colors.get( name );
113 if ( RenderableDomainArchitecture._Next_default_domain_color < RenderableDomainArchitecture.DEFAULT_DOMAINS_COLORS.length ) {
115 .decode( RenderableDomainArchitecture.DEFAULT_DOMAINS_COLORS[ RenderableDomainArchitecture._Next_default_domain_color++ ] );
116 RenderableDomainArchitecture.Domain_colors.put( name, c );
119 c = getConfiguration().getDomainStructureBaseColor();
126 private Color getColorTwo( final Color color_one ) {
127 final int red = color_one.getRed() + RenderableDomainArchitecture.BRIGHTEN_COLOR_BY;
128 final int green = color_one.getGreen() + RenderableDomainArchitecture.BRIGHTEN_COLOR_BY;
129 final int blue = color_one.getBlue() + RenderableDomainArchitecture.BRIGHTEN_COLOR_BY;
130 return new Color( red > 255 ? 255 : red, green > 255 ? 255 : green, blue > 255 ? 255 : blue );
134 public ProteinDomain getDomain( final int i ) {
135 return _domain_structure.getDomain( i );
139 public SortedMap<BigDecimal, ProteinDomain> getDomains() {
140 return _domain_structure.getDomains();
144 public int getNumberOfDomains() {
145 return _domain_structure.getNumberOfDomains();
149 public Dimension getOriginalSize() {
150 return new Dimension( _domain_structure.getTotalLength(), ForesterUtil.roundToInt( _rendering_height ) );
154 public Object getParameter() {
155 return new Integer( _e_value_threshold_exp );
158 public double getRenderingFactorWidth() {
159 return _rendering_factor_width;
163 public Dimension getRenderingSize() {
164 return new Dimension( ForesterUtil.roundToInt( _domain_structure.getTotalLength() * getRenderingFactorWidth() ),
165 ForesterUtil.roundToInt( _rendering_height ) );
169 public int getTotalLength() {
170 return _domain_structure.getTotalLength();
174 public boolean isEqual( final PhylogenyData data ) {
175 return _domain_structure.isEqual( data );
179 public void render( final double x1,
182 final TreePanel tree_panel,
183 final boolean to_pdf ) {
184 final double f = getRenderingFactorWidth();
185 final double y = y1 + ( _rendering_height / 2 );
186 final double start = x1 + 20.0;
187 g.setColor( getConfiguration().getDomainStructureFontColor() );
188 _rectangle.setFrame( start, y - 0.5, _domain_structure.getTotalLength() * f, 1 );
189 g.fill( _rectangle );
190 for( int i = 0; i < _domain_structure.getDomains().size(); ++i ) {
191 final ProteinDomain d = _domain_structure.getDomain( i );
192 if ( d.getConfidence() <= Math.pow( 10, _e_value_threshold_exp ) ) {
193 final double xa = start + d.getFrom() * f;
194 final double xb = xa + d.getLength() * f;
195 if ( tree_panel.getMainPanel().getOptions().isShowDomainLabels() ) {
196 g.setFont( tree_panel.getMainPanel().getTreeFontSet().getSmallFont() );
197 g.setColor( getConfiguration().getDomainStructureFontColor() );
198 PhylogenyDataUtil.drawString( d.getName(), xa, y1
199 + tree_panel.getMainPanel().getTreeFontSet()._fm_small.getAscent() + 6, g );
201 drawDomain( xa, y1, xb - xa, _rendering_height, d.getName(), g, to_pdf );
207 public void setParameter( final double e_value_threshold_exp ) {
208 _e_value_threshold_exp = ( int ) e_value_threshold_exp;
211 public void setRenderingFactorWidth( final double rendering_factor_width ) {
212 _rendering_factor_width = rendering_factor_width;
216 public void setRenderingHeight( final double rendering_height ) {
217 _rendering_height = rendering_height;
221 public StringBuffer toNHX() {
222 return _domain_structure.toNHX();
226 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
227 _domain_structure.toPhyloXML( writer, level, indentation );
230 public static void setColorMap( final Map<String, Color> domain_colors ) {
231 RenderableDomainArchitecture.Domain_colors = domain_colors;