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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/**
* Author: Mark Larkin
*
* Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sstream>
#include "ObjectiveScore.h"
#include "Alignment.h"
#include "../general/userparams.h"
#include "../general/debuglogObject.h"
namespace clustalw
{
ObjectiveScore::ObjectiveScore()
: score(0),
alignToScore(0),
scale(100000),
sagaGapEx(12),
sagaGapOp(8),
gapPos1(userParameters->getGapPos1()),
gapPos2(userParameters->getGapPos2())
{
}
long ObjectiveScore::getScore(const Alignment* alnToScore)
{
#if DEBUGFULL
if(logObject && DEBUGLOG)
{
logObject->logMsg("In getScore function");
}
#endif
alignToScore = alnToScore;
// If it doesnt point to any object return 0;
if(!alignToScore)
{
return 0;
}
int maxRes = subMatrix->getAlnScoreMatrix(matrix);
if (maxRes == 0)
{
utilityObject->error("Matrix for alignment scoring not found\n");
return 0;
}
const vector<int>* seqWeight = alignToScore->getSeqWeights();
vector<float> normalisedSeqWeights;
calcNormalisedSeqWeights(seqWeight, &normalisedSeqWeights);
int seq1, seq2;
float w1, w2 = 1.0;
float weight = 1.0;
score = 0;
float pwScoreLetters = 0, pwScoreGaps = 0, pwScore = 0;
float scoreTotal = 0.0;
int numSeqs = alignToScore->getNumSeqs();
int sizeNormalSeqWeight = normalisedSeqWeights.size();
for (seq1 = 1; seq1 <= numSeqs && seq1 <= sizeNormalSeqWeight; seq1++)
{
w1 = normalisedSeqWeights[seq1 - 1];
for (seq2 = seq1 + 1; seq2 <= numSeqs && seq2 <= sizeNormalSeqWeight; seq2++)
{
w2 = normalisedSeqWeights[seq2 - 1];
weight = w1 * w2;
pwScoreLetters = scoreLetters(seq1, seq2);
pwScoreGaps = scoreGaps(seq1, seq2);
pwScore = pwScoreLetters + pwScoreGaps;
scoreTotal += weight * pwScore;
#if DEBUGFULL
if(logObject && DEBUGLOG)
{
ostringstream outs;
outs << " weight = " << weight
<< " scoreLetters = " << pwScoreLetters << " scoreGaps = "
<< pwScoreGaps << " scorePair = "
<< pwScore << "\n"
<< "scoreTotal = " << scoreTotal << "\n";
logObject->logMsg(outs.str());
}
#endif
}
}
score = static_cast<long>(scoreTotal);
#if DEBUGFULL
if(logObject && DEBUGLOG)
{
ostringstream outs;
outs << " score = " << score;
logObject->logMsg(outs.str());
}
#endif
utilityObject->info("Alignment Score %d\n", score);
return score;
}
float ObjectiveScore::scoreLetters(int seq1, int seq2)
{
if(!alignToScore)
{
return 0;
}
const unsigned numColsSeq1 = alignToScore->getSeqLength(seq1);
const unsigned numColsSeq2 = alignToScore->getSeqLength(seq2);
if(numColsSeq1 != numColsSeq2)
{
return 0; // The sequences should be the same length after alignment.
}
float scoreLetters = 0;
unsigned colStart = 1;
bool gap1, gap2;
for(unsigned col = 1; col < numColsSeq1; col++)
{
gap1 = alignToScore->isGap(seq1, col);
gap2 = alignToScore->isGap(seq2, col);
if(!gap1 || !gap2)
{
colStart = col;
break;
}
}
unsigned colEnd = numColsSeq1;
for(unsigned col = numColsSeq1; col >= 1; --col)
{
gap1 = alignToScore->isGap(seq1, col);
gap2 = alignToScore->isGap(seq2, col);
if(!gap1 || !gap2)
{
colEnd = col;
break;
}
}
const SeqArray* seqArray = alignToScore->getSeqArray();
int scoreMatch = 0;
for(unsigned col = colStart; col <= colEnd; col++)
{
int res1 = (*seqArray)[seq1][col];
int res2 = (*seqArray)[seq2][col];
scoreMatch = matrix[res1][res2];
scoreLetters += scoreMatch;
}
return scoreLetters;
}
float ObjectiveScore::scoreGaps(int seq1, int seq2)
{
if(!alignToScore)
{
return 0;
}
const unsigned numColsSeq1 = alignToScore->getSeqLength(seq1);
const unsigned numColsSeq2 = alignToScore->getSeqLength(seq2);
if(numColsSeq1 != numColsSeq2)
{
return 0; // The sequences should be the same length after alignment.
}
unsigned colStart = 1;
bool gap1, gap2;
for(unsigned col = 1; col < numColsSeq1; col++)
{
gap1 = alignToScore->isGap(seq1, col);
gap2 = alignToScore->isGap(seq2, col);
if(!gap1 || !gap2)
{
colStart = col;
break;
}
}
unsigned colEnd = numColsSeq1;
for(unsigned col = numColsSeq1; col >= 1; --col)
{
gap1 = alignToScore->isGap(seq1, col);
gap2 = alignToScore->isGap(seq2, col);
if(!gap1 || !gap2)
{
colEnd = col;
break;
}
}
bool inGap1 = false;
bool inGap2 = false;
float gapOpen = userParameters->getGapOpen();
float gapExtend = userParameters->getGapExtend();
float scoreGaps = 0;
for(unsigned col = colStart; col <= colEnd; col++)
{
gap1 = alignToScore->isGap(seq1, col);
gap2 = alignToScore->isGap(seq2, col);
if(gap1 && gap2)
{
continue;
}
if(gap1)
{
if(!inGap1)
{
// NOTE I left out the option of having different end gaps stuff.
scoreGaps += gapOpen;
inGap1 = true; // Opening a gap in seq1
}
else
{
// Already in a gap
scoreGaps += gapExtend;
}
continue;
}
else if(gap2)
{
if(!inGap2)
{
// NOTE I left out the option of having different end gaps stuff.
scoreGaps += gapOpen;
inGap2 = true; // Opening a gap in seq2
}
else
{
// Already in a gap
scoreGaps += gapExtend;
}
continue;
}
inGap1 = inGap2 = false;
}
return scoreGaps;
}
void ObjectiveScore::calcNormalisedSeqWeights(const vector<int>* seqWeight,
vector<float>* normSeqWeight)
{
if(!seqWeight || !normSeqWeight)
{
return;
}
int sumWeights = 0;
for(int i = 0; i < (int)seqWeight->size() - 1; i++)
{
sumWeights += (*seqWeight)[i];
}
normSeqWeight->resize(seqWeight->size());
for(int i = 0; i < (int)seqWeight->size() - 1; i++)
{
(*normSeqWeight)[i] = static_cast<float>((*seqWeight)[i]) /
static_cast<float>(sumWeights);
#if DEBUGFULL
if(logObject && DEBUGLOG)
{
ostringstream outs;
outs << " normSeqWeight[i] = " << (*normSeqWeight)[i];
logObject->logMsg(outs.str());
}
#endif
}
}
}
|