JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.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
38   private AlignmentAnnotation strucConsensus;
39
40   private Hashtable[] hStrucConsensus;
41
42   private long nseq = -1;
43
44
45   @Override
46         public AlignCalcWorkerI getNewWorker() {
47                 return new StrucConsensusThread(alignViewport, ap);
48         }
49
50         public StrucConsensusThread(AlignViewportI alignViewport,
51           AlignmentViewPanel alignPanel)
52   {
53     super(alignViewport, alignPanel);
54   }
55
56   @Override
57   public void run()
58   {
59     try
60     {
61       if (calcMan.isPending(this))
62       {
63         return;
64       }
65       calcMan.notifyStart(this);
66       while (!calcMan.notifyWorking(this))
67       {
68         try
69         {
70           if (ap != null)
71           {
72             // ap.paintAlignment(false);
73           }
74
75           Thread.sleep(200);
76         } catch (Exception ex)
77         {
78           ex.printStackTrace();
79         }
80       }
81       if (alignViewport.isClosed())
82       {
83         abortAndDestroy();
84         return;
85       }
86       AlignmentI alignment = alignViewport.getAlignment();
87
88       int aWidth = -1;
89
90       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
91       {
92         calcMan.workerComplete(this);
93         return;
94       }
95       strucConsensus = alignViewport.getAlignmentStrucConsensusAnnotation();
96       hStrucConsensus = alignViewport.getRnaStructureConsensusHash();
97       strucConsensus.annotations = null;
98       strucConsensus.annotations = new Annotation[aWidth];
99
100       hStrucConsensus = new Hashtable[aWidth];
101
102       AlignmentAnnotation[] aa = alignViewport.getAlignment()
103               .getAlignmentAnnotation();
104       AlignmentAnnotation rnaStruc = null;
105       // select rna struct to use for calculation
106       for (int i = 0; i < aa.length; i++)
107       {
108         if (aa[i].getRNAStruc() != null && aa[i].isValidStruc())
109         {
110           rnaStruc = aa[i];
111           break;
112         }
113       }
114       // check to see if its valid
115
116       if (rnaStruc == null || !rnaStruc.isValidStruc())
117       {
118         calcMan.workerComplete(this);
119         return;
120       }
121
122       try
123       {
124         final SequenceI[] arr = alignment.getSequencesArray();
125         nseq = arr.length;
126         StructureFrequency.calculate(arr, 0,
127                 alignment.getWidth(), hStrucConsensus, true, rnaStruc);
128       } catch (ArrayIndexOutOfBoundsException x)
129       {
130         calcMan.workerComplete(this);
131         return;
132       }
133       alignViewport.setRnaStructureConsensusHash(hStrucConsensus);
134       // TODO AlignmentAnnotation rnaStruc!!!
135       updateResultAnnotation(true);
136       if (alignViewport.getGlobalColourScheme() != null)
137       {
138         alignViewport.getGlobalColourScheme().setConsensus(hStrucConsensus);
139       }
140
141     } catch (OutOfMemoryError error)
142     {
143       calcMan.workerCannotRun(this);
144
145       // consensus = null;
146       // hconsensus = null;
147       ap.raiseOOMWarning("calculating RNA structure consensus", error);
148     } finally
149     {
150       calcMan.workerComplete(this);
151       if (ap != null)
152       {
153         ap.paintAlignment(true);
154       }
155     }
156
157   }
158
159   /**
160    * update the consensus annotation from the sequence profile data using
161    * current visualization settings.
162    */
163   @Override
164   public void updateAnnotation()
165   {
166     updateResultAnnotation(false);
167   }
168
169   public void updateResultAnnotation(boolean immediate)
170   {
171     if (immediate || !calcMan.isWorking(this) && strucConsensus != null
172             && hStrucConsensus != null)
173     {
174       StructureFrequency.completeConsensus(strucConsensus, hStrucConsensus,
175               0, hStrucConsensus.length,
176               alignViewport.isIgnoreGapsConsensus(),
177               alignViewport.isShowSequenceLogo(), nseq);
178     }
179   }
180
181         @Override
182         protected void run1(int state) {
183                 // TODO Auto-generated method stub
184                 
185         }
186
187 }