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 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
|
|
| 0% |
Uncovered Elements: 79 (79) |
Complexity: 21 |
Complexity Density: 0.38 |
|
35 |
|
public class PCA implements Runnable |
36 |
|
{ |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
final private AlignmentView seqs; |
41 |
|
|
42 |
|
final private ScoreModelI scoreModel; |
43 |
|
|
44 |
|
final private SimilarityParamsI similarityParams; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
private MatrixI pairwiseScores; |
50 |
|
|
51 |
|
private MatrixI tridiagonal; |
52 |
|
|
53 |
|
private MatrixI eigenMatrix; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@param |
60 |
|
@param |
61 |
|
@param |
62 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
63 |
0 |
public PCA(AlignmentView sequences, ScoreModelI sm,... |
64 |
|
SimilarityParamsI options) |
65 |
|
{ |
66 |
0 |
this.seqs = sequences; |
67 |
0 |
this.scoreModel = sm; |
68 |
0 |
this.similarityParams = options; |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@param |
75 |
|
|
76 |
|
|
77 |
|
@return |
78 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
0 |
public double getEigenvalue(int i)... |
80 |
|
{ |
81 |
0 |
return eigenMatrix.getD()[i]; |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@param |
88 |
|
|
89 |
|
@param |
90 |
|
|
91 |
|
@param |
92 |
|
|
93 |
|
@param |
94 |
|
|
95 |
|
|
96 |
|
@return |
97 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
98 |
0 |
public Point[] getComponents(int l, int n, int mm, float factor)... |
99 |
|
{ |
100 |
0 |
Point[] out = new Point[getHeight()]; |
101 |
|
|
102 |
0 |
for (int i = 0; i < getHeight(); i++) |
103 |
|
{ |
104 |
0 |
float x = (float) component(i, l) * factor; |
105 |
0 |
float y = (float) component(i, n) * factor; |
106 |
0 |
float z = (float) component(i, mm) * factor; |
107 |
0 |
out[i] = new Point(x, y, z); |
108 |
|
} |
109 |
|
|
110 |
0 |
return out; |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
@param |
117 |
|
|
118 |
|
|
119 |
|
@return |
120 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
121 |
0 |
public double[] component(int n)... |
122 |
|
{ |
123 |
|
|
124 |
0 |
double[] out = new double[getHeight()]; |
125 |
|
|
126 |
0 |
for (int i = 0; i < out.length; i++) |
127 |
|
{ |
128 |
0 |
out[i] = component(i, n); |
129 |
|
} |
130 |
|
|
131 |
0 |
return out; |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
@param |
138 |
|
|
139 |
|
@param |
140 |
|
|
141 |
|
|
142 |
|
@return |
143 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
144 |
0 |
double component(int row, int n)... |
145 |
|
{ |
146 |
0 |
double out = 0.0; |
147 |
|
|
148 |
0 |
for (int i = 0; i < pairwiseScores.width(); i++) |
149 |
|
{ |
150 |
0 |
out += (pairwiseScores.getValue(row, i) * eigenMatrix.getValue(i, n)); |
151 |
|
} |
152 |
|
|
153 |
0 |
return out / eigenMatrix.getD()[n]; |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
@return |
161 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
|
162 |
0 |
public String getDetails()... |
163 |
|
{ |
164 |
0 |
StringBuilder sb = new StringBuilder(1024); |
165 |
0 |
sb.append("PCA calculation using ").append(scoreModel.getName()) |
166 |
|
.append(" sequence similarity matrix\n========\n\n"); |
167 |
0 |
PrintStream ps = wrapOutputBuffer(sb); |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
0 |
sb.append(" --- OrigT * Orig ---- \n"); |
173 |
0 |
pairwiseScores.print(ps, "%8.2f"); |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
0 |
sb.append(" ---Tridiag transform matrix ---\n"); |
179 |
0 |
sb.append(" --- D vector ---\n"); |
180 |
0 |
tridiagonal.printD(ps, "%15.4e"); |
181 |
0 |
ps.println(); |
182 |
0 |
sb.append("--- E vector ---\n"); |
183 |
0 |
tridiagonal.printE(ps, "%15.4e"); |
184 |
0 |
ps.println(); |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
0 |
sb.append(" --- New diagonalization matrix ---\n"); |
190 |
0 |
eigenMatrix.print(ps, "%8.2f"); |
191 |
0 |
sb.append(" --- Eigenvalues ---\n"); |
192 |
0 |
eigenMatrix.printD(ps, "%15.4e"); |
193 |
0 |
ps.println(); |
194 |
|
|
195 |
0 |
return sb.toString(); |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
201 |
0 |
@Override... |
202 |
|
public void run() |
203 |
|
{ |
204 |
0 |
try |
205 |
|
{ |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
0 |
pairwiseScores = scoreModel.findSimilarities(seqs, similarityParams); |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
0 |
tridiagonal = pairwiseScores.copy(); |
215 |
0 |
tridiagonal.tred(); |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
0 |
eigenMatrix = tridiagonal.copy(); |
221 |
0 |
eigenMatrix.tqli(); |
222 |
|
} catch (Exception q) |
223 |
|
{ |
224 |
0 |
Console.error("Error computing PCA: " + q.getMessage()); |
225 |
0 |
q.printStackTrace(); |
226 |
|
} |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
@param |
234 |
|
@return |
235 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
236 |
0 |
protected PrintStream wrapOutputBuffer(StringBuilder sb)... |
237 |
|
{ |
238 |
0 |
PrintStream ps = new PrintStream(System.out) |
239 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
240 |
0 |
@Override... |
241 |
|
public void print(String x) |
242 |
|
{ |
243 |
0 |
sb.append(x); |
244 |
|
} |
245 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
246 |
0 |
@Override... |
247 |
|
public void println() |
248 |
|
{ |
249 |
0 |
sb.append("\n"); |
250 |
|
} |
251 |
|
}; |
252 |
0 |
return ps; |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
@return |
260 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
261 |
0 |
public int getHeight()... |
262 |
|
{ |
263 |
|
|
264 |
0 |
return pairwiseScores.height(); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
@return |
272 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
273 |
0 |
public MatrixI getPairwiseScores()... |
274 |
|
{ |
275 |
0 |
return pairwiseScores; |
276 |
|
} |
277 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
278 |
0 |
public void setPairwiseScores(MatrixI m)... |
279 |
|
{ |
280 |
0 |
pairwiseScores = m; |
281 |
|
} |
282 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
283 |
0 |
public MatrixI getEigenmatrix()... |
284 |
|
{ |
285 |
0 |
return eigenMatrix; |
286 |
|
} |
287 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
288 |
0 |
public void setEigenmatrix(MatrixI m)... |
289 |
|
{ |
290 |
0 |
eigenMatrix = m; |
291 |
|
} |
292 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
293 |
0 |
public MatrixI getTridiagonal()... |
294 |
|
{ |
295 |
0 |
return tridiagonal; |
296 |
|
} |
297 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
298 |
0 |
public void setTridiagonal(MatrixI tridiagonal)... |
299 |
|
{ |
300 |
0 |
this.tridiagonal = tridiagonal; |
301 |
|
} |
302 |
|
} |