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