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