JAL-1681 work around ConcurrentModificationException on startup
[jalview.git] / src / jalview / workers / ComplementConsensusThread.java
1 package jalview.workers;
2
3 import java.util.ConcurrentModificationException;
4 import java.util.Hashtable;
5
6 import jalview.analysis.AAFrequency;
7 import jalview.api.AlignViewportI;
8 import jalview.api.AlignmentViewPanel;
9 import jalview.datamodel.AlignmentAnnotation;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.SequenceI;
12
13 /**
14  * A thread to recompute the consensus of the cDNA complement for a linked
15  * protein alignment.
16  * 
17  * @author gmcarstairs
18  *
19  */
20 public class ComplementConsensusThread extends ConsensusThread
21 {
22
23   public ComplementConsensusThread(AlignViewportI alignViewport,
24           AlignmentViewPanel alignPanel)
25   {
26     super(alignViewport, alignPanel);
27   }
28
29   @Override
30   protected AlignmentAnnotation getConsensusAnnotation()
31   {
32     return alignViewport.getComplementConsensusAnnotation();
33   }
34
35   @Override
36   protected Hashtable[] getViewportConsensus()
37   {
38     return alignViewport.getComplementConsensusHash();
39   }
40
41   /**
42    * Calculate the cDNA consensus and store it on the Viewport
43    */
44   @Override
45   protected void computeConsensus(AlignmentI alignment)
46   {
47     Hashtable[] hconsensus = new Hashtable[alignment.getWidth()];
48
49     SequenceI[] aseqs = getSequences();
50
51     /*
52      * Allow 3 tries at this, since this thread can start up while we are still
53      * modifying protein-codon mappings on the alignment
54      */
55     for (int i = 0; i < 3; i++)
56     {
57       try
58       {
59         AAFrequency.calculateCdna(alignment, hconsensus);
60         break;
61       } catch (ConcurrentModificationException e)
62       {
63         // try again
64       }
65     }
66
67     alignViewport.setComplementConsensusHash(hconsensus);
68   }
69
70   /**
71    * Convert the computed consensus data into the desired annotation for
72    * display.
73    * 
74    * @param consensusAnnotation
75    *          the annotation to be populated
76    * @param consensusData
77    *          the computed consensus data
78    */
79   @Override
80   protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
81           Hashtable[] consensusData)
82   {
83     AAFrequency.completeCdnaConsensus(consensusAnnotation, consensusData,
84             alignViewport.isShowSequenceLogo(), getSequences().length);
85   }
86
87 }