Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / src / jalview / workers / ComplementConsensusThread.java
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
29 import java.util.ConcurrentModificationException;
30 import java.util.Hashtable;
31
32 /**
33  * A thread to recompute the consensus of the cDNA complement for a linked
34  * protein alignment.
35  * 
36  * @author gmcarstairs
37  *
38  */
39 public class ComplementConsensusThread extends ConsensusThread
40 {
41
42   public ComplementConsensusThread(AlignViewportI alignViewport,
43           AlignmentViewPanel alignPanel)
44   {
45     super(alignViewport, alignPanel);
46   }
47
48   @Override
49   protected AlignmentAnnotation getConsensusAnnotation()
50   {
51     return alignViewport.getComplementConsensusAnnotation();
52   }
53
54   @Override
55   protected Hashtable<String, Object>[] getViewportConsensus()
56   {
57     return alignViewport.getComplementConsensusHash();
58   }
59
60   /**
61    * Calculate the cDNA consensus and store it on the Viewport
62    */
63   @Override
64   protected void computeConsensus(AlignmentI alignment)
65   {
66     @SuppressWarnings("unchecked")
67     Hashtable<String, Object>[] hconsensus = new Hashtable[alignment
68             .getWidth()];
69
70     // SequenceI[] aseqs = getSequences();
71
72     /*
73      * Allow 3 tries at this, since this thread can start up while we are still
74      * modifying protein-codon mappings on the alignment
75      */
76     for (int i = 0; i < 3; i++)
77     {
78       try
79       {
80         AAFrequency.calculateCdna(alignment, hconsensus);
81         break;
82       } catch (ConcurrentModificationException e)
83       {
84         // try again
85       }
86     }
87
88     alignViewport.setComplementConsensusHash(hconsensus);
89   }
90
91   /**
92    * Convert the computed consensus data into the desired annotation for
93    * display.
94    * 
95    * @param consensusAnnotation
96    *          the annotation to be populated
97    * @param consensusData
98    *          the computed consensus data
99    */
100   protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
101           Hashtable<String, Object>[] consensusData)
102   {
103     AAFrequency.completeCdnaConsensus(consensusAnnotation, consensusData,
104             alignViewport.isShowSequenceLogo(), getSequences().length);
105   }
106
107   @Override
108   public void updateResultAnnotation(boolean immediate)
109   {
110     AlignmentAnnotation consensus = getConsensusAnnotation();
111     Hashtable<String, Object>[] hconsensus = getViewportConsensus();
112     if (immediate || !calcMan.isWorking(this) && consensus != null
113             && hconsensus != null)
114     {
115       deriveConsensus(consensus, hconsensus);
116     }
117   }
118
119 }