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