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