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