1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.analysis; |
22 |
|
|
23 |
|
import jalview.api.analysis.ScoreModelI; |
24 |
|
import jalview.api.analysis.SimilarityParamsI; |
25 |
|
import jalview.bin.Console; |
26 |
|
import jalview.datamodel.AlignmentView; |
27 |
|
import jalview.datamodel.Point; |
28 |
|
import jalview.math.MatrixI; |
29 |
|
|
30 |
|
import java.io.PrintStream; |
31 |
|
import java.util.HashMap; |
32 |
|
import java.util.Map; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
|
|
| 49.6% |
Uncovered Elements: 64 (127) |
Complexity: 35 |
Complexity Density: 0.41 |
|
37 |
|
public class PCA implements Runnable, SpatialCalculationI |
38 |
|
{ |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
final private AlignmentView seqs; |
43 |
|
|
44 |
|
final private ScoreModelI scoreModel; |
45 |
|
|
46 |
|
final private SimilarityParamsI similarityParams; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
private MatrixI pairwiseScores; |
52 |
|
|
53 |
|
private MatrixI tridiagonal; |
54 |
|
|
55 |
|
private MatrixI eigenMatrix; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@param |
62 |
|
@param |
63 |
|
@param |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
65 |
2 |
public PCA(AlignmentView sequences, ScoreModelI sm,... |
66 |
|
SimilarityParamsI options) |
67 |
|
{ |
68 |
2 |
this.seqs = sequences; |
69 |
2 |
this.scoreModel = sm; |
70 |
2 |
this.similarityParams = options; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
|
78 |
|
|
79 |
|
@return |
80 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
0 |
public double getEigenvalue(int i)... |
82 |
|
{ |
83 |
0 |
return eigenMatrix.getD()[i]; |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@param |
90 |
|
|
91 |
|
@param |
92 |
|
|
93 |
|
@param |
94 |
|
|
95 |
|
@param |
96 |
|
|
97 |
|
|
98 |
|
@return |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
100 |
1 |
public Point[] getComponents(int l, int n, int mm, float factor)... |
101 |
|
{ |
102 |
1 |
Point[] out = new Point[getHeight()]; |
103 |
|
|
104 |
16 |
for (int i = 0; i < getHeight(); i++) |
105 |
|
{ |
106 |
15 |
float x = (float) component(i, l) * factor; |
107 |
15 |
float y = (float) component(i, n) * factor; |
108 |
15 |
float z = (float) component(i, mm) * factor; |
109 |
15 |
out[i] = new Point(x, y, z); |
110 |
|
} |
111 |
|
|
112 |
1 |
return out; |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
@param |
119 |
|
|
120 |
|
|
121 |
|
@return |
122 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
123 |
0 |
public double[] component(int n)... |
124 |
|
{ |
125 |
|
|
126 |
0 |
double[] out = new double[getHeight()]; |
127 |
|
|
128 |
0 |
for (int i = 0; i < out.length; i++) |
129 |
|
{ |
130 |
0 |
out[i] = component(i, n); |
131 |
|
} |
132 |
|
|
133 |
0 |
return out; |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
|
141 |
|
@param |
142 |
|
|
143 |
|
|
144 |
|
@return |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
146 |
45 |
double component(int row, int n)... |
147 |
|
{ |
148 |
45 |
double out = 0.0; |
149 |
|
|
150 |
720 |
for (int i = 0; i < pairwiseScores.width(); i++) |
151 |
|
{ |
152 |
675 |
out += (pairwiseScores.getValue(row, i) * eigenMatrix.getValue(i, n)); |
153 |
|
} |
154 |
|
|
155 |
45 |
return out / eigenMatrix.getD()[n]; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
@return |
163 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
|
164 |
0 |
public String getDetails()... |
165 |
|
{ |
166 |
0 |
StringBuilder sb = new StringBuilder(1024); |
167 |
0 |
sb.append("PCA calculation using ").append(scoreModel.getName()) |
168 |
|
.append(" sequence similarity matrix\n========\n\n"); |
169 |
0 |
PrintStream ps = wrapOutputBuffer(sb); |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
0 |
sb.append(" --- OrigT * Orig ---- \n"); |
175 |
0 |
pairwiseScores.print(ps, "%8.2f"); |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
0 |
sb.append(" ---Tridiag transform matrix ---\n"); |
181 |
0 |
sb.append(" --- D vector ---\n"); |
182 |
0 |
tridiagonal.printD(ps, "%15.4e"); |
183 |
0 |
ps.println(); |
184 |
0 |
sb.append("--- E vector ---\n"); |
185 |
0 |
tridiagonal.printE(ps, "%15.4e"); |
186 |
0 |
ps.println(); |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
0 |
sb.append(" --- New diagonalization matrix ---\n"); |
192 |
0 |
eigenMatrix.print(ps, "%8.2f"); |
193 |
0 |
sb.append(" --- Eigenvalues ---\n"); |
194 |
0 |
eigenMatrix.printD(ps, "%15.4e"); |
195 |
0 |
ps.println(); |
196 |
|
|
197 |
0 |
return sb.toString(); |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
|
|
| 50% |
Uncovered Elements: 17 (34) |
Complexity: 7 |
Complexity Density: 0.29 |
|
203 |
1 |
@Override... |
204 |
|
public void run() |
205 |
|
{ |
206 |
1 |
isCancelled=false; |
207 |
1 |
try |
208 |
|
{ |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
1 |
pairwiseScores = scoreModel.findSimilarities(seqs, similarityParams); |
213 |
1 |
if (isCancelled()) |
214 |
|
{ |
215 |
0 |
pairwiseScores=null; |
216 |
0 |
return; |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
1 |
tridiagonal = pairwiseScores.copy(); |
223 |
1 |
if (isCancelled()) { |
224 |
0 |
clearResults(); |
225 |
0 |
return; |
226 |
|
} |
227 |
1 |
tridiagonal.tred(); |
228 |
1 |
if (isCancelled()) { |
229 |
0 |
clearResults(); |
230 |
0 |
return; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
1 |
eigenMatrix = tridiagonal.copy(); |
236 |
1 |
if (isCancelled()) { |
237 |
0 |
clearResults(); |
238 |
0 |
return; |
239 |
|
} |
240 |
1 |
eigenMatrix.tqli(); |
241 |
1 |
if (isCancelled()) { |
242 |
0 |
clearResults(); |
243 |
0 |
return; |
244 |
|
} |
245 |
|
} catch (Exception q) |
246 |
|
{ |
247 |
0 |
Console.error("Error computing PCA: " + q.getMessage()); |
248 |
0 |
q.printStackTrace(); |
249 |
|
} |
250 |
|
} |
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
251 |
0 |
private void clearResults()... |
252 |
|
{ |
253 |
0 |
pairwiseScores = null; |
254 |
0 |
tridiagonal = null; |
255 |
0 |
eigenMatrix = null; |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
@param |
262 |
|
@return |
263 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
264 |
0 |
protected PrintStream wrapOutputBuffer(StringBuilder sb)... |
265 |
|
{ |
266 |
0 |
PrintStream ps = new PrintStream(System.out) |
267 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
268 |
0 |
@Override... |
269 |
|
public void print(String x) |
270 |
|
{ |
271 |
0 |
sb.append(x); |
272 |
|
} |
273 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
274 |
0 |
@Override... |
275 |
|
public void println() |
276 |
|
{ |
277 |
0 |
sb.append("\n"); |
278 |
|
} |
279 |
|
}; |
280 |
0 |
return ps; |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
@return |
288 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
289 |
20 |
public int getHeight()... |
290 |
|
{ |
291 |
|
|
292 |
20 |
return pairwiseScores.height(); |
293 |
|
} |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
294 |
1 |
@Override... |
295 |
|
public int getWidth() |
296 |
|
{ |
297 |
1 |
return getHeight(); |
298 |
|
} |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
299 |
0 |
@Override... |
300 |
|
public int getDim() |
301 |
|
{ |
302 |
0 |
return component(1).length; |
303 |
|
} |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
304 |
1 |
public int getTop()... |
305 |
|
{ |
306 |
1 |
return pairwiseScores.height()-1; |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
@return |
313 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
314 |
1 |
public MatrixI getPairwiseScores()... |
315 |
|
{ |
316 |
1 |
return pairwiseScores; |
317 |
|
} |
318 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
319 |
1 |
public void setPairwiseScores(MatrixI m)... |
320 |
|
{ |
321 |
1 |
pairwiseScores = m; |
322 |
|
} |
323 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
324 |
1 |
public MatrixI getEigenmatrix()... |
325 |
|
{ |
326 |
1 |
return eigenMatrix; |
327 |
|
} |
328 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
329 |
1 |
public void setEigenmatrix(MatrixI m)... |
330 |
|
{ |
331 |
1 |
eigenMatrix = m; |
332 |
|
} |
333 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
334 |
1 |
public MatrixI getTridiagonal()... |
335 |
|
{ |
336 |
1 |
return tridiagonal; |
337 |
|
} |
338 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
339 |
1 |
public void setTridiagonal(MatrixI tridiagonal)... |
340 |
|
{ |
341 |
1 |
this.tridiagonal = tridiagonal; |
342 |
|
} |
343 |
|
|
344 |
|
volatile boolean isCancelled = false; |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
345 |
0 |
@Override... |
346 |
|
public void cancel() |
347 |
|
{ |
348 |
0 |
isCancelled = true; |
349 |
|
} |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
350 |
0 |
@Override... |
351 |
|
public boolean isCancellable() |
352 |
|
{ |
353 |
0 |
return true; |
354 |
|
} |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
355 |
6 |
@Override... |
356 |
|
public boolean isCancelled() |
357 |
|
{ |
358 |
6 |
return isCancelled; |
359 |
|
} |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
360 |
0 |
@Override... |
361 |
|
public String getAlignmentOutput() |
362 |
|
{ |
363 |
|
|
364 |
0 |
return null; |
365 |
|
} |
366 |
|
|
367 |
|
public static final String PAIRWISE_SIMILARITY="PCA Input Similarity"; |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
368 |
1 |
@Override... |
369 |
|
public Map<String,MatrixI> getDistanceMatrices() |
370 |
|
{ |
371 |
1 |
Map<String, MatrixI> clusterables = new HashMap<String, MatrixI>(); |
372 |
1 |
clusterables.put(PAIRWISE_SIMILARITY,pairwiseScores); |
373 |
|
|
374 |
1 |
return clusterables; |
375 |
|
} |
376 |
|
|
377 |
|
} |