JAL-2371 correct Clustal, Blosum, PID colouring of consensus logo
[jalview.git] / src / jalview / schemes / ScoreColourScheme.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.datamodel.AnnotatedCollectionI;
24 import jalview.datamodel.SequenceCollectionI;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.Comparison;
27
28 import java.awt.Color;
29 import java.util.Map;
30
31 /**
32  * DOCUMENT ME!
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class ScoreColourScheme extends ResidueColourScheme
38 {
39   /** DOCUMENT ME!! */
40   public double min;
41
42   /** DOCUMENT ME!! */
43   public double max;
44
45   /** DOCUMENT ME!! */
46   public double[] scores;
47
48   /**
49    * Creates a new ScoreColourScheme object.
50    * 
51    * @param scores
52    *          DOCUMENT ME!
53    * @param min
54    *          DOCUMENT ME!
55    * @param max
56    *          DOCUMENT ME!
57    */
58   public ScoreColourScheme(int symbolIndex[], double[] scores, double min,
59           double max)
60   {
61     super(symbolIndex);
62
63     this.scores = scores;
64     this.min = min;
65     this.max = max;
66
67     // Make colours in constructor
68     // Why wasn't this done earlier?
69     int i, iSize = scores.length;
70     colors = new Color[scores.length];
71     for (i = 0; i < iSize; i++)
72     {
73       float red = (float) (scores[i] - (float) min) / (float) (max - min);
74
75       if (red > 1.0f)
76       {
77         red = 1.0f;
78       }
79
80       if (red < 0.0f)
81       {
82         red = 0.0f;
83       }
84       colors[i] = makeColour(red);
85     }
86   }
87
88   /**
89    * DOCUMENT ME!
90    * 
91    * @param s
92    *          DOCUMENT ME!
93    * @param j
94    *          DOCUMENT ME!
95    * 
96    * @return DOCUMENT ME!
97    */
98   @Override
99   public Color findColour(char c, int j, SequenceI seq)
100   {
101     if (Comparison.isGap(c))
102     {
103       return Color.white;
104     }
105
106     return colors[ResidueProperties.aaIndex[c]];
107   }
108
109   /**
110    * DOCUMENT ME!
111    * 
112    * @param c
113    *          DOCUMENT ME!
114    * 
115    * @return DOCUMENT ME!
116    */
117   public Color makeColour(float c)
118   {
119     return new Color(c, (float) 0.0, (float) 1.0 - c);
120   }
121
122   @Override
123   public String getSchemeName()
124   {
125     return "Score";
126   }
127
128   /**
129    * Returns a new instance of this colour scheme with which the given data may
130    * be coloured
131    */
132   @Override
133   public ColourSchemeI getInstance(AnnotatedCollectionI coll,
134           Map<SequenceI, SequenceCollectionI> hrs)
135   {
136     return new ScoreColourScheme(symbolIndex, scores, min, max);
137   }
138 }