e0b38334e171414bcb4de3e78431e36a1902dbfe
[jalview.git] / src / jalview / workers / StrucConsensusThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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].visible && aa[i].isRNA() && 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     } catch (OutOfMemoryError error)
130     {
131       calcMan.workerCannotRun(this);
132
133       // consensus = null;
134       // hconsensus = null;
135       ap.raiseOOMWarning("calculating RNA structure consensus", error);
136     } finally
137     {
138       calcMan.workerComplete(this);
139       if (ap != null)
140       {
141         ap.paintAlignment(true);
142       }
143     }
144
145   }
146
147   /**
148    * update the consensus annotation from the sequence profile data using
149    * current visualization settings.
150    */
151   @Override
152   public void updateAnnotation()
153   {
154     updateResultAnnotation(false);
155   }
156
157   public void updateResultAnnotation(boolean immediate)
158   {
159     if (immediate || !calcMan.isWorking(this) && strucConsensus != null
160             && hStrucConsensus != null)
161     {
162       StructureFrequency.completeConsensus(strucConsensus, hStrucConsensus,
163               0, hStrucConsensus.length,
164               alignViewport.isIgnoreGapsConsensus(),
165               alignViewport.isShowSequenceLogo(), nseq);
166     }
167   }
168
169 }