compactor work
[jalview.git] / forester / java / src / org / forester / archaeopteryx / phylogeny / data / RenderableMsaSequence.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
36 import org.forester.archaeopteryx.Configuration;
37 import org.forester.archaeopteryx.TreePanel;
38 import org.forester.phylogeny.data.PhylogenyData;
39 import org.forester.sequence.MolecularSequence;
40 import org.forester.sequence.MolecularSequence.TYPE;
41
42 public final class RenderableMsaSequence implements RenderablePhylogenyData {
43
44     final static int                     DEFAULT_HEIGHT          = 12;
45     final public static int              DEFAULT_WIDTH           = 400;
46     private double                       _rendering_factor_width = 1.0;
47     private char                         _seq[];
48     private final Rectangle2D            _rectangle              = new Rectangle2D.Float();
49     private double                       _height                 = DEFAULT_HEIGHT;
50     private final float                  _width                  = DEFAULT_WIDTH;
51     private MolecularSequence.TYPE       _type;
52     private static RenderableMsaSequence _instance               = null;
53
54     private RenderableMsaSequence() {
55         _seq = null;
56     }
57
58     @Override
59     public StringBuffer asSimpleText() {
60         return new StringBuffer( _seq.toString() );
61     }
62
63     @Override
64     public StringBuffer asText() {
65         return asSimpleText();
66     }
67
68     @Override
69     public Object clone() {
70         throw new NoSuchMethodError();
71     }
72
73     @Override
74     public PhylogenyData copy() {
75         throw new NoSuchMethodError();
76     }
77
78     @Override
79     public Dimension getOriginalSize() {
80         return new Dimension( getTotalLength(), ( int ) getRenderingHeight() );
81     }
82
83     @Override
84     public Object getParameter() {
85         return null;
86     }
87
88     public double getRenderingFactorWidth() {
89         return _rendering_factor_width;
90     }
91
92     @Override
93     public Dimension getRenderingSize() {
94         return getOriginalSize();
95     }
96
97     public int getTotalLength() {
98         return _seq.length;
99     }
100
101     @Override
102     public boolean isEqual( final PhylogenyData data ) {
103         throw new NoSuchMethodError();
104     }
105
106     @Override
107     public void render( final float x1,
108                         final float y1,
109                         final Graphics2D g,
110                         final TreePanel tree_panel,
111                         final boolean to_pdf ) {
112         final float y = y1;
113         final float start = x1 + 20;
114         final float width = _width / _seq.length;
115         for( int i = 0; i < _seq.length; ++i ) {
116             final char c = _seq[ i ];
117             if ( width < 4 ) {
118                 if ( c != '-' ) {
119                     g.setColor( calculateColor( c ) );
120                     _rectangle.setFrame( start + ( i * width ), y - 0.5, width + 1, getRenderingHeight() );
121                     g.fill( _rectangle );
122                 }
123             }
124             else {
125                 g.setColor( calculateColor( c ) );
126                 g.drawString( String.valueOf( c ), start + ( i * width ), y - 0.5f );
127             }
128         }
129     }
130
131     @Override
132     public void setParameter( final double parameter ) {
133         throw new NoSuchMethodError();
134     }
135
136     public void setRenderingFactorWidth( final double rendering_factor_width ) {
137         _rendering_factor_width = rendering_factor_width;
138     }
139
140     @Override
141     public void setRenderingHeight( final float height ) {
142         _height = height;
143     }
144
145     @Override
146     public StringBuffer toNHX() {
147         throw new NoSuchMethodError();
148     }
149
150     @Override
151     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
152         throw new NoSuchMethodError();
153     }
154
155     private Color calculateColor( final char c ) {
156         if ( _type == TYPE.AA ) {
157             return calculateAAColor( c );
158         }
159         return calculateNucleotideColor( c );
160     }
161
162     private Color calculateNucleotideColor( final char c ) {
163         if ( c == 'A' ) {
164             return Color.YELLOW;
165         }
166         if ( ( c == 'T' ) || ( c == 'U' ) ) {
167             return Color.ORANGE;
168         }
169         if ( c == 'G' ) {
170             return Color.BLUE;
171         }
172         if ( c == 'C' ) {
173             return Color.CYAN;
174         }
175         else if ( c == '-' ) {
176             return Color.GRAY;
177         }
178         else {
179             return Color.GRAY;
180         }
181     }
182
183     private Color calculateAAColor( final char c ) {
184         if ( ( c == 'G' ) || ( c == 'A' ) || ( c == 'S' ) || ( c == 'T' ) ) {
185             return Color.YELLOW;
186         }
187         else if ( ( c == 'N' ) || ( c == 'Q' ) || ( c == 'H' ) ) {
188             return Color.PINK;
189         }
190         else if ( ( c == 'D' ) || ( c == 'E' ) ) {
191             return Color.RED;
192         }
193         else if ( ( c == 'K' ) || ( c == 'R' ) ) {
194             return Color.BLUE;
195         }
196         else if ( c == '-' ) {
197             return Color.GRAY;
198         }
199         else if ( c == 'X' ) {
200             return Color.GRAY;
201         }
202         else {
203             return Color.GREEN;
204         }
205     }
206
207     private double getRenderingHeight() {
208         return _height;
209     }
210
211     public static RenderableMsaSequence createInstance( final String seq,
212                                                         final String type,
213                                                         final Configuration configuration ) {
214         if ( _instance == null ) {
215             _instance = new RenderableMsaSequence();
216         }
217         if ( type.equals( "protein" ) ) {
218             _instance._type = TYPE.AA;
219         }
220         else if ( type.equals( "dna" ) ) {
221             _instance._type = TYPE.DNA;
222         }
223         else {
224             _instance._type = TYPE.RNA;
225         }
226         _instance._seq = seq.toCharArray();
227         if ( configuration != null ) {
228         }
229         return _instance;
230     }
231 }