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