@@ -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 ;
0 commit comments