JAL-1432 updated copyright notices
[jalview.git] / src / jalview / schemes / TCoffeeColourScheme.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.schemes;
20
21 import jalview.analysis.SequenceIdMatcher;
22 import jalview.datamodel.AlignmentAnnotation;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.AnnotatedCollectionI;
25 import jalview.datamodel.Annotation;
26 import jalview.datamodel.SequenceCollectionI;
27 import jalview.datamodel.SequenceI;
28 import jalview.io.TCoffeeScoreFile;
29
30 import java.awt.Color;
31 import java.util.ArrayList;
32 import java.util.IdentityHashMap;
33 import java.util.Map;
34 import java.util.TreeMap;
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 =
49   { new Color(102, 102, 255), // #6666FF
50       new Color(0, 255, 0), // #00FF00
51       new Color(102, 255, 0), // #66FF00
52       new Color(204, 255, 0), // #CCFF00
53       new Color(255, 255, 0), // #FFFF00
54       new Color(255, 204, 0), // #FFCC00
55       new Color(255, 153, 0), // #FF9900
56       new Color(255, 102, 0), // #FF6600
57       new Color(255, 51, 0), // #FF3300
58       new Color(255, 34, 0) // #FF2000
59   };
60
61   IdentityHashMap<SequenceI, Color[]> seqMap;
62
63   /**
64    * the color scheme needs to look at the alignment to get and cache T-COFFEE
65    * scores
66    * 
67    * @param alignment
68    *          - annotated sequences to be searched
69    */
70   public TCoffeeColourScheme(AnnotatedCollectionI alignment)
71   {
72     alignmentChanged(alignment, null);
73   }
74
75   @Override
76   public void alignmentChanged(AnnotatedCollectionI alignment,
77           Map<SequenceI, SequenceCollectionI> hiddenReps)
78   {
79     // TODO: if sequences have been represented and they have scores, could
80     // compute an average sequence score for the representative
81
82     // assume only one set of TCOFFEE scores - but could have more than one
83     // potentially.
84     ArrayList<AlignmentAnnotation> annots = new ArrayList<AlignmentAnnotation>();
85     // Search alignment to get all tcoffee annotation and pick one set of
86     // annotation to use to colour seqs.
87     seqMap = new IdentityHashMap<SequenceI, Color[]>();
88     int w = 0;
89     for (AlignmentAnnotation al : alignment
90             .findAnnotation(TCoffeeScoreFile.TCOFFEE_SCORE))
91     {
92       if (al.sequenceRef != null && !al.belowAlignment)
93       {
94         annots.add(al);
95         if (w < al.annotations.length)
96         {
97           w = al.annotations.length;
98         }
99         Color[] scores = new Color[al.annotations.length];
100         int i = 0;
101         for (Annotation an : al.annotations)
102         {
103           scores[i++] = (an != null) ? an.colour : Color.white;
104         }
105         seqMap.put(al.sequenceRef, scores);
106       }
107     }
108     // TODO: compute average colour for each symbol type in each column - gives
109     // a second order colourscheme for colouring a sequence logo derived from
110     // the alignment (colour reflects quality of alignment for each residue
111     // class)
112   }
113
114   @Override
115   public Color findColour(char c, int j, SequenceI seq)
116   {
117     Color[] cols;
118
119     if (seqMap == null || (cols = seqMap.get(seq)) == null)
120     {
121       // see above TODO about computing a colour for each residue in each
122       // column: cc = _rcols[i][indexFor[c]];
123       return Color.white;
124     }
125
126     if (j < 0 || j >= cols.length)
127     {
128       return Color.white;
129     }
130     return cols[j];
131   }
132 }