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