formatting
[jalview.git] / src / jalview / schemes / TCoffeeColourScheme.java
1 package jalview.schemes;
2
3 import jalview.analysis.SequenceIdMatcher;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.AnnotatedCollectionI;
7 import jalview.datamodel.Annotation;
8 import jalview.datamodel.SequenceCollectionI;
9 import jalview.datamodel.SequenceI;
10 import jalview.io.TCoffeeScoreFile;
11
12 import java.awt.Color;
13 import java.util.ArrayList;
14 import java.util.IdentityHashMap;
15 import java.util.Map;
16 import java.util.TreeMap;
17
18 /**
19  * Defines the color score for T-Coffee MSA
20  * <p>
21  * See http://tcoffee.org
22  * 
23  * 
24  * @author Paolo Di Tommaso
25  * 
26  */
27 public class TCoffeeColourScheme extends ResidueColourScheme
28 {
29
30   static final Color[] colors =
31   { new Color(102, 102, 255), // #6666FF
32       new Color(0, 255, 0), // #00FF00
33       new Color(102, 255, 0), // #66FF00
34       new Color(204, 255, 0), // #CCFF00
35       new Color(255, 255, 0), // #FFFF00
36       new Color(255, 204, 0), // #FFCC00
37       new Color(255, 153, 0), // #FF9900
38       new Color(255, 102, 0), // #FF6600
39       new Color(255, 51, 0), // #FF3300
40       new Color(255, 34, 0) // #FF2000
41   };
42
43   IdentityHashMap<SequenceI, Color[]> seqMap;
44
45   /**
46    * the color scheme needs to look at the alignment to get and cache T-COFFEE
47    * scores
48    * 
49    * @param alignment
50    *          - annotated sequences to be searched
51    */
52   public TCoffeeColourScheme(AnnotatedCollectionI alignment)
53   {
54     alignmentChanged(alignment, null);
55   }
56
57   @Override
58   public void alignmentChanged(AnnotatedCollectionI alignment,
59           Map<SequenceI, SequenceCollectionI> hiddenReps)
60   {
61     // TODO: if sequences have been represented and they have scores, could
62     // compute an average sequence score for the representative
63
64     // assume only one set of TCOFFEE scores - but could have more than one
65     // potentially.
66     ArrayList<AlignmentAnnotation> annots = new ArrayList<AlignmentAnnotation>();
67     // Search alignment to get all tcoffee annotation and pick one set of
68     // annotation to use to colour seqs.
69     seqMap = new IdentityHashMap<SequenceI, Color[]>();
70     int w = 0;
71     for (AlignmentAnnotation al : alignment
72             .findAnnotation(TCoffeeScoreFile.TCOFFEE_SCORE))
73     {
74       if (al.sequenceRef != null && !al.belowAlignment)
75       {
76         annots.add(al);
77         if (w < al.annotations.length)
78         {
79           w = al.annotations.length;
80         }
81         Color[] scores = new Color[al.annotations.length];
82         int i = 0;
83         for (Annotation an : al.annotations)
84         {
85           scores[i++] = (an != null) ? an.colour : Color.white;
86         }
87         seqMap.put(al.sequenceRef, scores);
88       }
89     }
90     // TODO: compute average colour for each symbol type in each column - gives
91     // a second order colourscheme for colouring a sequence logo derived from
92     // the alignment (colour reflects quality of alignment for each residue
93     // class)
94   }
95
96   @Override
97   public Color findColour(char c, int j, SequenceI seq)
98   {
99     Color[] cols;
100
101     if (seqMap == null || (cols = seqMap.get(seq)) == null)
102     {
103       // see above TODO about computing a colour for each residue in each
104       // column: cc = _rcols[i][indexFor[c]];
105       return Color.white;
106     }
107
108     if (j < 0 || j >= cols.length)
109     {
110       return Color.white;
111     }
112     return cols[j];
113   }
114 }