JAL-1432 updated copyright notices
[jalview.git] / src / jalview / workers / ConsensusThread.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.workers;
20
21 import jalview.analysis.AAFrequency;
22 import jalview.api.AlignCalcWorkerI;
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Annotation;
28 import jalview.schemes.ColourSchemeI;
29
30 import java.util.Hashtable;
31
32 public class ConsensusThread extends AlignCalcWorker implements
33         AlignCalcWorkerI
34 {
35   public ConsensusThread(AlignViewportI alignViewport,
36           AlignmentViewPanel alignPanel)
37   {
38     super(alignViewport, alignPanel);
39   }
40
41   @Override
42   public void run()
43   {
44     if (calcMan.isPending(this))
45     {
46       return;
47     }
48     calcMan.notifyStart(this);
49     long started = System.currentTimeMillis();
50     try
51     {
52       AlignmentAnnotation consensus = alignViewport
53               .getAlignmentConsensusAnnotation();
54       if (consensus == null || calcMan.isPending(this))
55       {
56         calcMan.workerComplete(this);
57         return;
58       }
59       while (!calcMan.notifyWorking(this))
60       {
61         // System.err.println("Thread (Consensus"+Thread.currentThread().getName()+") Waiting around.");
62         try
63         {
64           if (ap != null)
65           {
66             ap.paintAlignment(false);
67           }
68           Thread.sleep(200);
69         } catch (Exception ex)
70         {
71           ex.printStackTrace();
72         }
73       }
74       if (alignViewport.isClosed())
75       {
76         abortAndDestroy();
77       }
78       AlignmentI alignment = alignViewport.getAlignment();
79
80       int aWidth = -1;
81
82       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
83       {
84         calcMan.workerComplete(this);
85         // .updatingConservation = false;
86         // AlignViewport.UPDATING_CONSERVATION = false;
87
88         return;
89       }
90       consensus = alignViewport.getAlignmentConsensusAnnotation();
91
92       consensus.annotations = null;
93       consensus.annotations = new Annotation[aWidth];
94       Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
95       hconsensus = new Hashtable[aWidth];
96       try
97       {
98         AAFrequency.calculate(alignment.getSequencesArray(), 0,
99                 alignment.getWidth(), hconsensus, true);
100       } catch (ArrayIndexOutOfBoundsException x)
101       {
102         // this happens due to a race condition -
103         // alignment was edited at same time as calculation was running
104         //
105         // calcMan.workerCannotRun(this);
106         calcMan.workerComplete(this);
107         return;
108       }
109       alignViewport.setSequenceConsensusHash(hconsensus);
110       updateResultAnnotation(true);
111       ColourSchemeI globalColourScheme = alignViewport
112               .getGlobalColourScheme();
113       if (globalColourScheme != null)
114       {
115         globalColourScheme.setConsensus(hconsensus);
116       }
117
118     } catch (OutOfMemoryError error)
119     {
120       calcMan.workerCannotRun(this);
121
122       // consensus = null;
123       // hconsensus = null;
124       ap.raiseOOMWarning("calculating consensus", error);
125     }
126
127     calcMan.workerComplete(this);
128     if (ap != null)
129     {
130       ap.paintAlignment(true);
131     }
132   }
133
134   /**
135    * update the consensus annotation from the sequence profile data using
136    * current visualization settings.
137    */
138   @Override
139   public void updateAnnotation()
140   {
141     updateResultAnnotation(false);
142   }
143
144   public void updateResultAnnotation(boolean immediate)
145   {
146     AlignmentAnnotation consensus = alignViewport
147             .getAlignmentConsensusAnnotation();
148     Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
149     if (immediate || !calcMan.isWorking(this) && consensus != null
150             && hconsensus != null)
151     {
152       AAFrequency.completeConsensus(consensus, hconsensus, 0,
153               hconsensus.length, alignViewport.getIgnoreGapsConsensus(),
154               alignViewport.isShowSequenceLogo());
155     }
156   }
157 }