JAL-1065 JAL-1066 ensure non-residue based schemes actually override the new findColo...
[jalview.git] / src / jalview / schemes / RNAHelicesColour.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.schemes;
19
20 import java.awt.*;
21 import java.util.Hashtable;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.SequenceI;
25
26 /**
27  * Looks at the information computed from an RNA Stockholm format file on the
28  * secondary structure of the alignment. Extracts the information on the
29  * positions of the helices present and assigns colors.
30  * 
31  * @author Lauren Michelle Lui
32  * @version 2.5
33  */
34 public class RNAHelicesColour extends ResidueColourScheme
35 {
36
37   /**
38    * Stores random colors generated for the number of helices
39    */
40   public Hashtable helixcolorhash = new Hashtable();
41
42   /**
43    * Maps sequence positions to the RNA helix they belong to. Key: position,
44    * Value: helix
45    */
46   public Hashtable positionsToHelix = new Hashtable();
47
48   /**
49    * Number of helices in the RNA secondary structure
50    */
51   int numHelix = 0;
52
53   public AlignmentAnnotation annotation;
54
55   /**
56    * Creates a new RNAHelicesColour object.
57    */
58   public RNAHelicesColour(AlignmentAnnotation annotation)
59   {
60     super(ResidueProperties.nucleotideIndex);
61     this.annotation = annotation;
62     refresh();
63   }
64
65   private long lastrefresh = -1;
66
67   public void refresh()
68   {
69     if (lastrefresh != annotation._rnasecstr.hashCode() && annotation.isValidStruc())
70     {
71       annotation.getRNAStruc();
72       lastrefresh = annotation._rnasecstr.hashCode();
73       numHelix = 0;
74       positionsToHelix = new Hashtable();
75
76       // Figure out number of helices
77       // Length of rnasecstr is the number of pairs of positions that base pair
78       // with each other in the secondary structure
79       for (int x = 0; x < this.annotation._rnasecstr.length; x++)
80       {
81
82         /*
83          * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
84          * this.annotation._rnasecstr[x].getBegin());
85          */
86         // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
87
88         positionsToHelix.put(this.annotation._rnasecstr[x].getBegin(),
89                 this.annotation._rnasecstr[x].getFeatureGroup());
90         positionsToHelix.put(this.annotation._rnasecstr[x].getEnd(),
91                 this.annotation._rnasecstr[x].getFeatureGroup());
92
93         if (Integer.parseInt(this.annotation._rnasecstr[x]
94                 .getFeatureGroup()) > numHelix)
95         {
96           numHelix = Integer.parseInt(this.annotation._rnasecstr[x]
97                   .getFeatureGroup());
98         }
99
100       }
101
102       // Generate random colors and store
103       for (int j = 0; j <= numHelix; j++)
104       {
105         if (!helixcolorhash.containsKey(Integer.toString(j)))
106         {
107           helixcolorhash.put(Integer.toString(j),
108                   jalview.util.ColorUtils.generateRandomColor(Color.white));
109         }
110       }
111     }
112   }
113
114   /**
115    * Returns default color base on purinepyrimidineIndex in
116    * jalview.schemes.ResidueProperties (Allows coloring in sequence logo)
117    * 
118    * @param c
119    *          Character in sequence
120    * 
121    * @return color in RGB
122    */
123   @Override
124   public Color findColour(char c)
125   {
126     return ResidueProperties.purinepyrimidine[ResidueProperties.purinepyrimidineIndex[c]];
127     // random colors for all positions
128     // jalview.util.ColorUtils.generateRandomColor(Color.white); If you want
129   }
130
131   /**
132    * Returns color based on helices
133    * 
134    * @param c
135    *          Character in sequence
136    * @param j
137    *          Threshold
138    * 
139    * @return Color in RGB
140    */
141   @Override
142   public Color findColour(char c, int j, SequenceI seq)
143   {
144     refresh();
145     Color currentColour = Color.white;
146     String currentHelix = null;
147     currentHelix = (String) positionsToHelix.get(j);
148
149     if (currentHelix != null)
150     {
151       currentColour = (Color) helixcolorhash.get(currentHelix);
152     }
153
154     // System.out.println(c + " " + j + " helix " + currentHelix + " " +
155     // currentColour);
156     return currentColour;
157   }
158 }