Repository navigation
Expand file tree
/
Copy pathSubstitutionMatrixScorer.java
More file actions
108 lines (97 loc) · 2.9 KB
/
Copy pathSubstitutionMatrixScorer.java
File metadata and controls
108 lines (97 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
*/
package org.biojava.nbio.alignment;
import org.biojava.nbio.alignment.template.AbstractScorer;
import org.biojava.nbio.alignment.template.PairwiseSequenceScorer;
import org.biojava.nbio.core.alignment.template.SequencePair;
import org.biojava.nbio.core.alignment.template.SubstitutionMatrix;
import org.biojava.nbio.core.sequence.template.Compound;
import org.biojava.nbio.core.sequence.template.Sequence;
/**
* Scores using a substitution matrix. Specifically, the score is the sum of the substitution matrix entries
* corresponding to the alignment. Gaps are scored according to the substitution matrix, just as matches and mismatches.
* @author dmyersturnbull
*
* @param <S>
* @param <C>
*/
public class SubstitutionMatrixScorer<S extends Sequence<C>, C extends Compound> extends AbstractScorer
implements PairwiseSequenceScorer<S, C> {
private final SubstitutionMatrix<C> matrix;
private S query;
private S target;
private double score;
public SubstitutionMatrixScorer(SequencePair<S, C> pair, SubstitutionMatrix<C> matrix) {
super();
this.query = pair.getQuery().getOriginalSequence();
this.target = pair.getTarget().getOriginalSequence();
this.matrix = matrix;
for (int i = 1; i <= pair.getLength(); i++) {
C query = pair.getCompoundAt(1, i);
C target = pair.getCompoundAt(2, i);
score += matrix.getValue(query, target);
}
}
/**
* @return The maximum score the query could be assigned when aligned against any target sequence.
*/
@Override
public double getMaxScore() {
// assume nothing about the matrix
double score = 0;
for (C queryC : query.getAsList()) {
short max = Short.MIN_VALUE;
for (Short value : matrix.getRow(queryC).values()) {
if (value > max) max = value;
}
score += max;
}
return score;
}
/**
* @return The minimum score the query could be assigned when aligned against any target sequence.
*/
@Override
public double getMinScore() {
// assume nothing about the matrix
double score = 0;
for (C queryC : query.getAsList()) {
short min = Short.MAX_VALUE;
for (Short value : matrix.getRow(queryC).values()) {
if (value < min) min = value;
}
score += min;
}
return score;
}
@Override
public double getScore() {
return score;
}
@Override
public S getQuery() {
return query;
}
@Override
public S getTarget() {
return target;
}
}