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