JAL-2360 ColourSchemes holds configured schemes, AlignFrame colour menu
[jalview.git] / src / jalview / schemes / TCoffeeColourScheme.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.schemes;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.SequenceCollectionI;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.TCoffeeScoreFile;
30
31 import java.awt.Color;
32 import java.util.ArrayList;
33 import java.util.IdentityHashMap;
34 import java.util.Map;
35
36 /**
37  * Defines the color score for T-Coffee MSA
38  * <p>
39  * See http://tcoffee.org
40  * 
41  * 
42  * @author Paolo Di Tommaso
43  * 
44  */
45 public class TCoffeeColourScheme extends ResidueColourScheme
46 {
47
48   static final Color[] colors = { new Color(102, 102, 255), // #6666FF
49       new Color(0, 255, 0), // #00FF00
50       new Color(102, 255, 0), // #66FF00
51       new Color(204, 255, 0), // #CCFF00
52       new Color(255, 255, 0), // #FFFF00
53       new Color(255, 204, 0), // #FFCC00
54       new Color(255, 153, 0), // #FF9900
55       new Color(255, 102, 0), // #FF6600
56       new Color(255, 51, 0), // #FF3300
57       new Color(255, 34, 0) // #FF2000
58   };
59
60   IdentityHashMap<SequenceI, Color[]> seqMap;
61
62   /**
63    * Default constructor (required for Class.newInstance())
64    */
65   public TCoffeeColourScheme()
66   {
67
68   }
69
70   /**
71    * the color scheme needs to look at the alignment to get and cache T-COFFEE
72    * scores
73    * 
74    * @param alignment
75    *          - annotated sequences to be searched
76    */
77   public TCoffeeColourScheme(AnnotatedCollectionI alignment)
78   {
79     alignmentChanged(alignment, null);
80   }
81
82   @Override
83   public void alignmentChanged(AnnotatedCollectionI alignment,
84           Map<SequenceI, SequenceCollectionI> hiddenReps)
85   {
86     // TODO: if sequences have been represented and they have scores, could
87     // compute an average sequence score for the representative
88
89     // assume only one set of TCOFFEE scores - but could have more than one
90     // potentially.
91     ArrayList<AlignmentAnnotation> annots = new ArrayList<AlignmentAnnotation>();
92     // Search alignment to get all tcoffee annotation and pick one set of
93     // annotation to use to colour seqs.
94     seqMap = new IdentityHashMap<SequenceI, Color[]>();
95     AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
96             : alignment.getContext();
97     if (alcontext == null)
98     {
99       return;
100     }
101     int w = 0;
102     for (AlignmentAnnotation al : alcontext
103             .findAnnotation(TCoffeeScoreFile.TCOFFEE_SCORE))
104     {
105       if (al.sequenceRef != null && !al.belowAlignment)
106       {
107         annots.add(al);
108         if (w < al.annotations.length)
109         {
110           w = al.annotations.length;
111         }
112         Color[] scores = new Color[al.annotations.length];
113         int i = 0;
114         for (Annotation an : al.annotations)
115         {
116           scores[i++] = (an != null) ? an.colour : Color.white;
117         }
118         seqMap.put(al.sequenceRef, scores);
119       }
120     }
121     // TODO: compute average colour for each symbol type in each column - gives
122     // a second order colourscheme for colouring a sequence logo derived from
123     // the alignment (colour reflects quality of alignment for each residue
124     // class)
125   }
126
127   @Override
128   public Color findColour(char c, int j, SequenceI seq)
129   {
130     Color[] cols;
131
132     if (seqMap == null || (cols = seqMap.get(seq)) == null)
133     {
134       // see above TODO about computing a colour for each residue in each
135       // column: cc = _rcols[i][indexFor[c]];
136       return Color.white;
137     }
138
139     if (j < 0 || j >= cols.length)
140     {
141       return Color.white;
142     }
143     return cols[j];
144   }
145
146   @Override
147   public ColourSchemeI getInstance(AnnotatedCollectionI sg,
148           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
149   {
150     return new TCoffeeColourScheme(sg);
151   }
152
153   /**
154    * Answers true if the annotated data has TCoffee score annotation
155    */
156   @Override
157   public boolean isApplicableTo(AnnotatedCollectionI ac)
158   {
159     if (ac.getContext() != null)
160     {
161       ac = ac.getContext();
162     }
163     AlignmentAnnotation[] anns = ac.getAlignmentAnnotation();
164     if (anns == null)
165     {
166       return false;
167     }
168     for (AlignmentAnnotation ann : anns)
169     {
170       if (TCoffeeScoreFile.TCOFFEE_SCORE.equals(ann.getCalcId()))
171       {
172         return true;
173       }
174     }
175     return false;
176   }
177
178   @Override
179   public String getSchemeName()
180   {
181     return JalviewColourScheme.TCoffee.toString();
182   }
183 }