(JAL-961) refactored consensus and rna structure consensus workers from alignViewport(s)
[jalview.git] / src / jalview / workers / StrucConsensusThread.java
1 package jalview.workers;
2
3 import java.util.Hashtable;
4
5 import jalview.analysis.AAFrequency;
6 import jalview.analysis.StructureFrequency;
7 import jalview.api.AlignCalcWorkerI;
8 import jalview.api.AlignViewportI;
9 import jalview.api.AlignmentViewPanel;
10 import jalview.datamodel.AlignmentAnnotation;
11 import jalview.datamodel.AlignmentI;
12 import jalview.datamodel.Annotation;
13
14 public class StrucConsensusThread extends AlignCalcWorker implements AlignCalcWorkerI
15 {
16   public StrucConsensusThread(AlignViewportI alignViewport,
17           AlignmentViewPanel alignPanel)
18   {
19     super(alignViewport, alignPanel);
20   }
21   public void run()
22   {
23     try
24     {
25       calcMan.notifyStart(this);
26       while (!calcMan.notifyWorking(this))
27       {
28         try
29         {
30           if (ap != null)
31           {
32             ap.paintAlignment(false);
33           }
34
35           Thread.sleep(200);
36         } catch (Exception ex)
37         {
38           ex.printStackTrace();
39         }
40       }
41       if (alignViewport.isClosed())
42       {
43         abortAndDestroy();
44       }
45       AlignmentI alignment = alignViewport.getAlignment();
46
47       int aWidth = -1;
48
49       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
50       {
51         calcMan.workerComplete(this);
52         return;
53       }
54       AlignmentAnnotation strucConsensus=alignViewport.getAlignmentStrucConsensusAnnotation();
55       Hashtable[] hStrucConsensus=alignViewport.getRnaStructureConsensusHash();
56       strucConsensus.annotations = null;
57       strucConsensus.annotations = new Annotation[aWidth];
58
59       hStrucConsensus = new Hashtable[aWidth];
60
61       AlignmentAnnotation[] aa = alignViewport.getAlignment()
62               .getAlignmentAnnotation();
63       AlignmentAnnotation rnaStruc = null;
64       for (int i = 0; i < aa.length; i++)
65       {
66         if (aa[i].getRNAStruc() != null)
67         {
68           rnaStruc = aa[i];
69           break;
70         }
71       }
72
73       jalview.analysis.StructureFrequency.calculate(alignment.getSequencesArray(), 0,
74               alignment.getWidth(), hStrucConsensus, true, rnaStruc);
75       alignViewport.setRnaStructureConsensusHash(hStrucConsensus);
76       // TODO AlignmentAnnotation rnaStruc!!!
77       updateResultAnnotation(true);
78       if (alignViewport.getGlobalColourScheme()!= null)
79       {
80         alignViewport.getGlobalColourScheme().setConsensus(hStrucConsensus);
81       }
82
83     } catch (OutOfMemoryError error)
84     {
85       calcMan.workerCannotRun(this);
86
87       // consensus = null;
88       // hconsensus = null;
89       ap.raiseOOMWarning("calculating RNA structure consensus", error);
90     }
91
92     calcMan.workerComplete(this);
93     if (ap != null)
94     {
95       ap.paintAlignment(true);
96     }
97
98   }
99   /**
100    * update the consensus annotation from the sequence profile data using
101    * current visualization settings.
102    */
103   public void updateAnnotation()
104   {
105     updateResultAnnotation(false);
106   }
107
108   public void updateResultAnnotation(boolean immediate)
109   {
110     AlignmentAnnotation strucConsensus = alignViewport
111             .getAlignmentStrucConsensusAnnotation();
112     Hashtable[] hStrucConsensus = alignViewport.getRnaStructureConsensusHash();
113     if (immediate || !calcMan.isWorking(this) && strucConsensus!=null && hStrucConsensus!=null)
114     {
115       StructureFrequency.completeConsensus(strucConsensus,
116               hStrucConsensus, 0, hStrucConsensus.length,
117               alignViewport.getIgnoreGapsConsensus(),
118             alignViewport.isShowSequenceLogo());
119     }
120   }
121
122 }