JAL-1373 pass size of sequence set to consensus rendering routines so appropriate...
[jalview.git] / src / jalview / workers / ConsensusThread.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.workers;
19
20 import jalview.analysis.AAFrequency;
21 import jalview.api.AlignCalcWorkerI;
22 import jalview.api.AlignViewportI;
23 import jalview.api.AlignmentViewPanel;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.SequenceI;
28 import jalview.schemes.ColourSchemeI;
29
30 import java.util.Hashtable;
31
32 public class ConsensusThread extends AlignCalcWorker implements
33         AlignCalcWorkerI
34 {
35   private long nseq=-1;
36
37   public ConsensusThread(AlignViewportI alignViewport,
38           AlignmentViewPanel alignPanel)
39   {
40     super(alignViewport, alignPanel);
41   }
42
43   @Override
44   public void run()
45   {
46     if (calcMan.isPending(this))
47     {
48       return;
49     }
50     calcMan.notifyStart(this);
51     long started = System.currentTimeMillis();
52     try
53     {
54       AlignmentAnnotation consensus = alignViewport
55               .getAlignmentConsensusAnnotation();
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       }
80       AlignmentI alignment = alignViewport.getAlignment();
81
82       int aWidth = -1;
83
84       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
85       {
86         calcMan.workerComplete(this);
87         // .updatingConservation = false;
88         // AlignViewport.UPDATING_CONSERVATION = false;
89
90         return;
91       }
92       consensus = alignViewport.getAlignmentConsensusAnnotation();
93
94       consensus.annotations = null;
95       consensus.annotations = new Annotation[aWidth];
96       Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
97       hconsensus = new Hashtable[aWidth];
98       try
99       {
100         SequenceI aseqs[] = alignment.getSequencesArray();
101         nseq = aseqs.length;
102         AAFrequency.calculate(aseqs, 0,
103                 alignment.getWidth(), hconsensus, true);
104       } catch (ArrayIndexOutOfBoundsException x)
105       {
106         // this happens due to a race condition -
107         // alignment was edited at same time as calculation was running
108         //
109         // calcMan.workerCannotRun(this);
110         calcMan.workerComplete(this);
111         return;
112       }
113       alignViewport.setSequenceConsensusHash(hconsensus);
114       updateResultAnnotation(true);
115       ColourSchemeI globalColourScheme = alignViewport
116               .getGlobalColourScheme();
117       if (globalColourScheme != null)
118       {
119         globalColourScheme.setConsensus(hconsensus);
120       }
121
122     } catch (OutOfMemoryError error)
123     {
124       calcMan.workerCannotRun(this);
125
126       // consensus = null;
127       // hconsensus = null;
128       ap.raiseOOMWarning("calculating consensus", error);
129     }
130
131     calcMan.workerComplete(this);
132     if (ap != null)
133     {
134       ap.paintAlignment(true);
135     }
136   }
137
138   /**
139    * update the consensus annotation from the sequence profile data using
140    * current visualization settings.
141    */
142   @Override
143   public void updateAnnotation()
144   {
145     updateResultAnnotation(false);
146   }
147
148   public void updateResultAnnotation(boolean immediate)
149   {
150     AlignmentAnnotation consensus = alignViewport
151             .getAlignmentConsensusAnnotation();
152     Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
153     if (immediate || !calcMan.isWorking(this) && consensus != null
154             && hconsensus != null)
155     {
156       AAFrequency.completeConsensus(consensus, hconsensus, 0,
157               hconsensus.length, alignViewport.getIgnoreGapsConsensus(),
158               alignViewport.isShowSequenceLogo(), nseq);
159     }
160   }
161 }