Clover icon

Coverage Report

  1. Project Clover database Mon Sep 2 2024 17:57:51 BST
  2. Package jalview.workers

File SecondaryStructureConsensusThread.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
21% of files have more coverage

Code metrics

24
62
13
1
259
161
33
0.53
4.77
13
2.54

Classes

Class Line # Actions
SecondaryStructureConsensusThread 33 62 33
0.777777877.8%
 

Contributing tests

This file is covered by 183 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 SecondaryStructureConsensusThread extends AlignCalcWorker
34    {
 
35  459 toggle public SecondaryStructureConsensusThread(AlignViewportI alignViewport,
36    AlignmentViewPanel alignPanel)
37    {
38  459 super(alignViewport, alignPanel);
39    }
40   
 
41  1036 toggle @Override
42    public void run()
43    {
44  1036 if (calcMan.isPending(this))
45    {
46  6 return;
47    }
48  1030 calcMan.notifyStart(this);
49    // long started = System.currentTimeMillis();
50  1030 try
51    {
52  1030 AlignmentAnnotation ssConsensus = getSSConsensusAnnotation();
53  1030 AlignmentAnnotation gap = getGapAnnotation();
54  1030 if ((ssConsensus == null && gap == null) || calcMan.isPending(this))
55    {
56  60 calcMan.workerComplete(this);
57  60 return;
58    }
59  970 while (!calcMan.notifyWorking(this))
60    {
61  0 try
62    {
63  0 if (ap != null)
64    {
65  0 ap.paintAlignment(false, false);
66    }
67  0 Thread.sleep(200);
68    } catch (Exception ex)
69    {
70  0 ex.printStackTrace();
71    }
72    }
73  970 if (alignViewport.isClosed())
74    {
75  0 abortAndDestroy();
76  0 return;
77    }
78  970 AlignmentI alignment = alignViewport.getAlignment();
79   
80  970 int aWidth = -1;
81   
82  ? if (alignment == null || (aWidth = alignment.getWidth()) < 0)
83    {
84  0 calcMan.workerComplete(this);
85  0 return;
86    }
87   
88  970 eraseSSConsensus(aWidth);
89  970 computeSSConsensus(alignment);
90  970 updateResultAnnotation(true);
91   
92  969 if (ap != null)
93    {
94  969 ap.paintAlignment(true, true);
95    }
96    } catch (OutOfMemoryError error)
97    {
98  0 calcMan.disableWorker(this);
99  0 ap.raiseOOMWarning("calculating consensus", error);
100    } finally
101    {
102    /*
103    * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition
104    * - alignment was edited at same time as calculation was running
105    */
106  1030 calcMan.workerComplete(this);
107    }
108    }
109   
110    /**
111    * Clear out any existing consensus annotations
112    *
113    * @param aWidth
114    * the width (number of columns) of the annotated alignment
115    */
 
116  970 toggle protected void eraseSSConsensus(int aWidth)
117    {
118  970 AlignmentAnnotation ssConsensus = getSSConsensusAnnotation();
119  970 if (ssConsensus != null)
120    {
121  970 ssConsensus.annotations = new Annotation[aWidth];
122    }
123  970 AlignmentAnnotation gap = getGapAnnotation();
124  970 if (gap != null)
125    {
126  964 gap.annotations = new Annotation[aWidth];
127    }
128    }
129   
130    /**
131    * @param alignment
132    */
 
133  970 toggle protected void computeSSConsensus(AlignmentI alignment)
134    {
135   
136  970 SequenceI[] aseqs = getSequences();
137  970 int width = alignment.getWidth();
138  970 ProfilesI hSSConsensus = AAFrequency.calculateSS(aseqs, width, 0, width,
139    true);
140   
141  970 alignViewport.setSequenceSSConsensusHash(hSSConsensus);
142  970 setColourSchemeConsensus(hSSConsensus);
143    }
144   
145    /**
146    * @return
147    */
 
148  2903 toggle protected SequenceI[] getSequences()
149    {
150  2903 return alignViewport.getAlignment().getSequencesArray();
151    }
152   
153    /**
154    * @param hconsensus
155    */
 
156  970 toggle protected void setColourSchemeConsensus(ProfilesI hSSconsensus)
157    {
158  970 ResidueShaderI cs = alignViewport.getResidueShading();
159  970 if (cs != null)
160    {
161  969 cs.setSsConsensus(hSSconsensus);
162    }
163    }
164   
165    /**
166    * Get the Consensus annotation for the alignment
167    *
168    * @return
169    */
 
170  2970 toggle protected AlignmentAnnotation getSSConsensusAnnotation()
171    {
172  2970 return alignViewport
173    .getAlignmentSecondaryStructureConsensusAnnotation();
174    }
175   
176    /**
177    * Get the Gap annotation for the alignment
178    *
179    * @return
180    */
 
181  2969 toggle protected AlignmentAnnotation getGapAnnotation()
182    {
183  2969 return alignViewport.getAlignmentGapAnnotation();
184    }
185   
186    /**
187    * update the consensus annotation from the sequence profile data using
188    * current visualization settings.
189    */
 
190  0 toggle @Override
191    public void updateAnnotation()
192    {
193  0 updateResultAnnotation(false);
194    }
195   
 
196  970 toggle public void updateResultAnnotation(boolean immediate)
197    {
198  970 AlignmentAnnotation ssConsensus = getSSConsensusAnnotation();
199  970 ProfilesI hSSConsensus = (ProfilesI) getViewportSSConsensus();
200  970 if (immediate || !calcMan.isWorking(this) && ssConsensus != null
201    && hSSConsensus != null)
202    {
203  970 deriveSSConsensus(ssConsensus, hSSConsensus);
204  969 AlignmentAnnotation gap = getGapAnnotation();
205  969 if (gap != null)
206    {
207  963 deriveGap(gap, hSSConsensus);
208    }
209    }
210    }
211   
212    /**
213    * Convert the computed consensus data into the desired annotation for
214    * display.
215    *
216    * @param consensusAnnotation
217    * the annotation to be populated
218    * @param hconsensus
219    * the computed consensus data
220    */
 
221  970 toggle protected void deriveSSConsensus(
222    AlignmentAnnotation ssConsensusAnnotation, ProfilesI hSSConsensus)
223    {
224   
225  970 long nseq = getSequences().length;
226  969 AAFrequency.completeSSConsensus(ssConsensusAnnotation, hSSConsensus,
227    hSSConsensus.getStartColumn(), hSSConsensus.getEndColumn() + 1,
228    alignViewport.isIgnoreGapsConsensus(),
229    alignViewport.isShowSequenceLogo(), nseq);
230    }
231   
232    /**
233    * Convert the computed consensus data into a gap annotation row for display.
234    *
235    * @param gapAnnotation
236    * the annotation to be populated
237    * @param hconsensus
238    * the computed consensus data
239    */
 
240  963 toggle protected void deriveGap(AlignmentAnnotation gapAnnotation,
241    ProfilesI hconsensus)
242    {
243  963 long nseq = getSequences().length;
244  963 AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
245    hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
246    nseq);
247    }
248   
249    /**
250    * Get the consensus data stored on the viewport.
251    *
252    * @return
253    */
 
254  970 toggle protected Object getViewportSSConsensus()
255    {
256    // TODO convert ComplementConsensusThread to use Profile
257  970 return alignViewport.getSequenceSSConsensusHash();
258    }
259    }