JAL-3878 Add getCalcName to AlignCalcWorkerI.
[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 String getCalcName()
49   {
50     return "Struc Consensus";
51   }
52
53   @Override
54   public void run()
55   {
56       if (alignViewport.isClosed())
57       {
58         abortAndDestroy();
59         return;
60       }
61       AlignmentI alignment = alignViewport.getAlignment();
62
63       int aWidth = -1;
64
65       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
66       {
67         return;
68       }
69       strucConsensus = alignViewport.getAlignmentStrucConsensusAnnotation();
70       hStrucConsensus = alignViewport.getRnaStructureConsensusHash();
71       strucConsensus.annotations = null;
72       strucConsensus.annotations = new Annotation[aWidth];
73
74       hStrucConsensus = new Hashtable[aWidth];
75
76       AlignmentAnnotation[] aa = alignViewport.getAlignment()
77               .getAlignmentAnnotation();
78       AlignmentAnnotation rnaStruc = null;
79       // select rna struct to use for calculation
80       if (aa != null)
81       {
82         for (int i = 0; i < aa.length; i++)
83         {
84           if (aa[i].visible && aa[i].isRNA() && aa[i].isValidStruc())
85           {
86             rnaStruc = aa[i];
87             break;
88           }
89         }
90       }
91       // check to see if its valid
92
93       if (rnaStruc == null || !rnaStruc.isValidStruc())
94       {
95         return;
96       }
97
98       try
99       {
100         final SequenceI[] arr = alignment.getSequencesArray();
101         nseq = arr.length;
102         jalview.analysis.StructureFrequency.calculate(arr, 0,
103                 alignment.getWidth(), hStrucConsensus, true, rnaStruc);
104       } catch (ArrayIndexOutOfBoundsException x)
105       {
106         return;
107       }
108       alignViewport.setRnaStructureConsensusHash(hStrucConsensus);
109       // TODO AlignmentAnnotation rnaStruc!!!
110       updateResultAnnotation(true);
111
112   }
113
114   /**
115    * update the consensus annotation from the sequence profile data using
116    * current visualization settings.
117    */
118   @Override
119   public void updateAnnotation()
120   {
121     updateResultAnnotation(false);
122   }
123
124   public void updateResultAnnotation(boolean immediate)
125   {
126     if (immediate || !calcMan.isWorking(this) && strucConsensus != null
127             && hStrucConsensus != null)
128     {
129       StructureFrequency.completeConsensus(strucConsensus, hStrucConsensus,
130               0, hStrucConsensus.length,
131               alignViewport.isIgnoreGapsConsensus(),
132               alignViewport.isShowSequenceLogo(), nseq);
133     }
134   }
135
136 }