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