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