JAL-1871 reinstate random generation of palette when applying RNA Helices colouring
[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.AnnotatedCollectionI;
25 import jalview.datamodel.SequenceCollectionI;
26 import jalview.datamodel.SequenceI;
27
28 import java.awt.Color;
29 import java.util.Hashtable;
30 import java.util.Map;
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   /**
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     for (int i = 0; i < annotations.length; i++)
96     {
97
98       // is this a sensible way of determining type of annotation?
99       if (annotations[i].visible && annotations[i].isRNA()
100               && annotations[i].isValidStruc())
101       {
102         annotation = annotations[i];
103         break;
104       }
105     }
106
107     refresh();
108
109   }
110
111   private long lastrefresh = -1;
112
113   public void refresh()
114   {
115
116     if (annotation != null
117             && ((annotation._rnasecstr == null || lastrefresh != annotation._rnasecstr
118                     .hashCode()) && annotation.isValidStruc()))
119     {
120       annotation.getRNAStruc();
121       lastrefresh = annotation._rnasecstr.hashCode();
122       numHelix = 0;
123       positionsToHelix = new Hashtable<Integer, String>();
124
125       // Figure out number of helices
126       // Length of rnasecstr is the number of pairs of positions that base pair
127       // with each other in the secondary structure
128       for (int x = 0; x < this.annotation._rnasecstr.length; x++)
129       {
130
131         /*
132          * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
133          * this.annotation._rnasecstr[x].getBegin());
134          */
135         // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
136
137         positionsToHelix.put(this.annotation._rnasecstr[x].getBegin(),
138                 this.annotation._rnasecstr[x].getFeatureGroup());
139         positionsToHelix.put(this.annotation._rnasecstr[x].getEnd(),
140                 this.annotation._rnasecstr[x].getFeatureGroup());
141
142         if (Integer.parseInt(this.annotation._rnasecstr[x]
143                 .getFeatureGroup()) > numHelix)
144         {
145           numHelix = Integer.parseInt(this.annotation._rnasecstr[x]
146                   .getFeatureGroup());
147         }
148
149       }
150       ColourSchemeProperty.initRnaHelicesShading(numHelix);
151     }
152   }
153
154   /**
155    * Returns default color base on purinepyrimidineIndex in
156    * jalview.schemes.ResidueProperties (Allows coloring in sequence logo)
157    * 
158    * @param c
159    *          Character in sequence
160    * 
161    * @return color in RGB
162    */
163   @Override
164   public Color findColour(char c)
165   {
166     return ResidueProperties.purinepyrimidine[ResidueProperties.purinepyrimidineIndex[c]];
167     // random colors for all positions
168     // jalview.util.ColorUtils.generateRandomColor(Color.white); If you want
169   }
170
171   /**
172    * Returns color based on helices
173    * 
174    * @param c
175    *          Character in sequence
176    * @param j
177    *          position in sequence - used to locate helix
178    * 
179    * @return Color in RGB
180    */
181   @Override
182   public Color findColour(char c, int j, SequenceI seq)
183   {
184     refresh();
185     Color currentColour = Color.white;
186     String currentHelix = null;
187     currentHelix = positionsToHelix.get(j);
188     if (currentHelix != null)
189     {
190       currentColour = ColourSchemeProperty.rnaHelices[Integer
191               .parseInt(currentHelix)];
192     }
193     return currentColour;
194   }
195
196   @Override
197   public ColourSchemeI applyTo(AnnotatedCollectionI sg,
198           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
199   {
200     return new RNAHelicesColour(this);
201   }
202 }