formatting
[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   AlignmentAnnotation strucConsensus;
22   Hashtable[] hStrucConsensus;
23
24   public void run()
25   {
26     try
27     {
28       calcMan.notifyStart(this);
29       while (!calcMan.notifyWorking(this))
30       {
31         try
32         {
33           if (ap != null)
34           {
35             ap.paintAlignment(false);
36           }
37
38           Thread.sleep(200);
39         } catch (Exception ex)
40         {
41           ex.printStackTrace();
42         }
43       }
44       if (alignViewport.isClosed())
45       {
46         abortAndDestroy();
47       }
48       AlignmentI alignment = alignViewport.getAlignment();
49
50       int aWidth = -1;
51
52       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
53       {
54         calcMan.workerComplete(this);
55         return;
56       }
57       strucConsensus=alignViewport.getAlignmentStrucConsensusAnnotation();
58       hStrucConsensus=alignViewport.getRnaStructureConsensusHash();
59       strucConsensus.annotations = null;
60       strucConsensus.annotations = new Annotation[aWidth];
61
62       hStrucConsensus = new Hashtable[aWidth];
63
64       AlignmentAnnotation[] aa = alignViewport.getAlignment()
65               .getAlignmentAnnotation();
66       AlignmentAnnotation rnaStruc = null;
67       // select rna struct to use for calculation
68       for (int i = 0; i < aa.length; i++)
69       {
70         if (aa[i].getRNAStruc() != null && aa[i].isValidStruc())
71         {
72           rnaStruc = aa[i];
73           break;
74         }
75       }
76       // check to see if its valid
77       
78       if (rnaStruc==null || !rnaStruc.isValidStruc())
79       {
80         calcMan.workerComplete(this);
81         return;
82       }
83       
84       jalview.analysis.StructureFrequency.calculate(alignment.getSequencesArray(), 0,
85               alignment.getWidth(), hStrucConsensus, true, rnaStruc);
86       alignViewport.setRnaStructureConsensusHash(hStrucConsensus);
87       // TODO AlignmentAnnotation rnaStruc!!!
88       updateResultAnnotation(true);
89       if (alignViewport.getGlobalColourScheme()!= null)
90       {
91         alignViewport.getGlobalColourScheme().setConsensus(hStrucConsensus);
92       }
93
94     } catch (OutOfMemoryError error)
95     {
96       calcMan.workerCannotRun(this);
97
98       // consensus = null;
99       // hconsensus = null;
100       ap.raiseOOMWarning("calculating RNA structure consensus", error);
101     }
102
103     calcMan.workerComplete(this);
104     if (ap != null)
105     {
106       ap.paintAlignment(true);
107     }
108
109   }
110   /**
111    * update the consensus annotation from the sequence profile data using
112    * current visualization settings.
113    */
114   public void updateAnnotation()
115   {
116     updateResultAnnotation(false);
117   }
118
119   public void updateResultAnnotation(boolean immediate)
120   {
121     if (immediate || !calcMan.isWorking(this) && strucConsensus!=null && hStrucConsensus!=null)
122     {
123       StructureFrequency.completeConsensus(strucConsensus,
124               hStrucConsensus, 0, hStrucConsensus.length,
125               alignViewport.getIgnoreGapsConsensus(),
126             alignViewport.isShowSequenceLogo());
127     }
128   }
129
130 }