Colour be RNA helices bug fixed
[jalview.git] / src / jalview / schemes / RNAHelicesColourChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.schemes;
19
20 import java.util.*;
21 import java.awt.event.*;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.*;
26 import jalview.schemes.*;
27
28 /**
29  * Helps generate the colors for RNA secondary structure. Future: add option to
30  * change colors based on covariation.
31  * 
32  * @author Lauren Michelle Lui
33  * 
34  */
35 public class RNAHelicesColourChooser
36 {
37
38   AlignViewportI av;
39
40   AlignmentViewPanel ap;
41
42   ColourSchemeI oldcs;
43
44   Hashtable oldgroupColours;
45
46   jalview.datamodel.AlignmentAnnotation currentAnnotation;
47
48   boolean adjusting = false;
49
50   public RNAHelicesColourChooser(AlignViewportI av,
51           final AlignmentViewPanel ap)
52   {
53     oldcs = av.getGlobalColourScheme();
54     if (av.getAlignment().getGroups() != null)
55     {
56       oldgroupColours = new Hashtable();
57       for (SequenceGroup sg : ap.getAlignment().getGroups())
58       {
59         if (sg.cs != null)
60         {
61           oldgroupColours.put(sg, sg.cs);
62         }
63       }
64     }
65     this.av = av;
66     this.ap = ap;
67
68     if (oldcs instanceof RNAHelicesColour)
69     {
70       RNAHelicesColour rhc = (RNAHelicesColour) oldcs;
71
72     }
73
74     adjusting = true;
75     Vector list = new Vector();
76     int index = 1;
77     for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
78     {
79       String label = av.getAlignment().getAlignmentAnnotation()[i].label;
80       if (!list.contains(label))
81         list.addElement(label);
82       else
83         list.addElement(label + "_" + (index++));
84     }
85
86     adjusting = false;
87
88     changeColour();
89
90   }
91
92   void changeColour()
93   {
94     // Check if combobox is still adjusting
95     if (adjusting)
96     {
97       return;
98     }
99
100     // This loop will find the first rna structure annotation by which to colour
101     //  the sequences.
102     AlignmentAnnotation[] annotations = av.getAlignment().getAlignmentAnnotation();
103     for (int i = 0; i < annotations.length; i++) {
104         
105         // is this a sensible way of determining type of annotation?
106         if (annotations[i].getRNAStruc() != null) { 
107                 currentAnnotation = annotations[i];
108                 break;
109         }
110     }
111     if (currentAnnotation == null)   
112     {
113         System.err.println("Jalview is about to try and colour by RNAHelices even"
114                         + " though there are no RNA secondary structure annotations present!");
115         currentAnnotation = av.getAlignment().getAlignmentAnnotation()[0];// annotations.getSelectedIndex()];
116     }
117     
118     RNAHelicesColour rhc = null;
119
120     rhc = new RNAHelicesColour(currentAnnotation);
121
122     av.setGlobalColourScheme(rhc);
123
124     if (av.getAlignment().getGroups() != null)
125     {
126       for (SequenceGroup sg : ap.getAlignment().getGroups())
127       {
128         if (sg.cs == null)
129         {
130           continue;
131         }
132
133         sg.cs = new RNAHelicesColour(currentAnnotation);
134
135       }
136     }
137
138     ap.paintAlignment(false);
139   }
140
141   void reset()
142   {
143     av.setGlobalColourScheme(oldcs);
144     if (av.getAlignment().getGroups() != null)
145     {
146       for (SequenceGroup sg : ap.getAlignment().getGroups())
147       {
148         sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
149       }
150     }
151   }
152
153   public void annotations_actionPerformed(ActionEvent e)
154   {
155     changeColour();
156   }
157
158 }