Clover icon

Coverage Report

  1. Project Clover database Fri Jan 16 2026 12:43:44 GMT
  2. Package jalview.viewmodel

File PCAModel.java

 

Coverage histogram

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

Code metrics

18
65
19
1
293
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    // Map to store node pos : annotation details
66    // (additional details from annotation property)
67    protected HashMap<Integer, String> annotationDetails;
68   
69   
70    protected List<String> labels;
71   
72   
73    ArrayList<AlignmentAnnotation> ssAnnotationForSeqs;
74   
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   
110   
111  1 pca.run(); // executes in same thread, wait for completion
112   
113    // Now find the component coordinates
114  1 int ii = 0;
115   
116  16 while ((ii < seqs.length) && (seqs[ii] != null))
117    {
118  15 ii++;
119    }
120   
121  1 int height = pca.getHeight();
122    // top = pca.getM().height() - 1;
123  1 top = height - 1;
124   
125  1 points = new Vector<>();
126  1 Point[] scores = pca.getComponents(top - 1, top - 2, top - 3, 1);
127   
128  16 for (int i = 0; i < height; i++)
129    {
130  15 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
131  15 points.add(sp);
132    }
133    }
134   
 
135  1 toggle public void updateRc(RotatableCanvasI rc)
136    {
137  1 rc.setPoints(points, pca.getHeight());
138    }
139   
 
140  0 toggle public boolean isNucleotide()
141    {
142  0 return nucleotide;
143    }
144   
 
145  0 toggle public void setNucleotide(boolean nucleotide)
146    {
147  0 this.nucleotide = nucleotide;
148    }
149   
150    /**
151    * Answers the index of the principal dimension of the PCA
152    *
153    * @return
154    */
 
155  1 toggle public int getTop()
156    {
157  1 return top;
158    }
159   
 
160  1 toggle public void setTop(int t)
161    {
162  1 top = t;
163    }
164   
165    /**
166    * Updates the 3D coordinates for the list of points to the given dimensions.
167    * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
168    * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
169    * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
170    *
171    * @param dim1
172    * @param dim2
173    * @param dim3
174    */
 
175  0 toggle public void updateRcView(int dim1, int dim2, int dim3)
176    {
177    // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
178  0 Point[] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 1);
179   
180  0 for (int i = 0; i < pca.getHeight(); i++)
181    {
182  0 points.get(i).coord = scores[i];
183    }
184    }
185   
 
186  0 toggle public String getDetails()
187    {
188  0 return pca.getDetails();
189    }
190   
 
191  0 toggle public AlignmentView getInputData()
192    {
193  0 return inputData;
194    }
195   
 
196  1 toggle public void setInputData(AlignmentView data)
197    {
198  1 inputData = data;
199    }
200   
 
201  0 toggle public String getPointsasCsv(boolean transformed, int xdim, int ydim,
202    int zdim)
203    {
204  0 StringBuffer csv = new StringBuffer();
205  0 csv.append("\"Sequence\"");
206  0 if (transformed)
207    {
208  0 csv.append(",");
209  0 csv.append(xdim);
210  0 csv.append(",");
211  0 csv.append(ydim);
212  0 csv.append(",");
213  0 csv.append(zdim);
214    }
215    else
216    {
217  0 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
218    {
219  0 csv.append("," + d);
220    }
221    }
222  0 csv.append("\n");
223  0 for (int s = 0; s < seqs.length; s++)
224    {
225  0 csv.append("\"" + seqs[s].getName() + "\"");
226  0 double fl[];
227  0 if (!transformed)
228    {
229    // output pca in correct order
230  0 fl = pca.component(s);
231  0 for (int d = fl.length - 1; d >= 0; d--)
232    {
233  0 csv.append(",");
234  0 csv.append(fl[d]);
235    }
236    }
237    else
238    {
239  0 Point p = points.get(s).coord;
240  0 csv.append(",").append(p.x);
241  0 csv.append(",").append(p.y);
242  0 csv.append(",").append(p.z);
243    }
244  0 csv.append("\n");
245    }
246  0 return csv.toString();
247    }
248   
 
249  3 toggle public String getScoreModelName()
250    {
251  3 return scoreModel == null ? "" : scoreModel.getName();
252    }
253   
 
254  0 toggle public void setScoreModel(ScoreModelI sm)
255    {
256  0 this.scoreModel = sm;
257    }
258   
259    /**
260    * Answers the parameters configured for pairwise similarity calculations
261    *
262    * @return
263    */
 
264  1 toggle public SimilarityParamsI getSimilarityParameters()
265    {
266  1 return similarityParams;
267    }
268   
 
269  1 toggle public List<SequencePoint> getSequencePoints()
270    {
271  1 return points;
272    }
273   
 
274  1 toggle public void setSequencePoints(List<SequencePoint> sp)
275    {
276  1 points = sp;
277    }
278   
279    /**
280    * Answers the object holding the values of the computed PCA
281    *
282    * @return
283    */
 
284  1 toggle public PCA getPcaData()
285    {
286  1 return pca;
287    }
288   
 
289  1 toggle public void setPCA(PCA data)
290    {
291  1 pca = data;
292    }
293    }