JAL-3675 release notes for JAL-3750 JAL-3751
[jalview.git] / src / jalview / schemes / TCoffeeColourScheme.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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.api.AlignViewportI;
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.List;
36 import java.util.Map;
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   IdentityHashMap<SequenceI, Color[]> seqMap;
50
51   /**
52    * Default constructor (required for Class.newInstance())
53    */
54   public TCoffeeColourScheme()
55   {
56
57   }
58
59   /**
60    * the color scheme needs to look at the alignment to get and cache T-COFFEE
61    * scores
62    * 
63    * @param alignment
64    *          - annotated sequences to be searched
65    */
66   public TCoffeeColourScheme(AnnotatedCollectionI alignment)
67   {
68     alignmentChanged(alignment, null);
69   }
70
71   /**
72    * Finds the TCoffeeScore annotation (if any) for each sequence and notes the
73    * annotation colour for each column position. The colours are fixed for
74    * scores 0-9 and are set when annotation is parsed.
75    * 
76    * @see TCoffeeScoreFile#annotateAlignment(AlignmentI, boolean)
77    */
78   @Override
79   public void alignmentChanged(AnnotatedCollectionI alignment,
80           Map<SequenceI, SequenceCollectionI> hiddenReps)
81   {
82     // TODO: if sequences have been represented and they have scores, could
83     // compute an average sequence score for the representative
84
85     // assume only one set of TCOFFEE scores - but could have more than one
86     // potentially.
87     List<AlignmentAnnotation> annots = new ArrayList<>();
88     // Search alignment to get all tcoffee annotation and pick one set of
89     // annotation to use to colour seqs.
90     seqMap = new IdentityHashMap<>();
91     AnnotatedCollectionI alcontext = alignment instanceof AlignmentI
92             ? alignment
93             : alignment.getContext();
94     if (alcontext == null)
95     {
96       return;
97     }
98     int w = 0;
99     for (AlignmentAnnotation al : alcontext
100             .findAnnotation(TCoffeeScoreFile.TCOFFEE_SCORE))
101     {
102       if (al.sequenceRef != null && !al.belowAlignment)
103       {
104         annots.add(al);
105         if (w < al.annotations.length)
106         {
107           w = al.annotations.length;
108         }
109         Color[] scores = new Color[al.annotations.length];
110         int i = 0;
111         for (Annotation an : al.annotations)
112         {
113           scores[i++] = (an != null) ? an.colour : Color.white;
114         }
115         seqMap.put(al.sequenceRef, scores);
116       }
117     }
118     // TODO: compute average colour for each symbol type in each column - gives
119     // a second order colourscheme for colouring a sequence logo derived from
120     // the alignment (colour reflects quality of alignment for each residue
121     // class)
122   }
123
124   @Override
125   public Color findColour(char c, int j, SequenceI seq)
126   {
127     if (seqMap == null)
128     {
129       return Color.WHITE;
130     }
131     Color[] cols = seqMap.get(seq);
132     if (cols == null)
133     {
134       // see above TODO about computing a colour for each residue in each
135       // column: cc = _rcols[i][indexFor[c]];
136       return Color.white;
137     }
138
139     if (j < 0 || j >= cols.length)
140     {
141       return Color.white;
142     }
143     return cols[j];
144   }
145
146   @Override
147   public ColourSchemeI getInstance(AlignViewportI view,
148           AnnotatedCollectionI sg)
149   {
150     return new TCoffeeColourScheme(sg);
151   }
152
153   /**
154    * Answers true if the data has TCoffee score annotation
155    */
156   @Override
157   public boolean isApplicableTo(AnnotatedCollectionI ac)
158   {
159     AnnotatedCollectionI alcontext = ac instanceof AlignmentI ? ac
160             : ac.getContext();
161     if (alcontext == null)
162     {
163       return false;
164     }
165     Iterable<AlignmentAnnotation> anns = alcontext
166             .findAnnotation(TCoffeeScoreFile.TCOFFEE_SCORE);
167     return anns.iterator().hasNext();
168   }
169
170   @Override
171   public String getSchemeName()
172   {
173     return JalviewColourScheme.TCoffee.toString();
174   }
175
176   @Override
177   public boolean isSimple()
178   {
179     return false;
180   }
181 }