3230a364ef57f5162c2d2d7cb30bca833ef896bd
[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 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 import jalview.datamodel.SequenceI;
31
32 import java.util.Hashtable;
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         return;
78       }
79       AlignmentI alignment = alignViewport.getAlignment();
80
81       int aWidth = -1;
82
83       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
84       {
85         calcMan.workerComplete(this);
86         return;
87       }
88       strucConsensus = alignViewport.getAlignmentStrucConsensusAnnotation();
89       hStrucConsensus = alignViewport.getRnaStructureConsensusHash();
90       strucConsensus.annotations = null;
91       strucConsensus.annotations = new Annotation[aWidth];
92
93       hStrucConsensus = new Hashtable[aWidth];
94
95       AlignmentAnnotation[] aa = alignViewport.getAlignment()
96               .getAlignmentAnnotation();
97       AlignmentAnnotation rnaStruc = null;
98       // select rna struct to use for calculation
99       for (int i = 0; i < aa.length; i++)
100       {
101         if (aa[i].getRNAStruc() != null && aa[i].isValidStruc())
102         {
103           rnaStruc = aa[i];
104           break;
105         }
106       }
107       // check to see if its valid
108
109       if (rnaStruc == null || !rnaStruc.isValidStruc())
110       {
111         calcMan.workerComplete(this);
112         return;
113       }
114
115       try
116       {
117         final SequenceI[] arr = alignment.getSequencesArray();
118         nseq = arr.length;
119         jalview.analysis.StructureFrequency.calculate(arr, 0,
120                 alignment.getWidth(), hStrucConsensus, true, rnaStruc);
121       } catch (ArrayIndexOutOfBoundsException x)
122       {
123         calcMan.workerComplete(this);
124         return;
125       }
126       alignViewport.setRnaStructureConsensusHash(hStrucConsensus);
127       // TODO AlignmentAnnotation rnaStruc!!!
128       updateResultAnnotation(true);
129       if (alignViewport.getGlobalColourScheme() != null)
130       {
131         alignViewport.getGlobalColourScheme().setConsensus(hStrucConsensus);
132       }
133
134     } catch (OutOfMemoryError error)
135     {
136       calcMan.workerCannotRun(this);
137
138       // consensus = null;
139       // hconsensus = null;
140       ap.raiseOOMWarning("calculating RNA structure consensus", error);
141     } finally
142     {
143       calcMan.workerComplete(this);
144       if (ap != null)
145       {
146         ap.paintAlignment(true);
147       }
148     }
149
150   }
151
152   /**
153    * update the consensus annotation from the sequence profile data using
154    * current visualization settings.
155    */
156   @Override
157   public void updateAnnotation()
158   {
159     updateResultAnnotation(false);
160   }
161
162   public void updateResultAnnotation(boolean immediate)
163   {
164     if (immediate || !calcMan.isWorking(this) && strucConsensus != null
165             && hStrucConsensus != null)
166     {
167       StructureFrequency.completeConsensus(strucConsensus, hStrucConsensus,
168               0, hStrucConsensus.length,
169               alignViewport.getIgnoreGapsConsensus(),
170               alignViewport.isShowSequenceLogo(), nseq);
171     }
172   }
173
174 }