Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.viewmodel

File PCAModel.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

20
60
13
1
233
145
24
0.4
4.62
13
1.85

Classes

Class Line # Actions
PCAModel 33 60 24 93
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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  0 toggle public PCAModel(AlignmentView seqData, SequenceI[] sqs, boolean nuc,
65    ScoreModelI modelName, SimilarityParamsI params)
66    {
67  0 seqstrings = seqData;
68  0 seqs = sqs;
69  0 nucleotide = nuc;
70  0 scoreModel = modelName;
71  0 similarityParams = params;
72    }
73   
 
74  0 toggle public void run()
75    {
76  0 pca = new PCA(seqstrings, scoreModel, similarityParams);
77  0 pca.run();
78   
79    // Now find the component coordinates
80  0 int ii = 0;
81   
82  0 while ((ii < seqs.length) && (seqs[ii] != null))
83    {
84  0 ii++;
85    }
86   
87  0 int height = pca.getHeight();
88    // top = pca.getM().height() - 1;
89  0 top = height - 1;
90   
91  0 points = new Vector<SequencePoint>();
92  0 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
93   
94  0 for (int i = 0; i < height; i++)
95    {
96  0 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
97  0 points.addElement(sp);
98    }
99    }
100   
 
101  0 toggle public void updateRc(RotatableCanvasI rc)
102    {
103  0 rc.setPoints(points, pca.getHeight());
104    }
105   
 
106  0 toggle public boolean isNucleotide()
107    {
108  0 return nucleotide;
109    }
110   
 
111  0 toggle public void setNucleotide(boolean nucleotide)
112    {
113  0 this.nucleotide = nucleotide;
114    }
115   
116    /**
117    *
118    *
119    * @return index of principle dimension of PCA
120    */
 
121  0 toggle public int getTop()
122    {
123  0 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  0 toggle 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  0 float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
140   
141  0 for (int i = 0; i < pca.getHeight(); i++)
142    {
143  0 points.elementAt(i).coord = scores[i];
144    }
145    }
146   
 
147  0 toggle public String getDetails()
148    {
149  0 return pca.getDetails();
150    }
151   
 
152  0 toggle public AlignmentView getSeqtrings()
153    {
154  0 return seqstrings;
155    }
156   
 
157  0 toggle public String getPointsasCsv(boolean transformed, int xdim, int ydim,
158    int zdim)
159    {
160  0 StringBuffer csv = new StringBuffer();
161  0 csv.append("\"Sequence\"");
162  0 if (transformed)
163    {
164  0 csv.append(",");
165  0 csv.append(xdim);
166  0 csv.append(",");
167  0 csv.append(ydim);
168  0 csv.append(",");
169  0 csv.append(zdim);
170    }
171    else
172    {
173  0 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
174    {
175  0 csv.append("," + d);
176    }
177    }
178  0 csv.append("\n");
179  0 for (int s = 0; s < seqs.length; s++)
180    {
181  0 csv.append("\"" + seqs[s].getName() + "\"");
182  0 double fl[];
183  0 if (!transformed)
184    {
185    // output pca in correct order
186  0 fl = pca.component(s);
187  0 for (int d = fl.length - 1; d >= 0; d--)
188    {
189  0 csv.append(",");
190  0 csv.append(fl[d]);
191    }
192    }
193    else
194    {
195    // output current x,y,z coords for points
196  0 fl = getPointPosition(s);
197  0 for (int d = 0; d < fl.length; d++)
198    {
199  0 csv.append(",");
200  0 csv.append(fl[d]);
201    }
202    }
203  0 csv.append("\n");
204    }
205  0 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  0 toggle public double[] getPointPosition(int s)
214    {
215  0 double pts[] = new double[3];
216  0 float[] p = points.elementAt(s).coord;
217  0 pts[0] = p[0];
218  0 pts[1] = p[1];
219  0 pts[2] = p[2];
220  0 return pts;
221    }
222   
 
223  0 toggle public String getScoreModelName()
224    {
225  0 return scoreModel == null ? "" : scoreModel.getName();
226    }
227   
 
228  0 toggle public void setScoreModel(ScoreModelI sm)
229    {
230  0 this.scoreModel = sm;
231    }
232   
233    }