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