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