Merge branch 'Release_2_8_0b1_Branch' into try_r20b1_merge
[jalview.git] / src / jalview / schemes / RNAHelicesColourChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.schemes;
20
21 import java.util.*;
22 import java.awt.event.*;
23
24 import jalview.api.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.datamodel.*;
27 import jalview.schemes.*;
28
29 /**
30  * Helps generate the colors for RNA secondary structure. Future: add option to
31  * change colors based on covariation.
32  * 
33  * @author Lauren Michelle Lui
34  * 
35  */
36 public class RNAHelicesColourChooser
37 {
38
39   AlignViewportI av;
40
41   AlignmentViewPanel ap;
42
43   ColourSchemeI oldcs;
44
45   Hashtable oldgroupColours;
46
47   jalview.datamodel.AlignmentAnnotation currentAnnotation;
48
49   boolean adjusting = false;
50
51   public RNAHelicesColourChooser(AlignViewportI av,
52           final AlignmentViewPanel ap)
53   {
54     oldcs = av.getGlobalColourScheme();
55     if (av.getAlignment().getGroups() != null)
56     {
57       oldgroupColours = new Hashtable();
58       for (SequenceGroup sg : ap.getAlignment().getGroups())
59       {
60         if (sg.cs != null)
61         {
62           oldgroupColours.put(sg, sg.cs);
63         }
64       }
65     }
66     this.av = av;
67     this.ap = ap;
68
69     if (oldcs instanceof RNAHelicesColour)
70     {
71       RNAHelicesColour rhc = (RNAHelicesColour) oldcs;
72
73     }
74
75     adjusting = true;
76     Vector list = new Vector();
77     int index = 1;
78     for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
79     {
80       String label = av.getAlignment().getAlignmentAnnotation()[i].label;
81       if (!list.contains(label))
82         list.addElement(label);
83       else
84         list.addElement(label + "_" + (index++));
85     }
86
87     adjusting = false;
88
89     changeColour();
90
91   }
92
93   void changeColour()
94   {
95     // Check if combobox is still adjusting
96     if (adjusting)
97     {
98       return;
99     }
100
101     // This loop will find the first rna structure annotation by which to colour
102     //  the sequences.
103     AlignmentAnnotation[] annotations = av.getAlignment().getAlignmentAnnotation();
104     for (int i = 0; i < annotations.length; i++) {
105         
106         // is this a sensible way of determining type of annotation?
107         if (annotations[i].getRNAStruc() != null) { 
108                 currentAnnotation = annotations[i];
109                 break;
110         }
111     }
112     if (currentAnnotation == null)   
113     {
114         System.err.println("Jalview is about to try and colour by RNAHelices even"
115                         + " though there are no RNA secondary structure annotations present!");
116         currentAnnotation = av.getAlignment().getAlignmentAnnotation()[0];// annotations.getSelectedIndex()];
117     }
118     
119     RNAHelicesColour rhc = null;
120
121     rhc = new RNAHelicesColour(currentAnnotation);
122
123     av.setGlobalColourScheme(rhc);
124
125     if (av.getAlignment().getGroups() != null)
126     {
127       for (SequenceGroup sg : ap.getAlignment().getGroups())
128       {
129         if (sg.cs == null)
130         {
131           continue;
132         }
133
134         sg.cs = new RNAHelicesColour(currentAnnotation);
135
136       }
137     }
138
139     ap.paintAlignment(false);
140   }
141
142   void reset()
143   {
144     av.setGlobalColourScheme(oldcs);
145     if (av.getAlignment().getGroups() != null)
146     {
147       for (SequenceGroup sg : ap.getAlignment().getGroups())
148       {
149         sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
150       }
151     }
152   }
153
154   public void annotations_actionPerformed(ActionEvent e)
155   {
156     changeColour();
157   }
158
159 }