7caa7351015d3f14dbb2e1c2e455dccc9e713b8f
[jalview.git] / src / jalview / schemes / TCoffeeColourScheme.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.analysis.SequenceIdMatcher;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.AnnotatedCollectionI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.SequenceCollectionI;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.TCoffeeScoreFile;
31
32 import java.awt.Color;
33 import java.util.ArrayList;
34 import java.util.IdentityHashMap;
35 import java.util.Map;
36 import java.util.TreeMap;
37
38 /**
39  * Defines the color score for T-Coffee MSA
40  * <p>
41  * See http://tcoffee.org
42  * 
43  * 
44  * @author Paolo Di Tommaso
45  * 
46  */
47 public class TCoffeeColourScheme extends ResidueColourScheme
48 {
49
50   static final Color[] colors =
51   { new Color(102, 102, 255), // #6666FF
52       new Color(0, 255, 0), // #00FF00
53       new Color(102, 255, 0), // #66FF00
54       new Color(204, 255, 0), // #CCFF00
55       new Color(255, 255, 0), // #FFFF00
56       new Color(255, 204, 0), // #FFCC00
57       new Color(255, 153, 0), // #FF9900
58       new Color(255, 102, 0), // #FF6600
59       new Color(255, 51, 0), // #FF3300
60       new Color(255, 34, 0) // #FF2000
61   };
62
63   IdentityHashMap<SequenceI, Color[]> seqMap;
64
65   /**
66    * the color scheme needs to look at the alignment to get and cache T-COFFEE
67    * scores
68    * 
69    * @param alignment
70    *          - annotated sequences to be searched
71    */
72   public TCoffeeColourScheme(AnnotatedCollectionI alignment)
73   {
74     alignmentChanged(alignment, null);
75   }
76
77   @Override
78   public void alignmentChanged(AnnotatedCollectionI alignment,
79           Map<SequenceI, SequenceCollectionI> hiddenReps)
80   {
81     // TODO: if sequences have been represented and they have scores, could
82     // compute an average sequence score for the representative
83
84     // assume only one set of TCOFFEE scores - but could have more than one
85     // potentially.
86     ArrayList<AlignmentAnnotation> annots = new ArrayList<AlignmentAnnotation>();
87     // Search alignment to get all tcoffee annotation and pick one set of
88     // annotation to use to colour seqs.
89     seqMap = new IdentityHashMap<SequenceI, Color[]>();
90     AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment : alignment.getContext();
91     int w = 0;
92     for (AlignmentAnnotation al : alcontext
93             .findAnnotation(TCoffeeScoreFile.TCOFFEE_SCORE))
94     {
95       if (al.sequenceRef != null && !al.belowAlignment)
96       {
97         annots.add(al);
98         if (w < al.annotations.length)
99         {
100           w = al.annotations.length;
101         }
102         Color[] scores = new Color[al.annotations.length];
103         int i = 0;
104         for (Annotation an : al.annotations)
105         {
106           scores[i++] = (an != null) ? an.colour : Color.white;
107         }
108         seqMap.put(al.sequenceRef, scores);
109       }
110     }
111     // TODO: compute average colour for each symbol type in each column - gives
112     // a second order colourscheme for colouring a sequence logo derived from
113     // the alignment (colour reflects quality of alignment for each residue
114     // class)
115   }
116
117   @Override
118   public Color findColour(char c, int j, SequenceI seq)
119   {
120     Color[] cols;
121
122     if (seqMap == null || (cols = seqMap.get(seq)) == null)
123     {
124       // see above TODO about computing a colour for each residue in each
125       // column: cc = _rcols[i][indexFor[c]];
126       return Color.white;
127     }
128
129     if (j < 0 || j >= cols.length)
130     {
131       return Color.white;
132     }
133     return cols[j];
134   }
135   
136   @Override
137   public ColourSchemeI applyTo(AnnotatedCollectionI sg,
138           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
139   {
140     return new TCoffeeColourScheme(sg);
141   }
142 }