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 AlignCalcWorkerI
15 {
16   public ConsensusThread(AlignViewportI alignViewport,
17           AlignmentViewPanel alignPanel)
18   {
19     super(alignViewport, alignPanel);
20   }
21
22   public void run()
23   {
24     try
25     {
26       AlignmentAnnotation consensus = alignViewport.getAlignmentConsensusAnnotation();
27       if (consensus==null) { return; 
28       }
29       calcMan.notifyStart(this); 
30       while (!calcMan.notifyWorking(this)) 
31       {
32         try
33         {
34           if (ap != null)
35           {
36             ap.paintAlignment(false);
37           }
38
39           Thread.sleep(200);
40         } catch (Exception ex)
41         {
42           ex.printStackTrace();
43         }
44       }
45       calcMan.notifyWorking(this);
46       if (alignViewport.isClosed())
47       {
48         abortAndDestroy();
49       }
50       AlignmentI alignment = alignViewport.getAlignment();
51
52       int aWidth = -1;
53
54       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
55       {
56         calcMan.workerComplete(this);
57         // .updatingConservation = false;
58         // AlignViewport.UPDATING_CONSERVATION = false;
59
60         return;
61       }
62       consensus = alignViewport
63               .getAlignmentConsensusAnnotation();
64
65       consensus.annotations = null;
66       consensus.annotations = new Annotation[aWidth];
67       Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
68       hconsensus = new Hashtable[aWidth];
69       AAFrequency.calculate(alignment.getSequencesArray(), 0,
70               alignment.getWidth(), hconsensus, true);
71       alignViewport.setSequenceConsensusHash(hconsensus);
72       updateResultAnnotation(true);
73       ColourSchemeI globalColourScheme = alignViewport
74               .getGlobalColourScheme();
75       if (globalColourScheme != null)
76       {
77         globalColourScheme.setConsensus(hconsensus);
78       }
79
80     } catch (OutOfMemoryError error)
81     {
82       calcMan.workerCannotRun(this);
83
84       // consensus = null;
85       // hconsensus = null;
86       ap.raiseOOMWarning("calculating consensus", error);
87     }
88
89     calcMan.workerComplete(this);
90     if (ap != null)
91     {
92       ap.paintAlignment(true);
93     }
94   }
95
96   /**
97    * update the consensus annotation from the sequence profile data using
98    * current visualization settings.
99    */
100   public void updateAnnotation()
101   {
102     updateResultAnnotation(false);
103   }
104
105   public void updateResultAnnotation(boolean immediate)
106   {
107     AlignmentAnnotation consensus = alignViewport
108             .getAlignmentConsensusAnnotation();
109     Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
110     if (immediate || !calcMan.isWorking(this) && consensus!=null && hconsensus!=null)
111     {
112       AAFrequency.completeConsensus(consensus, hconsensus, 0,
113             hconsensus.length, alignViewport.getIgnoreGapsConsensus(),
114             alignViewport.isShowSequenceLogo());
115     }
116   }
117 }