JAL-2360 refactoring for JalviewColourScheme enum,
[jalview.git] / src / jalview / schemes / RNAHelicesColour.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.AlignmentI;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.SequenceCollectionI;
27 import jalview.datamodel.SequenceI;
28
29 import java.awt.Color;
30 import java.util.Hashtable;
31 import java.util.Map;
32
33 /**
34  * Looks at the information computed from an RNA Stockholm format file on the
35  * secondary structure of the alignment. Extracts the information on the
36  * positions of the helices present and assigns colors.
37  * 
38  * @author Lauren Michelle Lui
39  * @version 2.5
40  */
41 public class RNAHelicesColour extends ResidueColourScheme
42 {
43
44   /**
45    * Maps sequence positions to the RNA helix they belong to. Key: position,
46    * Value: helix TODO: Revise or drop in favour of annotation position numbers
47    */
48   public Hashtable<Integer, String> positionsToHelix = new Hashtable<Integer, String>();
49
50   /**
51    * Number of helices in the RNA secondary structure
52    */
53   int numHelix = 0;
54
55   public AlignmentAnnotation annotation;
56
57   /**
58    * Creates a new RNAHelicesColour object.
59    */
60   public RNAHelicesColour(AlignmentAnnotation annotation)
61   {
62     super(ResidueProperties.nucleotideIndex);
63     this.annotation = annotation;
64     ColourSchemeProperty.resetRnaHelicesShading();
65     refresh();
66   }
67
68   public RNAHelicesColour(AnnotatedCollectionI alignment)
69   {
70     super(ResidueProperties.nucleotideIndex);
71     ColourSchemeProperty.resetRnaHelicesShading();
72     alignmentChanged(alignment, null);
73   }
74
75   /**
76    * clones colour settings and annotation row data
77    * 
78    * @param rnaHelicesColour
79    */
80   public RNAHelicesColour(RNAHelicesColour rnaHelicesColour)
81   {
82     super(ResidueProperties.nucleotideIndex);
83     annotation = rnaHelicesColour.annotation;
84     refresh();
85   }
86
87   @Override
88   public void alignmentChanged(AnnotatedCollectionI alignment,
89           Map<SequenceI, SequenceCollectionI> hiddenReps)
90   {
91
92     // This loop will find the first rna structure annotation by which to colour
93     // the sequences.
94     AlignmentAnnotation[] annotations = alignment.getAlignmentAnnotation();
95     if (annotations == null)
96     {
97       return;
98     }
99     for (int i = 0; i < annotations.length; i++)
100     {
101
102       // is this a sensible way of determining type of annotation?
103       if (annotations[i].visible && annotations[i].isRNA()
104               && annotations[i].isValidStruc())
105       {
106         annotation = annotations[i];
107         break;
108       }
109     }
110
111     refresh();
112
113   }
114
115   private long lastrefresh = -1;
116
117   public void refresh()
118   {
119
120     if (annotation != null
121             && ((annotation._rnasecstr == null || lastrefresh != annotation._rnasecstr
122                     .hashCode()) && annotation.isValidStruc()))
123     {
124       annotation.getRNAStruc();
125       lastrefresh = annotation._rnasecstr.hashCode();
126       numHelix = 0;
127       positionsToHelix = new Hashtable<Integer, String>();
128
129       // Figure out number of helices
130       // Length of rnasecstr is the number of pairs of positions that base pair
131       // with each other in the secondary structure
132       for (int x = 0; x < this.annotation._rnasecstr.length; x++)
133       {
134
135         /*
136          * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
137          * this.annotation._rnasecstr[x].getBegin());
138          */
139         // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
140
141         positionsToHelix.put(this.annotation._rnasecstr[x].getBegin(),
142                 this.annotation._rnasecstr[x].getFeatureGroup());
143         positionsToHelix.put(this.annotation._rnasecstr[x].getEnd(),
144                 this.annotation._rnasecstr[x].getFeatureGroup());
145
146         if (Integer.parseInt(this.annotation._rnasecstr[x]
147                 .getFeatureGroup()) > numHelix)
148         {
149           numHelix = Integer.parseInt(this.annotation._rnasecstr[x]
150                   .getFeatureGroup());
151         }
152
153       }
154       ColourSchemeProperty.initRnaHelicesShading(numHelix);
155     }
156   }
157
158   /**
159    * Returns default color base on purinepyrimidineIndex in
160    * jalview.schemes.ResidueProperties (Allows coloring in sequence logo)
161    * 
162    * @param c
163    *          Character in sequence
164    * 
165    * @return color in RGB
166    */
167   @Override
168   public Color findColour(char c)
169   {
170     return ResidueProperties.purinepyrimidine[ResidueProperties.purinepyrimidineIndex[c]];
171     // random colors for all positions
172     // jalview.util.ColorUtils.generateRandomColor(Color.white); If you want
173   }
174
175   /**
176    * Returns color based on helices
177    * 
178    * @param c
179    *          Character in sequence
180    * @param j
181    *          position in sequence - used to locate helix
182    * 
183    * @return Color in RGB
184    */
185   @Override
186   public Color findColour(char c, int j, SequenceI seq)
187   {
188     refresh();
189     Color currentColour = Color.white;
190     String currentHelix = null;
191     currentHelix = positionsToHelix.get(j);
192     if (currentHelix != null)
193     {
194       currentColour = ColourSchemeProperty.rnaHelices[Integer
195               .parseInt(currentHelix)];
196     }
197     return currentColour;
198   }
199
200   @Override
201   public ColourSchemeI applyTo(AnnotatedCollectionI sg,
202           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
203   {
204     return new RNAHelicesColour(this);
205   }
206
207   @Override
208   public boolean isNucleotideSpecific()
209   {
210     return true;
211   }
212
213   /**
214    * Answers true if the data has RNA secondary structure annotation
215    */
216   @Override
217   public boolean isApplicableTo(AnnotatedCollectionI ac)
218   {
219     AnnotatedCollectionI context = ac.getContext();
220     if (context == null)
221     {
222       context = ac;
223     }
224     if (context instanceof AlignmentI)
225     {
226       return ((AlignmentI) context).hasRNAStructure();
227     }
228     return false;
229   }
230
231   @Override
232   public String getSchemeName()
233   {
234     return JalviewColourScheme.RNAHelices.toString();
235   }
236 }