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