a7f919b9661f7cc0da0688f8b545a124df566051
[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               jalview.analysis.StructureFrequency.calculate(
93               alignment.getSequencesArray(), 0, alignment.getWidth(),
94               hStrucConsensus, true, rnaStruc);
95       } catch (ArrayIndexOutOfBoundsException x)
96       {
97         calcMan.workerComplete(this);
98         return;
99       }
100       alignViewport.setRnaStructureConsensusHash(hStrucConsensus);
101       // TODO AlignmentAnnotation rnaStruc!!!
102       updateResultAnnotation(true);
103       if (alignViewport.getGlobalColourScheme() != null)
104       {
105         alignViewport.getGlobalColourScheme().setConsensus(hStrucConsensus);
106       }
107
108     } catch (OutOfMemoryError error)
109     {
110       calcMan.workerCannotRun(this);
111
112       // consensus = null;
113       // hconsensus = null;
114       ap.raiseOOMWarning("calculating RNA structure consensus", error);
115     } finally
116     {
117       calcMan.workerComplete(this);
118       if (ap != null)
119       {
120         ap.paintAlignment(true);
121       }
122     }
123
124   }
125
126   /**
127    * update the consensus annotation from the sequence profile data using
128    * current visualization settings.
129    */
130   @Override
131   public void updateAnnotation()
132   {
133     updateResultAnnotation(false);
134   }
135
136   public void updateResultAnnotation(boolean immediate)
137   {
138     if (immediate || !calcMan.isWorking(this) && strucConsensus != null
139             && hStrucConsensus != null)
140     {
141       StructureFrequency.completeConsensus(strucConsensus, hStrucConsensus,
142               0, hStrucConsensus.length,
143               alignViewport.getIgnoreGapsConsensus(),
144               alignViewport.isShowSequenceLogo());
145     }
146   }
147
148 }