Skip to content

Commit ed9e23e

Browse files
committed
added caching of profile vectors
git-svn-id: http://code.open-bio.org/repos/biojava/biojava-live/trunk@8077 7c6358e6-4a41-0410-a743-a5b2a554c398
1 parent 5a0bcf3 commit ed9e23e

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

biojava3-alignment/src/main/java/org/biojava3/alignment/template/AbstractProfileProfileAligner.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public abstract class AbstractProfileProfileAligner<S extends Sequence<C>, C ext
4242
// additional input fields
4343
private Profile<S, C> query, target;
4444

45+
// cached fields
46+
private List<C> cslist;
47+
private float[][] qfrac, tfrac;
48+
4549
// additional output field
4650
protected ProfilePair<S, C> pair;
4751

@@ -167,37 +171,47 @@ protected boolean alignReady() {
167171
return false;
168172
}
169173

170-
// scores alignment of two columns; TODO add caching of column compounds
174+
// scores alignment of two columns
171175
@Override
172176
protected short alignScoreColumns(int queryColumn, int targetColumn) {
173-
List<C> cslist = query.getCompoundSet().getAllCompounds();
174-
float[] qfrac = query.getCompoundWeightsAt(queryColumn, cslist),
175-
tfrac = target.getCompoundWeightsAt(targetColumn, cslist);
177+
return alignScoreVectors(qfrac[queryColumn - 1], tfrac[targetColumn - 1]);
178+
}
179+
180+
// scores alignment of two column vectors
181+
private short alignScoreVectors(float[] qv, float[] tv) {
176182
float score = 0.0f;
177-
for (int q = 0; q < qfrac.length; q++) {
178-
if (qfrac[q] > 0.0f) {
179-
for (int t = 0; t < tfrac.length; t++) {
180-
if (tfrac[t] > 0.0f) {
181-
score += qfrac[q]*tfrac[t]*getSubstitutionMatrix().getValue(cslist.get(q), cslist.get(t));
183+
for (int q = 0; q < qv.length; q++) {
184+
if (qv[q] > 0.0f) {
185+
for (int t = 0; t < tv.length; t++) {
186+
if (tv[t] > 0.0f) {
187+
score += qv[q]*tv[t]*getSubstitutionMatrix().getValue(cslist.get(q), cslist.get(t));
182188
}
183189
}
184190
}
185191
}
186192
return (short) Math.round(score);
187193
}
188194

189-
// resets output fields; TODO better bounds for max and min
195+
// resets output fields; caches profile vectors
190196
@Override
191197
protected void reset() {
192-
GapPenalty gapPenalty = getGapPenalty();
193-
SubstitutionMatrix<C> subMatrix = getSubstitutionMatrix();
194-
if (query != null && target != null && gapPenalty != null && subMatrix != null) {
195-
int subLength = Math.min(query.getLength(), target.getLength()), maxLength = query.getLength()
196-
+ target.getLength(), penalties = gapPenalty.getOpenPenalty() + gapPenalty.getExtensionPenalty(),
197-
sumSize = query.getSize() * target.getSize();
198-
max = (short) (subLength * subMatrix.getMaxValue() * sumSize);
199-
score = min = (short) (Math.min(subLength * subMatrix.getMinValue() + (maxLength - subLength) * penalties,
200-
maxLength * penalties) * sumSize);
198+
if (query != null && target != null && getGapPenalty() != null && getSubstitutionMatrix() != null &&
199+
query.getCompoundSet().equals(target.getCompoundSet())) {
200+
int maxq = 0, maxt = 0;
201+
cslist = query.getCompoundSet().getAllCompounds();
202+
qfrac = new float[query.getLength()][];
203+
for (int i = 0; i < qfrac.length; i++) {
204+
qfrac[i] = query.getCompoundWeightsAt(i + 1, cslist);
205+
maxq += alignScoreVectors(qfrac[i], qfrac[i]);
206+
}
207+
tfrac = new float[target.getLength()][];
208+
for (int i = 0; i < tfrac.length; i++) {
209+
tfrac[i] = target.getCompoundWeightsAt(i + 1, cslist);
210+
maxt += alignScoreVectors(tfrac[i], tfrac[i]);
211+
}
212+
max = (short) Math.max(maxq, maxt);
213+
score = min = (short) (2 * getGapPenalty().getOpenPenalty() + (query.getLength() + target.getLength()) *
214+
getGapPenalty().getExtensionPenalty());
201215
}
202216
scores = null;
203217
pair = null;

biojava3-alignment/src/test/java/org/biojava3/alignment/SimpleProfileProfileAlignerTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ public void testGetScoreMatrixAsString() {
144144
"L -5 -5 -6 -8 -7%n" +
145145
"K -6 -6 -5 -7 -6%n"));
146146
assertEquals(sppa3.getScoreMatrixAsString(), String.format(
147-
" - H I L K%n" +
148-
" 0 -3 -4 -5 -6 -7%n" +
149-
"A -3 4 1 0 -1 -2%n" +
150-
"R -4 1 4 1 0 3%n" +
151-
"N -5 0 5 3 1 0%n" +
152-
"D -6 -1 2 7 4 3%n"));
147+
" - H I L K%n" +
148+
" 0 -3 -4 -5 -6 -7%n" +
149+
"A -3 4 1 0 -1 -2%n" +
150+
"R -4 1 4 1 0 3%n" +
151+
"N -5 0 5 3 1 0%n" +
152+
"D -6 -1 2 7 4 3%n"));
153153
}
154154

155155
@Test
@@ -175,16 +175,16 @@ public void testGetProfile() {
175175

176176
@Test // TODO better bounds
177177
public void testGetMaxScore() {
178-
assertEquals(sppa1.getMaxScore(), 44);
179-
assertEquals(sppa2.getMaxScore(), 44);
180-
assertEquals(sppa3.getMaxScore(), 176);
178+
assertEquals(sppa1.getMaxScore(), 21);
179+
assertEquals(sppa2.getMaxScore(), 21);
180+
assertEquals(sppa3.getMaxScore(), 21);
181181
}
182182

183183
@Test // TODO better bounds
184184
public void testGetMinScore() {
185-
assertEquals(sppa1.getMinScore(), -28);
186-
assertEquals(sppa2.getMinScore(), -28);
187-
assertEquals(sppa3.getMinScore(), -124);
185+
assertEquals(sppa1.getMinScore(), -12);
186+
assertEquals(sppa2.getMinScore(), -12);
187+
assertEquals(sppa3.getMinScore(), -13);
188188
}
189189

190190
@Test // TODO fix last score

0 commit comments

Comments
 (0)