JAL-3878 Add getCalcName to AlignCalcWorkerI.
[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   public String getCalcName()
50   {
51     return "Complement Consensus";
52   }
53
54   @Override
55   protected AlignmentAnnotation getConsensusAnnotation()
56   {
57     return alignViewport.getComplementConsensusAnnotation();
58   }
59
60   @Override
61   protected Hashtable<String, Object>[] getViewportConsensus()
62   {
63     return alignViewport.getComplementConsensusHash();
64   }
65
66   /**
67    * Calculate the cDNA consensus and store it on the Viewport
68    */
69   @Override
70   protected void computeConsensus(AlignmentI alignment)
71   {
72     @SuppressWarnings("unchecked")
73     Hashtable<String, Object>[] hconsensus = new Hashtable[alignment
74             .getWidth()];
75
76     // SequenceI[] aseqs = getSequences();
77
78     /*
79      * Allow 3 tries at this, since this thread can start up while we are still
80      * modifying protein-codon mappings on the alignment
81      */
82     for (int i = 0; i < 3; i++)
83     {
84       try
85       {
86         AAFrequency.calculateCdna(alignment, hconsensus);
87         break;
88       } catch (ConcurrentModificationException e)
89       {
90         // try again
91       }
92     }
93
94     alignViewport.setComplementConsensusHash(hconsensus);
95   }
96
97   /**
98    * Convert the computed consensus data into the desired annotation for
99    * display.
100    * 
101    * @param consensusAnnotation
102    *          the annotation to be populated
103    * @param consensusData
104    *          the computed consensus data
105    */
106   protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
107           Hashtable<String, Object>[] consensusData)
108   {
109     AAFrequency.completeCdnaConsensus(consensusAnnotation, consensusData,
110             alignViewport.isShowSequenceLogo(), getSequences().length);
111   }
112
113   @Override
114   public void updateResultAnnotation(boolean immediate)
115   {
116     AlignmentAnnotation consensus = getConsensusAnnotation();
117     Hashtable<String, Object>[] hconsensus = getViewportConsensus();
118     if (immediate || !calcMan.isWorking(this) && consensus != null
119             && hconsensus != null)
120     {
121       deriveConsensus(consensus, hconsensus);
122     }
123   }
124
125 }