JAL-2805 JAL-2847 JAL-281 getTreeFile made public
[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)*5;
115        g.setFont(g.getFont().deriveFont( width ));
116         for( int i = 0; i < _seq.length; ++i ) {
117             final char c = _seq[ i ];
118             if ( width < 4 ) {
119                 if ( c != '-' ) {
120                     g.setColor( calculateColor( c ) );
121                     _rectangle.setFrame( start + ( i * width ), y - 0.5, width + 1, getRenderingHeight() );
122                     g.drawString( String.valueOf( c ), start + ( i * width ), y - 0.5f );
123                     //g.fill( _rectangle );
124                 }
125             }
126             else {
127                 g.setColor( calculateColor( c ) );
128                 g.drawString( String.valueOf( c ), start + ( i * width ), y - 0.5f );
129             }
130         }
131     }
132
133     @Override
134     public void setParameter( final double parameter ) {
135         throw new NoSuchMethodError();
136     }
137
138     public void setRenderingFactorWidth( final double rendering_factor_width ) {
139         _rendering_factor_width = rendering_factor_width;
140     }
141
142     @Override
143     public void setRenderingHeight( final float height ) {
144         _height = height;
145     }
146
147     @Override
148     public StringBuffer toNHX() {
149         throw new NoSuchMethodError();
150     }
151
152     @Override
153     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
154         throw new NoSuchMethodError();
155     }
156
157     private Color calculateColor( final char c ) {
158         if ( _type == TYPE.AA ) {
159             return calculateAAColor( c );
160         }
161         return calculateNucleotideColor( c );
162     }
163
164     private Color calculateNucleotideColor( final char c ) {
165         if ( c == 'A' ) {
166             return Color.YELLOW;
167         }
168         if ( ( c == 'T' ) || ( c == 'U' ) ) {
169             return Color.ORANGE;
170         }
171         if ( c == 'G' ) {
172             return Color.BLUE;
173         }
174         if ( c == 'C' ) {
175             return Color.CYAN;
176         }
177         else if ( c == '-' ) {
178             return Color.GRAY;
179         }
180         else {
181             return Color.GRAY;
182         }
183     }
184
185     private Color calculateAAColor( final char c ) {
186         if ( ( c == 'G' ) || ( c == 'A' ) || ( c == 'S' ) || ( c == 'T' ) ) {
187             return Color.YELLOW;
188         }
189         else if ( ( c == 'N' ) || ( c == 'Q' ) || ( c == 'H' ) ) {
190             return Color.PINK;
191         }
192         else if ( ( c == 'D' ) || ( c == 'E' ) ) {
193             return Color.RED;
194         }
195         else if ( ( c == 'K' ) || ( c == 'R' ) ) {
196             return Color.BLUE;
197         }
198         else if ( c == '-' ) {
199             return Color.GRAY;
200         }
201         else if ( c == 'X' ) {
202             return Color.GRAY;
203         }
204         else {
205             return Color.GREEN;
206         }
207     }
208
209     private double getRenderingHeight() {
210         return _height;
211     }
212
213     public static RenderableMsaSequence createInstance( final String seq,
214                                                         final String type,
215                                                         final Configuration configuration ) {
216         if ( _instance == null ) {
217             _instance = new RenderableMsaSequence();
218         }
219         if ( type.equals( "protein" ) ) {
220             _instance._type = TYPE.AA;
221         }
222         else if ( type.equals( "dna" ) ) {
223             _instance._type = TYPE.DNA;
224         }
225         else {
226             _instance._type = TYPE.RNA;
227         }
228         _instance._seq = seq.toCharArray();
229         if ( configuration != null ) {
230         }
231         return _instance;
232     }
233 }