(JAL-889) ensure latest version of RNA pairlist is used to generate helical colouring
[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
25 /**
26  * Looks at the information computed from an RNA Stockholm format file on the
27  * secondary structure of the alignment. Extracts the information on the
28  * positions of the helices present and assigns colors.
29  * 
30  * @author Lauren Michelle Lui
31  * @version 2.5
32  */
33 public class RNAHelicesColour extends ResidueColourScheme
34 {
35
36   /**
37    * Stores random colors generated for the number of helices
38    */
39   public Hashtable helixcolorhash = new Hashtable();
40
41   /**
42    * Maps sequence positions to the RNA helix they belong to. Key: position,
43    * Value: helix
44    */
45   public Hashtable positionsToHelix = new Hashtable();
46
47   /**
48    * Number of helices in the RNA secondary structure
49    */
50   int numHelix = 0;
51
52   public AlignmentAnnotation annotation;
53
54   /**
55    * Creates a new RNAHelicesColour object.
56    */
57   public RNAHelicesColour(AlignmentAnnotation annotation)
58   {
59     this.annotation = annotation;
60     refresh();
61   }
62
63   private long lastrefresh = -1;
64
65   public void refresh()
66   {
67     if (lastrefresh != annotation._rnasecstr.hashCode() && annotation.isValidStruc())
68     {
69       annotation.getRNAStruc();
70       lastrefresh = annotation._rnasecstr.hashCode();
71       numHelix = 0;
72       positionsToHelix = new Hashtable();
73
74       // Figure out number of helices
75       // Length of rnasecstr is the number of pairs of positions that base pair
76       // with each other in the secondary structure
77       for (int x = 0; x < this.annotation._rnasecstr.length; x++)
78       {
79
80         /*
81          * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
82          * this.annotation._rnasecstr[x].getBegin());
83          */
84         // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
85
86         positionsToHelix.put(this.annotation._rnasecstr[x].getBegin(),
87                 this.annotation._rnasecstr[x].getFeatureGroup());
88         positionsToHelix.put(this.annotation._rnasecstr[x].getEnd(),
89                 this.annotation._rnasecstr[x].getFeatureGroup());
90
91         if (Integer.parseInt(this.annotation._rnasecstr[x]
92                 .getFeatureGroup()) > numHelix)
93         {
94           numHelix = Integer.parseInt(this.annotation._rnasecstr[x]
95                   .getFeatureGroup());
96         }
97
98       }
99
100       // Generate random colors and store
101       for (int j = 0; j <= numHelix; j++)
102       {
103         if (!helixcolorhash.containsKey(Integer.toString(j)))
104         {
105           helixcolorhash.put(Integer.toString(j),
106                   jalview.util.ColorUtils.generateRandomColor(Color.white));
107         }
108       }
109     }
110   }
111
112   /**
113    * Returns default color base on purinepyrimidineIndex in
114    * jalview.schemes.ResidueProperties (Allows coloring in sequence logo)
115    * 
116    * @param c
117    *          Character in sequence
118    * 
119    * @return color in RGB
120    */
121   public Color findColour(char c)
122   {
123     return ResidueProperties.purinepyrimidine[ResidueProperties.purinepyrimidineIndex[c]];
124     // random colors for all positions
125     // jalview.util.ColorUtils.generateRandomColor(Color.white); If you want
126   }
127
128   /**
129    * Returns color based on helices
130    * 
131    * @param c
132    *          Character in sequence
133    * @param j
134    *          Threshold
135    * 
136    * @return Color in RGB
137    */
138   public Color findColour(char c, int j)
139   {
140     refresh();
141     Color currentColour = Color.white;
142     String currentHelix = null;
143     currentHelix = (String) positionsToHelix.get(j);
144
145     if (currentHelix != null)
146     {
147       currentColour = (Color) helixcolorhash.get(currentHelix);
148     }
149
150     // System.out.println(c + " " + j + " helix " + currentHelix + " " +
151     // currentColour);
152     return currentColour;
153   }
154 }