JAL-629 Change all stdout and stderr output to use Console.outPrintln and Console...
[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.api.AlignViewportI;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.AnnotatedCollectionI;
27 import jalview.datamodel.SequenceCollectionI;
28 import jalview.datamodel.SequenceI;
29
30 import java.awt.Color;
31 import java.util.Hashtable;
32 import java.util.Map;
33
34 /**
35  * Looks at the information computed from an RNA Stockholm format file on the
36  * secondary structure of the alignment. Extracts the information on the
37  * positions of the helices present and assigns colors.
38  * 
39  * @author Lauren Michelle Lui
40  * @version 2.5
41  */
42 public class RNAHelicesColour extends ResidueColourScheme
43 {
44
45   /**
46    * Maps sequence positions to the RNA helix they belong to. Key: position,
47    * Value: helix TODO: Revise or drop in favour of annotation position numbers
48    */
49   public Hashtable<Integer, String> positionsToHelix = new Hashtable<>();
50
51   /**
52    * Number of helices in the RNA secondary structure
53    */
54   int numHelix = 0;
55
56   public AlignmentAnnotation annotation;
57
58   /**
59    * Default constructor (required for ColourSchemes cache)
60    */
61   public RNAHelicesColour()
62   {
63
64   }
65
66   /**
67    * Creates a new RNAHelicesColour object.
68    */
69   public RNAHelicesColour(AlignmentAnnotation annotation)
70   {
71     super(ResidueProperties.nucleotideIndex);
72     this.annotation = annotation;
73     ColourSchemeProperty.resetRnaHelicesShading();
74     refresh();
75   }
76
77   public RNAHelicesColour(AnnotatedCollectionI alignment)
78   {
79     super(ResidueProperties.nucleotideIndex);
80     ColourSchemeProperty.resetRnaHelicesShading();
81     alignmentChanged(alignment, null);
82   }
83
84   /**
85    * clones colour settings and annotation row data
86    * 
87    * @param rnaHelicesColour
88    */
89   public RNAHelicesColour(RNAHelicesColour rnaHelicesColour)
90   {
91     super(ResidueProperties.nucleotideIndex);
92     annotation = rnaHelicesColour.annotation;
93     refresh();
94   }
95
96   @Override
97   public void alignmentChanged(AnnotatedCollectionI alignment,
98           Map<SequenceI, SequenceCollectionI> hiddenReps)
99   {
100
101     // This loop will find the first rna structure annotation by which to colour
102     // the sequences.
103     AlignmentAnnotation[] annotations = alignment.getAlignmentAnnotation();
104     if (annotations == null)
105     {
106       return;
107     }
108     for (int i = 0; i < annotations.length; i++)
109     {
110
111       // is this a sensible way of determining type of annotation?
112       if (annotations[i].visible && annotations[i].isRNA()
113               && annotations[i].isValidStruc())
114       {
115         annotation = annotations[i];
116         break;
117       }
118     }
119
120     refresh();
121
122   }
123
124   private long lastrefresh = -1;
125
126   public void refresh()
127   {
128
129     if (annotation != null && ((annotation._rnasecstr == null
130             || lastrefresh != annotation._rnasecstr.hashCode())
131             && annotation.isValidStruc()))
132     {
133       annotation.getRNAStruc();
134       lastrefresh = annotation._rnasecstr.hashCode();
135       numHelix = 0;
136       positionsToHelix = new Hashtable<>();
137
138       // Figure out number of helices
139       // Length of rnasecstr is the number of pairs of positions that base pair
140       // with each other in the secondary structure
141       for (int x = 0; x < this.annotation._rnasecstr.length; x++)
142       {
143
144         /*
145          * jalview.bin.Console.outPrintln(this.annotation._rnasecstr[x] + " Begin" +
146          * this.annotation._rnasecstr[x].getBegin());
147          */
148         // jalview.bin.Console.outPrintln(this.annotation._rnasecstr[x].getFeatureGroup());
149
150         positionsToHelix.put(this.annotation._rnasecstr[x].getBegin(),
151                 this.annotation._rnasecstr[x].getFeatureGroup());
152         positionsToHelix.put(this.annotation._rnasecstr[x].getEnd(),
153                 this.annotation._rnasecstr[x].getFeatureGroup());
154
155         if (Integer.parseInt(
156                 this.annotation._rnasecstr[x].getFeatureGroup()) > numHelix)
157         {
158           numHelix = Integer.parseInt(
159                   this.annotation._rnasecstr[x].getFeatureGroup());
160         }
161
162       }
163       ColourSchemeProperty.initRnaHelicesShading(numHelix);
164     }
165   }
166
167   /**
168    * Returns default color base on purinepyrimidineIndex in
169    * jalview.schemes.ResidueProperties (Allows coloring in sequence logo)
170    * 
171    * @param c
172    *          Character in sequence
173    * 
174    * @return color in RGB
175    */
176   @Override
177   public Color findColour(char c)
178   {
179     return ResidueProperties.purinepyrimidine[ResidueProperties.purinepyrimidineIndex[c]];
180     // random colors for all positions
181     // jalview.util.ColorUtils.generateRandomColor(Color.white); If you want
182   }
183
184   /**
185    * Returns color based on helices
186    * 
187    * @param c
188    *          Character in sequence
189    * @param j
190    *          position in sequence - used to locate helix
191    * 
192    * @return Color in RGB
193    */
194   @Override
195   public Color findColour(char c, int j, SequenceI seq)
196   {
197     refresh();
198     Color currentColour = Color.white;
199     String currentHelix = null;
200     currentHelix = positionsToHelix.get(j);
201     if (currentHelix != null)
202     {
203       currentColour = ColourSchemeProperty.rnaHelices[Integer
204               .parseInt(currentHelix)];
205     }
206     return currentColour;
207   }
208
209   @Override
210   public ColourSchemeI getInstance(AlignViewportI view,
211           AnnotatedCollectionI sg)
212   {
213     return new RNAHelicesColour(sg);
214   }
215
216   @Override
217   public boolean isNucleotideSpecific()
218   {
219     return true;
220   }
221
222   /**
223    * Answers true if the data has RNA secondary structure annotation
224    */
225   @Override
226   public boolean isApplicableTo(AnnotatedCollectionI ac)
227   {
228     if (ac instanceof AlignmentI && ((AlignmentI) ac).hasRNAStructure())
229     {
230       return true;
231     }
232
233     /*
234      * not currently supporting this option for group annotation / colouring
235      */
236     return false;
237   }
238
239   @Override
240   public String getSchemeName()
241   {
242     return JalviewColourScheme.RNAHelices.toString();
243   }
244
245   @Override
246   public boolean isSimple()
247   {
248     return false;
249   }
250 }