JAL-1681 show cDNA consensus on protein alignment - first version
[jalview.git] / src / jalview / workers / ConsensusThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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.workers;
22
23 import jalview.analysis.AAFrequency;
24 import jalview.api.AlignCalcWorkerI;
25 import jalview.api.AlignViewportI;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.SequenceI;
31 import jalview.schemes.ColourSchemeI;
32
33 import java.util.Hashtable;
34
35 public class ConsensusThread extends AlignCalcWorker implements
36         AlignCalcWorkerI
37 {
38   public ConsensusThread(AlignViewportI alignViewport,
39           AlignmentViewPanel alignPanel)
40   {
41     super(alignViewport, alignPanel);
42   }
43
44   @Override
45   public void run()
46   {
47     if (calcMan.isPending(this))
48     {
49       return;
50     }
51     calcMan.notifyStart(this);
52     long started = System.currentTimeMillis();
53     try
54     {
55       AlignmentAnnotation consensus = getConsensusAnnotation();
56       if (consensus == null || calcMan.isPending(this))
57       {
58         calcMan.workerComplete(this);
59         return;
60       }
61       while (!calcMan.notifyWorking(this))
62       {
63         // System.err.println("Thread (Consensus"+Thread.currentThread().getName()+") Waiting around.");
64         try
65         {
66           if (ap != null)
67           {
68             ap.paintAlignment(false);
69           }
70           Thread.sleep(200);
71         } catch (Exception ex)
72         {
73           ex.printStackTrace();
74         }
75       }
76       if (alignViewport.isClosed())
77       {
78         abortAndDestroy();
79         return;
80       }
81       AlignmentI alignment = alignViewport.getAlignment();
82
83       int aWidth = -1;
84
85       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
86       {
87         calcMan.workerComplete(this);
88         return;
89       }
90       eraseConsensus(aWidth);
91       // long now = System.currentTimeMillis();
92       computeConsensus(alignment);
93       updateResultAnnotation(true);
94       // System.out.println(System.currentTimeMillis() - now);
95
96       if (ap != null)
97       {
98         ap.paintAlignment(true);
99       }
100     } catch (OutOfMemoryError error)
101     {
102       calcMan.workerCannotRun(this);
103       ap.raiseOOMWarning("calculating consensus", error);
104     } finally
105     {
106       /*
107        * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition
108        * - alignment was edited at same time as calculation was running
109        */
110       calcMan.workerComplete(this);
111     }
112   }
113
114   /**
115    * Clear out any existing consensus annotations
116    * 
117    * @param aWidth
118    *          the width (number of columns) of the annotated alignment
119    */
120   protected void eraseConsensus(int aWidth)
121   {
122     AlignmentAnnotation consensus = getConsensusAnnotation();
123     consensus.annotations = new Annotation[aWidth];
124   }
125
126   /**
127    * @param alignment
128    */
129   protected void computeConsensus(AlignmentI alignment)
130   {
131     Hashtable[] hconsensus = new Hashtable[alignment.getWidth()];
132
133     SequenceI[] aseqs = getSequences();
134     AAFrequency.calculate(aseqs, 0, alignment.getWidth(), hconsensus,
135             true);
136
137     alignViewport.setSequenceConsensusHash(hconsensus);
138     setColourSchemeConsensus(hconsensus);
139   }
140
141   /**
142    * @return
143    */
144   protected SequenceI[] getSequences()
145   {
146     return alignViewport.getAlignment().getSequencesArray();
147   }
148
149   /**
150    * @param hconsensus
151    */
152   protected void setColourSchemeConsensus(Hashtable[] hconsensus)
153   {
154     ColourSchemeI globalColourScheme = alignViewport
155             .getGlobalColourScheme();
156     if (globalColourScheme != null)
157     {
158       globalColourScheme.setConsensus(hconsensus);
159     }
160   }
161
162   /**
163    * Get the Consensus annotation for the alignment
164    * 
165    * @return
166    */
167   protected AlignmentAnnotation getConsensusAnnotation()
168   {
169     return alignViewport.getAlignmentConsensusAnnotation();
170   }
171
172   /**
173    * update the consensus annotation from the sequence profile data using
174    * current visualization settings.
175    */
176   @Override
177   public void updateAnnotation()
178   {
179     updateResultAnnotation(false);
180   }
181
182   public void updateResultAnnotation(boolean immediate)
183   {
184     AlignmentAnnotation consensus = getConsensusAnnotation();
185     Hashtable[] hconsensus = getViewportConsensus();
186     if (immediate || !calcMan.isWorking(this) && consensus != null
187             && hconsensus != null)
188     {
189       deriveConsensus(consensus, hconsensus);
190     }
191   }
192
193   /**
194    * Convert the computed consensus data into the desired annotation for
195    * display.
196    * 
197    * @param consensusAnnotation
198    *          the annotation to be populated
199    * @param consensusData
200    *          the computed consensus data
201    */
202   protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
203           Hashtable[] consensusData)
204   {
205     long nseq = getSequences().length;
206     AAFrequency.completeConsensus(consensusAnnotation, consensusData, 0,
207             consensusData.length, alignViewport.isIgnoreGapsConsensus(),
208             alignViewport.isShowSequenceLogo(), nseq);
209   }
210
211   /**
212    * Get the consensus data stored on the viewport.
213    * 
214    * @return
215    */
216   protected Hashtable[] getViewportConsensus()
217   {
218     return alignViewport.getSequenceConsensusHash();
219   }
220 }