JAL-1432 updated copyright notices
[jalview.git] / src / jalview / workers / StrucConsensusThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.workers;
20
21 import java.util.Hashtable;
22
23 import jalview.analysis.StructureFrequency;
24 import jalview.api.AlignCalcWorkerI;
25 import jalview.api.AlignViewportI;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30
31 public class StrucConsensusThread extends AlignCalcWorker implements
32         AlignCalcWorkerI
33 {
34   public StrucConsensusThread(AlignViewportI alignViewport,
35           AlignmentViewPanel alignPanel)
36   {
37     super(alignViewport, alignPanel);
38   }
39
40   AlignmentAnnotation strucConsensus;
41
42   Hashtable[] hStrucConsensus;
43
44   @Override
45   public void run()
46   {
47     try
48     {
49       if (calcMan.isPending(this))
50       {
51         return;
52       }
53       calcMan.notifyStart(this);
54       while (!calcMan.notifyWorking(this))
55       {
56         try
57         {
58           if (ap != null)
59           {
60             // ap.paintAlignment(false);
61           }
62
63           Thread.sleep(200);
64         } catch (Exception ex)
65         {
66           ex.printStackTrace();
67         }
68       }
69       if (alignViewport.isClosed())
70       {
71         abortAndDestroy();
72       }
73       AlignmentI alignment = alignViewport.getAlignment();
74
75       int aWidth = -1;
76
77       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
78       {
79         calcMan.workerComplete(this);
80         return;
81       }
82       strucConsensus = alignViewport.getAlignmentStrucConsensusAnnotation();
83       hStrucConsensus = alignViewport.getRnaStructureConsensusHash();
84       strucConsensus.annotations = null;
85       strucConsensus.annotations = new Annotation[aWidth];
86
87       hStrucConsensus = new Hashtable[aWidth];
88
89       AlignmentAnnotation[] aa = alignViewport.getAlignment()
90               .getAlignmentAnnotation();
91       AlignmentAnnotation rnaStruc = null;
92       // select rna struct to use for calculation
93       for (int i = 0; i < aa.length; i++)
94       {
95         if (aa[i].getRNAStruc() != null && aa[i].isValidStruc())
96         {
97           rnaStruc = aa[i];
98           break;
99         }
100       }
101       // check to see if its valid
102
103       if (rnaStruc == null || !rnaStruc.isValidStruc())
104       {
105         calcMan.workerComplete(this);
106         return;
107       }
108
109       try
110       {
111         jalview.analysis.StructureFrequency.calculate(
112                 alignment.getSequencesArray(), 0, alignment.getWidth(),
113                 hStrucConsensus, true, rnaStruc);
114       } catch (ArrayIndexOutOfBoundsException x)
115       {
116         calcMan.workerComplete(this);
117         return;
118       }
119       alignViewport.setRnaStructureConsensusHash(hStrucConsensus);
120       // TODO AlignmentAnnotation rnaStruc!!!
121       updateResultAnnotation(true);
122       if (alignViewport.getGlobalColourScheme() != null)
123       {
124         alignViewport.getGlobalColourScheme().setConsensus(hStrucConsensus);
125       }
126
127     } catch (OutOfMemoryError error)
128     {
129       calcMan.workerCannotRun(this);
130
131       // consensus = null;
132       // hconsensus = null;
133       ap.raiseOOMWarning("calculating RNA structure consensus", error);
134     } finally
135     {
136       calcMan.workerComplete(this);
137       if (ap != null)
138       {
139         ap.paintAlignment(true);
140       }
141     }
142
143   }
144
145   /**
146    * update the consensus annotation from the sequence profile data using
147    * current visualization settings.
148    */
149   @Override
150   public void updateAnnotation()
151   {
152     updateResultAnnotation(false);
153   }
154
155   public void updateResultAnnotation(boolean immediate)
156   {
157     if (immediate || !calcMan.isWorking(this) && strucConsensus != null
158             && hStrucConsensus != null)
159     {
160       StructureFrequency.completeConsensus(strucConsensus, hStrucConsensus,
161               0, hStrucConsensus.length,
162               alignViewport.getIgnoreGapsConsensus(),
163               alignViewport.isShowSequenceLogo());
164     }
165   }
166
167 }