JAL-728 offer choice of substitution matrix for Quality calculation
[jalview.git] / src / jalview / workers / ConservationThread.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.Conservation;
24 import jalview.analysis.scoremodels.ScoreMatrix;
25 import jalview.analysis.scoremodels.ScoreModels;
26 import jalview.api.AlignViewportI;
27 import jalview.api.AlignmentViewPanel;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.AlignmentI;
30 import jalview.util.MessageManager;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 public class ConservationThread extends AlignCalcWorker
36 {
37
38   private int ConsPercGaps = 25; // JBPNote : This should be a configurable
39                                  // property!
40
41   public ConservationThread(AlignViewportI alignViewport,
42           AlignmentViewPanel alignPanel)
43   {
44     super(alignViewport, alignPanel);
45     ConsPercGaps = alignViewport.getConsPercGaps();
46     setScoreModel(ScoreModels.getInstance().getBlosum62());
47   }
48
49   private Conservation cons;
50
51   AlignmentAnnotation conservation, quality;
52
53   int alWidth;
54
55   private ScoreMatrix scoreModel;
56
57   @Override
58   public void run()
59   {
60     try
61     {
62       calcMan.notifyStart(this); // updatingConservation = true;
63
64       while ((calcMan != null) && (!calcMan.notifyWorking(this)))
65       {
66         try
67         {
68           if (ap != null)
69           {
70             // ap.paintAlignment(false);
71           }
72           Thread.sleep(200);
73         } catch (Exception ex)
74         {
75           ex.printStackTrace();
76         }
77       }
78       if ((alignViewport == null) || (calcMan == null)
79               || (alignViewport.isClosed()))
80       {
81         abortAndDestroy();
82         return;
83       }
84       List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
85       AlignmentI alignment = alignViewport.getAlignment();
86       conservation = alignViewport.getAlignmentConservationAnnotation();
87       quality = alignViewport.getAlignmentQualityAnnot();
88       ourAnnot.add(conservation);
89       ourAnnot.add(quality);
90       ourAnnots = ourAnnot;
91       ConsPercGaps = alignViewport.getConsPercGaps();
92       // AlignViewport.UPDATING_CONSERVATION = true;
93
94       if (alignment == null || (alWidth = alignment.getWidth()) < 0)
95       {
96         calcMan.workerComplete(this);
97         // .updatingConservation = false;
98         // AlignViewport.UPDATING_CONSERVATION = false;
99
100         return;
101       }
102       try
103       {
104         cons = Conservation.calculateConservation("All",
105                 alignment.getSequences(), 0, alWidth - 1, false,
106                 ConsPercGaps, quality != null, scoreModel);
107       } catch (IndexOutOfBoundsException x)
108       {
109         // probable race condition. just finish and return without any fuss.
110         calcMan.workerComplete(this);
111         return;
112       }
113       updateResultAnnotation(true);
114     } catch (OutOfMemoryError error)
115     {
116       ap.raiseOOMWarning("calculating conservation", error);
117       calcMan.disableWorker(this);
118       // alignViewport.conservation = null;
119       // this.alignViewport.quality = null;
120
121     }
122     calcMan.workerComplete(this);
123
124     if ((alignViewport == null) || (calcMan == null)
125             || (alignViewport.isClosed()))
126     {
127       abortAndDestroy();
128       return;
129     }
130     if (ap != null)
131     {
132       ap.paintAlignment(true);
133     }
134
135   }
136
137   private void updateResultAnnotation(boolean b)
138   {
139     if (b || !calcMan.isWorking(this) && cons != null
140             && conservation != null && quality != null)
141     {
142       alignViewport.setConservation(cons);
143       cons.completeAnnotations(conservation, quality, 0, alWidth);
144     }
145   }
146
147   @Override
148   public void updateAnnotation()
149   {
150     updateResultAnnotation(false);
151
152   }
153
154   /**
155    * Sets the substitution matrix to be used in the Quality calculation. This
156    * method is not thread-safe.
157    * 
158    * @param model
159    */
160   public void setScoreModel(ScoreMatrix model)
161   {
162     scoreModel = model;
163     if (quality != null)
164     {
165       quality.description = MessageManager.formatMessage(
166               "label.quality_label", model.getName());
167     }
168   }
169
170   public ScoreMatrix getScoreModel()
171   {
172     return scoreModel;
173   }
174 }