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