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