JAL-1517 fix copyright for 2.8.2
[jalview.git] / src / jalview / workers / ConservationThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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 java.util.ArrayList;
24 import java.util.List;
25
26 import jalview.analysis.Conservation;
27 import jalview.api.AlignCalcWorkerI;
28 import jalview.api.AlignmentViewPanel;
29 import jalview.api.AlignViewportI;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.AlignmentI;
32
33 public class ConservationThread extends AlignCalcWorker implements
34         AlignCalcWorkerI
35 {
36
37   private int ConsPercGaps = 25; // JBPNote : This should be a configurable
38                                  // property!
39
40   public ConservationThread(AlignViewportI alignViewport,
41           AlignmentViewPanel alignPanel)
42   {
43     super(alignViewport, alignPanel);
44     ConsPercGaps = alignViewport.getConsPercGaps();
45   }
46
47   private Conservation cons;
48
49   AlignmentAnnotation conservation, quality;
50
51   int alWidth;
52
53   @Override
54   public void run()
55   {
56     try
57     {
58       calcMan.notifyStart(this); // updatingConservation = true;
59
60       while (!calcMan.notifyWorking(this))
61       {
62         try
63         {
64           if (ap != null)
65           {
66             // ap.paintAlignment(false);
67           }
68           Thread.sleep(200);
69         } catch (Exception ex)
70         {
71           ex.printStackTrace();
72         }
73       }
74       if (alignViewport.isClosed())
75       {
76         abortAndDestroy();
77       }
78       List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
79       AlignmentI alignment = alignViewport.getAlignment();
80       conservation = alignViewport.getAlignmentConservationAnnotation();
81       quality = alignViewport.getAlignmentQualityAnnot();
82       ourAnnot.add(conservation);
83       ourAnnot.add(quality);
84       ourAnnots = ourAnnot;
85       ConsPercGaps = alignViewport.getConsPercGaps();
86       // AlignViewport.UPDATING_CONSERVATION = true;
87
88       if (alignment == null || (alWidth = alignment.getWidth()) < 0)
89       {
90         calcMan.workerComplete(this);
91         // .updatingConservation = false;
92         // AlignViewport.UPDATING_CONSERVATION = false;
93
94         return;
95       }
96       try
97       {
98         cons = Conservation.calculateConservation("All",
99                 jalview.schemes.ResidueProperties.propHash, 3,
100                 alignment.getSequences(), 0, alWidth - 1, false,
101                 ConsPercGaps, quality != null);
102       } catch (IndexOutOfBoundsException x)
103       {
104         // probable race condition. just finish and return without any fuss.
105         calcMan.workerComplete(this);
106         return;
107       }
108       updateResultAnnotation(true);
109     } catch (OutOfMemoryError error)
110     {
111       ap.raiseOOMWarning("calculating conservation", error);
112       calcMan.workerCannotRun(this);
113       // alignViewport.conservation = null;
114       // this.alignViewport.quality = null;
115
116     }
117     calcMan.workerComplete(this);
118
119     if (ap != null)
120     {
121       ap.paintAlignment(true);
122     }
123
124   }
125
126   private void updateResultAnnotation(boolean b)
127   {
128     if (b || !calcMan.isWorking(this) && cons != null
129             && conservation != null && quality != null)
130     {
131       alignViewport.setConservation(cons);
132       cons.completeAnnotations(conservation, quality, 0, alWidth);
133     }
134   }
135
136   @Override
137   public void updateAnnotation()
138   {
139     updateResultAnnotation(false);
140
141   }
142 }