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