JAL-34 - quick hack to see how we can do alignment comparison for splitframes linking...
[jalview.git] / src / jalview / workers / AlignmentComparisonThread.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.AAFrequency;
24 import jalview.analysis.Conservation;
25 import jalview.analysis.scoremodels.ScoreModels;
26 import jalview.api.AlignViewportI;
27 import jalview.api.AlignmentViewPanel;
28 import jalview.datamodel.AlignedCodonFrame;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.Annotation;
32 import jalview.datamodel.SequenceI;
33 import jalview.util.MappingUtils;
34
35 import java.util.ArrayList;
36 import java.util.ConcurrentModificationException;
37 import java.util.Hashtable;
38 import java.util.List;
39
40 /**
41  * A thread to compute the columnwise correspondence between aligned positions
42  * and their positions in a linked alignment
43  * 
44  * @author jprocter
45  *
46  */
47 public class AlignmentComparisonThread extends AlignCalcWorker
48 {
49
50   public AlignmentComparisonThread(AlignViewportI alignViewport,
51           AlignmentViewPanel alignPanel)
52   {
53     super(alignViewport, alignPanel);
54   }
55
56   Annotation[] correspondence = new Annotation[] {};
57   AlignmentAnnotation comparisonAnnot=null;
58   int alWidth;
59
60   @Override
61   public void run()
62   {
63     try
64     {
65       calcMan.notifyStart(this); // updatingConservation = true;
66
67       while ((calcMan != null) && (!calcMan.notifyWorking(this)))
68       {
69         try
70         {
71           if (ap != null)
72           {
73             // ap.paintAlignment(false);
74           }
75           Thread.sleep(200);
76         } catch (Exception ex)
77         {
78           ex.printStackTrace();
79         }
80       }
81       if ((alignViewport == null) || (calcMan == null)
82               || (alignViewport.isClosed()))
83       {
84         abortAndDestroy();
85         return;
86       }
87       List<AlignmentAnnotation> ourAnnot = new ArrayList<>();
88
89       AlignmentI alignment = alignViewport.getAlignment();
90       AlignViewportI codingComplement = alignViewport.getCodingComplement();
91       if (alignment == null || (alWidth = alignment.getWidth()) < 0
92               || (codingComplement == null))
93       {
94         calcMan.workerComplete(this);
95         // .updatingConservation = false;
96         // AlignViewport.UPDATING_CONSERVATION = false;
97
98         return;
99       }
100       comparisonAnnot = alignViewport.getAlignment().findOrCreateAnnotation("Comparison", "CORRESPONDENCE", true, null, null);
101       comparisonAnnot.annotations=correspondence;
102       ourAnnot.add(comparisonAnnot);
103       ourAnnots = ourAnnot;
104
105       try
106       {
107         computeColumnCorrespondence(alignViewport, codingComplement);
108       } catch (IndexOutOfBoundsException x)
109       {
110         // probable race condition. just finish and return without any fuss.
111         calcMan.workerComplete(this);
112         return;
113       }
114       updateResultAnnotation(true);
115     } catch (OutOfMemoryError error)
116     {
117       ap.raiseOOMWarning("calculating conservation", error);
118       calcMan.disableWorker(this);
119       // alignViewport.conservation = null;
120       // this.alignViewport.quality = null;
121
122     }
123     calcMan.workerComplete(this);
124
125     if ((alignViewport == null) || (calcMan == null)
126             || (alignViewport.isClosed()))
127     {
128       abortAndDestroy();
129       return;
130     }
131     if (ap != null)
132     {
133       ap.paintAlignment(true, true);
134     }
135
136   }
137
138   private void computeColumnCorrespondence(AlignViewportI alignViewport,
139           AlignViewportI codingComplement)
140   {
141     List<SequenceI> us = alignViewport.getAlignment().getSequences();
142     List<AlignedCodonFrame> ourMappings = alignViewport.getAlignment()
143             .getCodonFrames();
144     List<SequenceI> them = codingComplement.getAlignment().getSequences();
145     List<AlignedCodonFrame> theirMappings = codingComplement.getAlignment()
146             .getCodonFrames();
147     if (us == null || them == null || us.isEmpty() || them.isEmpty())
148     {
149       return;
150     }
151
152     int colEnd = alignViewport.getAlignment().getWidth();
153     Annotation[] colCorrsp = new Annotation[colEnd];
154     for (int col = 0; col < colEnd; col++)
155     {
156       int[] theirWidth = MappingUtils.findMappedColumns(col, ourMappings,
157               us, them, alignViewport.getGapCharacter());
158       colCorrsp[col] = new Annotation(
159               theirWidth != null ? Math.abs(theirWidth[1] - theirWidth[0])
160                       : 0);
161     }
162     correspondence=colCorrsp;
163   }
164
165   private void updateResultAnnotation(boolean b)
166   {
167     if (b || !calcMan.isWorking(this) && correspondence!=null)
168     {
169       comparisonAnnot.annotations = correspondence;
170       comparisonAnnot.validateRangeAndDisplay();
171     }
172   }
173
174   @Override
175   public void updateAnnotation()
176   {
177     updateResultAnnotation(false);
178
179   }
180
181 }