2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.workers;
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;
33 import java.util.Hashtable;
35 public class ConsensusThread extends AlignCalcWorker implements
38 public ConsensusThread(AlignViewportI alignViewport,
39 AlignmentViewPanel alignPanel)
41 super(alignViewport, alignPanel);
47 if (calcMan.isPending(this))
51 calcMan.notifyStart(this);
52 long started = System.currentTimeMillis();
55 AlignmentAnnotation consensus = getConsensusAnnotation();
56 if (consensus == null || calcMan.isPending(this))
58 calcMan.workerComplete(this);
61 while (!calcMan.notifyWorking(this))
63 // System.err.println("Thread (Consensus"+Thread.currentThread().getName()+") Waiting around.");
68 ap.paintAlignment(false);
71 } catch (Exception ex)
76 if (alignViewport.isClosed())
81 AlignmentI alignment = alignViewport.getAlignment();
85 if (alignment == null || (aWidth = alignment.getWidth()) < 0)
87 calcMan.workerComplete(this);
90 eraseConsensus(aWidth);
91 // long now = System.currentTimeMillis();
92 computeConsensus(alignment);
93 updateResultAnnotation(true);
94 // System.out.println(System.currentTimeMillis() - now);
98 ap.paintAlignment(true);
100 } catch (OutOfMemoryError error)
102 calcMan.workerCannotRun(this);
103 ap.raiseOOMWarning("calculating consensus", error);
107 * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition
108 * - alignment was edited at same time as calculation was running
110 calcMan.workerComplete(this);
115 * Clear out any existing consensus annotations
118 * the width (number of columns) of the annotated alignment
120 protected void eraseConsensus(int aWidth)
122 AlignmentAnnotation consensus = getConsensusAnnotation();
123 consensus.annotations = new Annotation[aWidth];
129 protected void computeConsensus(AlignmentI alignment)
131 Hashtable[] hconsensus = new Hashtable[alignment.getWidth()];
133 SequenceI[] aseqs = getSequences();
134 AAFrequency.calculate(aseqs, 0, alignment.getWidth(), hconsensus,
137 alignViewport.setSequenceConsensusHash(hconsensus);
138 setColourSchemeConsensus(hconsensus);
144 protected SequenceI[] getSequences()
146 return alignViewport.getAlignment().getSequencesArray();
152 protected void setColourSchemeConsensus(Hashtable[] hconsensus)
154 ColourSchemeI globalColourScheme = alignViewport
155 .getGlobalColourScheme();
156 if (globalColourScheme != null)
158 globalColourScheme.setConsensus(hconsensus);
163 * Get the Consensus annotation for the alignment
167 protected AlignmentAnnotation getConsensusAnnotation()
169 return alignViewport.getAlignmentConsensusAnnotation();
173 * update the consensus annotation from the sequence profile data using
174 * current visualization settings.
177 public void updateAnnotation()
179 updateResultAnnotation(false);
182 public void updateResultAnnotation(boolean immediate)
184 AlignmentAnnotation consensus = getConsensusAnnotation();
185 Hashtable[] hconsensus = getViewportConsensus();
186 if (immediate || !calcMan.isWorking(this) && consensus != null
187 && hconsensus != null)
189 deriveConsensus(consensus, hconsensus);
194 * Convert the computed consensus data into the desired annotation for
197 * @param consensusAnnotation
198 * the annotation to be populated
199 * @param consensusData
200 * the computed consensus data
202 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
203 Hashtable[] consensusData)
205 long nseq = getSequences().length;
206 AAFrequency.completeConsensus(consensusAnnotation, consensusData, 0,
207 consensusData.length, alignViewport.isIgnoreGapsConsensus(),
208 alignViewport.isShowSequenceLogo(), nseq);
212 * Get the consensus data stored on the viewport.
216 protected Hashtable[] getViewportConsensus()
218 return alignViewport.getSequenceConsensusHash();