Merge branch 'releases/Release_2_11_1_Branch' into feature/JAL-3719_gapColourInAlignm...
[jalview.git] / src / jalview / renderer / ResidueColourFinder.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.renderer;
22
23 import jalview.datamodel.SequenceGroup;
24 import jalview.datamodel.SequenceI;
25 import jalview.renderer.seqfeatures.FeatureColourFinder;
26 import jalview.util.Comparison;
27
28 import java.awt.Color;
29
30 public class ResidueColourFinder
31 {
32   private static final Color BACKGROUND_COLOUR = Color.white;
33
34   protected Color GAP_COLOUR=Color.white; // default colour to use at gaps
35
36   protected Color RESIDUE_COLOUR=BACKGROUND_COLOUR; // default colour to use at residues
37
38   public ResidueColourFinder()
39   {
40   }
41   public ResidueColourFinder(Color gapColour)
42   {
43     GAP_COLOUR = gapColour;
44   }
45
46   /**
47    * Get the colour of a residue in a sequence
48    * 
49    * @param showBoxes
50    *          true if the viewport's Show Boxes setting is true
51    * @param shader
52    *          the viewport's colour scheme
53    * @param allGroups
54    *          all the groups which seq participates in
55    * @param seq
56    *          the sequence containing the residue
57    * @param position
58    *          the position of the residue in the sequence
59    * @param finder
60    *          FeatureColourFinder for the viewport
61    * @return colour of the residue
62    */
63   public Color getResidueColour(boolean showBoxes, ResidueShaderI shader,
64           SequenceGroup[] allGroups,
65           final SequenceI seq, int position, FeatureColourFinder finder)
66   {
67     Color col = getResidueBoxColour(showBoxes, shader, allGroups, seq,
68             position);
69
70     // if there's a FeatureColourFinder we might override the residue colour
71     // here with feature colouring
72     if (finder != null)
73     {
74       col = finder.findFeatureColour(col, seq, position);
75     }
76     return col;
77   }
78
79   /**
80    * Get the residue colour without accounting for any features
81    * 
82    * @param showBoxes
83    *          true if the viewport's Show Boxes setting is true
84    * @param shader
85    *          the viewport's colour scheme
86    * @param allGroups
87    *          all the groups which seq participates in
88    * @param seq
89    *          the sequence containing the residue
90    * @param i
91    *          the position of the residue in the sequence
92    * @return
93    */
94   protected Color getResidueBoxColour(boolean showBoxes,
95           ResidueShaderI shader,
96           SequenceGroup[] allGroups,
97           SequenceI seq, int i)
98   {
99     SequenceGroup currentSequenceGroup = getCurrentSequenceGroup(allGroups,
100             i);
101     if (currentSequenceGroup != null)
102     {
103       if (currentSequenceGroup.getDisplayBoxes())
104       {
105         return getBoxColour(currentSequenceGroup.getGroupColourScheme(),
106                 seq, i);
107       }
108     }
109     else if (showBoxes)
110     {
111       return getBoxColour(shader, seq, i);
112     }
113   
114     return BACKGROUND_COLOUR;
115   }
116
117   /**
118    * Search all the groups for a sequence to find the one which a given res
119    * falls into
120    * 
121    * @param allGroups
122    *          all the groups a sequence participates in
123    * @param res
124    *          the residue to search for
125    * @return a sequence group for res, or null if no sequence group applies
126    */
127   public SequenceGroup getCurrentSequenceGroup(SequenceGroup[] allGroups,
128           int res)
129   {
130     if (allGroups == null)
131     {
132       return null;
133     }
134
135     for (int i = 0; i < allGroups.length; i++)
136     {
137       if ((allGroups[i].getStartRes() <= res)
138               && (allGroups[i].getEndRes() >= res))
139       {
140         return (allGroups[i]);
141       }
142     }
143
144     return null;
145   }
146
147   /**
148    * Find the colour for a sequence's position in the alignment 
149    * 
150    * @param shader
151    *          the viewport's colour scheme
152    * @param seq
153    *          the sequence containing the residue
154    * @param i
155    *          the position of the residue in the sequence
156    */
157   public Color getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
158   {
159     Color resBoxColour = RESIDUE_COLOUR;
160     char currentChar = seq.getCharAt(i);
161
162     // In the overview window, gaps are coloured grey, unless the colour scheme
163     // specifies a gap colour, in which case gaps honour the colour scheme
164     // settings
165     if (shader.getColourScheme() != null)
166     {
167       if (Comparison.isGap(currentChar)
168               && (!shader.getColourScheme().hasGapColour()))
169       {
170         resBoxColour = GAP_COLOUR;
171       }
172       else
173       {
174         resBoxColour = shader.findColour(currentChar, i, seq);
175       }
176     }
177     else if (Comparison.isGap(currentChar))
178     {
179       resBoxColour = GAP_COLOUR;
180     }
181
182     return resBoxColour;
183   }
184
185 }