1 /*
2  * ParsimonyBranchDecorator.java
3  *
4  * Copyright (C) 2006-2014 Andrew Rambaut
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 
21 package figtree.treeviewer.decorators;
22 
23 import jebl.evolution.trees.Tree;
24 import jebl.evolution.graphs.Node;
25 import jebl.evolution.alignments.Pattern;
26 import jebl.evolution.parsimony.ParsimonyCriterion;
27 import jebl.evolution.parsimony.FitchParsimony;
28 import jebl.evolution.sequences.State;
29 import jebl.evolution.sequences.Nucleotides;
30 import jebl.evolution.sequences.SequenceType;
31 import jebl.evolution.sequences.AminoAcids;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.awt.*;
36 
37 /**
38  * @author Andrew Rambaut
39  * @version $Id$
40  *
41  * $HeadURL$
42  *
43  * $LastChangedBy$
44  * $LastChangedDate$
45  * $LastChangedRevision$
46  */
47 public class ParsimonyBranchDecorator {
48 
ParsimonyBranchDecorator(Pattern pattern)49     public ParsimonyBranchDecorator(Pattern pattern) {
50         List<Pattern> patterns = new ArrayList<Pattern>();
51         patterns.add(pattern);
52         parsimony = new FitchParsimony(patterns, true);
53 
54         if (pattern.getSequenceType() == SequenceType.NUCLEOTIDE) {
55             paints = new Paint[Nucleotides.getStateCount()];
56             paints[Nucleotides.A_STATE.getIndex()] = Color.RED;
57             paints[Nucleotides.C_STATE.getIndex()] = Color.BLUE;
58             paints[Nucleotides.G_STATE.getIndex()] = Color.BLACK;
59             paints[Nucleotides.T_STATE.getIndex()] = Color.GREEN;
60             paints[Nucleotides.R_STATE.getIndex()] = Color.MAGENTA;
61             paints[Nucleotides.Y_STATE.getIndex()] = Color.DARK_GRAY;
62             paints[Nucleotides.M_STATE.getIndex()] = Color.DARK_GRAY;
63             paints[Nucleotides.W_STATE.getIndex()] = Color.DARK_GRAY;
64             paints[Nucleotides.S_STATE.getIndex()] = Color.DARK_GRAY;
65             paints[Nucleotides.K_STATE.getIndex()] = Color.DARK_GRAY;
66             paints[Nucleotides.B_STATE.getIndex()] = Color.DARK_GRAY;
67             paints[Nucleotides.D_STATE.getIndex()] = Color.DARK_GRAY;
68             paints[Nucleotides.H_STATE.getIndex()] = Color.DARK_GRAY;
69             paints[Nucleotides.V_STATE.getIndex()] = Color.DARK_GRAY;
70             paints[Nucleotides.N_STATE.getIndex()] = Color.GRAY;
71             paints[Nucleotides.UNKNOWN_STATE.getIndex()] = Color.GRAY;
72             paints[Nucleotides.GAP_STATE.getIndex()] = Color.GRAY;
73         } else if (pattern.getSequenceType() == SequenceType.AMINO_ACID) {
74             paints = new Paint[AminoAcids.getStateCount()];
75             paints[AminoAcids.A_STATE.getIndex()] = new Color(204, 255, 255);
76             paints[AminoAcids.C_STATE.getIndex()] = new Color(0, 255, 255);
77             paints[AminoAcids.D_STATE.getIndex()] = new Color(255, 204, 153);
78             paints[AminoAcids.E_STATE.getIndex()] = new Color(255, 204, 0);
79             paints[AminoAcids.F_STATE.getIndex()] = new Color(0, 204, 255);
80             paints[AminoAcids.G_STATE.getIndex()] = new Color(0, 255, 0);
81             paints[AminoAcids.H_STATE.getIndex()] = new Color(255, 255, 153);
82             paints[AminoAcids.I_STATE.getIndex()] = new Color(0, 0, 128);
83             paints[AminoAcids.K_STATE.getIndex()] = new Color(198, 66, 0);
84             paints[AminoAcids.L_STATE.getIndex()] = new Color(51, 102, 255);
85             paints[AminoAcids.M_STATE.getIndex()] = new Color(153, 204, 255);
86             paints[AminoAcids.N_STATE.getIndex()] = new Color(255, 153, 0);
87             paints[AminoAcids.P_STATE.getIndex()] = new Color(255, 255, 0);
88             paints[AminoAcids.Q_STATE.getIndex()] = new Color(255, 102, 0);
89             paints[AminoAcids.R_STATE.getIndex()] = new Color(230, 6, 6);
90             paints[AminoAcids.S_STATE.getIndex()] = new Color(204, 255, 153);
91             paints[AminoAcids.T_STATE.getIndex()] = new Color(0, 255, 153);
92             paints[AminoAcids.V_STATE.getIndex()] = new Color(0, 0, 255);
93             paints[AminoAcids.W_STATE.getIndex()] = new Color(204, 153, 255);
94             paints[AminoAcids.Y_STATE.getIndex()] = new Color(204, 255, 204);
95             paints[AminoAcids.B_STATE.getIndex()] = Color.DARK_GRAY;
96             paints[AminoAcids.Z_STATE.getIndex()] = Color.DARK_GRAY;
97             paints[AminoAcids.X_STATE.getIndex()] = Color.GRAY;
98             paints[AminoAcids.UNKNOWN_STATE.getIndex()] = Color.GRAY;
99             paints[AminoAcids.STOP_STATE.getIndex()] = Color.GRAY;
100             paints[AminoAcids.GAP_STATE.getIndex()] = Color.GRAY;
101         } else {
102             throw new IllegalArgumentException("Unsupported sequence type");
103         }
104     }
105 
getBranchPaint(Tree tree, Node node)106     public Paint getBranchPaint(Tree tree, Node node) {
107         State[] states = parsimony.getStates(tree, node);
108 
109         return paints[states[0].getIndex()];
110     }
111 
112     private final ParsimonyCriterion parsimony;
113     private final Paint[] paints;
114 }