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