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