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