formatting
[jalview.git] / src / jalview / workers / ConsensusThread.java
1 package jalview.workers;
2
3 import jalview.analysis.AAFrequency;
4 import jalview.api.AlignCalcWorkerI;
5 import jalview.api.AlignViewportI;
6 import jalview.api.AlignmentViewPanel;
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.Annotation;
10 import jalview.schemes.ColourSchemeI;
11
12 import java.util.Hashtable;
13
14 public class ConsensusThread extends AlignCalcWorker implements
15         AlignCalcWorkerI
16 {
17   public ConsensusThread(AlignViewportI alignViewport,
18           AlignmentViewPanel alignPanel)
19   {
20     super(alignViewport, alignPanel);
21   }
22
23   @Override
24   public void run()
25   {
26     if (calcMan.isPending(this))
27     {
28       return;
29     }
30     calcMan.notifyStart(this);
31     long started = System.currentTimeMillis();
32     try
33     {
34       AlignmentAnnotation consensus = alignViewport
35               .getAlignmentConsensusAnnotation();
36       if (consensus == null || calcMan.isPending(this))
37       {
38         calcMan.workerComplete(this);
39         return;
40       }
41       while (!calcMan.notifyWorking(this))
42       {
43         // System.err.println("Thread (Consensus"+Thread.currentThread().getName()+") Waiting around.");
44         try
45         {
46           if (ap != null)
47           {
48             ap.paintAlignment(false);
49           }
50           Thread.sleep(200);
51         } catch (Exception ex)
52         {
53           ex.printStackTrace();
54         }
55       }
56       if (alignViewport.isClosed())
57       {
58         abortAndDestroy();
59       }
60       AlignmentI alignment = alignViewport.getAlignment();
61
62       int aWidth = -1;
63
64       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
65       {
66         calcMan.workerComplete(this);
67         // .updatingConservation = false;
68         // AlignViewport.UPDATING_CONSERVATION = false;
69
70         return;
71       }
72       consensus = alignViewport.getAlignmentConsensusAnnotation();
73
74       consensus.annotations = null;
75       consensus.annotations = new Annotation[aWidth];
76       Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
77       hconsensus = new Hashtable[aWidth];
78       try
79       {
80         AAFrequency.calculate(alignment.getSequencesArray(), 0,
81                 alignment.getWidth(), hconsensus, true);
82       } catch (ArrayIndexOutOfBoundsException x)
83       {
84         // this happens due to a race condition -
85         // alignment was edited at same time as calculation was running
86         //
87         // calcMan.workerCannotRun(this);
88         calcMan.workerComplete(this);
89         return;
90       }
91       alignViewport.setSequenceConsensusHash(hconsensus);
92       updateResultAnnotation(true);
93       ColourSchemeI globalColourScheme = alignViewport
94               .getGlobalColourScheme();
95       if (globalColourScheme != null)
96       {
97         globalColourScheme.setConsensus(hconsensus);
98       }
99
100     } catch (OutOfMemoryError error)
101     {
102       calcMan.workerCannotRun(this);
103
104       // consensus = null;
105       // hconsensus = null;
106       ap.raiseOOMWarning("calculating consensus", error);
107     }
108
109     calcMan.workerComplete(this);
110     if (ap != null)
111     {
112       ap.paintAlignment(true);
113     }
114   }
115
116   /**
117    * update the consensus annotation from the sequence profile data using
118    * current visualization settings.
119    */
120   @Override
121   public void updateAnnotation()
122   {
123     updateResultAnnotation(false);
124   }
125
126   public void updateResultAnnotation(boolean immediate)
127   {
128     AlignmentAnnotation consensus = alignViewport
129             .getAlignmentConsensusAnnotation();
130     Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
131     if (immediate || !calcMan.isWorking(this) && consensus != null
132             && hconsensus != null)
133     {
134       AAFrequency.completeConsensus(consensus, hconsensus, 0,
135               hconsensus.length, alignViewport.getIgnoreGapsConsensus(),
136               alignViewport.isShowSequenceLogo());
137     }
138   }
139 }