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