Merge branch 'develop' into bug/JAL-4421_customise_volume_name_of_DMG_installers
[jalview.git] / src / jalview / viewmodel / PaSiMapModel.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.viewmodel;
22
23 import jalview.analysis.PaSiMap;
24 import jalview.api.RotatableCanvasI;
25 import jalview.api.analysis.ScoreModelI;
26 import jalview.api.analysis.SimilarityParamsI;
27 import jalview.datamodel.AlignmentView;
28 import jalview.datamodel.Point;
29 import jalview.datamodel.SequenceI;
30 import jalview.datamodel.SequencePoint;
31 import jalview.gui.PairwiseAlignPanel;
32 import jalview.viewmodel.AlignmentViewport;
33
34 import java.util.List;
35 import java.util.Vector;
36
37 public class PaSiMapModel
38 {
39   /*
40    * inputs
41    */
42   private AlignmentViewport inputData;
43
44   private final SequenceI[] seqs;
45
46   /*
47    * options - score model, nucleotide / protein
48    */
49   private ScoreModelI scoreModel;
50
51   private boolean nucleotide = false;
52
53   /*
54    * outputs
55    */
56   private PaSiMap pasimap;
57
58   int top;
59
60   private List<SequencePoint> points;
61
62   /**
63    * Constructor given sequence data, score model and score calculation
64    * parameter options.
65    * 
66    * @param seqData
67    * @param sqs
68    * @param nuc
69    * @param modelName
70    * @param params
71    */
72   public PaSiMapModel(AlignmentViewport seqData, SequenceI[] sqs, boolean nuc,
73           ScoreModelI modelName)
74   {
75     inputData = seqData;
76     seqs = sqs;
77     nucleotide = nuc;
78     scoreModel = modelName;
79   }
80
81   /**
82    * Performs the PaSiMap calculation (in the same thread) and extracts result data
83    * needed for visualisation by PaSiMapPanel
84    */
85   public void calculate(PairwiseAlignPanel pap)
86   {
87     pasimap = new PaSiMap(inputData, scoreModel, pap);
88     pasimap.run(); // executes in same thread, wait for completion
89
90     // Now find the component coordinates
91     int ii = 0;
92
93     while ((ii < seqs.length) && (seqs[ii] != null))
94     {
95       ii++;
96     }
97
98     int width = pasimap.getWidth();
99     int height = pasimap.getHeight();
100     top = width;
101
102     points = new Vector<>();
103     Point[] scores = pasimap.getComponents(width - 1, width - 2, width - 3, 1);
104
105     for (int i = 0; i < height; i++)
106     {
107       SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
108       points.add(sp);
109     }
110   }
111
112   public void updateRc(RotatableCanvasI rc)
113   {
114     rc.setPoints(points, pasimap.getHeight());
115   }
116
117   public boolean isNucleotide()
118   {
119     return nucleotide;
120   }
121
122   public void setNucleotide(boolean nucleotide)
123   {
124     this.nucleotide = nucleotide;
125   }
126
127   /**
128    * Answers the index of the principal dimension of the PaSiMap
129    * 
130    * @return
131    */
132   public int getTop()
133   {
134     return top;
135   }
136
137   public void setTop(int t)
138   {
139     top = t;
140   }
141
142   /**
143    * Updates the 3D coordinates for the list of points to the given dimensions.
144    * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
145    * Note - pasimap.getComponents starts counting the spectrum from rank-2 to zero,
146    * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
147    * 
148    * @param dim1
149    * @param dim2
150    * @param dim3
151    */
152   public void updateRcView(int dim1, int dim2, int dim3)
153   {
154     // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
155     Point[] scores = pasimap.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 1);
156
157     for (int i = 0; i < pasimap.getHeight(); i++)
158     {
159       points.get(i).coord = scores[i];
160     }
161   }
162
163   public String getDetails()
164   {
165     return pasimap.getDetails();
166   }
167
168   public String getAlignmentOutput()
169   {
170     return pasimap.getAlignmentOutput();
171   }
172
173   public AlignmentViewport getInputData()
174   {
175     return inputData;
176   }
177
178   public void setInputData(AlignmentViewport data)
179   {
180     inputData = data;
181   }
182
183   public String getPointsasCsv(boolean transformed, int xdim, int ydim,
184           int zdim)
185   {
186     StringBuffer csv = new StringBuffer();
187     csv.append("\"Sequence\"");
188     if (transformed)
189     {
190       csv.append(",");
191       csv.append(xdim);
192       csv.append(",");
193       csv.append(ydim);
194       csv.append(",");
195       csv.append(zdim);
196     }
197     else
198     {
199       for (int d = 1, dmax = (int) pasimap.getDim(); d <= dmax; d++)
200       {
201         csv.append("," + d);
202       }
203     }
204     csv.append("\n");
205     for (int s = 0; s < seqs.length; s++)
206     {
207       csv.append("\"" + seqs[s].getName() + "\"");
208       if (!transformed)
209       {
210         double[] fl = pasimap.component(s);
211         for (int d = fl.length - 1; d >= 0; d--)
212         {
213           csv.append(",");
214           csv.append(fl[d]);
215         }
216       } else {
217         Point p = points.get(s).coord;
218         csv.append(",").append(p.x);
219         csv.append(",").append(p.y);
220         csv.append(",").append(p.z);
221       }
222       csv.append("\n");
223     }
224     return csv.toString();
225   }
226
227   public String getScoreModelName()
228   {
229     return scoreModel == null ? "" : scoreModel.getName();
230   }
231
232   public void setScoreModel(ScoreModelI sm)
233   {
234     this.scoreModel = sm;
235   }
236
237   public List<SequencePoint> getSequencePoints()
238   {
239     return points;
240   }
241
242   public void setSequencePoints(List<SequencePoint> sp)
243   {
244     points = sp;
245   }
246
247   /**
248    * Answers the object holding the values of the computed PaSiMap
249    * 
250    * @return
251    */
252   public PaSiMap getPasimapData()
253   {
254     return pasimap;
255   }
256
257   public void setPaSiMap(PaSiMap data)
258   {
259     pasimap = data;
260   }
261 }