1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.analysis.scoremodels; |
22 |
|
|
23 |
|
import jalview.api.AlignmentViewPanel; |
24 |
|
import jalview.api.analysis.PairwiseScoreModelI; |
25 |
|
import jalview.api.analysis.ScoreModelI; |
26 |
|
import jalview.api.analysis.SimilarityParamsI; |
27 |
|
import jalview.datamodel.AlignmentView; |
28 |
|
import jalview.math.Matrix; |
29 |
|
import jalview.math.MatrixI; |
30 |
|
import jalview.util.Comparison; |
31 |
|
|
32 |
|
import java.util.Arrays; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
|
|
| 93.7% |
Uncovered Elements: 14 (223) |
Complexity: 67 |
Complexity Density: 0.54 |
|
39 |
|
public class ScoreMatrix extends SimilarityScoreModel |
40 |
|
implements PairwiseScoreModelI |
41 |
|
{ |
42 |
|
private static final char GAP_CHARACTER = Comparison.GAP_DASH; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
private static final int UNKNOWN_IDENTITY_SCORE = 1; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private static boolean scoreGapAsAny = false; |
58 |
|
|
59 |
|
public static final short UNMAPPED = (short) -1; |
60 |
|
|
61 |
|
private static final String BAD_ASCII_ERROR = "Unexpected character %s in getPairwiseScore"; |
62 |
|
|
63 |
|
private static final int MAX_ASCII = 127; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private String name; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
private String description; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
private char[] symbols; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private float[][] matrix; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
private short[] symbolIndex; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
private boolean peptide; |
97 |
|
|
98 |
|
private float minValue; |
99 |
|
|
100 |
|
private float maxValue; |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
@param |
108 |
|
|
109 |
|
@param |
110 |
|
|
111 |
|
@param |
112 |
|
|
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
14 |
public ScoreMatrix(String theName, char[] alphabet, float[][] values)... |
115 |
|
{ |
116 |
14 |
this(theName, null, alphabet, values); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
@param |
125 |
|
|
126 |
|
@param |
127 |
|
|
128 |
|
@param |
129 |
|
|
130 |
|
@param |
131 |
|
|
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
|
133 |
19 |
public ScoreMatrix(String theName, String theDescription, char[] alphabet,... |
134 |
|
float[][] values) |
135 |
|
{ |
136 |
19 |
if (alphabet.length != values.length) |
137 |
|
{ |
138 |
2 |
throw new IllegalArgumentException( |
139 |
|
"score matrix size must match alphabet size"); |
140 |
|
} |
141 |
17 |
for (float[] row : values) |
142 |
|
{ |
143 |
157 |
if (row.length != alphabet.length) |
144 |
|
{ |
145 |
1 |
throw new IllegalArgumentException( |
146 |
|
"score matrix size must be square"); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
16 |
this.matrix = values; |
151 |
16 |
this.name = theName; |
152 |
16 |
this.description = theDescription; |
153 |
16 |
this.symbols = alphabet; |
154 |
|
|
155 |
16 |
symbolIndex = buildSymbolIndex(alphabet); |
156 |
|
|
157 |
16 |
findMinMax(); |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
16 |
peptide = alphabet.length >= 20; |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
168 |
16 |
protected void findMinMax()... |
169 |
|
{ |
170 |
16 |
float min = Float.MAX_VALUE; |
171 |
16 |
float max = -Float.MAX_VALUE; |
172 |
16 |
if (matrix != null) |
173 |
|
{ |
174 |
16 |
for (float[] row : matrix) |
175 |
|
{ |
176 |
155 |
if (row != null) |
177 |
|
{ |
178 |
155 |
for (float f : row) |
179 |
|
{ |
180 |
2917 |
min = Math.min(min, f); |
181 |
2917 |
max = Math.max(max, f); |
182 |
|
} |
183 |
|
} |
184 |
|
} |
185 |
|
} |
186 |
16 |
minValue = min; |
187 |
16 |
maxValue = max; |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
@param |
205 |
|
@return |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 5 |
Complexity Density: 0.42 |
|
207 |
18 |
short[] buildSymbolIndex(char[] alphabet)... |
208 |
|
{ |
209 |
18 |
short[] index = new short[MAX_ASCII + 1]; |
210 |
18 |
Arrays.fill(index, UNMAPPED); |
211 |
18 |
short pos = 0; |
212 |
18 |
for (char c : alphabet) |
213 |
|
{ |
214 |
165 |
if (c <= MAX_ASCII) |
215 |
|
{ |
216 |
163 |
index[c] = pos; |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
165 |
if (c >= 'A' && c <= 'Z') |
223 |
|
{ |
224 |
148 |
short lowerCase = (short) (c + ('a' - 'A')); |
225 |
148 |
if (index[lowerCase] == UNMAPPED) |
226 |
|
{ |
227 |
147 |
index[lowerCase] = pos; |
228 |
|
} |
229 |
|
} |
230 |
165 |
pos++; |
231 |
|
} |
232 |
18 |
return index; |
233 |
|
} |
234 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
235 |
1291 |
@Override... |
236 |
|
public String getName() |
237 |
|
{ |
238 |
1291 |
return name; |
239 |
|
} |
240 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
241 |
4 |
@Override... |
242 |
|
public String getDescription() |
243 |
|
{ |
244 |
4 |
return description; |
245 |
|
} |
246 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
247 |
3 |
@Override... |
248 |
|
public boolean isDNA() |
249 |
|
{ |
250 |
3 |
return !peptide; |
251 |
|
} |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
253 |
3 |
@Override... |
254 |
|
public boolean isProtein() |
255 |
|
{ |
256 |
3 |
return peptide; |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
@return |
266 |
|
@see |
267 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
268 |
483 |
public float[][] getMatrix()... |
269 |
|
{ |
270 |
483 |
float[][] v = new float[matrix.length][matrix.length]; |
271 |
12019 |
for (int i = 0; i < matrix.length; i++) |
272 |
|
{ |
273 |
11536 |
v[i] = Arrays.copyOf(matrix[i], matrix[i].length); |
274 |
|
} |
275 |
483 |
return v; |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
@param |
284 |
|
@return |
285 |
|
@see |
286 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
287 |
720541 |
public int getMatrixIndex(char c)... |
288 |
|
{ |
289 |
720546 |
if (c < symbolIndex.length) |
290 |
|
{ |
291 |
720547 |
return symbolIndex[c]; |
292 |
|
} |
293 |
|
else |
294 |
|
{ |
295 |
1 |
return UNMAPPED; |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 6 |
Complexity Density: 0.55 |
|
304 |
6211636 |
@Override... |
305 |
|
public float getPairwiseScore(char c, char d) |
306 |
|
{ |
307 |
6211636 |
if (c >= symbolIndex.length) |
308 |
|
{ |
309 |
1 |
System.err.println(String.format(BAD_ASCII_ERROR, c)); |
310 |
1 |
return 0; |
311 |
|
} |
312 |
6211635 |
if (d >= symbolIndex.length) |
313 |
|
{ |
314 |
2 |
System.err.println(String.format(BAD_ASCII_ERROR, d)); |
315 |
2 |
return 0; |
316 |
|
} |
317 |
|
|
318 |
6211633 |
int cIndex = symbolIndex[c]; |
319 |
6211633 |
int dIndex = symbolIndex[d]; |
320 |
6211633 |
if (cIndex != UNMAPPED && dIndex != UNMAPPED) |
321 |
|
{ |
322 |
6211588 |
return matrix[cIndex][dIndex]; |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
45 |
return c == d ? UNKNOWN_IDENTITY_SCORE : getMinimumScore(); |
332 |
|
} |
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
337 |
0 |
@Override... |
338 |
|
public String toString() |
339 |
|
{ |
340 |
0 |
return outputMatrix(false); |
341 |
|
} |
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
@param |
351 |
|
@return |
352 |
|
|
|
|
| 97.5% |
Uncovered Elements: 1 (40) |
Complexity: 11 |
Complexity Density: 0.55 |
|
353 |
2 |
public String outputMatrix(boolean html)... |
354 |
|
{ |
355 |
2 |
StringBuilder sb = new StringBuilder(512); |
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
2 |
if (html) |
361 |
|
{ |
362 |
1 |
sb.append("<table border=\"1\">"); |
363 |
1 |
sb.append(html ? "<tr><th></th>" : ""); |
364 |
|
} |
365 |
|
else |
366 |
|
{ |
367 |
1 |
sb.append("ScoreMatrix ").append(getName()).append("\n"); |
368 |
|
} |
369 |
2 |
for (char sym : symbols) |
370 |
|
{ |
371 |
26 |
if (html) |
372 |
|
{ |
373 |
2 |
sb.append("<th> ").append(sym).append(" </th>"); |
374 |
|
} |
375 |
|
else |
376 |
|
{ |
377 |
24 |
sb.append("\t").append(sym); |
378 |
|
} |
379 |
|
} |
380 |
2 |
sb.append(html ? "</tr>\n" : "\n"); |
381 |
|
|
382 |
|
|
383 |
|
|
384 |
|
|
385 |
2 |
for (char c1 : symbols) |
386 |
|
{ |
387 |
26 |
if (html) |
388 |
|
{ |
389 |
2 |
sb.append("<tr><td>"); |
390 |
|
} |
391 |
26 |
sb.append(c1).append(html ? "</td>" : ""); |
392 |
26 |
for (char c2 : symbols) |
393 |
|
{ |
394 |
580 |
sb.append(html ? "<td>" : "\t") |
395 |
|
.append(matrix[symbolIndex[c1]][symbolIndex[c2]]) |
396 |
580 |
.append(html ? "</td>" : ""); |
397 |
|
} |
398 |
26 |
sb.append(html ? "</tr>\n" : "\n"); |
399 |
|
} |
400 |
2 |
if (html) |
401 |
|
{ |
402 |
1 |
sb.append("</table>"); |
403 |
|
} |
404 |
2 |
return sb.toString(); |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
|
411 |
|
@return |
412 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
413 |
483 |
public int getSize()... |
414 |
|
{ |
415 |
483 |
return symbols.length; |
416 |
|
} |
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
|
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 1 |
|
439 |
0 |
@Override... |
440 |
|
public MatrixI findSimilarities(AlignmentView seqstrings, |
441 |
|
SimilarityParamsI options) |
442 |
|
{ |
443 |
0 |
char gapChar = scoreGapAsAny ? (seqstrings.isNa() ? 'N' : 'X') |
444 |
|
: GAP_CHARACTER; |
445 |
0 |
String[] seqs = seqstrings.getSequenceStrings(gapChar); |
446 |
0 |
return findSimilarities(seqs, options); |
447 |
|
} |
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
|
453 |
|
@param |
454 |
|
@param |
455 |
|
@return |
456 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
457 |
1 |
protected MatrixI findSimilarities(String[] seqs,... |
458 |
|
SimilarityParamsI params) |
459 |
|
{ |
460 |
1 |
double[][] values = new double[seqs.length][]; |
461 |
5 |
for (int row = 0; row < seqs.length; row++) |
462 |
|
{ |
463 |
4 |
values[row] = new double[seqs.length]; |
464 |
20 |
for (int col = 0; col < seqs.length; col++) |
465 |
|
{ |
466 |
16 |
double total = computeSimilarity(seqs[row], seqs[col], params); |
467 |
16 |
values[row][col] = total; |
468 |
|
} |
469 |
|
} |
470 |
1 |
return new Matrix(values); |
471 |
|
} |
472 |
|
|
473 |
|
|
474 |
|
|
475 |
|
|
476 |
|
|
477 |
|
@param |
478 |
|
@param |
479 |
|
@param |
480 |
|
@return |
481 |
|
|
|
|
| 97.4% |
Uncovered Elements: 1 (39) |
Complexity: 13 |
Complexity Density: 0.62 |
|
482 |
32 |
protected double computeSimilarity(String seq1, String seq2,... |
483 |
|
SimilarityParamsI params) |
484 |
|
{ |
485 |
32 |
int len1 = seq1.length(); |
486 |
32 |
int len2 = seq2.length(); |
487 |
32 |
double total = 0; |
488 |
|
|
489 |
32 |
int width = Math.max(len1, len2); |
490 |
168 |
for (int i = 0; i < width; i++) |
491 |
|
{ |
492 |
144 |
if (i >= len1 || i >= len2) |
493 |
|
{ |
494 |
|
|
495 |
|
|
496 |
|
|
497 |
|
|
498 |
16 |
if (params.denominateByShortestLength()) |
499 |
|
{ |
500 |
8 |
break; |
501 |
|
} |
502 |
|
} |
503 |
|
|
504 |
136 |
char c1 = i >= len1 ? GAP_CHARACTER : seq1.charAt(i); |
505 |
136 |
char c2 = i >= len2 ? GAP_CHARACTER : seq2.charAt(i); |
506 |
136 |
boolean gap1 = Comparison.isGap(c1); |
507 |
136 |
boolean gap2 = Comparison.isGap(c2); |
508 |
|
|
509 |
136 |
if (gap1 && gap2) |
510 |
|
{ |
511 |
|
|
512 |
|
|
513 |
|
|
514 |
17 |
if (!params.includeGappedColumns()) |
515 |
|
{ |
516 |
8 |
continue; |
517 |
|
} |
518 |
|
} |
519 |
119 |
else if (gap1 || gap2) |
520 |
|
{ |
521 |
|
|
522 |
|
|
523 |
|
|
524 |
46 |
if (!params.includeGaps()) |
525 |
|
{ |
526 |
20 |
continue; |
527 |
|
} |
528 |
|
} |
529 |
108 |
float score = getPairwiseScore(c1, c2); |
530 |
108 |
total += score; |
531 |
|
} |
532 |
32 |
return total; |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
|
|
537 |
|
|
538 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
539 |
4 |
@Override... |
540 |
|
public int hashCode() |
541 |
|
{ |
542 |
4 |
int hs = Arrays.hashCode(symbols); |
543 |
4 |
for (float[] row : matrix) |
544 |
|
{ |
545 |
96 |
hs = hs * 31 + Arrays.hashCode(row); |
546 |
|
} |
547 |
4 |
return hs; |
548 |
|
} |
549 |
|
|
550 |
|
|
551 |
|
|
552 |
|
|
553 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
554 |
4 |
@Override... |
555 |
|
public boolean equals(Object obj) |
556 |
|
{ |
557 |
4 |
if (!(obj instanceof ScoreMatrix)) |
558 |
|
{ |
559 |
1 |
return false; |
560 |
|
} |
561 |
3 |
ScoreMatrix sm = (ScoreMatrix) obj; |
562 |
3 |
if (Arrays.equals(symbols, sm.symbols) |
563 |
|
&& Arrays.deepEquals(matrix, sm.matrix)) |
564 |
|
{ |
565 |
2 |
return true; |
566 |
|
} |
567 |
1 |
return false; |
568 |
|
} |
569 |
|
|
570 |
|
|
571 |
|
|
572 |
|
|
573 |
|
@return |
574 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
575 |
1 |
String getSymbols()... |
576 |
|
{ |
577 |
1 |
return new String(symbols); |
578 |
|
} |
579 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
580 |
4431317 |
public float getMinimumScore()... |
581 |
|
{ |
582 |
4431488 |
return minValue; |
583 |
|
} |
584 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
585 |
1 |
public float getMaximumScore()... |
586 |
|
{ |
587 |
1 |
return maxValue; |
588 |
|
} |
589 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
590 |
1 |
@Override... |
591 |
|
public ScoreModelI getInstance(AlignmentViewPanel avp) |
592 |
|
{ |
593 |
1 |
return this; |
594 |
|
} |
595 |
|
} |