JAL-2360 ColourSchemes holds configured schemes, AlignFrame colour menu
[jalview.git] / src / jalview / schemes / Blosum62ColourScheme.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.AlignmentAnnotation;
24 import jalview.datamodel.AnnotatedCollectionI;
25 import jalview.datamodel.SequenceCollectionI;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.Comparison;
28
29 import java.awt.Color;
30 import java.util.Map;
31
32 public class Blosum62ColourScheme extends ResidueColourScheme
33 {
34   private static final Color LIGHT_BLUE = new Color(204, 204, 255);
35   private static final Color DARK_BLUE = new Color(154, 154, 255);
36
37   public Blosum62ColourScheme()
38   {
39     super();
40   }
41
42   /**
43    * Returns a new instance of this colour scheme with which the given data may
44    * be coloured
45    */
46   @Override
47   public ColourSchemeI getInstance(AnnotatedCollectionI coll,
48           Map<SequenceI, SequenceCollectionI> hrs)
49   {
50     return new Blosum62ColourScheme();
51   }
52
53   @Override
54   public Color findColour(char res, int j, SequenceI seq)
55   {
56     if ('a' <= res && res <= 'z')
57     {
58       // TO UPPERCASE !!!
59       res -= ('a' - 'A');
60     }
61
62     if (consensus == null || consensus.get(j) == null
63             || (threshold != 0 && !aboveThreshold(res, j)))
64     {
65       return Color.white;
66     }
67
68     Color currentColour;
69
70     if (!Comparison.isGap(res))
71     {
72       /*
73        * test if this is the consensus (or joint consensus) residue
74        */
75       String max = consensus.get(j).getModalResidue();
76
77       if (max.indexOf(res) > -1)
78       {
79         currentColour = DARK_BLUE;
80       }
81       else
82       {
83         int c = 0;
84         int max_aa = 0;
85         int n = max.length();
86
87         do
88         {
89           c += ResidueProperties.getBLOSUM62(max.charAt(max_aa), res);
90         } while (++max_aa < n);
91
92         if (c > 0)
93         {
94           currentColour = LIGHT_BLUE;
95         }
96         else
97         {
98           currentColour = Color.white;
99         }
100       }
101
102       if (conservationColouring)
103       {
104         currentColour = applyConservation(currentColour, j);
105       }
106     }
107     else
108     {
109       return Color.white;
110     }
111
112     return currentColour;
113   }
114
115   @Override
116   public boolean isPeptideSpecific()
117   {
118     return true;
119   }
120
121   @Override
122   public String getSchemeName()
123   {
124     return JalviewColourScheme.Blosum62.toString();
125   }
126
127   /**
128    * Answers true if Conservation annotation is present, else false
129    */
130   @Override
131   public boolean isApplicableTo(AnnotatedCollectionI ac)
132   {
133     AlignmentAnnotation[] anns = ac.getAlignmentAnnotation();
134     if (anns == null)
135     {
136       return false;
137     }
138     for (AlignmentAnnotation ann : anns)
139     {
140       if ("Conservation".equals(ann.label))
141       {
142         return true;
143       }
144     }
145     return false;
146   }
147 }