JAL-1767 minor field tidying prior to 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 volatile 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   public void run()
81   {
82     pca = new PCA(seqstrings, scoreModel, similarityParams);
83     pca.run();
84
85     // Now find the component coordinates
86     int ii = 0;
87
88     while ((ii < seqs.length) && (seqs[ii] != null))
89     {
90       ii++;
91     }
92
93     int height = pca.getHeight();
94     // top = pca.getM().height() - 1;
95     top = height - 1;
96
97     points = new Vector<>();
98     float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
99
100     for (int i = 0; i < height; i++)
101     {
102       SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
103       points.addElement(sp);
104     }
105   }
106
107   public void updateRc(RotatableCanvasI rc)
108   {
109     rc.setPoints(points, pca.getHeight());
110   }
111
112   public boolean isNucleotide()
113   {
114     return nucleotide;
115   }
116
117   public void setNucleotide(boolean nucleotide)
118   {
119     this.nucleotide = nucleotide;
120   }
121
122   /**
123    * 
124    * 
125    * @return index of principle dimension of PCA
126    */
127   public int getTop()
128   {
129     return top;
130   }
131
132   /**
133    * update the 2d coordinates for the list of points to the given dimensions
134    * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
135    * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
136    * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
137    * 
138    * @param dim1
139    * @param dim2
140    * @param dim3
141    */
142   public void updateRcView(int dim1, int dim2, int dim3)
143   {
144     // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
145     float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
146
147     for (int i = 0; i < pca.getHeight(); i++)
148     {
149       points.elementAt(i).coord = scores[i];
150     }
151   }
152
153   public String getDetails()
154   {
155     return pca.getDetails();
156   }
157
158   public AlignmentView getSeqtrings()
159   {
160     return seqstrings;
161   }
162
163   public String getPointsasCsv(boolean transformed, int xdim, int ydim,
164           int zdim)
165   {
166     StringBuffer csv = new StringBuffer();
167     csv.append("\"Sequence\"");
168     if (transformed)
169     {
170       csv.append(",");
171       csv.append(xdim);
172       csv.append(",");
173       csv.append(ydim);
174       csv.append(",");
175       csv.append(zdim);
176     }
177     else
178     {
179       for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
180       {
181         csv.append("," + d);
182       }
183     }
184     csv.append("\n");
185     for (int s = 0; s < seqs.length; s++)
186     {
187       csv.append("\"" + seqs[s].getName() + "\"");
188       double fl[];
189       if (!transformed)
190       {
191         // output pca in correct order
192         fl = pca.component(s);
193         for (int d = fl.length - 1; d >= 0; d--)
194         {
195           csv.append(",");
196           csv.append(fl[d]);
197         }
198       }
199       else
200       {
201         // output current x,y,z coords for points
202         fl = getPointPosition(s);
203         for (int d = 0; d < fl.length; d++)
204         {
205           csv.append(",");
206           csv.append(fl[d]);
207         }
208       }
209       csv.append("\n");
210     }
211     return csv.toString();
212   }
213
214   /**
215    * 
216    * @return x,y,z positions of point s (index into points) under current
217    *         transform.
218    */
219   public double[] getPointPosition(int s)
220   {
221     double pts[] = new double[3];
222     float[] p = points.elementAt(s).coord;
223     pts[0] = p[0];
224     pts[1] = p[1];
225     pts[2] = p[2];
226     return pts;
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 }