bbd2a23c4c4bce57c2a004f31d8f9d8f6b04aabb
[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.SequenceI;
9 import jalview.io.TCoffeeScoreFile;
10
11 import java.awt.Color;
12 import java.util.ArrayList;
13 import java.util.IdentityHashMap;
14 import java.util.TreeMap;
15
16 /**
17  * Defines the color score for T-Coffee MSA 
18  * <p>
19  * See http://tcoffee.org
20  * 
21  * 
22  * @author Paolo Di Tommaso
23  *
24  */
25 public class TCoffeeColourScheme extends ResidueColourScheme {
26
27         static final Color[] colors = {
28                 new Color( 102, 102, 255 ),     // #6666FF
29                 new Color( 0, 255, 0),          // #00FF00
30                 new Color( 102, 255, 0),        // #66FF00
31                 new Color( 204, 255, 0),        // #CCFF00
32                 new Color( 255, 255, 0),        // #FFFF00
33                 new Color( 255, 204, 0),        // #FFCC00
34                 new Color( 255, 153, 0),        // #FF9900
35                 new Color( 255, 102, 0),        // #FF6600
36                 new Color( 255, 51, 0),         // #FF3300
37                 new Color( 255, 34, 0)          // #FF2000
38         };
39
40
41         IdentityHashMap<SequenceI, Color[]> seqMap;
42         /**
43          * the color scheme needs to look at the alignment to get and cache T-COFFEE scores 
44          * 
45          * @param alignment - annotated sequences to be searched
46          */
47         public TCoffeeColourScheme(AnnotatedCollectionI alignment) {
48           alignmentChanged(alignment);
49         }
50         @Override public void alignmentChanged(AnnotatedCollectionI alignment)
51         {
52           // assume only one set of TCOFFEE scores - but could have more than one potentially.
53           ArrayList<AlignmentAnnotation> annots = new ArrayList<AlignmentAnnotation>();
54           // Search alignment to get all tcoffee annotation and pick one set of annotation to use to colour seqs.
55           seqMap = new IdentityHashMap<SequenceI, Color[]>();
56           for (AlignmentAnnotation al:alignment.getAlignmentAnnotation())
57           {
58             if (al.sequenceRef!=null && !al.belowAlignment && al.label!=null && (al.label==TCoffeeScoreFile.TCOFFEE_SCORE || al.label.equals(TCoffeeScoreFile.TCOFFEE_SCORE)))
59             {
60               annots.add(al);
61               Color[] scores=new Color[al.annotations.length];
62               int i=0;
63               for (Annotation an:al.annotations)
64               {
65                 scores[i++]=(an!=null) ? an.colour : Color.white;
66               }
67               seqMap.put(al.sequenceRef, scores);
68             }
69           }
70         }
71         @Override
72         public Color findColour(char c, int j, SequenceI seq) {
73           Color[] cols;
74
75           if( seqMap==null || (cols=seqMap.get(seq))==null) {
76             return Color.white;
77           }
78           
79           if( j < 0 || j>= cols.length ) {
80             return Color.white;
81           }
82           return cols[j];
83         }
84 }