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);
44 AlignmentAnnotation consensus = getConsensusAnnotation();
45 AlignmentAnnotation gap = getGapAnnotation();
46 if ((consensus == null && gap == null))
50 if (alignViewport.isClosed())
55 AlignmentI alignment = alignViewport.getAlignment();
59 if (alignment == null || (aWidth = alignment.getWidth()) < 0)
64 eraseConsensus(aWidth);
65 computeConsensus(alignment);
66 updateResultAnnotation(true);
70 ap.paintAlignment(true, true);
75 * Clear out any existing consensus annotations
78 * the width (number of columns) of the annotated alignment
80 protected void eraseConsensus(int aWidth)
82 AlignmentAnnotation consensus = getConsensusAnnotation();
83 if (consensus != null)
85 consensus.annotations = new Annotation[aWidth];
87 AlignmentAnnotation gap = getGapAnnotation();
90 gap.annotations = new Annotation[aWidth];
97 protected void computeConsensus(AlignmentI alignment)
100 SequenceI[] aseqs = getSequences();
101 int width = alignment.getWidth();
102 ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0, width,
105 alignViewport.setSequenceConsensusHash(hconsensus);
106 setColourSchemeConsensus(hconsensus);
112 protected SequenceI[] getSequences()
114 return alignViewport.getAlignment().getSequencesArray();
120 protected void setColourSchemeConsensus(ProfilesI hconsensus)
122 ResidueShaderI cs = alignViewport.getResidueShading();
125 cs.setConsensus(hconsensus);
130 * Get the Consensus annotation for the alignment
134 protected AlignmentAnnotation getConsensusAnnotation()
136 return alignViewport.getAlignmentConsensusAnnotation();
140 * Get the Gap annotation for the alignment
144 protected AlignmentAnnotation getGapAnnotation()
146 return alignViewport.getAlignmentGapAnnotation();
150 * update the consensus annotation from the sequence profile data using
151 * current visualization settings.
154 public void updateAnnotation()
156 updateResultAnnotation(false);
159 public void updateResultAnnotation(boolean immediate)
161 AlignmentAnnotation consensus = getConsensusAnnotation();
162 ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
163 if (immediate || !calcMan.isWorking(this) && consensus != null
164 && hconsensus != null)
166 deriveConsensus(consensus, hconsensus);
167 AlignmentAnnotation gap = getGapAnnotation();
170 deriveGap(gap, hconsensus);
176 * Convert the computed consensus data into the desired annotation for
179 * @param consensusAnnotation
180 * the annotation to be populated
182 * the computed consensus data
184 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
185 ProfilesI hconsensus)
187 long nseq = getSequences().length;
188 AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
189 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
190 alignViewport.isIgnoreGapsConsensus(),
191 alignViewport.isShowSequenceLogo(), nseq);
195 * Convert the computed consensus data into a gap annotation row for display.
197 * @param gapAnnotation
198 * the annotation to be populated
200 * the computed consensus data
202 protected void deriveGap(AlignmentAnnotation gapAnnotation,
203 ProfilesI hconsensus)
205 long nseq = getSequences().length;
206 AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
207 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
212 * Get the consensus data stored on the viewport.
216 protected Object getViewportConsensus()
218 // TODO convert ComplementConsensusThread to use Profile
219 return alignViewport.getSequenceConsensusHash();