d17f510be1ed9c2b0e63cd07ab5eae05afd67f9f
[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    * Maps sequence positions to the RNA helix they belong to. Key: position,
45    * Value: helix TODO: Revise or drop in favour of annotation position numbers
46    */
47   public Hashtable<Integer, String> positionsToHelix = new Hashtable<Integer, String>();
48
49   /**
50    * Number of helices in the RNA secondary structure
51    */
52   int numHelix = 0;
53
54   public AlignmentAnnotation annotation;
55
56   /**
57    * Creates a new RNAHelicesColour object.
58    */
59   public RNAHelicesColour(AlignmentAnnotation annotation)
60   {
61     super(ResidueProperties.nucleotideIndex);
62     this.annotation = annotation;
63     ColourSchemeProperty.resetRnaHelicesShading();
64     refresh();
65   }
66
67   public RNAHelicesColour(AnnotatedCollectionI alignment)
68   {
69     super(ResidueProperties.nucleotideIndex);
70     ColourSchemeProperty.resetRnaHelicesShading();
71     alignmentChanged(alignment, null);
72   }
73
74   /**
75    * clones colour settings and annotation row data
76    * 
77    * @param rnaHelicesColour
78    */
79   public RNAHelicesColour(RNAHelicesColour rnaHelicesColour)
80   {
81     super(ResidueProperties.nucleotideIndex);
82     annotation = rnaHelicesColour.annotation;
83     refresh();
84   }
85
86   @Override
87   public void alignmentChanged(AnnotatedCollectionI alignment,
88           Map<SequenceI, SequenceCollectionI> hiddenReps)
89   {
90
91     // This loop will find the first rna structure annotation by which to colour
92     // the sequences.
93     AlignmentAnnotation[] annotations = alignment.getAlignmentAnnotation();
94     for (int i = 0; i < annotations.length; i++)
95     {
96
97       // is this a sensible way of determining type of annotation?
98       if (annotations[i].visible && annotations[i].isRNA()
99               && annotations[i].isValidStruc())
100       {
101         annotation = annotations[i];
102         break;
103       }
104     }
105
106     refresh();
107
108   }
109
110   private long lastrefresh = -1;
111
112   public void refresh()
113   {
114
115     if (annotation != null
116             && ((annotation._rnasecstr == null || lastrefresh != annotation._rnasecstr
117                     .hashCode()) && annotation.isValidStruc()))
118     {
119       annotation.getRNAStruc();
120       lastrefresh = annotation._rnasecstr.hashCode();
121       numHelix = 0;
122       positionsToHelix = new Hashtable<Integer, String>();
123
124       // Figure out number of helices
125       // Length of rnasecstr is the number of pairs of positions that base pair
126       // with each other in the secondary structure
127       for (int x = 0; x < this.annotation._rnasecstr.length; x++)
128       {
129
130         /*
131          * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
132          * this.annotation._rnasecstr[x].getBegin());
133          */
134         // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
135
136         positionsToHelix.put(this.annotation._rnasecstr[x].getBegin(),
137                 this.annotation._rnasecstr[x].getFeatureGroup());
138         positionsToHelix.put(this.annotation._rnasecstr[x].getEnd(),
139                 this.annotation._rnasecstr[x].getFeatureGroup());
140
141         if (Integer.parseInt(this.annotation._rnasecstr[x]
142                 .getFeatureGroup()) > numHelix)
143         {
144           numHelix = Integer.parseInt(this.annotation._rnasecstr[x]
145                   .getFeatureGroup());
146         }
147
148       }
149       ColourSchemeProperty.initRnaHelicesShading(numHelix);
150     }
151   }
152
153   /**
154    * Returns default color base on purinepyrimidineIndex in
155    * jalview.schemes.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    *          position in sequence - used to locate helix
177    * 
178    * @return Color in RGB
179    */
180   @Override
181   public Color findColour(char c, int j, SequenceI seq)
182   {
183     refresh();
184     Color currentColour = Color.white;
185     String currentHelix = null;
186     currentHelix = positionsToHelix.get(j);
187     if (currentHelix != null)
188     {
189       currentColour = ColourSchemeProperty.rnaHelices[Integer
190               .parseInt(currentHelix)];
191     }
192     return currentColour;
193   }
194
195   @Override
196   public ColourSchemeI applyTo(AnnotatedCollectionI sg,
197           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
198   {
199     return new RNAHelicesColour(this);
200   }
201 }