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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
|
/***************************************************************************
* Copyright (C) 2006 by BUI Quang Minh, Steffen Klaere, Arndt von Haeseler *
* minh.bui@univie.ac.at *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "splitgraph.h"
#include <iostream>
#include <fstream>
#include <cctype>
#include <algorithm>
#include "tree/node.h"
#include "ncl/ncl.h"
#include "nclextra/myreader.h"
#include "tree/mtree.h"
#include "tree/mtreeset.h"
bool compareSplit(Split* sp1, Split* sp2) {
if (sp1->countTaxa() != sp2->countTaxa())
return sp1->countTaxa() < sp2->countTaxa();
else
return sp1->firstTaxon() < sp2->firstTaxon();
}
//#define MY_DEBUG
/********************************************************
Defining SplitGraph methods
********************************************************/
SplitGraph::SplitGraph()
: vector<Split*>()
{
pda = NULL;
taxa = NULL;
splits = NULL;
sets = NULL;
trees = NULL;
mtrees = NULL;
areas_boundary = NULL;
}
SplitGraph::SplitGraph(Params ¶ms) : vector<Split*>() {
init(params);
}
void SplitGraph::createBlocks() {
taxa = new NxsTaxaBlock();
splits = new MSplitsBlock(this);
pda = new MPdaBlock(this);
sets = new MSetsBlock();
trees = new TreesBlock(taxa);
//mtrees = NULL;
}
void SplitGraph::init(Params ¶ms)
{
mtrees = NULL;
if (params.intype == IN_NEWICK) {
// read the input file, can contain more than 1 tree
mtrees = new MTreeSet(params.user_file, params.is_rooted, params.tree_burnin, params.tree_max_count);
//mtree = new MTree(params.user_file, params.is_rooted);
if (params.is_rooted) {
params.sub_size++;
params.min_size++;
}
if (mtrees->isRooted() && params.root != NULL)
outError(ERR_CONFLICT_ROOT);
//SplitIntMap hash_ss;
mtrees->convertSplits(*this, params.split_threshold, params.split_weight_summary, params.split_weight_threshold);
if (verbose_mode >= VB_DEBUG)
saveFileStarDot(cout);
} else {
createBlocks();
if (params.is_rooted)
outError(ERR_ROOT_NET);
cout << "Reading input file " << params.user_file << "..." << endl;
MyReader nexus(params.user_file);
nexus.Add(taxa);
nexus.Add(splits);
nexus.Add(pda);
nexus.Add(sets);
nexus.Add(trees);
MyToken token(nexus.inf);
nexus.Execute(token);
if (trees->GetNumTrees() > 0) {
if (getNSplits() > 0)
outError("Ambiguous input file, pls only specify either SPLITS block or TREES block");
convertFromTreesBlock(params.tree_burnin, params.tree_max_count, params.split_threshold,
params.split_weight_summary, params.split_weight_threshold, params.tree_weight_file);
}
}
if (verbose_mode >= VB_DEBUG)
taxa->Report(cout);
//splits->Report(cout);
//reportConflict(cout);
if (params.pdtaxa_file != NULL) {
if (sets->getNSets() > 0)
outError("Taxa sets were already specified in the input file");
cout << "Reading taxa sets in file " << params.pdtaxa_file << "..." << endl;
bool nexus_formated = (detectInputFile(params.pdtaxa_file) == IN_NEXUS);
if (nexus_formated) {
MyReader nexus(params.pdtaxa_file);
nexus.Add(sets);
MyToken token(nexus.inf);
nexus.Execute(token);
} else {
readTaxaSets(params.pdtaxa_file, sets);
}
if (sets->getNSets() == 0)
outError("No taxa sets found");
}
areas_boundary = NULL;
if (params.areas_boundary_file) {
if (sets->getNSets() == 0) outError("No taxon sets defined yet");
areas_boundary = new double [sets->getNSets() * sets->getNSets()];
cout << "Reading sets relation file " << params.areas_boundary_file << "..." << endl;
readAreasBoundary(params.areas_boundary_file, sets, areas_boundary);
}
if (verbose_mode >= VB_DEBUG && sets->getNSets() > 0)
sets->Report(cout);
if (sets->getNSets() > 0 && taxa->GetNumTaxonLabels() == 0) {
AddTaxaFromSets();
}
if (taxa->GetNumTaxonLabels() == 0)
outError("No taxa found");
if (getNSplits() == 0) {
//outError(ERR_NO_SPLITS);
createStarTree();
}
cout << getNTaxa()-params.is_rooted <<
" taxa and " << getNSplits()-params.is_rooted << " splits." << endl;
}
void SplitGraph::saveCheckpoint() {
if (empty()) return;
int ntax = getNTaxa();
// checkpoint->startStruct("S");
CKP_SAVE(ntax);
int nsplits = size();
CKP_SAVE(nsplits);
checkpoint->startList(size());
for (iterator it = begin(); it != end(); it++) {
checkpoint->addListElement();
stringstream ss;
ss << (*it)->getWeight();
for (int i = 0; i < ntax; i++)
if ((*it)->containTaxon(i))
ss << " " << i;
checkpoint->put("", ss.str());
}
checkpoint->endList();
// checkpoint->endStruct();
CheckpointFactory::saveCheckpoint();
}
void SplitGraph::restoreCheckpoint() {
int ntax, nsplits;
CheckpointFactory::restoreCheckpoint();
// checkpoint->startStruct("S");
if (!CKP_RESTORE(ntax)) return;
CKP_RESTORE(nsplits);
checkpoint->startList(nsplits);
for (int split = 0; split < nsplits; split++) {
checkpoint->addListElement();
string str;
bool found = checkpoint->getString("", str);
ASSERT(found);
stringstream ss(str);
double weight;
ss >> weight;
Split *sp = new Split(ntax, weight);
for (int i = 0; i < ntax; i++) {
int tax;
if (ss >> tax) {
sp->addTaxon(tax);
} else
break;
}
push_back(sp);
}
checkpoint->endList();
// checkpoint->endStruct();
}
int SplitGraph::getNTrivialSplits() {
int count = 0;
for (iterator it = begin(); it != end(); it++)
if ((*it)->trivial() >= 0)
count++;
return count;
}
void SplitGraph::createStarTree() {
cout << "No splits found, creating a star tree with branch length of 1..." << endl;
int ntaxa = taxa->GetNumTaxonLabels();
for (int i = 0; i < ntaxa; i++) {
Split *sp = new Split(ntaxa, 1.0);
sp->addTaxon(i);
push_back(sp);
}
cout << "NOTE: subsequent PD will correspond to species richness." << endl;
}
void SplitGraph::AddTaxaFromSets() {
cout << "Taking taxa from SETS block..." << endl;
for (int i = 0; i < sets->getNSets(); i++)
for(vector<string>::iterator it = sets->getSet(i)->taxlist.begin();
it != sets->getSet(i)->taxlist.end(); it++)
if (!taxa->IsAlreadyDefined(NxsString(it->c_str()))) {
taxa->AddTaxonLabel(NxsString(it->c_str()));
}
}
void SplitGraph::freeMem() {
for (reverse_iterator it = rbegin(); it != rend(); it++) {
//(*it)->report(cout);
delete *it;
}
clear();
if (areas_boundary) delete areas_boundary;
if (trees) delete trees;
if (sets) delete sets;
if (pda) delete pda;
if (splits) delete splits;
if (taxa) delete taxa;
if (mtrees) delete mtrees;
}
SplitGraph::~SplitGraph()
{
freeMem();
}
void SplitGraph::convertFromTreesBlock(int burnin, int max_count, double split_threshold,
int split_weight_summary, double weight_threshold, const char *tree_weight_file) {
cout << trees->GetNumTrees() << " tree(s) loaded" << endl;
if (burnin >= trees->GetNumTrees())
outError("Burnin value is too large");
if (burnin > 0)
cout << burnin << " beginning tree(s) discarded" << endl;
mtrees = new MTreeSet();
for (int i = burnin; i < trees->GetNumTrees() && (i < burnin+max_count); i++) {
stringstream strs(trees->GetTranslatedTreeDescription(i), ios::in | ios::out | ios::app);
strs << ";";
MTree *tree = mtrees->newTree();
bool myrooted = trees->IsRootedTree(i);
tree->readTree(strs, myrooted);
mtrees->push_back(tree);
mtrees->tree_weights.push_back(1);
}
mtrees->checkConsistency();
//SplitIntMap hash_ss;
if (tree_weight_file)
readIntVector(tree_weight_file, burnin, max_count, mtrees->tree_weights);
/* else if (!weights)
tree_weights.resize(size(), 1);*/
if (mtrees->size() != mtrees->tree_weights.size())
outError("Tree file and tree weight file have different number of entries");
mtrees->convertSplits(*this, split_threshold, split_weight_summary, weight_threshold);
}
void SplitGraph::report(ostream &out)
{
out << endl;
out << "Split network contains ";
if (size() == 0)
{
out << "no split" << endl;
}
else if (size() == 1)
out << "one split" << endl;
else
out << size() << " splits" << endl;
if (size() == 0)
return;
sort(begin(), end(), compareSplit);
int k = 0;
for (iterator it = begin(); it != end(); it++,k++)
{
out << '\t' << (k+1) << '\t';
(*it)->report(out);
}
}
void SplitGraph::reportConflict(ostream &out)
{
int k = 0;
out << "Compatible splits: " << endl;
for (iterator i = begin(); i != end(); i++, k++)
{
out << (k+1) << '\t';
int k2 = 1;
for (iterator j = begin(); j != end(); j++, k2++)
if ( j != i && (*i)->compatible(*(*j)))
{
out << k2 << " ";
}
out << endl;
}
}
/**
calculate sum of weights of preserved splits in the taxa_set
@param taxa_set a set of taxa
*/
double SplitGraph::calcWeight(Split &taxa_set)
{
double sum = 0.0;
for (iterator it = begin(); it != end(); it++)
if ((*it)->preserved(taxa_set))
sum += (*it)->getWeight();
return sum;
}
int SplitGraph::countSplits(Split &taxa_set)
{
int cnt = 0;
for (iterator it = begin(); it != end(); it++)
if ((*it)->preserved(taxa_set))
cnt++;
return cnt;
}
int SplitGraph::countInternalSplits(Split &taxa_set)
{
int cnt = 0;
for (iterator it = begin(); it != end(); it++)
if ((*it)->trivial() < 0 && (*it)->preserved(taxa_set))
cnt++;
return cnt;
}
/**
calculate sum of weights of all splits
*/
double SplitGraph::calcWeight() {
double sum = 0.0;
for (iterator it = begin(); it != end(); it++)
sum += (*it)->weight;
return sum;
}
double SplitGraph::calcTrivialWeight() {
double sum = 0.0;
for (iterator it = begin(); it != end(); it++)
if ((*it)->trivial() >= 0)
sum += (*it)->weight;
return sum;
}
double SplitGraph::maxWeight() {
double m = -1e6;
for (iterator it = begin(); it != end(); it++)
if (m < (*it)->weight) m = (*it)->weight;
return m;
}
void SplitGraph::generateTaxaSet(char *filename, int size, int overlap, int times) {
ofstream out(filename);
if (!out.is_open())
outError(ERR_WRITE_OUTPUT, filename);
ASSERT(overlap <= size);
int total = 2*size - overlap;
int ntaxa = getNTaxa();
for (int cnt = 0; cnt < times; cnt++) {
// generate random taxon index
IntVector ranvec;
BoolVector occur(ntaxa, false);
int i;
for (i = 0; i < total; i++) {
int rnum;
do { rnum = random_int(ntaxa); } while (occur[rnum]);
ranvec.push_back(rnum);
occur[rnum] = true;
}
// now write the first set
out << size << endl;
for (i = 0; i < size; i++)
out << taxa->GetTaxonLabel(ranvec[i]) << endl;
out << endl;
// now write the second set
out << size << endl;
for (i = size-overlap; i < total; i++)
out << taxa->GetTaxonLabel(ranvec[i]) << endl;
out << endl;
}
out.close();
}
void SplitGraph::calcDistance(char *filename) {
ofstream out(filename);
if (!out.is_open())
outError(ERR_WRITE_OUTPUT, filename);
mmatrix(double) dist;
int i, j;
calcDistance(dist);
int ntaxa = getNTaxa();
// now write the distances in phylip .dist format
out << ntaxa << endl;
for (i = 0; i < ntaxa; i++) {
out << taxa->GetTaxonLabel(i) << " ";
for (j = 0; j < ntaxa; j++) {
out << dist[i][j] << " ";
}
out << endl;
}
out.close();
}
void SplitGraph::calcDistance(mmatrix(double) &dist) {
int ntaxa = getNTaxa();
iterator it;
vector<int> vi, vj;
vector<int>::iterator i, j;
dist.resize(ntaxa);
for (mmatrix(double)::iterator di = dist.begin(); di != dist.end(); di++)
(*di).resize(ntaxa, 0);
for (it = begin(); it != end(); it++) {
(*it)->getTaxaList(vi, vj);
for (i = vi.begin(); i != vi.end(); i++)
for (j = vj.begin(); j < vj.end(); j++) {
dist[*i][*j] += (*it)->weight;
dist[*j][*i] += (*it)->weight;
}
}
}
void SplitGraph::calcDistance(mmatrix(double) &dist, vector<int> &taxa_order) {
int ntaxa = getNTaxa();
int i, j;
mmatrix(double) my_dist;
calcDistance(my_dist);
dist.resize(ntaxa);
for (i = 0; i < ntaxa; i++) {
dist[i].resize(ntaxa);
for (j = 0; j < ntaxa; j++)
dist[i][j] = my_dist[taxa_order[i]][taxa_order[j]];
}
}
bool SplitGraph::checkCircular(mmatrix(double) &dist) {
return true;
int ntaxa = getNTaxa();
Split taxa_set(ntaxa, 0.0);
for (int i = 0; i < ntaxa-2; i++)
for (int j = i+1; j < ntaxa-1; j++)
for (int k = j+1; k < ntaxa; k++) {
taxa_set.addTaxon(i);
taxa_set.addTaxon(j);
taxa_set.addTaxon(k);
taxa_set.weight = calcWeight(taxa_set);
if (fabs(2 * taxa_set.weight - (dist[i][j] + dist[i][k] + dist[j][k])) > 0.0001) {
cout << "Taxa " << i << " " << j << " " << k;
cout << " do not satisfy circular equation!" << endl;
cout << "Weight = " << taxa_set.weight << endl;
cout << "Sum dist/2 = " << (dist[i][j] + dist[i][k] + dist[j][k]) / 2.0 << endl;
cout << "dist = " << dist[i][j] << " " << dist[i][k] << " "
<< dist[j][k] << endl;
return false;
}
taxa_set.removeTaxon(i);
taxa_set.removeTaxon(j);
taxa_set.removeTaxon(k);
}
return true;
}
void SplitGraph::generateCircular(Params ¶ms) {
int i, j;
int ntaxa = params.sub_size;
int num_splits = (params.num_splits > 0) ? params.num_splits : 3 * ntaxa;
if (num_splits < ntaxa)
outError(ERR_FEW_SPLITS);
taxa = new NxsTaxaBlock();
splits = new MSplitsBlock(this);
double threshold = (ntaxa > 3) ? (double)(num_splits - ntaxa) / (ntaxa*(ntaxa-3)/2) : 0.0;
// insert all trivial splits
for (i = 0; i < ntaxa; i++) {
double weight = randomLen(params);
Split *sp = new Split(ntaxa, weight);
sp->addTaxon(i);
push_back(sp);
ostringstream str;
str << "T" << (i+1);
taxa->AddTaxonLabel(NxsString(str.str().c_str()));
splits->cycle.push_back(i);
}
// randomly insert internal splits
for (i = 0; i < ntaxa-2 && getNSplits() < num_splits; i++)
for (j = i+1; j < ntaxa && j < ntaxa-3+i; j++) {
double choice = random_double();
if (choice > threshold) continue;
double weight = randomLen(params);
Split *sp = new Split(ntaxa, weight);
for (int k = i; k <= j; k++)
sp->addTaxon(k);
push_back(sp);
if (getNSplits() >= num_splits) break;
}
ofstream out(params.user_file);
if (!out.is_open()) {
outError(ERR_WRITE_OUTPUT, params.user_file);
}
saveFileNexus(out);
out.close();
}
void SplitGraph::saveFileNexus(ostream &out, bool omit_trivial) {
int ntaxa = getNTaxa();
int i;
out << "#nexus" << endl << endl;
out << "BEGIN Taxa;" << endl;
out << "DIMENSIONS ntax=" << ntaxa << ";" << endl;
out << "TAXLABELS" << endl;
for (i = 0; i < ntaxa; i++)
out << "[" << i+1 << "] '" << taxa->GetTaxonLabel(i) << "'" << endl;
out << ";" << endl << "END; [Taxa]" << endl << endl;
out << "BEGIN Splits;" << endl;
out << "DIMENSIONS ntax=" << ntaxa << " nsplits=" << ((omit_trivial) ? getNSplits() - getNTrivialSplits() : getNSplits()) << ";" << endl;
out << "FORMAT labels=no weights=yes confidences=no intervals=no;" << endl;
if (isCircular()) {
out << "CYCLE";
for (i = 0; i < ntaxa; i++)
out << " " << splits->cycle[i] + 1;
out << ";" << endl;
}
out << "MATRIX" << endl;
int near_zeros = 0;
int zeros = 0;
for (iterator it = begin(); it != end(); it++) {
if (omit_trivial && (*it)->trivial() >= 0) continue;
if ((*it)->weight == 0.0) zeros ++;
if ((*it)->weight <= 1e-6) near_zeros ++;
out << "\t" << (*it)->weight << "\t";
for (i = 0; i < ntaxa; i++)
if ((*it)->containTaxon(i))
out << " " << i+1;
out << "," << endl;
}
out << ";" << endl << "END; [Splits]" << endl << endl;
if (near_zeros) {
//outWarning("Some nearly-zero split weights observed");
//cout << zeros << " zero-weights and " << near_zeros << " near zero weights!" << endl;
}
}
void SplitGraph::saveFileStarDot(ostream &out, bool omit_trivial) {
int ntaxa = getNTaxa();
int i;
for (iterator it = begin(); it != end(); it++) {
if (omit_trivial && (*it)->trivial() >= 0) continue;
bool swap_code = !(*it)->containTaxon(0);
if (swap_code) {
for (i = 0; i < ntaxa; i++)
out << (((*it)->containTaxon(i)) ? '.' : '*');
} else {
for (i = 0; i < ntaxa; i++)
out << (((*it)->containTaxon(i)) ? '*' : '.');
}
out << "\t" << (*it)->weight << endl;
}
}
void SplitGraph::saveFile(const char* out_file, InputType file_format, bool omit_trivial) {
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(out_file);
if (file_format == IN_NEXUS)
saveFileNexus(out, omit_trivial);
else
saveFileStarDot(out, omit_trivial);
out.close();
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, out_file);
}
}
void SplitGraph::scaleWeight(double norm, bool make_int, int precision) {
for (iterator itg = begin(); itg != end(); itg ++ )
if (make_int)
(*itg)->setWeight( round((*itg)->getWeight()*norm) );
else if (precision < 0)
(*itg)->setWeight( (*itg)->getWeight()*norm);
else
(*itg)->setWeight( round((*itg)->getWeight()*norm*pow((double)10.0,precision))/pow((double)10.0,precision));
}
// TODO Implement a more efficient function using Hash Table
bool SplitGraph::containSplit(Split &sp) {
Split invert_sp(sp);
invert_sp.invert();
for (iterator it = begin(); it != end(); it++)
if ((*(*it)) == sp || (*(*it)) == invert_sp)
return true;
return false;
}
double SplitGraph::computeBoundary(Split &area) {
if (!areas_boundary) return 0.0;
int nareas = sets->getNSets();
double boundary = 0.0;
for (int i = 0; i < nareas; i++)
if (area.containTaxon(i)) {
boundary += areas_boundary[i*nareas+i];
for (int j = i+1; j < nareas; j++)
if (area.containTaxon(j))
boundary -= 2.0 * areas_boundary[i*nareas+j];
}
return boundary;
}
bool SplitGraph::compatible(Split *sp) {
for (iterator it = begin(); it != end(); it++)
if (!(*it)->compatible(*sp))
return false;
return true;
}
void SplitGraph::findMaxCompatibleSplits(SplitGraph &maxsg) {
// maximum number of compatible splits = 2n-3!
int max_splits = getNTaxa() * 2 - 3;
// myset will be sorted by weight in descending order
SplitSet myset;
myset.insert(myset.end(), begin(), end());
sort(myset.begin(), myset.end(), splitweightcmp);
// now build the spset
if (!maxsg.taxa)
maxsg.taxa = new NxsTaxaBlock();
if (!maxsg.splits)
maxsg.splits = new MSplitsBlock(&maxsg);
if (!maxsg.pda)
maxsg.pda = new MPdaBlock(&maxsg);
for (int i = 0; i < getNTaxa(); i++)
maxsg.taxa->AddTaxonLabel(taxa->GetTaxonLabel(i));
// make the cycle
maxsg.splits->cycle = splits->cycle;
// make the splits
for (SplitSet::iterator it = myset.begin(); it != myset.end(); it++)
if (maxsg.compatible(*it)){
maxsg.push_back(new Split(*(*it)));
//(*it)->report(cout);
if (maxsg.size() >= max_splits)
break;
}
myset.clear();
}
bool SplitGraph::isWeaklyCompatible() {
if (getNSplits() < 3) return true;
for (iterator it1 = begin(); it1+2 != end(); it1++)
for (iterator it2 = it1+1; it2+1 != end(); it2++)
for (iterator it3 = it2+1; it3 != end(); it3++) {
Split sp1(*(*it1));
Split sp2(*(*it2));
Split sp3(*(*it3));
Split sp(sp1);
sp *= sp2;
sp *= sp3;
if (sp.isEmpty()) continue;
sp1.invert();
sp2.invert();
sp = sp1;
sp *= sp2;
sp *= sp3;
if (sp.isEmpty()) continue;
sp2.invert();
sp3.invert();
sp = sp1;
sp *= sp2;
sp *= sp3;
if (sp.isEmpty()) continue;
sp1.invert();
sp2.invert();
sp = sp1;
sp *= sp2;
sp *= sp3;
if (sp.isEmpty()) continue;
return false;
}
return true;
}
void SplitGraph::getTaxaName(vector<string> &taxname) {
taxname.clear();
for (int i = 0; i < getNTaxa(); i++)
taxname.push_back(taxa->GetTaxonLabel(i));
}
int SplitGraph::findLeafName(string &name) {
for (int i = 0; i < getNTaxa(); i++)
if (taxa->GetTaxonLabel(i) == name)
return i;
return -1;
}
int SplitGraph::removeTrivialSplits() {
int removed = 0;
for (iterator itg = begin(); itg != end(); ) {
if ((*itg)->trivial() >= 0) {
removed++;
delete (*itg);
(*itg) = back();
pop_back();
} else itg++;
}
return removed;
}
|