JAL-1114 - refactor methods handling Vectors and Hashtables to Lists and Maps, and...
[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         static final Color[] colors = {
30                 new Color( 102, 102, 255 ),     // #6666FF
31                 new Color( 0, 255, 0),          // #00FF00
32                 new Color( 102, 255, 0),        // #66FF00
33                 new Color( 204, 255, 0),        // #CCFF00
34                 new Color( 255, 255, 0),        // #FFFF00
35                 new Color( 255, 204, 0),        // #FFCC00
36                 new Color( 255, 153, 0),        // #FF9900
37                 new Color( 255, 102, 0),        // #FF6600
38                 new Color( 255, 51, 0),         // #FF3300
39                 new Color( 255, 34, 0)          // #FF2000
40         };
41
42
43         IdentityHashMap<SequenceI, Color[]> seqMap;
44         /**
45          * the color scheme needs to look at the alignment to get and cache T-COFFEE scores 
46          * 
47          * @param alignment - annotated sequences to be searched
48          */
49         public TCoffeeColourScheme(AnnotatedCollectionI alignment) {
50           alignmentChanged(alignment, null);
51         }
52         @Override 
53         public void alignmentChanged(AnnotatedCollectionI alignment, Map<SequenceI, SequenceCollectionI> hiddenReps)
54         {
55           // TODO: if sequences have been represented and they have scores, could compute an average sequence score for the representative
56           
57           // assume only one set of TCOFFEE scores - but could have more than one potentially.
58           ArrayList<AlignmentAnnotation> annots = new ArrayList<AlignmentAnnotation>();
59           // Search alignment to get all tcoffee annotation and pick one set of annotation to use to colour seqs.
60           seqMap = new IdentityHashMap<SequenceI, Color[]>();
61           int w=0;
62           for (AlignmentAnnotation al:alignment.findAnnotation(TCoffeeScoreFile.TCOFFEE_SCORE))
63           {
64             if (al.sequenceRef!=null && !al.belowAlignment)
65             {
66               annots.add(al);
67               if (w<al.annotations.length)
68               {
69                 w=al.annotations.length;
70               }
71               Color[] scores=new Color[al.annotations.length];
72               int i=0;
73               for (Annotation an:al.annotations)
74               {
75                 scores[i++]=(an!=null) ? an.colour : Color.white;
76               }
77               seqMap.put(al.sequenceRef, scores);
78             }
79           }
80     // TODO: compute average colour for each symbol type in each column - gives
81     // a second order colourscheme for colouring a sequence logo derived from
82     // the alignment (colour reflects quality of alignment for each residue
83     // class)
84         }
85         @Override
86         public Color findColour(char c, int j, SequenceI seq) {
87           Color[] cols;
88
89           if( seqMap==null || (cols=seqMap.get(seq))==null) {
90 //      see above TODO about computing a colour for each residue in each column:            cc = _rcols[i][indexFor[c]];
91             return Color.white;
92           }
93           
94           if( j < 0 || j>= cols.length ) {
95             return Color.white;
96           }
97           return cols[j];
98         }
99 }