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 java.util.Hashtable;
25 import jalview.analysis.AAFrequency;
26 import jalview.api.AlignCalcWorkerI;
27 import jalview.api.AlignViewportI;
28 import jalview.api.AlignmentViewPanel;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.Annotation;
32 import jalview.datamodel.SequenceI;
33 import jalview.schemes.ColourSchemeI;
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);
91 eraseConsensus(aWidth);
92 computeConsensus(alignment);
93 updateResultAnnotation(true);
97 ap.paintAlignment(true);
99 } catch (OutOfMemoryError error)
101 calcMan.workerCannotRun(this);
102 ap.raiseOOMWarning("calculating consensus", error);
106 * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition
107 * - alignment was edited at same time as calculation was running
109 calcMan.workerComplete(this);
114 * Clear out any existing consensus annotations
117 * the width (number of columns) of the annotated alignment
119 protected void eraseConsensus(int aWidth)
121 AlignmentAnnotation consensus = getConsensusAnnotation();
122 consensus.annotations = new Annotation[aWidth];
128 protected void computeConsensus(AlignmentI alignment)
130 Hashtable[] hconsensus = new Hashtable[alignment.getWidth()];
132 SequenceI[] aseqs = getSequences();
133 AAFrequency.calculate(aseqs, 0, alignment.getWidth(), hconsensus,
136 alignViewport.setSequenceConsensusHash(hconsensus);
137 setColourSchemeConsensus(hconsensus);
143 protected SequenceI[] getSequences()
145 return alignViewport.getAlignment().getSequencesArray();
151 protected void setColourSchemeConsensus(Hashtable[] hconsensus)
153 ColourSchemeI globalColourScheme = alignViewport
154 .getGlobalColourScheme();
155 if (globalColourScheme != null)
157 globalColourScheme.setConsensus(hconsensus);
162 * Get the Consensus annotation for the alignment
166 protected AlignmentAnnotation getConsensusAnnotation()
168 return alignViewport.getAlignmentConsensusAnnotation();
172 * update the consensus annotation from the sequence profile data using
173 * current visualization settings.
176 public void updateAnnotation()
178 updateResultAnnotation(false);
181 public void updateResultAnnotation(boolean immediate)
183 AlignmentAnnotation consensus = getConsensusAnnotation();
184 Hashtable[] hconsensus = getViewportConsensus();
185 if (immediate || !calcMan.isWorking(this) && consensus != null
186 && hconsensus != null)
188 deriveConsensus(consensus, hconsensus);
193 * Convert the computed consensus data into the desired annotation for
196 * @param consensusAnnotation
197 * the annotation to be populated
198 * @param consensusData
199 * the computed consensus data
201 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
202 Hashtable[] consensusData)
204 long nseq = getSequences().length;
205 AAFrequency.completeConsensus(consensusAnnotation, consensusData, 0,
206 consensusData.length, alignViewport.isIgnoreGapsConsensus(),
207 alignViewport.isShowSequenceLogo(), nseq);
211 * Get the consensus data stored on the viewport.
215 protected Hashtable[] getViewportConsensus()
217 return alignViewport.getSequenceConsensusHash();