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