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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
#include "muscle.h"
#include "msa.h"
#include "seqvect.h"
#include "profile.h"
#include "tree.h"
// These global variables are a hack to allow the tree
// dependent iteration code to communicate the edge
// used to divide the tree. The three-way weighting
// scheme needs to know this edge in order to compute
// sequence weights.
static const Tree *g_ptrMuscleTree = 0;
unsigned g_uTreeSplitNode1 = NULL_NEIGHBOR;
unsigned g_uTreeSplitNode2 = NULL_NEIGHBOR;
void MSA::GetFractionalWeightedCounts(unsigned uColIndex, bool bNormalize,
FCOUNT fcCounts[], FCOUNT *ptrfcGapStart, FCOUNT *ptrfcGapEnd,
FCOUNT *ptrfcGapExtend, FCOUNT *ptrfOcc,
FCOUNT *ptrfcLL, FCOUNT *ptrfcLG, FCOUNT *ptrfcGL, FCOUNT *ptrfcGG) const
{
const unsigned uSeqCount = GetSeqCount();
const unsigned uColCount = GetColCount();
memset(fcCounts, 0, g_AlphaSize*sizeof(FCOUNT));
WEIGHT wTotal = 0;
FCOUNT fGap = 0;
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
{
const WEIGHT w = GetSeqWeight(uSeqIndex);
if (IsGap(uSeqIndex, uColIndex))
{
fGap += w;
continue;
}
else if (IsWildcard(uSeqIndex, uColIndex))
{
const unsigned uLetter = GetLetterEx(uSeqIndex, uColIndex);
switch (g_Alpha)
{
case ALPHA_Amino:
switch (uLetter)
{
case AX_B: // D or N
fcCounts[AX_D] += w/2;
fcCounts[AX_N] += w/2;
break;
case AX_Z: // E or Q
fcCounts[AX_E] += w/2;
fcCounts[AX_Q] += w/2;
break;
default: // any
{
const FCOUNT f = w/20;
for (unsigned uLetter = 0; uLetter < 20; ++uLetter)
fcCounts[uLetter] += f;
break;
}
}
break;
case ALPHA_DNA:
case ALPHA_RNA:
switch (uLetter)
{
case AX_R: // G or A
fcCounts[NX_G] += w/2;
fcCounts[NX_A] += w/2;
break;
case AX_Y: // C or T/U
fcCounts[NX_C] += w/2;
fcCounts[NX_T] += w/2;
break;
default: // any
const FCOUNT f = w/20;
for (unsigned uLetter = 0; uLetter < 4; ++uLetter)
fcCounts[uLetter] += f;
break;
}
break;
default:
Quit("Alphabet %d not supported", g_Alpha);
}
continue;
}
unsigned uLetter = GetLetter(uSeqIndex, uColIndex);
fcCounts[uLetter] += w;
wTotal += w;
}
*ptrfOcc = (float) (1.0 - fGap);
if (bNormalize && wTotal > 0)
{
if (wTotal > 1.001)
Quit("wTotal=%g\n", wTotal);
for (unsigned uLetter = 0; uLetter < g_AlphaSize; ++uLetter)
fcCounts[uLetter] /= wTotal;
// AssertNormalized(fcCounts);
}
FCOUNT fcStartCount = 0;
if (uColIndex == 0)
{
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
if (IsGap(uSeqIndex, uColIndex))
fcStartCount += GetSeqWeight(uSeqIndex);
}
else
{
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
if (IsGap(uSeqIndex, uColIndex) && !IsGap(uSeqIndex, uColIndex - 1))
fcStartCount += GetSeqWeight(uSeqIndex);
}
FCOUNT fcEndCount = 0;
if (uColCount - 1 == uColIndex)
{
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
if (IsGap(uSeqIndex, uColIndex))
fcEndCount += GetSeqWeight(uSeqIndex);
}
else
{
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
if (IsGap(uSeqIndex, uColIndex) && !IsGap(uSeqIndex, uColIndex + 1))
fcEndCount += GetSeqWeight(uSeqIndex);
}
FCOUNT LL = 0;
FCOUNT LG = 0;
FCOUNT GL = 0;
FCOUNT GG = 0;
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
{
WEIGHT w = GetSeqWeight(uSeqIndex);
bool bLetterHere = !IsGap(uSeqIndex, uColIndex);
bool bLetterPrev = (uColIndex == 0 || !IsGap(uSeqIndex, uColIndex - 1));
if (bLetterHere)
{
if (bLetterPrev)
LL += w;
else
GL += w;
}
else
{
if (bLetterPrev)
LG += w;
else
GG += w;
}
}
FCOUNT fcExtendCount = 0;
if (uColIndex > 0 && uColIndex < GetColCount() - 1)
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
if (IsGap(uSeqIndex, uColIndex) && IsGap(uSeqIndex, uColIndex - 1) &&
IsGap(uSeqIndex, uColIndex + 1))
fcExtendCount += GetSeqWeight(uSeqIndex);
*ptrfcLL = LL;
*ptrfcLG = LG;
*ptrfcGL = GL;
*ptrfcGG = GG;
*ptrfcGapStart = fcStartCount;
*ptrfcGapEnd = fcEndCount;
*ptrfcGapExtend = fcExtendCount;
}
// Return true if the given column has no gaps and all
// its residues are in the same biochemical group.
bool MSAColIsConservative(const MSA &msa, unsigned uColIndex)
{
extern unsigned ResidueGroup[];
const unsigned uSeqCount = msa.GetColCount();
if (0 == uSeqCount)
Quit("MSAColIsConservative: empty alignment");
if (msa.IsGap(0, uColIndex))
return false;
unsigned uLetter = msa.GetLetterEx(0, uColIndex);
const unsigned uGroup = ResidueGroup[uLetter];
for (unsigned uSeqIndex = 1; uSeqIndex < uSeqCount; ++uSeqIndex)
{
if (msa.IsGap(uSeqIndex, uColIndex))
return false;
uLetter = msa.GetLetter(uSeqIndex, uColIndex);
if (ResidueGroup[uLetter] != uGroup)
return false;
}
return true;
}
void MSAFromSeqRange(const MSA &msaIn, unsigned uFromSeqIndex, unsigned uSeqCount,
MSA &msaOut)
{
const unsigned uColCount = msaIn.GetColCount();
msaOut.SetSize(uSeqCount, uColCount);
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
{
const char *ptrName = msaIn.GetSeqName(uFromSeqIndex + uSeqIndex);
msaOut.SetSeqName(uSeqIndex, ptrName);
for (unsigned uColIndex = 0; uColIndex < uColCount; ++uColIndex)
{
const char c = msaIn.GetChar(uFromSeqIndex + uSeqIndex, uColIndex);
msaOut.SetChar(uSeqIndex, uColIndex, c);
}
}
}
void MSAFromColRange(const MSA &msaIn, unsigned uFromColIndex, unsigned uColCount,
MSA &msaOut)
{
const unsigned uSeqCount = msaIn.GetSeqCount();
const unsigned uInColCount = msaIn.GetColCount();
if (uFromColIndex + uColCount - 1 > uInColCount)
Quit("MSAFromColRange, out of bounds");
msaOut.SetSize(uSeqCount, uColCount);
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
{
const char *ptrName = msaIn.GetSeqName(uSeqIndex);
unsigned uId = msaIn.GetSeqId(uSeqIndex);
msaOut.SetSeqName(uSeqIndex, ptrName);
msaOut.SetSeqId(uSeqIndex, uId);
for (unsigned uColIndex = 0; uColIndex < uColCount; ++uColIndex)
{
const char c = msaIn.GetChar(uSeqIndex, uFromColIndex + uColIndex);
msaOut.SetChar(uSeqIndex, uColIndex, c);
}
}
}
void SeqVectFromMSA(const MSA &msa, SeqVect &v)
{
v.Clear();
const unsigned uSeqCount = msa.GetSeqCount();
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
{
Seq s;
msa.GetSeq(uSeqIndex, s);
s.StripGaps();
//if (0 == s.Length())
// continue;
const char *ptrName = msa.GetSeqName(uSeqIndex);
s.SetName(ptrName);
v.AppendSeq(s);
}
}
void DeleteGappedCols(MSA &msa)
{
unsigned uColIndex = 0;
for (;;)
{
if (uColIndex >= msa.GetColCount())
break;
if (msa.IsGapColumn(uColIndex))
msa.DeleteCol(uColIndex);
else
++uColIndex;
}
}
void MSAFromSeqSubset(const MSA &msaIn, const unsigned uSeqIndexes[], unsigned uSeqCount,
MSA &msaOut)
{
const unsigned uColCount = msaIn.GetColCount();
msaOut.SetSize(uSeqCount, uColCount);
for (unsigned uSeqIndexOut = 0; uSeqIndexOut < uSeqCount; ++uSeqIndexOut)
{
unsigned uSeqIndexIn = uSeqIndexes[uSeqIndexOut];
const char *ptrName = msaIn.GetSeqName(uSeqIndexIn);
unsigned uId = msaIn.GetSeqId(uSeqIndexIn);
msaOut.SetSeqName(uSeqIndexOut, ptrName);
msaOut.SetSeqId(uSeqIndexOut, uId);
for (unsigned uColIndex = 0; uColIndex < uColCount; ++uColIndex)
{
const char c = msaIn.GetChar(uSeqIndexIn, uColIndex);
msaOut.SetChar(uSeqIndexOut, uColIndex, c);
}
}
}
void AssertMSAEqIgnoreCaseAndGaps(const MSA &msa1, const MSA &msa2)
{
const unsigned uSeqCount1 = msa1.GetSeqCount();
const unsigned uSeqCount2 = msa2.GetSeqCount();
if (uSeqCount1 != uSeqCount2)
Quit("Seq count differs");
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount1; ++uSeqIndex)
{
Seq seq1;
msa1.GetSeq(uSeqIndex, seq1);
unsigned uId = msa1.GetSeqId(uSeqIndex);
unsigned uSeqIndex2 = msa2.GetSeqIndex(uId);
Seq seq2;
msa2.GetSeq(uSeqIndex2, seq2);
if (!seq1.EqIgnoreCaseAndGaps(seq2))
{
Log("Input:\n");
seq1.LogMe();
Log("Output:\n");
seq2.LogMe();
Quit("Seq %s differ ", msa1.GetSeqName(uSeqIndex));
}
}
}
void AssertMSAEq(const MSA &msa1, const MSA &msa2)
{
const unsigned uSeqCount1 = msa1.GetSeqCount();
const unsigned uSeqCount2 = msa2.GetSeqCount();
if (uSeqCount1 != uSeqCount2)
Quit("Seq count differs");
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount1; ++uSeqIndex)
{
Seq seq1;
msa1.GetSeq(uSeqIndex, seq1);
unsigned uId = msa1.GetSeqId(uSeqIndex);
unsigned uSeqIndex2 = msa2.GetSeqIndex(uId);
Seq seq2;
msa2.GetSeq(uSeqIndex2, seq2);
if (!seq1.Eq(seq2))
{
Log("Input:\n");
seq1.LogMe();
Log("Output:\n");
seq2.LogMe();
Quit("Seq %s differ ", msa1.GetSeqName(uSeqIndex));
}
}
}
void SetMSAWeightsMuscle(MSA &msa)
{
SEQWEIGHT Method = GetSeqWeightMethod();
switch (Method)
{
case SEQWEIGHT_None:
msa.SetUniformWeights();
return;
case SEQWEIGHT_Henikoff:
msa.SetHenikoffWeights();
return;
case SEQWEIGHT_HenikoffPB:
msa.SetHenikoffWeightsPB();
return;
case SEQWEIGHT_GSC:
msa.SetGSCWeights();
return;
case SEQWEIGHT_ClustalW:
SetClustalWWeightsMuscle(msa);
return;
case SEQWEIGHT_ThreeWay:
SetThreeWayWeightsMuscle(msa);
return;
}
Quit("SetMSAWeightsMuscle, Invalid method=%d", Method);
}
static WEIGHT *g_MuscleWeights;
static unsigned g_uMuscleIdCount;
WEIGHT GetMuscleSeqWeightById(unsigned uId)
{
if (0 == g_MuscleWeights)
Quit("g_MuscleWeights = 0");
if (uId >= g_uMuscleIdCount)
Quit("GetMuscleSeqWeightById(%u): count=%u",
uId, g_uMuscleIdCount);
return g_MuscleWeights[uId];
}
void SetMuscleTree(const Tree &tree)
{
g_ptrMuscleTree = &tree;
if (SEQWEIGHT_ClustalW != GetSeqWeightMethod())
return;
delete[] g_MuscleWeights;
const unsigned uLeafCount = tree.GetLeafCount();
g_uMuscleIdCount = uLeafCount;
g_MuscleWeights = new WEIGHT[uLeafCount];
CalcClustalWWeights(tree, g_MuscleWeights);
}
void SetClustalWWeightsMuscle(MSA &msa)
{
if (0 == g_MuscleWeights)
Quit("g_MuscleWeights = 0");
const unsigned uSeqCount = msa.GetSeqCount();
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
{
const unsigned uId = msa.GetSeqId(uSeqIndex);
if (uId >= g_uMuscleIdCount)
Quit("SetClustalWWeightsMuscle: id out of range");
msa.SetSeqWeight(uSeqIndex, g_MuscleWeights[uId]);
}
msa.NormalizeWeights((WEIGHT) 1.0);
}
#define LOCAL_VERBOSE 0
void SetThreeWayWeightsMuscle(MSA &msa)
{
if (NULL_NEIGHBOR == g_uTreeSplitNode1 || NULL_NEIGHBOR == g_uTreeSplitNode2)
{
msa.SetHenikoffWeightsPB();
return;
}
const unsigned uMuscleSeqCount = g_ptrMuscleTree->GetLeafCount();
WEIGHT *Weights = new WEIGHT[uMuscleSeqCount];
CalcThreeWayWeights(*g_ptrMuscleTree, g_uTreeSplitNode1, g_uTreeSplitNode2,
Weights);
const unsigned uMSASeqCount = msa.GetSeqCount();
for (unsigned uSeqIndex = 0; uSeqIndex < uMSASeqCount; ++uSeqIndex)
{
const unsigned uId = msa.GetSeqId(uSeqIndex);
if (uId >= uMuscleSeqCount)
Quit("SetThreeWayWeightsMuscle: id out of range");
msa.SetSeqWeight(uSeqIndex, Weights[uId]);
}
#if LOCAL_VERBOSE
{
Log("SetThreeWayWeightsMuscle\n");
for (unsigned n = 0; n < uMSASeqCount; ++n)
{
const unsigned uId = msa.GetSeqId(n);
Log("%20.20s %6.3f\n", msa.GetSeqName(n), Weights[uId]);
}
}
#endif
msa.NormalizeWeights((WEIGHT) 1.0);
delete[] Weights;
}
// Append msa2 at the end of msa1
void MSAAppend(MSA &msa1, const MSA &msa2)
{
const unsigned uSeqCount = msa1.GetSeqCount();
const unsigned uColCount1 = msa1.GetColCount();
const unsigned uColCount2 = msa2.GetColCount();
const unsigned uColCountCat = uColCount1 + uColCount2;
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
{
unsigned uId = msa1.GetSeqId(uSeqIndex);
unsigned uSeqIndex2 = msa2.GetSeqIndex(uId);
for (unsigned uColIndex = 0; uColIndex < uColCount2; ++uColIndex)
{
const char c = msa2.GetChar(uSeqIndex2, uColIndex);
msa1.SetChar(uSeqIndex, uColCount1 + uColIndex, c);
}
}
}
// "Catenate" two MSAs (by bad analogy with UNIX cat command).
// msa1 and msa2 must have same sequence names, but possibly
// in a different order.
// msaCat is the combined alignment produce by appending
// sequences in msa2 to sequences in msa1.
void MSACat(const MSA &msa1, const MSA &msa2, MSA &msaCat)
{
const unsigned uSeqCount = msa1.GetSeqCount();
const unsigned uColCount1 = msa1.GetColCount();
const unsigned uColCount2 = msa2.GetColCount();
const unsigned uColCountCat = uColCount1 + uColCount2;
msaCat.SetSize(uSeqCount, uColCountCat);
for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
{
for (unsigned uColIndex = 0; uColIndex < uColCount1; ++uColIndex)
{
const char c = msa1.GetChar(uSeqIndex, uColIndex);
msaCat.SetChar(uSeqIndex, uColIndex, c);
}
const char *ptrSeqName = msa1.GetSeqName(uSeqIndex);
unsigned uSeqIndex2;
msaCat.SetSeqName(uSeqIndex, ptrSeqName);
bool bFound = msa2.GetSeqIndex(ptrSeqName, &uSeqIndex2);
if (bFound)
{
for (unsigned uColIndex = 0; uColIndex < uColCount2; ++uColIndex)
{
const char c = msa2.GetChar(uSeqIndex2, uColIndex);
msaCat.SetChar(uSeqIndex, uColCount1 + uColIndex, c);
}
}
else
{
for (unsigned uColIndex = 0; uColIndex < uColCount2; ++uColIndex)
msaCat.SetChar(uSeqIndex, uColCount1 + uColIndex, '-');
}
}
}
|