Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.workers

File ConsensusThread.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
13% of files have more coverage

Code metrics

18
49
13
1
226
131
27
0.55
3.77
13
2.08

Classes

Class Line # Actions
ConsensusThread 33 49 27
0.87587.5%
 

Contributing tests

This file is covered by 205 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.workers;
22   
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;
32   
 
33    public class ConsensusThread extends AlignCalcWorker
34    {
 
35  486 toggle public ConsensusThread(AlignViewportI alignViewport,
36    AlignmentViewPanel alignPanel)
37    {
38  486 super(alignViewport, alignPanel);
39    }
40   
 
41  925 toggle @Override
42    public void run()
43    {
44  925 AlignmentAnnotation consensus = getConsensusAnnotation();
45  925 AlignmentAnnotation gap = getGapAnnotation();
46  925 if ((consensus == null && gap == null))
47    {
48  0 return;
49    }
50  925 if (alignViewport.isClosed())
51    {
52  0 abortAndDestroy();
53  0 return;
54    }
55  925 AlignmentI alignment = alignViewport.getAlignment();
56   
57  925 int aWidth = -1;
58   
59  ? if (alignment == null || (aWidth = alignment.getWidth()) < 0)
60    {
61  0 return;
62    }
63   
64  925 eraseConsensus(aWidth);
65  925 computeConsensus(alignment);
66  925 updateResultAnnotation(true);
67   
68  924 if (ap != null)
69    {
70  924 ap.paintAlignment(true, true);
71    }
72    }
73   
74    /**
75    * Clear out any existing consensus annotations
76    *
77    * @param aWidth
78    * the width (number of columns) of the annotated alignment
79    */
 
80  925 toggle protected void eraseConsensus(int aWidth)
81    {
82  925 AlignmentAnnotation consensus = getConsensusAnnotation();
83  925 if (consensus != null)
84    {
85  925 consensus.annotations = new Annotation[aWidth];
86    }
87  925 AlignmentAnnotation gap = getGapAnnotation();
88  925 if (gap != null)
89    {
90  919 gap.annotations = new Annotation[aWidth];
91    }
92    }
93   
94    /**
95    * @param alignment
96    */
 
97  922 toggle protected void computeConsensus(AlignmentI alignment)
98    {
99   
100  922 SequenceI[] aseqs = getSequences();
101  922 int width = alignment.getWidth();
102  922 ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0, width,
103    true);
104   
105  922 alignViewport.setSequenceConsensusHash(hconsensus);
106  922 setColourSchemeConsensus(hconsensus);
107    }
108   
109    /**
110    * @return
111    */
 
112  2765 toggle protected SequenceI[] getSequences()
113    {
114  2765 return alignViewport.getAlignment().getSequencesArray();
115    }
116   
117    /**
118    * @param hconsensus
119    */
 
120  922 toggle protected void setColourSchemeConsensus(ProfilesI hconsensus)
121    {
122  922 ResidueShaderI cs = alignViewport.getResidueShading();
123  922 if (cs != null)
124    {
125  921 cs.setConsensus(hconsensus);
126    }
127    }
128   
129    /**
130    * Get the Consensus annotation for the alignment
131    *
132    * @return
133    */
 
134  2786 toggle protected AlignmentAnnotation getConsensusAnnotation()
135    {
136  2786 return alignViewport.getAlignmentConsensusAnnotation();
137    }
138   
139    /**
140    * Get the Gap annotation for the alignment
141    *
142    * @return
143    */
 
144  2773 toggle protected AlignmentAnnotation getGapAnnotation()
145    {
146  2773 return alignViewport.getAlignmentGapAnnotation();
147    }
148   
149    /**
150    * update the consensus annotation from the sequence profile data using
151    * current visualization settings.
152    */
 
153  20 toggle @Override
154    public void updateAnnotation()
155    {
156  20 updateResultAnnotation(false);
157    }
158   
 
159  942 toggle public void updateResultAnnotation(boolean immediate)
160    {
161  942 AlignmentAnnotation consensus = getConsensusAnnotation();
162  942 ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
163  942 if (immediate || !calcMan.isWorking(this) && consensus != null
164    && hconsensus != null)
165    {
166  924 deriveConsensus(consensus, hconsensus);
167  923 AlignmentAnnotation gap = getGapAnnotation();
168  923 if (gap != null)
169    {
170  916 deriveGap(gap, hconsensus);
171    }
172    }
173    }
174   
175    /**
176    * Convert the computed consensus data into the desired annotation for
177    * display.
178    *
179    * @param consensusAnnotation
180    * the annotation to be populated
181    * @param hconsensus
182    * the computed consensus data
183    */
 
184  924 toggle protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
185    ProfilesI hconsensus)
186    {
187  924 long nseq = getSequences().length;
188  923 consensusAnnotation.setNoOfSequencesIncluded(nseq);
189  923 AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
190    hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
191    alignViewport.isIgnoreGapsConsensus(),
192    alignViewport.isShowSequenceLogo(), nseq);
193    // TODO: JAL-4425 - from JAL-4392 - uncommenting this line will mean consensus annotation is hidden if only one sequence is shown.
194    // consensusAnnotation.hasData=hconsensus.getCount()>1;
195   
196    }
197   
198    /**
199    * Convert the computed consensus data into a gap annotation row for display.
200    *
201    * @param gapAnnotation
202    * the annotation to be populated
203    * @param hconsensus
204    * the computed consensus data
205    */
 
206  916 toggle protected void deriveGap(AlignmentAnnotation gapAnnotation,
207    ProfilesI hconsensus)
208    {
209  916 long nseq = getSequences().length;
210  916 gapAnnotation.setNoOfSequencesIncluded(nseq);
211  916 AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
212    hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
213    nseq);
214    }
215   
216    /**
217    * Get the consensus data stored on the viewport.
218    *
219    * @return
220    */
 
221  942 toggle protected Object getViewportConsensus()
222    {
223    // TODO convert ComplementConsensusThread to use Profile
224  942 return alignViewport.getSequenceConsensusHash();
225    }
226    }