abbfb23ff22cc49926b2ae7049d8de83c92a58d2
[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.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
15         AlignCalcWorkerI
16 {
17   public StrucConsensusThread(AlignViewportI alignViewport,
18           AlignmentViewPanel alignPanel)
19   {
20     super(alignViewport, alignPanel);
21   }
22
23   AlignmentAnnotation strucConsensus;
24
25   Hashtable[] hStrucConsensus;
26
27   @Override
28   public void run()
29   {
30     try
31     {
32       if (calcMan.isPending(this))
33       {
34         return;
35       }
36       calcMan.notifyStart(this);
37       while (!calcMan.notifyWorking(this))
38       {
39         try
40         {
41           if (ap != null)
42           {
43             // ap.paintAlignment(false);
44           }
45
46           Thread.sleep(200);
47         } catch (Exception ex)
48         {
49           ex.printStackTrace();
50         }
51       }
52       if (alignViewport.isClosed())
53       {
54         abortAndDestroy();
55       }
56       AlignmentI alignment = alignViewport.getAlignment();
57
58       int aWidth = -1;
59
60       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
61       {
62         calcMan.workerComplete(this);
63         return;
64       }
65       strucConsensus = alignViewport.getAlignmentStrucConsensusAnnotation();
66       hStrucConsensus = alignViewport.getRnaStructureConsensusHash();
67       strucConsensus.annotations = null;
68       strucConsensus.annotations = new Annotation[aWidth];
69
70       hStrucConsensus = new Hashtable[aWidth];
71
72       AlignmentAnnotation[] aa = alignViewport.getAlignment()
73               .getAlignmentAnnotation();
74       AlignmentAnnotation rnaStruc = null;
75       // select rna struct to use for calculation
76       for (int i = 0; i < aa.length; i++)
77       {
78         if (aa[i].getRNAStruc() != null && aa[i].isValidStruc())
79         {
80           rnaStruc = aa[i];
81           break;
82         }
83       }
84       // check to see if its valid
85
86       if (rnaStruc == null || !rnaStruc.isValidStruc())
87       {
88         calcMan.workerComplete(this);
89         return;
90       }
91
92       try {
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 }