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