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