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);
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, true);
135 alignViewport.setSequenceConsensusHash(hconsensus);
136 setColourSchemeConsensus(hconsensus);
142 protected SequenceI[] getSequences()
144 return alignViewport.getAlignment().getSequencesArray();
150 protected void setColourSchemeConsensus(Hashtable[] hconsensus)
152 ColourSchemeI globalColourScheme = alignViewport
153 .getGlobalColourScheme();
154 if (globalColourScheme != null)
156 globalColourScheme.setConsensus(hconsensus);
161 * Get the Consensus annotation for the alignment
165 protected AlignmentAnnotation getConsensusAnnotation()
167 return alignViewport.getAlignmentConsensusAnnotation();
171 * update the consensus annotation from the sequence profile data using
172 * current visualization settings.
175 public void updateAnnotation()
177 updateResultAnnotation(false);
180 public void updateResultAnnotation(boolean immediate)
182 AlignmentAnnotation consensus = getConsensusAnnotation();
183 Hashtable[] hconsensus = getViewportConsensus();
184 if (immediate || !calcMan.isWorking(this) && consensus != null
185 && hconsensus != null)
187 deriveConsensus(consensus, hconsensus);
192 * Convert the computed consensus data into the desired annotation for
195 * @param consensusAnnotation
196 * the annotation to be populated
197 * @param consensusData
198 * the computed consensus data
200 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
201 Hashtable[] consensusData)
203 long nseq = getSequences().length;
204 AAFrequency.completeConsensus(consensusAnnotation, consensusData, 0,
205 consensusData.length, alignViewport.isIgnoreGapsConsensus(),
206 alignViewport.isShowSequenceLogo(), nseq);
210 * Get the consensus data stored on the viewport.
214 protected Hashtable[] getViewportConsensus()
216 return alignViewport.getSequenceConsensusHash();