Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:56:54 GMT
  2. Package jalview.workers

File SecondaryStructureConsensusThread.java

 

Coverage histogram

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

Code metrics

34
87
15
1
325
218
40
0.46
5.8
15
2.67

Classes

Class Line # Actions
SecondaryStructureConsensusThread 41 87 40
0.794117679.4%
 

Contributing tests

This file is covered by 182 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 java.util.Collections;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import jalview.analysis.AAFrequency;
29    import jalview.analysis.AlignmentUtils;
30    import jalview.api.AlignViewportI;
31    import jalview.api.AlignmentViewPanel;
32    import jalview.datamodel.AlignmentAnnotation;
33    import jalview.datamodel.AlignmentI;
34    import jalview.datamodel.Annotation;
35    import jalview.datamodel.ProfilesI;
36    import jalview.datamodel.SequenceI;
37    import jalview.renderer.ResidueShaderI;
38    import jalview.util.Constants;
39    import jalview.util.MessageManager;
40   
 
41    public class SecondaryStructureConsensusThread extends AlignCalcWorker
42    {
 
43  454 toggle public SecondaryStructureConsensusThread(AlignViewportI alignViewport,
44    AlignmentViewPanel alignPanel)
45    {
46  454 super(alignViewport, alignPanel);
47    }
48   
 
49  1005 toggle @Override
50    public void run()
51    {
52  1005 if (calcMan.isPending(this))
53    {
54  6 return;
55    }
56  999 calcMan.notifyStart(this);
57    // long started = System.currentTimeMillis();
58  999 try
59    {
60  999 List<AlignmentAnnotation> ssConsensus = getSSConsensusAnnotation();
61  999 AlignmentAnnotation gap = getGapAnnotation();
62  999 if ((ssConsensus == null && gap == null) || calcMan.isPending(this))
63    {
64  52 calcMan.workerComplete(this);
65  52 return;
66    }
67  947 while (!calcMan.notifyWorking(this))
68    {
69  0 try
70    {
71  0 if (ap != null)
72    {
73  0 ap.paintAlignment(false, false);
74    }
75  0 Thread.sleep(200);
76    } catch (Exception ex)
77    {
78  0 ex.printStackTrace();
79    }
80    }
81  947 if (alignViewport.isClosed())
82    {
83  0 abortAndDestroy();
84  0 return;
85    }
86  947 AlignmentI alignment = alignViewport.getAlignment();
87   
88  947 int aWidth = -1;
89   
90  ? if (alignment == null || (aWidth = alignment.getWidth()) < 0)
91    {
92  0 calcMan.workerComplete(this);
93  0 return;
94    }
95   
96  947 setSecondaryStructureSources();
97  947 eraseSSConsensus(aWidth);
98  947 computeSSConsensus(alignment);
99  947 updateResultAnnotation(true);
100   
101  943 if (ap != null)
102    {
103  943 ap.paintAlignment(true, true);
104    }
105    } catch (OutOfMemoryError error)
106    {
107  0 calcMan.disableWorker(this);
108  0 ap.raiseOOMWarning("calculating consensus", error);
109    } finally
110    {
111    /*
112    * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition
113    * - alignment was edited at same time as calculation was running
114    */
115  999 calcMan.workerComplete(this);
116    }
117    }
118   
119    /**
120    * Clear out any existing consensus annotations
121    *
122    * @param aWidth
123    * the width (number of columns) of the annotated alignment
124    */
 
125  947 toggle protected void eraseSSConsensus(int aWidth)
126    {
127  947 List<AlignmentAnnotation> ssConsensuses = getSSConsensusAnnotation();
128  947 for (AlignmentAnnotation ssConsensus : ssConsensuses)
129    {
130  901 if (ssConsensus != null)
131    {
132  901 ssConsensus.annotations = new Annotation[aWidth];
133    }
134    }
135  947 AlignmentAnnotation gap = getGapAnnotation();
136  947 if (gap != null)
137    {
138  940 gap.annotations = new Annotation[aWidth];
139    }
140    }
141   
142    /**
143    * @param alignment
144    */
 
145  947 toggle protected void computeSSConsensus(AlignmentI alignment)
146    {
147   
148  947 SequenceI[] aseqs = getSequences();
149  947 int width = alignment.getWidth();
150  947 Map<String, ProfilesI> hSSConsensusProfileMap = new HashMap<String, ProfilesI>();
151  947 List<String> ssSources = getSecondaryStructureSources();
152  947 for (String ssSource : ssSources)
153    {
154  999 ProfilesI hSSConsensus = AAFrequency.calculateSS(aseqs, width, 0,
155    width, true, ssSource);
156  999 hSSConsensusProfileMap.put(ssSource, hSSConsensus);
157    }
158   
159  947 alignViewport.setSequenceSSConsensusHash(hSSConsensusProfileMap);
160  947 setColourSchemeConsensus(hSSConsensusProfileMap);
161    }
162   
163    /**
164    * @return
165    */
 
166  2748 toggle protected SequenceI[] getSequences()
167    {
168  2748 return alignViewport.getAlignment().getSequencesArray();
169    }
170   
171    /**
172    * @param hconsensus
173    */
 
174  947 toggle protected void setColourSchemeConsensus(
175    Map<String, ProfilesI> ssConsensusProfileMap)
176    {
177  947 ResidueShaderI cs = alignViewport.getResidueShading();
178  947 if (cs != null)
179    {
180  947 cs.setSSConsensusProfileMap(ssConsensusProfileMap);
181    }
182    }
183   
184    /**
185    * Get the Consensus annotation for the alignment
186    *
187    * @return
188    */
 
189  2893 toggle protected List<AlignmentAnnotation> getSSConsensusAnnotation()
190    {
191  2893 return alignViewport
192    .getAlignmentSecondaryStructureConsensusAnnotation();
193    }
194   
195    /**
196    * Get the Consensus annotation for the alignment
197    *
198    * @return
199    */
 
200  947 toggle protected void setSecondaryStructureSources()
201    {
202  947 List<String> sources = null;
203  947 AlignmentAnnotation[] aa = alignViewport.getAlignment()
204    .getAlignmentAnnotation();
205  947 if (aa != null)
206    {
207  947 sources = AlignmentUtils.extractSSSourceInAlignmentAnnotation(aa);
208  947 if (sources != null)
209    {
210  947 sources.add(0, Constants.SS_ALL_PROVIDERS);
211  947 alignViewport.setSecondaryStructureSources(sources);
212    }
213    }
214    }
215   
 
216  947 toggle protected List<String> getSecondaryStructureSources()
217    {
218  947 return alignViewport.getSecondaryStructureSources();
219    }
220   
221    /**
222    * Get the Gap annotation for the alignment
223    *
224    * @return
225    */
 
226  2851 toggle protected AlignmentAnnotation getGapAnnotation()
227    {
228  2851 return alignViewport.getAlignmentGapAnnotation();
229    }
230   
231    /**
232    * update the consensus annotation from the sequence profile data using
233    * current visualization settings.
234    */
 
235  0 toggle @Override
236    public void updateAnnotation()
237    {
238  0 updateResultAnnotation(false);
239    }
240   
 
241  947 toggle public void updateResultAnnotation(boolean immediate)
242    {
243  947 List<AlignmentAnnotation> ssConsensuses = getSSConsensusAnnotation();
244  947 Map<String, ProfilesI> ssConsensusProfileMap = getViewportSSConsensus();
245  947 for (AlignmentAnnotation ssConsensus : ssConsensuses)
246    {
247  905 ProfilesI ssConsensusProfile = null;
248  905 for (String source : ssConsensusProfileMap.keySet())
249    {
250  940 if (ssConsensus.description.startsWith(source))
251    {
252  905 ssConsensusProfile = ssConsensusProfileMap.get(source);
253  905 break;
254    }
255    }
256  905 if (ssConsensusProfile == null)
257    {
258  0 continue;
259    }
260  905 if (immediate || !calcMan.isWorking(this) && ssConsensus != null
261    && ssConsensusProfile != null)
262    {
263  905 if (ssConsensusProfile.get(0) != null)
264  905 ssConsensus.setNoOfSequencesIncluded(
265    ssConsensusProfile.get(0).getSeqWithSSCount());
266  905 deriveSSConsensus(ssConsensus, ssConsensusProfile);
267  905 ssConsensus.hasData=ssConsensusProfile.getCount()>0;
268   
269  905 AlignmentAnnotation gap = getGapAnnotation();
270  905 if (gap != null)
271    {
272  896 deriveGap(gap, ssConsensusProfile);
273    }
274    }
275    }
276    }
277   
278    /**
279    * Convert the computed consensus data into the desired annotation for
280    * display.
281    *
282    * @param consensusAnnotation
283    * the annotation to be populated
284    * @param hconsensus
285    * the computed consensus data
286    */
 
287  905 toggle protected void deriveSSConsensus(AlignmentAnnotation ssConsensus,
288    ProfilesI hSSConsensus)
289    {
290   
291  905 long nseq = getSequences().length;
292  905 AAFrequency.completeSSConsensus(ssConsensus, hSSConsensus,
293    hSSConsensus.getStartColumn(), hSSConsensus.getEndColumn() + 1,
294    alignViewport.isIgnoreGapsConsensus(),
295    alignViewport.isShowSequenceLogo(), nseq);
296    }
297   
298    /**
299    * Convert the computed consensus data into a gap annotation row for display.
300    *
301    * @param gapAnnotation
302    * the annotation to be populated
303    * @param hconsensus
304    * the computed consensus data
305    */
 
306  896 toggle protected void deriveGap(AlignmentAnnotation gapAnnotation,
307    ProfilesI hconsensus)
308    {
309  896 long nseq = getSequences().length;
310  896 AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
311    hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
312    nseq);
313    }
314   
315    /**
316    * Get the consensus data stored on the viewport.
317    *
318    * @return
319    */
 
320  947 toggle protected Map<String, ProfilesI> getViewportSSConsensus()
321    {
322    // TODO convert ComplementConsensusThread to use Profile
323  947 return alignViewport.getSequenceSSConsensusHash();
324    }
325    }