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
|
/**
* Author: Mark Larkin
*
* Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "UnRootedClusterTree.h"
#include "../general/utils.h"
#include "RandomGenerator.h"
#include <math.h>
#include <sstream>
#include "../general/OutputFile.h"
namespace clustalw
{
UnRootedClusterTree::UnRootedClusterTree()
{
}
/*
* The function treeFromAlignment is called to generate a tree from an alignment
* without a distance matrix. It calculates the distance matrix from the alignment
* quickly.
*/
// Note this function was called phylogenetic_tree before
void UnRootedClusterTree::treeFromAlignment(TreeNames* treeNames, Alignment *alignPtr)
{
try
{
OutputFile phylipPhyTreeFile;
OutputFile clustalPhyTreeFile;
OutputFile distancesPhyTreeFile;
OutputFile nexusPhyTreeFile;
OutputFile pimFile;
string path;
int i, j;
int overspill = 0;
int totalDists;
numSeqs = alignPtr->getNumSeqs(); // NOTE class variable
/**
* Check if numSeqs is ok
*/
if(!checkIfConditionsMet(numSeqs, 2))
{
return;
}
firstSeq = 1;
lastSeq = numSeqs;
phyloTree = new PhyloTree;
// The SeqInfo struct is passed to reduce the number of parameters passed!
SeqInfo info;
info.firstSeq = firstSeq;
info.lastSeq = lastSeq;
info.numSeqs = numSeqs;
outputTree = new ClusterTreeOutput(&info, 0); // No bootstrap!
phyloTree->treeDesc.resize(numSeqs + 1, vector<int>(numSeqs + 1));
TreeGroups saveTree(numSeqs + 1, vector<int>(numSeqs + 1));
// NOTE at the moment there is only one type of clustering algorithm, but there will
// be more!
clusAlgorithm = new NJTree();
utilityObject->getPath(userParameters->getSeqName(), &path);
/**
* Open the required output files.
*/
if(!openFilesForTreeFromAlignment(&clustalPhyTreeFile, &phylipPhyTreeFile,
&distancesPhyTreeFile, &nexusPhyTreeFile, &pimFile, treeNames, &path))
{
return; // Problem opeing one of the files, cannot continue!
}
int _lenFirstSeq = alignPtr->getSeqLength(firstSeq);
bootPositions.resize(_lenFirstSeq + 2);
for (j = 1; j <= _lenFirstSeq; ++j)
{
bootPositions[j] = j;
}
/**
* Calculate quickDist and overspill
*/
overspill = calcQuickDistMatForAll(clustalPhyTreeFile.getPtrToFile(),
phylipPhyTreeFile.getPtrToFile(), nexusPhyTreeFile.getPtrToFile(),
pimFile.getPtrToFile(), distancesPhyTreeFile.getPtrToFile(), alignPtr);
// check if any distances overflowed the distance corrections
if (overspill > 0)
{
totalDists = (numSeqs *(numSeqs - 1)) / 2;
overspillMessage(overspill, totalDists);
}
if (userParameters->getOutputTreeClustal())
{
verbose = true;
}
// Turn on file output
if (userParameters->getOutputTreeClustal() || userParameters->getOutputTreePhylip()
|| userParameters->getOutputTreeNexus())
{
clusAlgorithm->setVerbose(true);
clusAlgorithm->generateTree(phyloTree, quickDistMat.get(), &info,
clustalPhyTreeFile.getPtrToFile());
clusAlgorithm->setVerbose(false);
}
for (i = 1; i < numSeqs + 1; i++)
for (j = 1; j < numSeqs + 1; j++)
{
saveTree[i][j] = phyloTree->treeDesc[i][j];
}
if (userParameters->getOutputTreePhylip())
{
outputTree->printPhylipTree(phyloTree, phylipPhyTreeFile.getPtrToFile(), alignPtr,
quickDistMat.get(), &bootTotals);
}
for (i = 1; i < numSeqs + 1; i++)
for (j = 1; j < numSeqs + 1; j++)
{
phyloTree->treeDesc[i][j] = saveTree[i][j];
}
if (userParameters->getOutputTreeNexus())
{
outputTree->printNexusTree(phyloTree, nexusPhyTreeFile.getPtrToFile(), alignPtr,
quickDistMat.get(), &bootTotals);
}
/** Free up resources!!!!! */
treeGaps.clear();
bootPositions.clear();
delete clusAlgorithm;
delete phyloTree;
delete outputTree;
}
catch(const exception& ex)
{
cerr << ex.what() << endl;
utilityObject->error("Terminating program. Cannot continue\n");
exit(1);
}
}
/*
* Routine for producing unrooted NJ trees from seperately aligned
* pairwise distances. This produces the GUIDE DENDROGRAMS in
* PHYLIP format.
*/
void UnRootedClusterTree::treeFromDistMatrix(DistMatrix* distMat,
Alignment *alignPtr, int seq1, int nSeqs,
string& phylipName)
{
OutputFile phylipPhyTreeFile;
try
{
// Test to see if the inputs are valid
if(seq1 < 1 || nSeqs < 1)
{
cerr << "Invalid inputs into treeFromDistMatrix \n"
<< "seq1 = " << seq1 << " nSeqs = " << nSeqs << "\n"
<< "Need to end program!\n";
exit(1);
return;
}
PhyloTree phyloTree;
float dist;
string path;
verbose = false;
firstSeq = seq1;
lastSeq = firstSeq + nSeqs - 1;
SeqInfo info;
info.firstSeq = firstSeq;
info.lastSeq = lastSeq;
info.numSeqs = nSeqs;
// Open up the phylipPhyTreeFile now!
// NOTE that this is not exactly correct. This may cause a problem when outputing
// a tree for each of the profiles. But then we can pass it in, maybe.
utilityObject->getPath(userParameters->getSeqName(), &path);
if(nSeqs >= 2)
{
string name = phylipName;
if(!phylipPhyTreeFile.openFile(&name,
"\nEnter name for new GUIDE TREE file ", &path, "dnd",
"Guide tree"))
{
return;
}
phylipName = name;
}
else
{
return;
}
// Not sure about bootstrapping here!
clusAlgorithm = new NJTree();
outputTree = new ClusterTreeOutput(&info, 0);
ofstream* ptrToFile = phylipPhyTreeFile.getPtrToFile();
if (nSeqs == 2)
{
dist = (*distMat)(firstSeq, firstSeq + 1) / 2.0;
if(ptrToFile->is_open())
{
(*ptrToFile) << "(" << alignPtr->getName(firstSeq) << ":"
<< setprecision(5)
<< dist << "," << alignPtr->getName(firstSeq + 1) << ":"
<< setprecision(5) << dist <<");\n";
}
}
else
{
int dimensions = lastSeq - firstSeq + 2;
phyloTree.treeDesc.resize(dimensions, vector<int>(dimensions));
/* debugging, also used when -OUTPUTTREE is used */
#if 0
ofstream debuglog("debug.treeFromDistMatrix.txt", ios::out);
clusAlgorithm->setVerbose(true);
clusAlgorithm->generateTree(&phyloTree, distMat, &info, &debuglog);
#else
clusAlgorithm->generateTree(&phyloTree, distMat, &info);
#endif
outputTree->printPhylipTree(&phyloTree, ptrToFile, alignPtr,
distMat, &bootTotals);
}
delete clusAlgorithm;
delete outputTree;
//phylipPhyTreeFile.close();
}
catch(const exception &ex)
{
cerr << "ERROR: Error has occurred in treeFromDistMatrix. "
<< "Need to terminate program.\n"
<< ex.what();
exit(1);
}
catch(...)
{
cerr << "ERROR: Error has occurred in treeFromDistMatrix. "
<< "Need to terminate program.\n";
exit(1);
}
}
/*
* Note before I had this function was accepting a distance matrix. But I think it
* just uses the quickly generated one. So it doesnt need it anymore. But be careful !!!!
*/
void UnRootedClusterTree::bootstrapTree(TreeNames* treeNames, Alignment *alignPtr)
{
int i, j;
int ranno;
string path;
OutputFile clustalPhyTreeFile;
ofstream* ptrToClustalFile;
OutputFile phylipPhyTreeFile;
OutputFile nexusPhyTreeFile;
try
{
phyloTree = new PhyloTree;
PhyloTree sampleTree;
PhyloTree standardTree;
PhyloTree saveTree;
int totalDists, overspill = 0, totalOverspill = 0;
int nfails = 0;
numSeqs = alignPtr->getNumSeqs();
firstSeq = 1;
lastSeq = numSeqs;
clusAlgorithm = new NJTree();
SeqInfo info;
info.firstSeq = firstSeq;
info.lastSeq = lastSeq;
info.numSeqs = numSeqs;
/**
* Check if numSeqs is ok
*/
if(!checkIfConditionsMet(numSeqs, 4))
{
return;
}
if (!userParameters->getOutputTreeClustal() && !userParameters->getOutputTreePhylip()
&& !userParameters->getOutputTreeNexus())
{
utilityObject->error("You must select either clustal or phylip or nexus tree output format");
return;
}
utilityObject->getPath(userParameters->getSeqName(), &path);
if(!openFilesForBootstrap(&clustalPhyTreeFile, &phylipPhyTreeFile, &nexusPhyTreeFile,
treeNames, &path))
{
return; // There was a problem opening the output files.
}
int _lenFirstSeq = alignPtr->getSeqLength(firstSeq);
bootTotals.clear();
bootTotals.resize(numSeqs + 1);
bootPositions.clear();
bootPositions.resize(_lenFirstSeq + 2);
for (j = 1; j <= _lenFirstSeq; ++j)
// First select all positions for
{
bootPositions[j] = j;
}
// the "standard" tree
overspill = calcQuickDistMatForSubSet(clustalPhyTreeFile.getPtrToFile(),
phylipPhyTreeFile.getPtrToFile(), nexusPhyTreeFile.getPtrToFile(), alignPtr);
// check if any distances overflowed the distance corrections
if (overspill > 0)
{
totalDists = (numSeqs *(numSeqs - 1)) / 2;
overspillMessage(overspill, totalDists);
}
treeGaps.clear();
if (userParameters->getOutputTreeClustal())
{
verbose = true;
}
// Turn on screen output
phyloTree->treeDesc.resize(numSeqs + 1, vector<int>(numSeqs + 1));
// compute the standard tree
if (userParameters->getOutputTreeClustal() || userParameters->getOutputTreePhylip() ||
userParameters->getOutputTreeNexus())
{
clusAlgorithm->setVerbose(true);
clusAlgorithm->generateTree(phyloTree, quickDistMat.get(), &info,
clustalPhyTreeFile.getPtrToFile());
}
ptrToClustalFile = clustalPhyTreeFile.getPtrToFile();
promptForBoolSeedAndNumTrials();
RandomGenerator randGenerator(userParameters->getBootRanSeed());
/**
* Print bootstrap information to top of clustal bootstrap file!
*/
if (userParameters->getOutputTreeClustal())
{
printBootstrapHeaderToClustalFile(ptrToClustalFile);
}
verbose = false; // Turn OFF screen output
clusAlgorithm->setVerbose(false);
sampleTree.treeDesc.resize(numSeqs + 1, vector<int>(numSeqs + 1));
if (userParameters->getMenuFlag())
{
cout << "\n\nEach dot represents 10 trials\n\n";
}
totalOverspill = 0;
nfails = 0;
int lenSeq1 = alignPtr->getSeqLength(1);
for (i = 1; i <= userParameters->getBootNumTrials(); ++i)
{
for (j = 1; j <= alignPtr->getSeqLength(firstSeq); ++j)
{
// select alignment positions for
ranno = randGenerator.addRand((unsigned long)lenSeq1) + 1;
bootPositions[j] = ranno; // bootstrap sample
}
overspill = calcQuickDistMatForSubSet(clustalPhyTreeFile.getPtrToFile(),
phylipPhyTreeFile.getPtrToFile(), nexusPhyTreeFile.getPtrToFile(), alignPtr,
true);
if (overspill > 0)
{
totalOverspill = totalOverspill + overspill;
nfails++;
}
treeGaps.clear();
if (userParameters->getOutputTreeClustal() ||
userParameters->getOutputTreePhylip() || userParameters->getOutputTreeNexus())
{
// NOTE this is bad to pass in clustalPhyTreeFile
clusAlgorithm->generateTree(&sampleTree, quickDistMat.get(), &info,
clustalPhyTreeFile.getPtrToFile());
}
sampleTree.leftBranch.clear();
sampleTree.rightBranch.clear();
compareTree(phyloTree, &sampleTree, &bootTotals, lastSeq - firstSeq + 1);
if (userParameters->getMenuFlag())
{
if (i % 10 == 0)
{
cout << ".";
}
if (i % 100 == 0)
{
cout << "\n";
}
}
}
// check if any distances overflowed the distance corrections
if (nfails > 0)
{
totalDists = (numSeqs *(numSeqs - 1)) / 2;
printErrorMessageForBootstrap(totalOverspill, totalDists, nfails);
}
bootPositions.clear();
outputTree = new ClusterTreeOutput(&info, userParameters->getBootstrapFormat());
/**
* Print ClustalTree with bootTotals
*/
if (userParameters->getOutputTreeClustal())
{
outputTree->printTree(phyloTree, clustalPhyTreeFile.getPtrToFile(), &bootTotals);
}
/**
* Print phylip tree with boottotals.
*/
if (userParameters->getOutputTreePhylip())
{
// Save the old tree!
saveTree.treeDesc.resize(numSeqs + 1, vector<int>(numSeqs + 1));
saveTree.treeDesc.assign(phyloTree->treeDesc.begin(), phyloTree->treeDesc.end());
outputTree->printPhylipTree(phyloTree, phylipPhyTreeFile.getPtrToFile(),
alignPtr, quickDistMat.get(), &bootTotals);
// reassign the old values!
phyloTree->treeDesc.assign(saveTree.treeDesc.begin(), saveTree.treeDesc.end());
}
/**
* print nexus tree with boottotals
*/
if (userParameters->getOutputTreeNexus())
{
outputTree->printNexusTree(phyloTree, nexusPhyTreeFile.getPtrToFile(), alignPtr,
quickDistMat.get(), &bootTotals);
}
delete phyloTree;
delete clusAlgorithm;
delete outputTree;
}
catch(const exception &ex)
{
}
}
}
|