Clover icon

Coverage Report

  1. Project Clover database Fri Dec 12 2025 14:18:41 GMT
  2. Package jalview.viewmodel

File PCAModel.java

 

Coverage histogram

../../img/srcFileCovDistChart5.png
44% of files have more coverage

Code metrics

18
65
19
1
291
175
29
0.45
3.42
19
1.53

Classes

Class Line # Actions
PCAModel 38 65 29
0.4705882447.1%
 

Contributing tests

This file is covered by 1 test. .

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.AlignmentAnnotation;
28    import jalview.datamodel.AlignmentView;
29    import jalview.datamodel.Point;
30    import jalview.datamodel.SequenceI;
31    import jalview.datamodel.SequencePoint;
32   
33    import java.util.ArrayList;
34    import java.util.HashMap;
35    import java.util.List;
36    import java.util.Vector;
37   
 
38    public class PCAModel
39    {
40    /*
41    * inputs
42    */
43    private AlignmentView inputData;
44   
45    private SequenceI[] seqs;
46   
47    private final SimilarityParamsI similarityParams;
48   
49    /*
50    * options - score model, nucleotide / protein
51    */
52    private ScoreModelI scoreModel;
53   
54    private boolean nucleotide = false;
55   
56    /*
57    * outputs
58    */
59    private PCA pca;
60   
61    int top;
62   
63    private List<SequencePoint> points;
64   
65   
66    // Map to store node pos : annotation details
67    // (additional details from annotation property)
68    protected HashMap<Integer, String> annotationDetails;
69   
70   
71    protected List<String> labels;
72   
73   
74    ArrayList<AlignmentAnnotation> ssAnnotationForSeqs;
75   
76    /**
77    * Constructor given sequence data, score model and score calculation
78    * parameter options.
79    *
80    * @param seqData
81    * @param sqs
82    * @param nuc
83    * @param modelName
84    * @param params
85    */
 
86  2 toggle public PCAModel(AlignmentView seqData, SequenceI[] sqs, boolean nuc,
87    ScoreModelI modelName, SimilarityParamsI params)
88    {
89  2 inputData = seqData;
90  2 seqs = sqs;
91  2 nucleotide = nuc;
92  2 scoreModel = modelName;
93  2 similarityParams = params;
94    }
95   
96    /**
97    * Performs the PCA calculation (in the same thread) and extracts result data
98    * needed for visualisation by PCAPanel
99    */
 
100  1 toggle public void calculate()
101    {
102  1 pca = new PCA(inputData, scoreModel, similarityParams);
103   
104  1 labels = new ArrayList<String>();
105  1 annotationDetails = new HashMap<Integer, String>();
106  1 ssAnnotationForSeqs = new ArrayList<AlignmentAnnotation>();
107  1 seqs = scoreModel.expandSeqData(seqs, inputData, similarityParams,
108    labels, ssAnnotationForSeqs, annotationDetails);
109  1 pca.run(); // executes in same thread, wait for completion
110   
111    // Now find the component coordinates
112  1 int ii = 0;
113   
114  16 while ((ii < seqs.length) && (seqs[ii] != null))
115    {
116  15 ii++;
117    }
118   
119  1 int height = pca.getHeight();
120    // top = pca.getM().height() - 1;
121  1 top = height - 1;
122   
123  1 points = new Vector<>();
124  1 Point[] scores = pca.getComponents(top - 1, top - 2, top - 3, 1);
125   
126  16 for (int i = 0; i < height; i++)
127    {
128  15 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
129  15 points.add(sp);
130    }
131    }
132   
 
133  1 toggle public void updateRc(RotatableCanvasI rc)
134    {
135  1 rc.setPoints(points, pca.getHeight());
136    }
137   
 
138  0 toggle public boolean isNucleotide()
139    {
140  0 return nucleotide;
141    }
142   
 
143  0 toggle public void setNucleotide(boolean nucleotide)
144    {
145  0 this.nucleotide = nucleotide;
146    }
147   
148    /**
149    * Answers the index of the principal dimension of the PCA
150    *
151    * @return
152    */
 
153  1 toggle public int getTop()
154    {
155  1 return top;
156    }
157   
 
158  1 toggle public void setTop(int t)
159    {
160  1 top = t;
161    }
162   
163    /**
164    * Updates the 3D coordinates for the list of points to the given dimensions.
165    * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
166    * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
167    * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
168    *
169    * @param dim1
170    * @param dim2
171    * @param dim3
172    */
 
173  0 toggle public void updateRcView(int dim1, int dim2, int dim3)
174    {
175    // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
176  0 Point[] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 1);
177   
178  0 for (int i = 0; i < pca.getHeight(); i++)
179    {
180  0 points.get(i).coord = scores[i];
181    }
182    }
183   
 
184  0 toggle public String getDetails()
185    {
186  0 return pca.getDetails();
187    }
188   
 
189  0 toggle public AlignmentView getInputData()
190    {
191  0 return inputData;
192    }
193   
 
194  1 toggle public void setInputData(AlignmentView data)
195    {
196  1 inputData = data;
197    }
198   
 
199  0 toggle public String getPointsasCsv(boolean transformed, int xdim, int ydim,
200    int zdim)
201    {
202  0 StringBuffer csv = new StringBuffer();
203  0 csv.append("\"Sequence\"");
204  0 if (transformed)
205    {
206  0 csv.append(",");
207  0 csv.append(xdim);
208  0 csv.append(",");
209  0 csv.append(ydim);
210  0 csv.append(",");
211  0 csv.append(zdim);
212    }
213    else
214    {
215  0 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
216    {
217  0 csv.append("," + d);
218    }
219    }
220  0 csv.append("\n");
221  0 for (int s = 0; s < seqs.length; s++)
222    {
223  0 csv.append("\"" + seqs[s].getName() + "\"");
224  0 double fl[];
225  0 if (!transformed)
226    {
227    // output pca in correct order
228  0 fl = pca.component(s);
229  0 for (int d = fl.length - 1; d >= 0; d--)
230    {
231  0 csv.append(",");
232  0 csv.append(fl[d]);
233    }
234    }
235    else
236    {
237  0 Point p = points.get(s).coord;
238  0 csv.append(",").append(p.x);
239  0 csv.append(",").append(p.y);
240  0 csv.append(",").append(p.z);
241    }
242  0 csv.append("\n");
243    }
244  0 return csv.toString();
245    }
246   
 
247  3 toggle public String getScoreModelName()
248    {
249  3 return scoreModel == null ? "" : scoreModel.getName();
250    }
251   
 
252  0 toggle public void setScoreModel(ScoreModelI sm)
253    {
254  0 this.scoreModel = sm;
255    }
256   
257    /**
258    * Answers the parameters configured for pairwise similarity calculations
259    *
260    * @return
261    */
 
262  1 toggle public SimilarityParamsI getSimilarityParameters()
263    {
264  1 return similarityParams;
265    }
266   
 
267  1 toggle public List<SequencePoint> getSequencePoints()
268    {
269  1 return points;
270    }
271   
 
272  1 toggle public void setSequencePoints(List<SequencePoint> sp)
273    {
274  1 points = sp;
275    }
276   
277    /**
278    * Answers the object holding the values of the computed PCA
279    *
280    * @return
281    */
 
282  1 toggle public PCA getPcaData()
283    {
284  1 return pca;
285    }
286   
 
287  1 toggle public void setPCA(PCA data)
288    {
289  1 pca = data;
290    }
291    }