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 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
// File : PNode.cpp
// Purpose : Phylogenetic Tree Node
//
// Developer : David Knox (david.knox@colorado.edu) Jan 2011
// Copyright : Copyright (C) 2007-2011 David Knox
//
// Web site : http://parsinsert.sourceforge.net/
//
///////////////////////////////////////////////////////////////////////////////////////////////////
// This file is part of ParsInsert.
//
// ParsInsert 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 3 of the License, or
// (at your option) any later version.
//
// ParsInsert 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 ParsInsert. If not, see <http://www.gnu.org/licenses/>.
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "PNode.h"
#include "Attrs.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Class variables
//
int CPTree::PRECISION = 5; // number of decimal points to use when printing
///////////////////////////////////////////////////////////////////////////////////////////////////
string& MakeCSVQuoted(string &q_str, LPCSTR others=NULL);
string& MakeQuoted(string &q_str, LPCSTR others=NULL);
///////////////////////////////////////////////////////////////////////////////////////////////////
CPTree :: CPTree()
{
root = NULL;
internal = 0;
nodeList.clear();
Progress = NULL;
totalProcess = 0;
nProcess = 0;
bufFile = NULL;
}
CPTree :: ~CPTree()
{
CPNodeListIter iter = nodeList.begin();
while (iter != nodeList.end())
{
if ((*iter) != NULL)
{
(*iter)->tree = NULL;
delete (*iter);
}
++iter;
}
nodeList.clear();
root = 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CPTree :: SetProgressFunction(ProgressFunction callBack)
{
Progress = callBack;
return TRUE;
}
BOOL CPTree :: ClearProgressFunction()
{
Progress = NULL;
return TRUE;
}
BOOL CPTree :: ShowProgress(LPCSTR msg, int n, int total, PVOID data)
{
if (Progress != NULL)
return (*Progress)(msg, n, total, data);
printf("Progress: (%d of %d) %s\n", n, total, msg);
return TRUE;
}
void CPTree::ShowError(int codeline, LPCSTR msg)
{
printf("ERROR [CodeLine:%d] %s [Input line:%d, col: %d]\n", codeline, msg, lineCount, colCount);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CPTree :: WriteNewickTree(LPCSTR filename, CPNode *base, BOOL withComment)
{
if (filename == NULL)
return FALSE;
if (base == NULL)
return FALSE;
FILE *f = fopen(filename, "wb+");
if (f == NULL)
return FALSE;
base->WriteNode(f, 0, FALSE, withComment);
fprintf(f, ";\n");
fclose(f);
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL IsAllDigits(LPCSTR s)
{
while ((*s != 0) && isdigit(*s))
++s;
return (*s == 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPTree :: GetQuotedString(LPCSTR &str, string &v)
{
// search for ending quote
char quote = *str;
v += *str; // add beginning quote
// read until next matching quote
do {
++str;
++nProcess;
v += *str;
if ((*str) == '\\')
{
++str;
v += *str;
}
if ( ((*str) == '\'') && (str[1] == '\''))
{
++str;
++nProcess;
v += *str; // skip 2nd quote
++str;
++nProcess;
if ((*str) != '\'')
{ // get the next char after it
v += *str;
}
}
} while ( ((*str) != quote) && (*str != 0) );
if (*str == quote)
{
++str; // skip ending quote
++nProcess;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPTree :: ReadComment(LPCSTR &v, string &comment)
{
// skip blanks
while (*v == ' ')
{
++v;
++nProcess;
}
if (*v == '[')
{
++v; // skip opening
++nProcess;
while ((*v != 0) && (*v != ']'))
{
if (*v == '\"')
GetQuotedString(v, comment);
else
{
comment += *v;
++v;
++nProcess;
}
}
if (*v != 0)
{
++v; // skip ending
++nProcess;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPTree :: ReadToken(LPCSTR &str, string &v)
{
LPCSTR delim = " ,:[]()";
while ((*str != 0) && (strchr(delim, *str) == NULL) )
{
if ((*str == '\'') || (*str == '\"'))
{
GetQuotedString(str, v);
}
else
{
v += *str;
++str;
++nProcess;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
string& CPTree :: QuoteString(string& comment, char quote)
{
/// comment.Replace("\'", "\'\'");
/// comment.Replace("\"", "\\\"");
if (!comment.empty())
{
comment.insert(comment.begin(), quote);
comment += quote;
}
return comment;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPTree::Parse(LPCSTR str)
{
BOOL done = FALSE;
// strip initial arb comment
while (!done && (*str != 0))
{
switch (*str)
{
default: done = TRUE;
case ' ':
{
++str;
}
break;
case '[':
{
while ((*str != ']') && (str != 0))
++str;
}
break;
}
}
totalProcess = strlen(str);
nProcess = 0;
endParseStr = str + totalProcess;
root = ParseTree(str);
if ((nProcess < totalProcess) && (*str == ',')) // more to process
{
CPNode *child = root;
root = new CPNode(this);
int prev_processed = 0;
while ((nProcess < totalProcess) && (child != NULL))
{
if (*str == ',') ++str;
root->AddEdge(child);
//DisplayL("Processed %d chars of %d, [%-100.100s]\n", nProcess, totalProcess, str);
prev_processed = nProcess;
child = ParseTree(str);
if (nProcess == prev_processed)
{
DisplayL("Could not process all the chars in NEWICK file\n");
break;
}
}
}
DisplayL("Processed %d chars of %d\n", nProcess, totalProcess);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
CPNode* CPTree::ParseTree(LPCSTR &str)
{
while (*str != 0)
{
//TRACE("[%d] [%s]\n", __LINE__, str);
nProcess = totalProcess - (endParseStr - str); // chars processed is total chars - chars left to process
switch (*str)
{
default: {
// read next token
string v;
v.clear();
while ((*str != 0) && (*str != ',') && (*str != ')') && (*str != ';'))
{
if ((*str == '\'') || (*str == '\"'))
{
GetQuotedString(str, v);
}
else
{
v += *str;
++str;
// ++nProcess;
}
}
// TRACE("[%d] Create new node [%08X][%s]\n", __LINE__, this, v.c_str());
return new CPNode(this, v.c_str());
}
case ' ': ++str;
// ++nProcess;
break;
case ':': {
// TRACE("[%d] [:]\n", __LINE__);
// find attrribute data upto ',' or ')'
string v;
v.clear();
while ((*str != 0) && (*str != ',') && (*str != ')') && (*str != ';'))
{
if ((*str == '\'') || (*str == '\"'))
GetQuotedString(str, v);
else
{
v += *str;
++str;
// ++nProcess;
}
}
}
break;
case ';': { // end of the data
// TRACE("[%d] [;]\n", __LINE__);
nProcess = totalProcess;
str = endParseStr;
}
break;
case '[':
{
// TRACE("[%d] [\\[]\n", __LINE__);
string comment;
ReadComment(str, comment);
if (comment[0] == '{')
{
//add attrs to tree
ProcessAttrList(comment.c_str(), NULL);
}
else
{
// strip quotes around whole comment
if ((comment[0] == '\"') && (comment[comment.length()-1] == '\"'))
comment = comment.substr(1,comment.length()-2);
SetAttr(ATTR_COMMENT, comment.c_str());
}
}
break;
case '(': {
// TRACE("[%d] [%s]\n", __LINE__, str);
BOOL complete = FALSE;
CPNode *R = new CPNode(this);
++str;
// ++nProcess;
while (!complete)
{
CPNode *S = ParseTree(str);
R->AddEdge(S);
if ((*str == ')') || (*str == 0))
complete = TRUE;
++str;
// ++nProcess;
}
// find attrribute data upto ',' or ')'
string v;
v.clear();
while ((*str != 0) && (*str != ',') && (*str != ')') && (*str != ';'))
{
if ((*str == '\'') || (*str == '\"'))
GetQuotedString(str, v);
else
{
v += *str;
++str;
// ++nProcess;
}
}
// TRACE("[%d] ATTRS[%s]\n", __LINE__, v.c_str());
R->AddAttrs(v.c_str());
return R;
}
case ')': {
// This is for the root
// TRACE("[%d] Close[%s]\n", __LINE__, str);
++str;
// find attrribute data upto ',' or ')'
string v;
v.clear();
while ((*str != 0) && (*str != ',') && (*str != ')') && (*str != ';'))
{
if ((*str == '\'') || (*str == '\"'))
GetQuotedString(str, v);
else
{
v += *str;
++str;
// ++nProcess;
}
}
// TRACE("[%d] CLOSE ATTRS[%s]\n", __LINE__, v.c_str());
root->AddAttrs(v.c_str());
}
}
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPTree :: SetAttr(LPCSTR k, LPCSTR v)
{
attrs.m[k] = v;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPTree :: ProcessAttrList(LPCSTR str, CPNode *node)
{
int pos;
while (*str != '\0')
{
switch (*str)
{
case '{': {
// add new attribute
pos = strcspn(++str, "=}");
// ++nProcess;
string key(str, pos);
str += pos; // skip to next char
// nProcess += pos;
pos = strcspn(++str, "}");
// ++nProcess;
string value(str, pos);
str += pos; // skip to next char
// nProcess += pos;
++str; // skip closing brace
// ++nProcess;
//TRACE("[%s] %s = %s\n", title, key, value);
if (node == NULL)
SetAttr(key.c_str(), value.c_str());
else
node->SetAttr(key.c_str(), value.c_str());
}
break;
default:
++str;
// ++nProcess;
break;
}
}
}
//////////////////////////////////////////////////////////////////////
CPNode* CPTree :: Find(LPCSTR v)
{
CPNodeListIter iter = nodeList.begin();
while (iter != nodeList.end())
{
//CPNode *n = *iter;
if ((*iter)->title.compare(v) == 0)
return *iter;
++iter;
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
CPNode :: CPNode(CPTree *ptree, LPCSTR v)
{
char str[2048];
tree = ptree;
if (tree->internal%100 == 0)
{
string partial = v;
partial = partial.substr(0, 100);
/// char msg[2048];
/// sprintf(msg,"Adding node %d {%s}\n", tree->internal, partial);
/// tree->ShowProgress(msg, tree->nProcess, tree->totalProcess, NULL);
}
if ((v == NULL) || (strlen(v) == 0))
{
id = ++(tree->internal);
sprintf(str, "%s_%04d", "I", id);
title = str;
}
else
{
id = ++(tree->internal);
// copy upto the ':' or '['
string name;
name.clear();
while ((*v != 0) && (*v != ':') && (*v != '['))
{
if ((*v == '\'') || (*v == '\"'))
tree->GetQuotedString(v, name);
else
{
name += *v;
++v;
}
}
AddAttrs(v);
if (!name.empty() && (name[0] == '\''))
{
// this is from ARB. Strip the quotes, use chars upto first ',' as name
string realName;
string comment;
int first = name.find_first_not_of("\' ");
int last = name.find_last_not_of("\' ");
name = name.substr(first, last-first+1);
int n = name.find(",");
realName = name.substr(0,n);
comment = name.substr(n+1);
name = realName;
if (!comment.empty())
{
// strip quotes around whole comment
if ((comment[0] == '\"') && (comment[comment.length()-1] == '\"'))
comment = comment.substr(1,comment.length()-2);
attrs.Add(ATTR_COMMENT, comment);
}
}
int first = name.find_first_not_of(" ");
int last = name.find_last_not_of(" ");
name = name.substr(first, last-first+1);
attrs.Add(ATTR_NAME, name);
if (name.empty())
{
sprintf(str, "%s_%04d", "I", ++(tree->internal));
title = str;
}
else
title = name;
}
parent = NULL;
tree->nodeList.push_back(this);
}
CPNode :: ~CPNode()
{
// find position in tree's node list and remove it
if (tree != NULL)
{
CPNodeList::iterator iter = tree->nodeList.begin();
while ((iter != tree->nodeList.end()) && (*iter != this))
++iter;
if (iter != tree->nodeList.end())
tree->nodeList.erase(iter);
}
id = -1;
parent = NULL;
attrs.Clear();
edges.clear();
title = "DELETED";
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPNode :: AddEdge(CPNode *other)
{
edges.push_back(other);
other->parent = this;
// TRACE("Edge from [%s] to [%s]\n", title, other->title);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPNode :: InvertEdges(BOOL recurse)
{
// invert the edge list
CPNodeList orig;
orig.insert(orig.begin(), edges.begin(), edges.end());
edges.clear();
for (CPNodeListIter iter=orig.end() ; iter != orig.begin() ; --iter)
edges.push_back(*iter);
if (recurse)
{
for (CPNodeListIter iter=edges.begin() ; iter != edges.end() ; ++iter)
(*iter)->InvertEdges(TRUE);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPNode :: ResetParent(CPNode *p)
{
// traverse the tree setting parent to correct items
// used by tree inversion
parent = p;
for (CPNodeListIter iter=edges.begin() ; iter != edges.end() ; ++iter)
(*iter)->ResetParent(this);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
string& CPNode :: CleanString(string& s, BOOL searchForBootstrap)
{
if (s.empty()) return s;
// strip quotes, convert escaped chars
int pos;
do {
pos = s.find("\\\"");
if (pos != -1)
s.replace(pos, 2, "\""); // backslash double quote to double quote
} while (pos >= 0);
do {
pos = s.find("\\\\");
if (pos != -1)
s.replace(pos, 2, "\\"); // double backslash to single backslash
} while (pos >= 0);
do {
pos = s.find("\'\'");
if (pos != -1)
s.replace(pos, 2, ""); // two single quotes to nothing
} while (pos >= 0);
do {
pos = s.find("\\\'");
if (pos != -1)
s.replace(pos, 2, ""); // backslash single quote to nothing
} while (pos >= 0);
// Remove beginning and ending double quotes
if (s[0] == '\"')
s = s.substr(1);
if (s[s.length()-1] == '\"')
s = s.substr(0, s.length()-1);
// Remove beginning and ending single quotes
if (s[0] == '\'')
s = s.substr(1);
if (s[s.length()-1] == '\'')
s = s.substr(0, s.length()-1);
if (searchForBootstrap)
{
int pos = s.find_first_not_of("0123456789:", 0);
string boot = s.substr(0,pos);
int n = boot.find(":");
if (s.length() == boot.length())
n = s.length(); // only digits, no colon
if (n > 1)
{
// bootstrap data on leading edge
boot = boot.substr(0,n);
attrs.Add(ATTR_BOOTSTRAP_DATA, boot);
if (s.length() == n)
s.clear();
else
s = s.substr(n+1);
}
}
return s;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPNode :: SetAttr(LPCSTR k, LPCSTR v)
{
attrs.Add(k,v);
}
void CPNode :: SetAttr(LPCSTR k, string& v)
{
attrs.Add(k,v);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void CPNode :: AddAttrs(LPCSTR v)
{
while (*v != 0)
{
if (*v == ':')
{
++v; // consume the ':'
int i = strcspn(v, "[");
string dist(v, i);
v += i;
Trim(dist, " \n\r\t");
attrs.Add(ATTR_DISTANCE, dist);
}
else if (*v == '[')
{
++v; // skip '['
int i = strcspn(v, "]");
string comment(v,i);
v += i;
if (*v == ']')
++v;
if (comment.length() > 0)
{
// strip quotes around whole comment
if ((comment[0] == '\"') && (comment[comment.length()-1] == '\"'))
comment = comment.substr(1,comment.length()-2);
if ((comment[0] == '{') && (comment[comment.length()-1] == '}'))
tree->ProcessAttrList(comment.c_str(), this);
else if (IsBranchNode())
{
attrs.Add(ATTR_TAXLABEL, CleanString(comment, TRUE));
}
else
attrs.Add(ATTR_COMMENT, CleanString(comment));
}
}
else if ((*v == '\'') || (*v == '\"'))
{
string comment;
tree->GetQuotedString(v, comment);
// strip quotes around whole comment
if ((comment[0] == '\"') && (comment[comment.length()-1] == '\"'))
comment = comment.substr(1,comment.length()-2);
if (comment.length() > 0)
{
// strip quotes around whole comment
if ((comment[0] == '\"') && (comment[comment.length()-1] == '\"'))
comment = comment.substr(1,comment.length()-2);
if ((comment[0] == '{') && (comment[comment.length()-1] == '}'))
tree->ProcessAttrList(comment.c_str(), this);
else if (IsBranchNode())
attrs.Add(ATTR_TAXLABEL, CleanString(comment, TRUE));
else
attrs.Add(ATTR_COMMENT, CleanString(comment));
}
}
else if (IsBranchNode())
{
// read title of this item
while (isspace(*v)) ++v; // skip white space
int i = strcspn(v, "[;:),\'\"");
string iname(v, i);
v += i;
if ((iname.length() > 1) && (iname[0] == 'I') && (iname[1] == '_'))
title = iname;
else if (iname.length() > 0)
attrs.Add(ATTR_TAXLABEL, CleanString(iname, TRUE));
}
else
++v; // consume the chars up to [:
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CPNode :: WriteAttrs(FILE *f)
{
// for each attr
// add key,value to file
// for each edge
// edge->ReadAttrs(filename);
fprintf(f, "[%s]\n", title.c_str());
CAttrDictIter iter;
for (iter=attrs.m.begin() ; iter != attrs.m.end() ; ++iter)
{
if (((*iter).first.length() > 2) && ((*iter).first[0] == '-') && ((*iter).first[1] == '-')) continue;
if ((*iter).second.length() > 0)
fprintf(f, "%s=\"%s\"\n", (*iter).first.c_str(), (*iter).second.c_str());
}
for (CPNodeListIter iter=edges.begin() ; iter != edges.end() ; ++iter)
(*iter)->WriteAttrs(f);
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CPNode :: WriteNode(FILE *f, int indent, BOOL includeAttrs, BOOL withComment)
{
char str[2048];
if (f == NULL)
return FALSE;
if (IsBranchNode())
{
fprintf(f, "(");
indent += 1;
fprintf(f, "\n%*s", indent, "");
for (CPNodeListIter iter=edges.begin() ; iter != edges.end() ; ++iter)
{
if (iter != edges.begin())
{
fprintf(f, ",");
fprintf(f, "\n%*s", indent, "");
}
(*iter)->WriteNode(f, indent, includeAttrs, withComment);
}
indent -= 1;
fprintf(f, "\n%*s", indent, "");
fprintf(f, ")");
}
string name = attrs.GetString(ATTR_NAME,"");
if (includeAttrs)
{
string boot = attrs.GetString(ATTR_BOOTSTRAP_DATA);
if (IsBranchNode())
{
name = attrs.GetString(ATTR_TAXLABEL);
if (!boot.empty())
{
// prepend the boot to the name, place in quotes
if (name.empty())
name = boot;
else
{
sprintf(str, "\'%s:%s\'", boot.c_str(), name.c_str());
name = str;
}
}
fprintf(f, "%s", name.c_str());
}
else
{
fprintf(f, "%s", name.c_str());
if (!boot.empty())
fprintf(f, "[%s]", boot.c_str());
}
string comment;
GetComment(ATTR_COMMENT, comment);
tree->QuoteString(comment);
if (!comment.empty())
fprintf(f, " [%s]", comment.c_str());
string key;
string value;
string attrStr;
CAttrDictIter iter;
for (iter=attrs.m.begin() ; iter != attrs.m.end() ; ++iter)
{
key = (*iter).first;
value = (*iter).second;
if ((key.length() > 2) && (key[0] == '-') && (key[1] == '-')) continue;
//ignore attributes printed elsewhere
if (key.compare(ATTR_NAME) == 0) continue;
if (key.compare(ATTR_DISTANCE) == 0) continue;
if (key.compare(ATTR_TAXLABEL) == 0) continue;
if (key.compare(ATTR_COMMENT) == 0) continue;
if (key.compare(ATTR_BOOTSTRAP_DATA) == 0) continue;
if (attrStr.empty())
attrStr += '[';
sprintf(str, "{%s=%s}", key.c_str(), value.c_str());
attrStr += str;
}
if (!attrStr.empty())
{
attrStr += ']';
}
}
else if (withComment)
{
string comment;
GetComment(ATTR_COMMENT, comment);
if (!comment.empty())
{
name += ", ";
name += comment;
tree->QuoteString(name, '\'');
}
fprintf(f, "%s", name.c_str());
}
else
fprintf(f, "%s", name.c_str());
double dist = attrs.GetDouble(ATTR_DISTANCE, 0);
fprintf(f, ":%.*f", CPTree::PRECISION, dist);
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CPNode :: GetComment(LPCSTR attr, string &str, LPCSTR _default)
{
str = attrs.GetString(attr, _default);
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
int CPNode :: GetAncestors(CPNodeList &list, BOOL fromBottom)
{
// return the names of the ancestors
list.clear();
int n = 0;
CPNode *p = parent;
while (p != NULL)
{
if (fromBottom)
list.push_back(p);
else
list.insert(list.begin(), p);
p = p->parent;
++n;
}
return n;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
int CPNode :: GetDecendants(CPNodeList &list, BOOL leafOnly)
{
// return the names of the decendants
// for each edge
// add edge name
// add all decendants of edge
for (CPNodeListIter iter=edges.begin() ; iter != edges.end() ; ++iter)
{
CPNode *node = (*iter);
if (node->IsBranchNode())
{
if (!leafOnly)
list.push_back(node);
node->GetDecendants(list, leafOnly);
}
else
list.push_back(node);
}
return list.size();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
string& MakeCSVQuoted(string &q_str, LPCSTR others)
{
// Escape the following chars
// " double quote
string local = q_str;
LPCSTR s = local.data();
q_str = "\"";
while (*s != 0)
{
if (*s == '\"')
q_str += "\"\"";
else
{
// if (others && (strchr(others, *s) != NULL))
// q_str += "\\"; // escape the char
q_str += *s;
}
++s;
}
q_str += "\"";
return q_str;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
string& MakeQuoted(string &q_str, LPCSTR others)
{
// Escape the following chars
// / backslash
// " double quote
// < open
// > close
string local = q_str;
LPCSTR s = local.data();
q_str = "\"";
while (*s != 0)
{
if (*s == '\\')
q_str += "\\\\";
else if (*s == '\"')
q_str += "\\\"";
else if (*s == '<')
q_str += "\\<";
else if (*s == '>')
q_str += "\\>";
else
{
if (others && (strchr(others, *s) != NULL))
q_str += "\\"; // escape the char
q_str += *s;
}
++s;
}
q_str += "\"";
return q_str;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
|