Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.workers

File ConsensusThread.java

 

Coverage histogram

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

Code metrics

22
62
13
1
257
158
32
0.52
4.77
13
2.46

Classes

Class Line # Actions
ConsensusThread 33 62 32 21
0.7835051478.4%
 

Contributing tests

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