Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 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 214 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  951 toggle @Override
42    public void run()
43    {
44  951 AlignmentAnnotation consensus = getConsensusAnnotation();
45  951 AlignmentAnnotation gap = getGapAnnotation();
46  951 if ((consensus == null && gap == null))
47    {
48  0 return;
49    }
50  951 if (alignViewport.isClosed())
51    {
52  0 abortAndDestroy();
53  0 return;
54    }
55  951 AlignmentI alignment = alignViewport.getAlignment();
56   
57  951 int aWidth = -1;
58   
59  ? if (alignment == null || (aWidth = alignment.getWidth()) < 0)
60    {
61  0 return;
62    }
63   
64  951 eraseConsensus(aWidth);
65  951 computeConsensus(alignment);
66  951 updateResultAnnotation(true);
67   
68  948 if (ap != null)
69    {
70  948 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  951 toggle protected void eraseConsensus(int aWidth)
81    {
82  951 AlignmentAnnotation consensus = getConsensusAnnotation();
83  951 if (consensus != null)
84    {
85  951 consensus.annotations = new Annotation[aWidth];
86    }
87  951 AlignmentAnnotation gap = getGapAnnotation();
88  951 if (gap != null)
89    {
90  944 gap.annotations = new Annotation[aWidth];
91    }
92    }
93   
94    /**
95    * @param alignment
96    */
 
97  946 toggle protected void computeConsensus(AlignmentI alignment)
98    {
99   
100  946 SequenceI[] aseqs = getSequences();
101  946 int width = alignment.getWidth();
102  946 ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0, width,
103    true);
104   
105  946 alignViewport.setSequenceConsensusHash(hconsensus);
106  946 setColourSchemeConsensus(hconsensus);
107    }
108   
109    /**
110    * @return
111    */
 
112  2837 toggle protected SequenceI[] getSequences()
113    {
114  2837 return alignViewport.getAlignment().getSequencesArray();
115    }
116   
117    /**
118    * @param hconsensus
119    */
 
120  946 toggle protected void setColourSchemeConsensus(ProfilesI hconsensus)
121    {
122  946 ResidueShaderI cs = alignViewport.getResidueShading();
123  946 if (cs != null)
124    {
125  943 cs.setConsensus(hconsensus);
126    }
127    }
128   
129    /**
130    * Get the Consensus annotation for the alignment
131    *
132    * @return
133    */
 
134  2858 toggle protected AlignmentAnnotation getConsensusAnnotation()
135    {
136  2858 return alignViewport.getAlignmentConsensusAnnotation();
137    }
138   
139    /**
140    * Get the Gap annotation for the alignment
141    *
142    * @return
143    */
 
144  2847 toggle protected AlignmentAnnotation getGapAnnotation()
145    {
146  2847 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  966 toggle public void updateResultAnnotation(boolean immediate)
160    {
161  966 AlignmentAnnotation consensus = getConsensusAnnotation();
162  966 ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
163  966 if (immediate || !calcMan.isWorking(this) && consensus != null
164    && hconsensus != null)
165    {
166  948 deriveConsensus(consensus, hconsensus);
167  945 AlignmentAnnotation gap = getGapAnnotation();
168  945 if (gap != null)
169    {
170  937 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  948 toggle protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
185    ProfilesI hconsensus)
186    {
187  948 long nseq = getSequences().length;
188  945 consensusAnnotation.setNoOfSequencesIncluded(nseq);
189  945 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  938 toggle protected void deriveGap(AlignmentAnnotation gapAnnotation,
207    ProfilesI hconsensus)
208    {
209  938 long nseq = getSequences().length;
210  938 gapAnnotation.setNoOfSequencesIncluded(nseq);
211  938 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  966 toggle protected Object getViewportConsensus()
222    {
223    // TODO convert ComplementConsensusThread to use Profile
224  966 return alignViewport.getSequenceConsensusHash();
225    }
226    }