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