06553f05811694a0e5c524e89fa4a5ad73df9769
[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 import java.util.Map;
24
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AnnotatedCollectionI;
27 import jalview.datamodel.SequenceCollectionI;
28 import jalview.datamodel.SequenceI;
29
30 /**
31  * Looks at the information computed from an RNA Stockholm format file on the
32  * secondary structure of the alignment. Extracts the information on the
33  * positions of the helices present and assigns colors.
34  * 
35  * @author Lauren Michelle Lui
36  * @version 2.5
37  */
38 public class RNAHelicesColour extends ResidueColourScheme
39 {
40
41   /**
42    * Stores random colors generated for the number of helices
43    */
44   public Hashtable helixcolorhash = new Hashtable();
45
46   /**
47    * Maps sequence positions to the RNA helix they belong to. Key: position,
48    * Value: helix
49    */
50   public Hashtable positionsToHelix = new Hashtable();
51
52   /**
53    * Number of helices in the RNA secondary structure
54    */
55   int numHelix = 0;
56
57   public AlignmentAnnotation annotation;
58
59   /**
60    * Creates a new RNAHelicesColour object.
61    */
62   public RNAHelicesColour(AlignmentAnnotation annotation)
63   {
64     super(ResidueProperties.nucleotideIndex);
65     this.annotation = annotation;
66     refresh();
67   }
68   public RNAHelicesColour(AnnotatedCollectionI alignment)
69   {
70     super(ResidueProperties.nucleotideIndex);
71     alignmentChanged(alignment, null);
72   }
73
74   @Override
75   public void alignmentChanged(AnnotatedCollectionI alignment,
76           Map<SequenceI, SequenceCollectionI> hiddenReps)
77   {
78
79     // This loop will find the first rna structure annotation by which to colour
80     //  the sequences.
81     AlignmentAnnotation[] annotations = alignment.getAlignmentAnnotation();
82     for (int i = 0; i < annotations.length; i++) {
83         
84         // is this a sensible way of determining type of annotation?
85         if (annotations[i].getRNAStruc() != null) { 
86                 annotation = annotations[i];
87                 break;
88         }
89     }
90
91     refresh();
92
93   }
94   private long lastrefresh = -1;
95
96   public void refresh()
97   {
98     
99     if (annotation!=null && ((annotation._rnasecstr == null
100                 || lastrefresh != annotation._rnasecstr.hashCode())
101             && annotation.isValidStruc()))
102     {
103       annotation.getRNAStruc();
104       lastrefresh = annotation._rnasecstr.hashCode();
105       numHelix = 0;
106       positionsToHelix = new Hashtable();
107
108       // Figure out number of helices
109       // Length of rnasecstr is the number of pairs of positions that base pair
110       // with each other in the secondary structure
111       for (int x = 0; x < this.annotation._rnasecstr.length; x++)
112       {
113
114         /*
115          * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
116          * this.annotation._rnasecstr[x].getBegin());
117          */
118         // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
119
120         positionsToHelix.put(this.annotation._rnasecstr[x].getBegin(),
121                 this.annotation._rnasecstr[x].getFeatureGroup());
122         positionsToHelix.put(this.annotation._rnasecstr[x].getEnd(),
123                 this.annotation._rnasecstr[x].getFeatureGroup());
124
125         if (Integer.parseInt(this.annotation._rnasecstr[x]
126                 .getFeatureGroup()) > numHelix)
127         {
128           numHelix = Integer.parseInt(this.annotation._rnasecstr[x]
129                   .getFeatureGroup());
130         }
131
132       }
133
134       // Generate random colors and store
135       for (int j = 0; j <= numHelix; j++)
136       {
137         if (!helixcolorhash.containsKey(Integer.toString(j)))
138         {
139           helixcolorhash.put(Integer.toString(j),
140                   jalview.util.ColorUtils.generateRandomColor(Color.white));
141         }
142       }
143     }
144   }
145
146   /**
147    * Returns default color base on purinepyrimidineIndex in
148    * jalview.schemes.ResidueProperties (Allows coloring in sequence logo)
149    * 
150    * @param c
151    *          Character in sequence
152    * 
153    * @return color in RGB
154    */
155   @Override
156   public Color findColour(char c)
157   {
158     return ResidueProperties.purinepyrimidine[ResidueProperties.purinepyrimidineIndex[c]];
159     // random colors for all positions
160     // jalview.util.ColorUtils.generateRandomColor(Color.white); If you want
161   }
162
163   /**
164    * Returns color based on helices
165    * 
166    * @param c
167    *          Character in sequence
168    * @param j
169    *          Threshold
170    * 
171    * @return Color in RGB
172    */
173   @Override
174   public Color findColour(char c, int j, SequenceI seq)
175   {
176     refresh();
177     Color currentColour = Color.white;
178     String currentHelix = null;
179     currentHelix = (String) positionsToHelix.get(j);
180
181     if (currentHelix != null)
182     {
183       currentColour = (Color) helixcolorhash.get(currentHelix);
184     }
185
186     // System.out.println(c + " " + j + " helix " + currentHelix + " " +
187     // currentColour);
188     return currentColour;
189   }
190 }