ac173bbd2d4b2580a0d730c5146a83238dff8f2f
[jalview.git] / src / jalview / schemes / TCoffeeColourScheme.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.schemes;
19
20 import jalview.analysis.SequenceIdMatcher;
21 import jalview.datamodel.AlignmentAnnotation;
22 import jalview.datamodel.AlignmentI;
23 import jalview.datamodel.AnnotatedCollectionI;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.SequenceCollectionI;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.TCoffeeScoreFile;
28
29 import java.awt.Color;
30 import java.util.ArrayList;
31 import java.util.IdentityHashMap;
32 import java.util.Map;
33 import java.util.TreeMap;
34
35 /**
36  * Defines the color score for T-Coffee MSA
37  * <p>
38  * See http://tcoffee.org
39  * 
40  * 
41  * @author Paolo Di Tommaso
42  * 
43  */
44 public class TCoffeeColourScheme extends ResidueColourScheme
45 {
46
47   static final Color[] colors =
48   { 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    * the color scheme needs to look at the alignment to get and cache T-COFFEE
64    * scores
65    * 
66    * @param alignment
67    *          - annotated sequences to be searched
68    */
69   public TCoffeeColourScheme(AnnotatedCollectionI alignment)
70   {
71     alignmentChanged(alignment, null);
72   }
73
74   @Override
75   public void alignmentChanged(AnnotatedCollectionI alignment,
76           Map<SequenceI, SequenceCollectionI> hiddenReps)
77   {
78     // TODO: if sequences have been represented and they have scores, could
79     // compute an average sequence score for the representative
80
81     // assume only one set of TCOFFEE scores - but could have more than one
82     // potentially.
83     ArrayList<AlignmentAnnotation> annots = new ArrayList<AlignmentAnnotation>();
84     // Search alignment to get all tcoffee annotation and pick one set of
85     // annotation to use to colour seqs.
86     seqMap = new IdentityHashMap<SequenceI, Color[]>();
87     int w = 0;
88     for (AlignmentAnnotation al : alignment
89             .findAnnotation(TCoffeeScoreFile.TCOFFEE_SCORE))
90     {
91       if (al.sequenceRef != null && !al.belowAlignment)
92       {
93         annots.add(al);
94         if (w < al.annotations.length)
95         {
96           w = al.annotations.length;
97         }
98         Color[] scores = new Color[al.annotations.length];
99         int i = 0;
100         for (Annotation an : al.annotations)
101         {
102           scores[i++] = (an != null) ? an.colour : Color.white;
103         }
104         seqMap.put(al.sequenceRef, scores);
105       }
106     }
107     // TODO: compute average colour for each symbol type in each column - gives
108     // a second order colourscheme for colouring a sequence logo derived from
109     // the alignment (colour reflects quality of alignment for each residue
110     // class)
111   }
112
113   @Override
114   public Color findColour(char c, int j, SequenceI seq)
115   {
116     Color[] cols;
117
118     if (seqMap == null || (cols = seqMap.get(seq)) == null)
119     {
120       // see above TODO about computing a colour for each residue in each
121       // column: cc = _rcols[i][indexFor[c]];
122       return Color.white;
123     }
124
125     if (j < 0 || j >= cols.length)
126     {
127       return Color.white;
128     }
129     return cols[j];
130   }
131 }