2da7233193b2cf44275ee0f6d34fcabe810e395a
[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
27 import java.awt.Color;
28
29 public class ResidueColourFinder
30 {
31   public ResidueColourFinder()
32   {
33   }
34
35   /**
36    * Get the colour of a residue in a sequence
37    * 
38    * @param showBoxes
39    *          true if the viewport's Show Boxes setting is true
40    * @param shader
41    *          the viewport's colour scheme
42    * @param allGroups
43    *          all the groups which seq participates in
44    * @param seq
45    *          the sequence containing the residue
46    * @param position
47    *          the position of the residue in the sequence
48    * @param finder
49    *          FeatureColourFinder for the viewport
50    * @return colour of the residue
51    */
52   public Color getResidueColour(boolean showBoxes, ResidueShaderI shader,
53           SequenceGroup[] allGroups,
54           final SequenceI seq, int position, FeatureColourFinder finder)
55   {
56     Color col = getResidueBoxColour(showBoxes, shader, allGroups, seq,
57             position);
58
59     // if there's a FeatureColourFinder we might override the residue colour
60     // here with feature colouring
61     if (finder != null)
62     {
63       col = finder.findFeatureColour(col, seq, position);
64     }
65     return col;
66   }
67
68   /**
69    * Get the residue colour without accounting for any features
70    * 
71    * @param showBoxes
72    *          true if the viewport's Show Boxes setting is true
73    * @param shader
74    *          the viewport's colour scheme
75    * @param allGroups
76    *          all the groups which seq participates in
77    * @param seq
78    *          the sequence containing the residue
79    * @param i
80    *          the position of the residue in the sequence
81    * @return
82    */
83   protected Color getResidueBoxColour(boolean showBoxes,
84           ResidueShaderI shader,
85           SequenceGroup[] allGroups,
86           SequenceI seq, int i)
87   {
88     SequenceGroup currentSequenceGroup = getCurrentSequenceGroup(allGroups,
89             i);
90     if (currentSequenceGroup != null)
91     {
92       if (currentSequenceGroup.getDisplayBoxes())
93       {
94         return getBoxColour(currentSequenceGroup.getGroupColourScheme(),
95                 seq, i);
96       }
97     }
98     else if (showBoxes)
99     {
100       return getBoxColour(shader, seq, i);
101     }
102   
103     return Color.white;
104   }
105
106   /**
107    * Search all the groups for a sequence to find the one which a given res
108    * falls into
109    * 
110    * @param allGroups
111    *          all the groups a sequence participates in
112    * @param res
113    *          the residue to search for
114    * @return a sequence group for res, or null if no sequence group applies
115    */
116   public SequenceGroup getCurrentSequenceGroup(SequenceGroup[] allGroups,
117           int res)
118   {
119     if (allGroups == null)
120     {
121       return null;
122     }
123
124     for (int i = 0; i < allGroups.length; i++)
125     {
126       if ((allGroups[i].getStartRes() <= res)
127               && (allGroups[i].getEndRes() >= res))
128       {
129         return (allGroups[i]);
130       }
131     }
132
133     return null;
134   }
135
136   /**
137    * DOCUMENT ME!
138    * 
139    * @param shader
140    *          the viewport's colour scheme
141    * @param seq
142    *          the sequence containing the residue
143    * @param i
144    *          the position of the residue in the sequence
145    */
146   public Color getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
147   {
148     Color resBoxColour = Color.white;
149     if (shader.getColourScheme() != null)
150     {
151       resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
152     }
153     return resBoxColour;
154   }
155
156 }