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