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.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.ProfilesI;
30 import jalview.datamodel.SequenceI;
31 import jalview.renderer.ResidueShaderI;
33 public class ConsensusThread extends AlignCalcWorker
35 public ConsensusThread(AlignViewportI alignViewport,
36 AlignmentViewPanel alignPanel)
38 super(alignViewport, alignPanel);
42 public String getCalcName()
50 AlignmentAnnotation consensus = getConsensusAnnotation();
51 AlignmentAnnotation gap = getGapAnnotation();
52 if ((consensus == null && gap == null))
56 if (alignViewport.isClosed())
61 AlignmentI alignment = alignViewport.getAlignment();
65 if (alignment == null || (aWidth = alignment.getWidth()) < 0)
70 eraseConsensus(aWidth);
71 computeConsensus(alignment);
72 updateResultAnnotation(true);
76 ap.paintAlignment(true, true);
81 * Clear out any existing consensus annotations
84 * the width (number of columns) of the annotated alignment
86 protected void eraseConsensus(int aWidth)
88 AlignmentAnnotation consensus = getConsensusAnnotation();
89 if (consensus != null)
91 consensus.annotations = new Annotation[aWidth];
93 AlignmentAnnotation gap = getGapAnnotation();
96 gap.annotations = new Annotation[aWidth];
103 protected void computeConsensus(AlignmentI alignment)
106 SequenceI[] aseqs = getSequences();
107 int width = alignment.getWidth();
108 ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0, width,
111 alignViewport.setSequenceConsensusHash(hconsensus);
112 setColourSchemeConsensus(hconsensus);
118 protected SequenceI[] getSequences()
120 return alignViewport.getAlignment().getSequencesArray();
126 protected void setColourSchemeConsensus(ProfilesI hconsensus)
128 ResidueShaderI cs = alignViewport.getResidueShading();
131 cs.setConsensus(hconsensus);
136 * Get the Consensus annotation for the alignment
140 protected AlignmentAnnotation getConsensusAnnotation()
142 return alignViewport.getAlignmentConsensusAnnotation();
146 * Get the Gap annotation for the alignment
150 protected AlignmentAnnotation getGapAnnotation()
152 return alignViewport.getAlignmentGapAnnotation();
156 * update the consensus annotation from the sequence profile data using
157 * current visualization settings.
160 public void updateAnnotation()
162 updateResultAnnotation(false);
165 public void updateResultAnnotation(boolean immediate)
167 AlignmentAnnotation consensus = getConsensusAnnotation();
168 ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
169 if (immediate || !calcMan.isWorking(this) && consensus != null
170 && hconsensus != null)
172 deriveConsensus(consensus, hconsensus);
173 AlignmentAnnotation gap = getGapAnnotation();
176 deriveGap(gap, hconsensus);
182 * Convert the computed consensus data into the desired annotation for
185 * @param consensusAnnotation
186 * the annotation to be populated
188 * the computed consensus data
190 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
191 ProfilesI hconsensus)
193 long nseq = getSequences().length;
194 AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
195 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
196 alignViewport.isIgnoreGapsConsensus(),
197 alignViewport.isShowSequenceLogo(), nseq);
201 * Convert the computed consensus data into a gap annotation row for display.
203 * @param gapAnnotation
204 * the annotation to be populated
206 * the computed consensus data
208 protected void deriveGap(AlignmentAnnotation gapAnnotation,
209 ProfilesI hconsensus)
211 long nseq = getSequences().length;
212 AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
213 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
218 * Get the consensus data stored on the viewport.
222 protected Object getViewportConsensus()
224 // TODO convert ComplementConsensusThread to use Profile
225 return alignViewport.getSequenceConsensusHash();