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