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