86c33365271338885f94f896d225d86facdd9652
[jalview.git] / src / jalview / workers / StrucConsensusThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.workers;
19
20 import java.util.Hashtable;
21
22 import jalview.analysis.StructureFrequency;
23 import jalview.api.AlignCalcWorkerI;
24 import jalview.api.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.Annotation;
29
30 public class StrucConsensusThread extends AlignCalcWorker implements
31         AlignCalcWorkerI
32 {
33   public StrucConsensusThread(AlignViewportI alignViewport,
34           AlignmentViewPanel alignPanel)
35   {
36     super(alignViewport, alignPanel);
37   }
38
39   AlignmentAnnotation strucConsensus;
40
41   Hashtable[] hStrucConsensus;
42
43   @Override
44   public void run()
45   {
46     try
47     {
48       if (calcMan.isPending(this))
49       {
50         return;
51       }
52       calcMan.notifyStart(this);
53       while (!calcMan.notifyWorking(this))
54       {
55         try
56         {
57           if (ap != null)
58           {
59             // ap.paintAlignment(false);
60           }
61
62           Thread.sleep(200);
63         } catch (Exception ex)
64         {
65           ex.printStackTrace();
66         }
67       }
68       if (alignViewport.isClosed())
69       {
70         abortAndDestroy();
71       }
72       AlignmentI alignment = alignViewport.getAlignment();
73
74       int aWidth = -1;
75
76       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
77       {
78         calcMan.workerComplete(this);
79         return;
80       }
81       strucConsensus = alignViewport.getAlignmentStrucConsensusAnnotation();
82       hStrucConsensus = alignViewport.getRnaStructureConsensusHash();
83       strucConsensus.annotations = null;
84       strucConsensus.annotations = new Annotation[aWidth];
85
86       hStrucConsensus = new Hashtable[aWidth];
87
88       AlignmentAnnotation[] aa = alignViewport.getAlignment()
89               .getAlignmentAnnotation();
90       AlignmentAnnotation rnaStruc = null;
91       // select rna struct to use for calculation
92       for (int i = 0; i < aa.length; i++)
93       {
94         if (aa[i].getRNAStruc() != null && aa[i].isValidStruc())
95         {
96           rnaStruc = aa[i];
97           break;
98         }
99       }
100       // check to see if its valid
101
102       if (rnaStruc == null || !rnaStruc.isValidStruc())
103       {
104         calcMan.workerComplete(this);
105         return;
106       }
107
108       try
109       {
110         jalview.analysis.StructureFrequency.calculate(
111                 alignment.getSequencesArray(), 0, alignment.getWidth(),
112                 hStrucConsensus, true, rnaStruc);
113       } catch (ArrayIndexOutOfBoundsException x)
114       {
115         calcMan.workerComplete(this);
116         return;
117       }
118       alignViewport.setRnaStructureConsensusHash(hStrucConsensus);
119       // TODO AlignmentAnnotation rnaStruc!!!
120       updateResultAnnotation(true);
121       if (alignViewport.getGlobalColourScheme() != null)
122       {
123         alignViewport.getGlobalColourScheme().setConsensus(hStrucConsensus);
124       }
125
126     } catch (OutOfMemoryError error)
127     {
128       calcMan.workerCannotRun(this);
129
130       // consensus = null;
131       // hconsensus = null;
132       ap.raiseOOMWarning("calculating RNA structure consensus", error);
133     } finally
134     {
135       calcMan.workerComplete(this);
136       if (ap != null)
137       {
138         ap.paintAlignment(true);
139       }
140     }
141
142   }
143
144   /**
145    * update the consensus annotation from the sequence profile data using
146    * current visualization settings.
147    */
148   @Override
149   public void updateAnnotation()
150   {
151     updateResultAnnotation(false);
152   }
153
154   public void updateResultAnnotation(boolean immediate)
155   {
156     if (immediate || !calcMan.isWorking(this) && strucConsensus != null
157             && hStrucConsensus != null)
158     {
159       StructureFrequency.completeConsensus(strucConsensus, hStrucConsensus,
160               0, hStrucConsensus.length,
161               alignViewport.getIgnoreGapsConsensus(),
162               alignViewport.isShowSequenceLogo());
163     }
164   }
165
166 }