JAL-1233 JAL-1576 copy constructor to propagate helix colours and applyTo implementat...
[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 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   /**
78    * clones colour settings and annotation row data
79    * 
80    * @param rnaHelicesColour
81    */
82   public RNAHelicesColour(RNAHelicesColour rnaHelicesColour)
83   {
84     super(ResidueProperties.nucleotideIndex);
85     helixcolorhash = rnaHelicesColour.helixcolorhash;
86     annotation = rnaHelicesColour.annotation;
87     refresh();
88   }
89
90   @Override
91   public void alignmentChanged(AnnotatedCollectionI alignment,
92           Map<SequenceI, SequenceCollectionI> hiddenReps)
93   {
94
95     // This loop will find the first rna structure annotation by which to colour
96     // the sequences.
97     AlignmentAnnotation[] annotations = alignment.getAlignmentAnnotation();
98     for (int i = 0; i < annotations.length; i++)
99     {
100
101       // is this a sensible way of determining type of annotation?
102       if (annotations[i].getRNAStruc() != null)
103       {
104         annotation = annotations[i];
105         break;
106       }
107     }
108
109     refresh();
110
111   }
112
113   private long lastrefresh = -1;
114
115   public void refresh()
116   {
117
118     if (annotation != null
119             && ((annotation._rnasecstr == null || lastrefresh != annotation._rnasecstr
120                     .hashCode()) && annotation.isValidStruc()))
121     {
122       annotation.getRNAStruc();
123       lastrefresh = annotation._rnasecstr.hashCode();
124       numHelix = 0;
125       positionsToHelix = new Hashtable();
126
127       // Figure out number of helices
128       // Length of rnasecstr is the number of pairs of positions that base pair
129       // with each other in the secondary structure
130       for (int x = 0; x < this.annotation._rnasecstr.length; x++)
131       {
132
133         /*
134          * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
135          * this.annotation._rnasecstr[x].getBegin());
136          */
137         // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
138
139         positionsToHelix.put(this.annotation._rnasecstr[x].getBegin(),
140                 this.annotation._rnasecstr[x].getFeatureGroup());
141         positionsToHelix.put(this.annotation._rnasecstr[x].getEnd(),
142                 this.annotation._rnasecstr[x].getFeatureGroup());
143
144         if (Integer.parseInt(this.annotation._rnasecstr[x]
145                 .getFeatureGroup()) > numHelix)
146         {
147           numHelix = Integer.parseInt(this.annotation._rnasecstr[x]
148                   .getFeatureGroup());
149         }
150
151       }
152
153       // Generate random colors and store
154       for (int j = 0; j <= numHelix; j++)
155       {
156         if (!helixcolorhash.containsKey(Integer.toString(j)))
157         {
158           helixcolorhash.put(Integer.toString(j),
159                   jalview.util.ColorUtils.generateRandomColor(Color.white));
160         }
161       }
162     }
163   }
164
165   /**
166    * Returns default color base on purinepyrimidineIndex in
167    * jalview.schemes.ResidueProperties (Allows coloring in sequence logo)
168    * 
169    * @param c
170    *          Character in sequence
171    * 
172    * @return color in RGB
173    */
174   @Override
175   public Color findColour(char c)
176   {
177     return ResidueProperties.purinepyrimidine[ResidueProperties.purinepyrimidineIndex[c]];
178     // random colors for all positions
179     // jalview.util.ColorUtils.generateRandomColor(Color.white); If you want
180   }
181
182   /**
183    * Returns color based on helices
184    * 
185    * @param c
186    *          Character in sequence
187    * @param j
188    *          Threshold
189    * 
190    * @return Color in RGB
191    */
192   @Override
193   public Color findColour(char c, int j, SequenceI seq)
194   {
195     refresh();
196     Color currentColour = Color.white;
197     String currentHelix = null;
198     currentHelix = (String) positionsToHelix.get(j);
199
200     if (currentHelix != null)
201     {
202       currentColour = (Color) helixcolorhash.get(currentHelix);
203     }
204
205     // System.out.println(c + " " + j + " helix " + currentHelix + " " +
206     // currentColour);
207     return currentColour;
208   }
209
210   @Override
211   public ColourSchemeI applyTo(AnnotatedCollectionI sg,
212           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
213   {
214     return new RNAHelicesColour(this);
215   }
216 }