]> git.donarmstrong.com Git - mothur.git/blob - tree.cpp
added tree reader class to handle reading trees. Reworked the tree map to tree class...
[mothur.git] / tree.cpp
1 /*
2  *  tree.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/22/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "tree.h"
11
12 /*****************************************************************/
13 Tree::Tree(int num, TreeMap* t) : tmap(t) {
14         try {
15                 m = MothurOut::getInstance();
16                 
17                 numLeaves = num;  
18                 numNodes = 2*numLeaves - 1;
19         
20                 tree.resize(numNodes);
21         }
22         catch(exception& e) {
23                 m->errorOut(e, "Tree", "Tree - numNodes");
24                 exit(1);
25         }
26 }
27 /*****************************************************************/
28 Tree::Tree(string g) { //do not use tree generated by this its just to extract the treenames, its a chicken before the egg thing that needs to be revisited.
29         try {
30                 m = MothurOut::getInstance();
31                 parseTreeFile();  m->runParse = false;  
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "Tree", "Tree - just parse");
35                 exit(1);
36         }
37 }
38 /*****************************************************************/
39 Tree::Tree(TreeMap* t) : tmap(t) {
40         try {
41                 m = MothurOut::getInstance();
42                 
43                 if (m->runParse == true) {  parseTreeFile();  m->runParse = false;  }
44 //for(int i = 0; i <    globaldata->Treenames.size(); i++) { cout << i << '\t' << globaldata->Treenames[i] << endl;  }  
45                 numLeaves = m->Treenames.size();
46                 numNodes = 2*numLeaves - 1;
47                 
48                 tree.resize(numNodes);
49                         
50                 //initialize groupNodeInfo
51                 for (int i = 0; i < (tmap->getNamesOfGroups()).size(); i++) {
52                         groupNodeInfo[(tmap->getNamesOfGroups())[i]].resize(0);
53                 }
54                 
55                 //initialize tree with correct number of nodes, name and group info.
56                 for (int i = 0; i < numNodes; i++) {
57                         //initialize leaf nodes
58                         if (i <= (numLeaves-1)) {
59                                 tree[i].setName(m->Treenames[i]);
60                                 
61                                 //save group info
62                                 string group = tmap->getGroup(m->Treenames[i]);
63                                 
64                                 vector<string> tempGroups; tempGroups.push_back(group);
65                                 tree[i].setGroup(tempGroups);
66                                 groupNodeInfo[group].push_back(i); 
67                                 
68                                 //set pcount and pGroup for groupname to 1.
69                                 tree[i].pcount[group] = 1;
70                                 tree[i].pGroups[group] = 1;
71                                 
72                                 //Treemap knows name, group and index to speed up search
73                                 tmap->setIndex(m->Treenames[i], i);
74         
75                         //intialize non leaf nodes
76                         }else if (i > (numLeaves-1)) {
77                                 tree[i].setName("");
78                                 vector<string> tempGroups;
79                                 tree[i].setGroup(tempGroups);
80                         }
81                 }
82                 
83         }
84         catch(exception& e) {
85                 m->errorOut(e, "Tree", "Tree");
86                 exit(1);
87         }
88 }
89 /*****************************************************************/
90 Tree::Tree(TreeMap* t, vector< vector<double> >& sims) : tmap(t) {
91         try {
92                 m = MothurOut::getInstance();
93                 
94                 if (m->runParse == true) {  parseTreeFile();  m->runParse = false;  }
95         //for(int i = 0; i <    globaldata->Treenames.size(); i++) { cout << i << '\t' << globaldata->Treenames[i] << endl;  }  
96                 numLeaves = m->Treenames.size();
97                 numNodes = 2*numLeaves - 1;
98                 
99                 tree.resize(numNodes);
100         
101                 //initialize groupNodeInfo
102                 for (int i = 0; i < (tmap->getNamesOfGroups()).size(); i++) {
103                         groupNodeInfo[(tmap->getNamesOfGroups())[i]].resize(0);
104                 }
105                 
106                 //initialize tree with correct number of nodes, name and group info.
107                 for (int i = 0; i < numNodes; i++) {
108                         //initialize leaf nodes
109                         if (i <= (numLeaves-1)) {
110                                 tree[i].setName(m->Treenames[i]);
111                                 
112                                 //save group info
113                                 string group = tmap->getGroup(m->Treenames[i]);
114                                 
115                                 vector<string> tempGroups; tempGroups.push_back(group);
116                                 tree[i].setGroup(tempGroups);
117                                 groupNodeInfo[group].push_back(i); 
118                                 
119                                 //set pcount and pGroup for groupname to 1.
120                                 tree[i].pcount[group] = 1;
121                                 tree[i].pGroups[group] = 1;
122                                 
123                                 //Treemap knows name, group and index to speed up search
124                                 tmap->setIndex(m->Treenames[i], i);
125                 
126                 //intialize non leaf nodes
127                         }else if (i > (numLeaves-1)) {
128                                 tree[i].setName("");
129                                 vector<string> tempGroups;
130                                 tree[i].setGroup(tempGroups);
131                         }
132                 }
133         
134         //build tree from matrix
135         //initialize indexes
136         map<int, int> indexes;  //maps row in simMatrix to vector index in the tree
137         int numGroups = (tmap->getNamesOfGroups()).size();
138         for (int g = 0; g < numGroups; g++) {   indexes[g] = g; }
139                 
140                 //do merges and create tree structure by setting parents and children
141                 //there are numGroups - 1 merges to do
142                 for (int i = 0; i < (numGroups - 1); i++) {
143                         float largest = -1000.0;
144                         
145                         if (m->control_pressed) { break; }
146                         
147                         int row, column;
148                         //find largest value in sims matrix by searching lower triangle
149                         for (int j = 1; j < sims.size(); j++) {
150                                 for (int k = 0; k < j; k++) {
151                                         if (sims[j][k] > largest) {  largest = sims[j][k]; row = j; column = k;  }
152                                 }
153                         }
154             
155                         //set non-leaf node info and update leaves to know their parents
156                         //non-leaf
157                         tree[numGroups + i].setChildren(indexes[row], indexes[column]);
158                         
159                         //parents
160                         tree[indexes[row]].setParent(numGroups + i);
161                         tree[indexes[column]].setParent(numGroups + i);
162                         
163                         //blength = distance / 2;
164                         float blength = ((1.0 - largest) / 2);
165                         
166                         //branchlengths
167                         tree[indexes[row]].setBranchLength(blength - tree[indexes[row]].getLengthToLeaves());
168                         tree[indexes[column]].setBranchLength(blength - tree[indexes[column]].getLengthToLeaves());
169                         
170                         //set your length to leaves to your childs length plus branchlength
171                         tree[numGroups + i].setLengthToLeaves(tree[indexes[row]].getLengthToLeaves() + tree[indexes[row]].getBranchLength());
172                         
173                         
174                         //update index 
175                         indexes[row] = numGroups+i;
176                         indexes[column] = numGroups+i;
177                         
178                         //remove highest value that caused the merge.
179                         sims[row][column] = -1000.0;
180                         sims[column][row] = -1000.0;
181                         
182                         //merge values in simsMatrix
183                         for (int n = 0; n < sims.size(); n++)   {
184                                 //row becomes merge of 2 groups
185                                 sims[row][n] = (sims[row][n] + sims[column][n]) / 2;
186                                 sims[n][row] = sims[row][n];
187                                 //delete column
188                                 sims[column][n] = -1000.0;
189                                 sims[n][column] = -1000.0;
190                         }
191                 }
192                 
193                 //adjust tree to make sure root to tip length is .5
194                 int root = findRoot();
195                 tree[root].setBranchLength((0.5 - tree[root].getLengthToLeaves()));
196         
197     }
198         catch(exception& e) {
199                 m->errorOut(e, "Tree", "Tree");
200                 exit(1);
201         }
202 }
203 /*****************************************************************/
204 Tree::~Tree() {}
205 /*****************************************************************/
206 void Tree::addNamesToCounts(map<string, string> nameMap) {
207         try {
208                 //ex. seq1      seq2,seq3,se4
209                 //              seq1 = pasture
210                 //              seq2 = forest
211                 //              seq4 = pasture
212                 //              seq3 = ocean
213                 
214                 //before this function seq1.pcount = pasture -> 1
215                 //after                            seq1.pcount = pasture -> 2, forest -> 1, ocean -> 1
216                 
217                 //before this function seq1.pgroups = pasture -> 1
218                 //after                            seq1.pgroups = pasture -> 1 since that is the dominant group
219
220                                 
221                 //go through each leaf and update its pcounts and pgroups
222                 
223                 //float A = clock();
224
225                 for (int i = 0; i < numLeaves; i++) {
226
227                         string name = tree[i].getName();
228                 
229                         map<string, string>::iterator itNames = nameMap.find(name);
230                 
231                         if (itNames == nameMap.end()) { m->mothurOut(name + " is not in your name file, please correct."); m->mothurOutEndLine(); exit(1);  }
232                         else {
233                                 vector<string> dupNames;
234                                 m->splitAtComma(nameMap[name], dupNames);
235                                 
236                                 map<string, int>::iterator itCounts;
237                                 int maxPars = 1;
238                                 set<string> groupsAddedForThisNode;
239                                 for (int j = 0; j < dupNames.size(); j++) {
240                                         
241                                         string group = tmap->getGroup(dupNames[j]);
242                                         
243                                         if (dupNames[j] != name) {//you already added yourself in the constructor
244                                 
245                                                 if (groupsAddedForThisNode.count(group) == 0)  {  groupNodeInfo[group].push_back(i);  groupsAddedForThisNode.insert(group);  } //if you have not already added this node for this group, then add it
246                                                 
247                                                 //update pcounts
248                                                 itCounts = tree[i].pcount.find(group);
249                                                 if (itCounts == tree[i].pcount.end()) { //new group, add it
250                                                         tree[i].pcount[group] = 1;
251                                                 }else {
252                                                         tree[i].pcount[group]++;
253                                                 }
254                                                         
255                                                 //update pgroups
256                                                 itCounts = tree[i].pGroups.find(group);
257                                                 if (itCounts == tree[i].pGroups.end()) { //new group, add it
258                                                         tree[i].pGroups[group] = 1;
259                                                 }else{
260                                                         tree[i].pGroups[group]++;
261                                                 }
262                                                 
263                                                 //keep highest group
264                                                 if(tree[i].pGroups[group] > maxPars){
265                                                         maxPars = tree[i].pGroups[group];
266                                                 }
267                                         }else {  groupsAddedForThisNode.insert(group);  } //add it so you don't add it to groupNodeInfo again
268                                 }//end for
269                                 
270                                 if (maxPars > 1) { //then we have some more dominant groups
271                                         //erase all the groups that are less than maxPars because you found a more dominant group.
272                                         for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();){
273                                                 if(it->second < maxPars){
274                                                         tree[i].pGroups.erase(it++);
275                                                 }else { it++; }
276                                         }
277                                         //set one remaining groups to 1
278                                         for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();it++){
279                                                 tree[i].pGroups[it->first] = 1;
280                                         }
281                                 }//end if
282                                 
283                                 //update groups to reflect all the groups this node represents
284                                 vector<string> nodeGroups;
285                                 map<string, int>::iterator itGroups;
286                                 for (itGroups = tree[i].pcount.begin(); itGroups != tree[i].pcount.end(); itGroups++) {
287                                         nodeGroups.push_back(itGroups->first);
288                                 }
289                                 tree[i].setGroup(nodeGroups);
290                                 
291                         }//end else
292                 }//end for              
293                 
294                 //float B = clock();
295                 //cout << "addNamesToCounts\t" << (B - A) / CLOCKS_PER_SEC << endl;     
296
297         }
298         catch(exception& e) {
299                 m->errorOut(e, "Tree", "addNamesToCounts");
300                 exit(1);
301         }
302 }
303 /*****************************************************************/
304 int Tree::getIndex(string searchName) {
305         try {
306                 //Treemap knows name, group and index to speed up search
307                 // getIndex function will return the vector index or -1 if seq is not found.
308                 int index = tmap->getIndex(searchName);
309                 return index;
310                 
311         }
312         catch(exception& e) {
313                 m->errorOut(e, "Tree", "getIndex");
314                 exit(1);
315         }
316 }
317 /*****************************************************************/
318
319 void Tree::setIndex(string searchName, int index) {
320         try {
321                 //set index in treemap
322                 tmap->setIndex(searchName, index);
323         }
324         catch(exception& e) {
325                 m->errorOut(e, "Tree", "setIndex");
326                 exit(1);
327         }
328 }
329 /*****************************************************************/
330 int Tree::assembleTree(map<string, string> nameMap) {
331         try {
332                 //save for later
333         names = nameMap;
334
335                 //if user has given a names file we want to include that info in the pgroups and pcount info.
336                 if(nameMap.size() != 0) {  addNamesToCounts(nameMap);  }
337                 
338                 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
339                 for (int i = numLeaves; i < numNodes; i++) {
340                         if (m->control_pressed) { return 1; }
341
342                         tree[i].pGroups = (mergeGroups(i));
343                         tree[i].pcount = (mergeGcounts(i));
344                 }
345                 
346                 return 0;
347         }
348         catch(exception& e) {
349                 m->errorOut(e, "Tree", "assembleTree");
350                 exit(1);
351         }
352 }
353 /*****************************************************************
354 int Tree::assembleTree(string n) {
355         try {
356                 
357                 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
358                 for (int i = numLeaves; i < numNodes; i++) {
359                         if (m->control_pressed) { return 1; }
360
361                         tree[i].pGroups = (mergeGroups(i));
362                         tree[i].pcount = (mergeGcounts(i));
363                 }
364                 //float B = clock();
365                 //cout << "assembleTree\t" << (B-A) / CLOCKS_PER_SEC << endl;
366                 return 0;
367         }
368         catch(exception& e) {
369                 m->errorOut(e, "Tree", "assembleTree");
370                 exit(1);
371         }
372 }
373 /*****************************************************************/
374 //assumes leaf node names are in groups and no names file - used by indicator command
375 void Tree::getSubTree(Tree* Ctree, vector<string> Groups) {
376         try {
377         
378         //copy Tree since we are going to destroy it
379         Tree* copy = new Tree(tmap);
380         copy->getCopy(Ctree);
381         map<string, string> empty;
382         copy->assembleTree(empty);
383         
384                 //we want to select some of the leaf nodes to create the output tree
385                 //go through the input Tree starting at parents of leaves
386                 for (int i = 0; i < numNodes; i++) {
387                         
388                         //initialize leaf nodes
389                         if (i <= (numLeaves-1)) {
390                                 tree[i].setName(Groups[i]);
391                                 
392                                 //save group info
393                                 string group = tmap->getGroup(Groups[i]);
394                                 vector<string> tempGroups; tempGroups.push_back(group);
395                                 tree[i].setGroup(tempGroups);
396                                 groupNodeInfo[group].push_back(i); 
397                                 
398                                 //set pcount and pGroup for groupname to 1.
399                                 tree[i].pcount[group] = 1;
400                                 tree[i].pGroups[group] = 1;
401                                 
402                                 //Treemap knows name, group and index to speed up search
403                                 tmap->setIndex(Groups[i], i);
404                                 
405                                 //intialize non leaf nodes
406                         }else if (i > (numLeaves-1)) {
407                                 tree[i].setName("");
408                                 vector<string> tempGroups;
409                                 tree[i].setGroup(tempGroups);
410                         }
411                 }
412                 
413                 set<int> removedLeaves;
414                 for (int i = 0; i < copy->getNumLeaves(); i++) {
415                         
416                         if (removedLeaves.count(i) == 0) {
417                         
418                         //am I in the group
419                         int parent = copy->tree[i].getParent();
420                         
421                         if (parent != -1) {
422                                 
423                                 if (m->inUsersGroups(copy->tree[i].getName(), Groups)) {
424                                         //find my siblings name
425                                         int parentRC = copy->tree[parent].getRChild();
426                                         int parentLC = copy->tree[parent].getLChild();
427                                         
428                                         //if I am the right child, then my sib is the left child
429                                         int sibIndex = parentRC;
430                                         if (parentRC == i) { sibIndex = parentLC; }
431                                         
432                                         string sibsName = copy->tree[sibIndex].getName();
433                                         
434                                         //if yes, is my sibling
435                                         if ((m->inUsersGroups(sibsName, Groups)) || (sibsName == "")) {
436                                                 //we both are okay no trimming required
437                                         }else{
438                                                 //i am, my sib is not, so remove sib by setting my parent to my grandparent
439                                                 int grandparent = copy->tree[parent].getParent();
440                                                 int grandparentLC = copy->tree[grandparent].getLChild();
441                                                 int grandparentRC = copy->tree[grandparent].getRChild();
442                                                 
443                                                 //whichever of my granparents children was my parent now equals me
444                                                 if (grandparentLC == parent) { grandparentLC = i; }
445                                                 else { grandparentRC = i; }
446                                                 
447                                                 copy->tree[i].setParent(grandparent);
448                                                 copy->tree[i].setBranchLength((copy->tree[i].getBranchLength()+copy->tree[parent].getBranchLength()));
449                                                 if (grandparent != -1) {
450                                                         copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
451                                                 }
452                                                 removedLeaves.insert(sibIndex);
453                                         }
454                                 }else{
455                                         //find my siblings name
456                                         int parentRC = copy->tree[parent].getRChild();
457                                         int parentLC = copy->tree[parent].getLChild();
458                                         
459                                         //if I am the right child, then my sib is the left child
460                                         int sibIndex = parentRC;
461                                         if (parentRC == i) { sibIndex = parentLC; }
462                                         
463                                         string sibsName = copy->tree[sibIndex].getName();
464                                         
465                                         //if no is my sibling
466                                         if ((m->inUsersGroups(sibsName, Groups)) || (sibsName == "")) {
467                                                 //i am not, but my sib is
468                                                 int grandparent = copy->tree[parent].getParent();
469                                                 int grandparentLC = copy->tree[grandparent].getLChild();
470                                                 int grandparentRC = copy->tree[grandparent].getRChild();
471                                                 
472                                                 //whichever of my granparents children was my parent now equals my sib
473                                                 if (grandparentLC == parent) { grandparentLC = sibIndex; }
474                                                 else { grandparentRC = sibIndex; }
475                                                 
476                                                 copy->tree[sibIndex].setParent(grandparent);
477                                                 copy->tree[sibIndex].setBranchLength((copy->tree[sibIndex].getBranchLength()+copy->tree[parent].getBranchLength()));
478                                                 if (grandparent != -1) {
479                                                         copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
480                                                 }
481                                                 removedLeaves.insert(i);
482                                         }else{
483                                                 //neither of us are, so we want to eliminate ourselves and our parent
484                                                 //so set our parents sib to our great-grandparent
485                                                 int parent = copy->tree[i].getParent();
486                                                 int grandparent = copy->tree[parent].getParent();
487                                                 int parentsSibIndex;
488                                                 if (grandparent != -1) {
489                                                         int greatgrandparent = copy->tree[grandparent].getParent();
490                                                         int greatgrandparentLC, greatgrandparentRC;
491                                                         if (greatgrandparent != -1) {
492                                                                 greatgrandparentLC = copy->tree[greatgrandparent].getLChild();
493                                                                 greatgrandparentRC = copy->tree[greatgrandparent].getRChild();
494                                                         }
495                                                         
496                                                         int grandparentLC = copy->tree[grandparent].getLChild();
497                                                         int grandparentRC = copy->tree[grandparent].getRChild();
498                                                         
499                                                         parentsSibIndex = grandparentLC;
500                                                         if (grandparentLC == parent) { parentsSibIndex = grandparentRC; }
501
502                                                         //whichever of my greatgrandparents children was my grandparent
503                                                         if (greatgrandparentLC == grandparent) { greatgrandparentLC = parentsSibIndex; }
504                                                         else { greatgrandparentRC = parentsSibIndex; }
505                                                         
506                                                         copy->tree[parentsSibIndex].setParent(greatgrandparent);
507                                                         copy->tree[parentsSibIndex].setBranchLength((copy->tree[parentsSibIndex].getBranchLength()+copy->tree[grandparent].getBranchLength()));
508                                                         if (greatgrandparent != -1) {
509                                                                 copy->tree[greatgrandparent].setChildren(greatgrandparentLC, greatgrandparentRC);
510                                                         }
511                                                 }else{
512                                                         copy->tree[parent].setParent(-1);
513                                                         //cout << "issues with making subtree" << endl;
514                                                 }
515                                                 removedLeaves.insert(sibIndex);
516                                                 removedLeaves.insert(i);
517                                         }
518                                 }
519                         }
520                         }
521                 }
522                 
523                 int root = 0;
524                 for (int i = 0; i < copy->getNumNodes(); i++) {
525                         //you found the root
526                         if (copy->tree[i].getParent() == -1) { root = i; break; }
527                 }
528         
529                 int nextSpot = numLeaves;
530                 populateNewTree(copy->tree, root, nextSpot);
531         
532         delete copy;
533         }
534         catch(exception& e) {
535                 m->errorOut(e, "Tree", "getSubTree");
536                 exit(1);
537         }
538 }
539 /*****************************************************************/
540 //assumes nameMap contains unique names as key or is empty. 
541 //assumes numLeaves defined in tree constructor equals size of seqsToInclude and seqsToInclude only contains unique seqs.
542 int Tree::getSubTree(Tree* copy, vector<string> seqsToInclude, map<string, string> nameMap) {
543         try {
544         
545         if (numLeaves != seqsToInclude.size()) { m->mothurOut("[ERROR]: numLeaves does not equal numUniques, cannot create subtree.\n"); m->control_pressed = true; return 0; }
546         
547         getSubTree(copy, seqsToInclude);
548         if (nameMap.size() != 0) {  addNamesToCounts(nameMap);  }
549         
550         //build the pGroups in non leaf nodes to be used in the parsimony calcs.
551                 for (int i = numLeaves; i < numNodes; i++) {
552                         if (m->control_pressed) { return 1; }
553             
554                         tree[i].pGroups = (mergeGroups(i));
555                         tree[i].pcount = (mergeGcounts(i));
556                 }
557         
558         return 0;
559     }
560         catch(exception& e) {
561                 m->errorOut(e, "Tree", "getSubTree");
562                 exit(1);
563         }
564 }
565 /*****************************************************************/
566 int Tree::populateNewTree(vector<Node>& oldtree, int node, int& index) {
567         try {
568                 
569                 if (oldtree[node].getLChild() != -1) {
570                         int rc = populateNewTree(oldtree, oldtree[node].getLChild(), index);
571                         int lc = populateNewTree(oldtree, oldtree[node].getRChild(), index);
572
573                         tree[index].setChildren(lc, rc);
574                         tree[rc].setParent(index);
575                         tree[lc].setParent(index);
576                         
577                         tree[index].setBranchLength(oldtree[node].getBranchLength());
578                         tree[rc].setBranchLength(oldtree[oldtree[node].getLChild()].getBranchLength());
579                         tree[lc].setBranchLength(oldtree[oldtree[node].getRChild()].getBranchLength());
580                         
581                         return (index++);
582                 }else { //you are a leaf
583                         int indexInNewTree = tmap->getIndex(oldtree[node].getName());
584                         return indexInNewTree;
585                 }
586         }
587         catch(exception& e) {
588                 m->errorOut(e, "Tree", "populateNewTree");
589                 exit(1);
590         }
591 }
592 /*****************************************************************/
593 void Tree::getCopy(Tree* copy) {
594         try {
595         
596                 //for each node in the tree copy its info
597                 for (int i = 0; i < numNodes; i++) {
598                         //copy name
599                         tree[i].setName(copy->tree[i].getName());
600                 
601                         //copy group
602                         tree[i].setGroup(copy->tree[i].getGroup());
603                         
604                         //copy branch length
605                         tree[i].setBranchLength(copy->tree[i].getBranchLength());
606                 
607                         //copy parent
608                         tree[i].setParent(copy->tree[i].getParent());
609                 
610                         //copy children
611                         tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
612                 
613                         //copy index in node and tmap
614                         tree[i].setIndex(copy->tree[i].getIndex());
615                         setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
616                         
617                         //copy pGroups
618                         tree[i].pGroups = copy->tree[i].pGroups;
619                 
620                         //copy pcount
621                         tree[i].pcount = copy->tree[i].pcount;
622                 }
623                 
624                 groupNodeInfo = copy->groupNodeInfo;
625                 
626         }
627         catch(exception& e) {
628                 m->errorOut(e, "Tree", "getCopy");
629                 exit(1);
630         }
631 }
632 /*****************************************************************/
633 //returns a map with a groupname and the number of times that group was seen in the children
634 //for instance if your children are white and black then it would return a map with 2 entries
635 // p[white] = 1 and p[black] = 1.  Now go up a level and merge that with a node who has p[white] = 1
636 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
637
638 map<string, int> Tree::mergeGroups(int i) {
639         try {
640                 int lc = tree[i].getLChild();
641                 int rc = tree[i].getRChild();
642
643                 //set parsimony groups to left child
644                 map<string,int> parsimony = tree[lc].pGroups;
645                 
646                 int maxPars = 1;
647
648                 //look at right child groups and update maxPars if right child has something higher for that group.
649                 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
650                         it2 = parsimony.find(it->first);
651                         if (it2 != parsimony.end()) {
652                                 parsimony[it->first]++;
653                         }else {
654                                 parsimony[it->first] = 1;
655                         }
656                         
657                         if(parsimony[it->first] > maxPars){
658                                 maxPars = parsimony[it->first];
659                         }
660                 }
661         
662                 // this is true if right child had a greater parsimony for a certain group
663                 if(maxPars > 1){
664                         //erase all the groups that are only 1 because you found something with 2.
665                         for(it=parsimony.begin();it!=parsimony.end();){
666                                 if(it->second == 1){
667                                         parsimony.erase(it++);
668                                 }else { it++; }
669                         }
670                         //set one remaining groups to 1
671                         //so with our above example p[white] = 2 would be left and it would become p[white] = 1
672                         for(it=parsimony.begin();it!=parsimony.end();it++){
673                                 parsimony[it->first] = 1;
674                         }
675                 
676                 }
677         
678                 return parsimony;
679         }
680         catch(exception& e) {
681                 m->errorOut(e, "Tree", "mergeGroups");
682                 exit(1);
683         }
684 }
685 /*****************************************************************/
686 //returns a map with a groupname and the number of times that group was seen in the children
687 //for instance if your children are white and black then it would return a map with 2 entries
688 // p[white] = 1 and p[black] = 1.  Now go up a level and merge that with a node who has p[white] = 1
689 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
690
691 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
692         try {
693         
694                 int lc = tree[i].getLChild();
695                 int rc = tree[i].getRChild();
696                 
697                 //loop through nodes groups removing the ones the user doesn't want
698                 for(it=tree[lc].pGroups.begin();it!=tree[lc].pGroups.end();){
699                                 if (m->inUsersGroups(it->first, g) != true) {
700                                         tree[lc].pGroups.erase(it++);
701                                 }else { it++; }
702                 }
703
704                 //loop through nodes groups removing the ones the user doesn't want
705                 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();){
706                                 if (m->inUsersGroups(it->first, g) != true) {
707                                         tree[rc].pGroups.erase(it++);
708                                 }else { it++; }
709                 }
710
711                 //set parsimony groups to left child
712                 map<string,int> parsimony = tree[lc].pGroups;
713                 
714                 int maxPars = 1;
715
716                 //look at right child groups and update maxPars if right child has something higher for that group.
717                 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
718                         it2 = parsimony.find(it->first);
719                         if (it2 != parsimony.end()) {
720                                 parsimony[it->first]++;
721                         }else {
722                                 parsimony[it->first] = 1;
723                         }
724                         
725                         if(parsimony[it->first] > maxPars){
726                                 maxPars = parsimony[it->first];
727                         }
728                 }
729                         
730                 // this is true if right child had a greater parsimony for a certain group
731                 if(maxPars > 1){
732                         //erase all the groups that are only 1 because you found something with 2.
733                         for(it=parsimony.begin();it!=parsimony.end();){
734                                 if(it->second == 1){
735                                         parsimony.erase(it++);
736                                 }else { it++; }
737                         }
738
739                         for(it=parsimony.begin();it!=parsimony.end();it++){
740                                 parsimony[it->first] = 1;
741                         }
742                 }               
743                 
744                 return parsimony;
745         }
746         catch(exception& e) {
747                 m->errorOut(e, "Tree", "mergeUserGroups");
748                 exit(1);
749         }
750 }
751
752
753 /**************************************************************************************************/
754
755 map<string,int> Tree::mergeGcounts(int position) {
756         try{
757                 map<string,int>::iterator pos;
758         
759                 int lc = tree[position].getLChild();
760                 int rc = tree[position].getRChild();
761         
762                 map<string,int> sum = tree[lc].pcount;
763     
764                 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
765                         sum[it->first] += it->second;
766                 }
767                 return sum;
768         }
769         catch(exception& e) {
770                 m->errorOut(e, "Tree", "mergeGcounts");
771                 exit(1);
772         }
773 }
774 /**************************************************************************************************/
775 void Tree::randomLabels(vector<string> g) {
776         try {
777         
778                 //initialize groupNodeInfo
779                 for (int i = 0; i < (tmap->getNamesOfGroups()).size(); i++) {
780                         groupNodeInfo[(tmap->getNamesOfGroups())[i]].resize(0);
781                 }
782                 
783                 for(int i = 0; i < numLeaves; i++){
784                         int z;
785                         //get random index to switch with
786                         z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));        
787                         
788                         //you only want to randomize the nodes that are from a group the user wants analyzed, so
789                         //if either of the leaf nodes you are about to switch are not in the users groups then you don't want to switch them.
790                         bool treez, treei;
791                 
792                         treez = m->inUsersGroups(tree[z].getGroup(), g);
793                         treei = m->inUsersGroups(tree[i].getGroup(), g);
794                         
795                         if ((treez == true) && (treei == true)) {
796                                 //switches node i and node z's info.
797                                 map<string,int> lib_hold = tree[z].pGroups;
798                                 tree[z].pGroups = (tree[i].pGroups);
799                                 tree[i].pGroups = (lib_hold);
800                                 
801                                 vector<string> zgroup = tree[z].getGroup();
802                                 tree[z].setGroup(tree[i].getGroup());
803                                 tree[i].setGroup(zgroup);
804                                 
805                                 string zname = tree[z].getName();
806                                 tree[z].setName(tree[i].getName());
807                                 tree[i].setName(zname);
808                                 
809                                 map<string,int> gcount_hold = tree[z].pcount;
810                                 tree[z].pcount = (tree[i].pcount);
811                                 tree[i].pcount = (gcount_hold);
812                         }
813                         
814                         for (int k = 0; k < (tree[i].getGroup()).size(); k++) {  groupNodeInfo[(tree[i].getGroup())[k]].push_back(i); }
815                         for (int k = 0; k < (tree[z].getGroup()).size(); k++) {  groupNodeInfo[(tree[z].getGroup())[k]].push_back(z); }
816                 }
817         }
818         catch(exception& e) {
819                 m->errorOut(e, "Tree", "randomLabels");
820                 exit(1);
821         }
822 }
823 /**************************************************************************************************/
824 void Tree::randomBlengths()  {
825         try {
826                 for(int i=numNodes-1;i>=0;i--){
827                         int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));    
828                 
829                         float bl_hold = tree[z].getBranchLength();
830                         tree[z].setBranchLength(tree[i].getBranchLength());
831                         tree[i].setBranchLength(bl_hold);
832                 }
833         }
834         catch(exception& e) {
835                 m->errorOut(e, "Tree", "randomBlengths");
836                 exit(1);
837         }
838 }
839 /*************************************************************************************************/
840 void Tree::assembleRandomUnifracTree(vector<string> g) {
841         randomLabels(g);
842     map<string, string> empty;
843         assembleTree(empty);
844 }
845 /*************************************************************************************************/
846 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
847         vector<string> temp; temp.push_back(groupA); temp.push_back(groupB);
848         randomLabels(temp);
849     map<string, string> empty;
850         assembleTree(empty);
851 }
852
853 /*************************************************************************************************/
854 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
855 void Tree::assembleRandomTree() {
856         randomTopology();
857     map<string, string> empty;
858         assembleTree(empty);
859 }
860 /**************************************************************************************************/
861
862 void Tree::randomTopology() {
863         try {
864                 for(int i=0;i<numNodes;i++){
865                         tree[i].setParent(-1);
866                 }
867                 for(int i=numLeaves;i<numNodes;i++){
868                         tree[i].setChildren(-1, -1); 
869                 }
870     
871                 for(int i=numLeaves;i<numNodes;i++){
872                         int escape =0;
873                         int rnd_index1, rnd_index2;
874                         while(escape == 0){
875                                 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
876                                 if(tree[rnd_index1].getParent() == -1){escape = 1;}
877                         }
878                 
879                         escape = 0;
880                         while(escape == 0){
881                                 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
882                                 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
883                                         escape = 1;
884                                 }               
885                         }
886         
887                         tree[i].setChildren(rnd_index1,rnd_index2);
888                         tree[i].setParent(-1);
889                         tree[rnd_index1].setParent(i);
890                         tree[rnd_index2].setParent(i);
891                 }
892         }
893         catch(exception& e) {
894                 m->errorOut(e, "Tree", "randomTopology");
895                 exit(1);
896         }
897 }
898 /*****************************************************************/
899 void Tree::print(ostream& out) {
900         try {
901                 int root = findRoot();
902                 printBranch(root, out, "branch");
903                 out << ";" << endl;
904         }
905         catch(exception& e) {
906                 m->errorOut(e, "Tree", "print");
907                 exit(1);
908         }
909 }
910 /*****************************************************************/
911 void Tree::print(ostream& out, map<string, string> nameMap) {
912         try {
913                 int root = findRoot();
914                 printBranch(root, out, nameMap);
915                 out << ";" << endl;
916         }
917         catch(exception& e) {
918                 m->errorOut(e, "Tree", "print");
919                 exit(1);
920         }
921 }
922 /*****************************************************************/
923 void Tree::print(ostream& out, string mode) {
924         try {
925                 int root = findRoot();
926                 printBranch(root, out, mode);
927                 out << ";" << endl;
928         }
929         catch(exception& e) {
930                 m->errorOut(e, "Tree", "print");
931                 exit(1);
932         }
933 }
934 /*****************************************************************/
935 // This prints out the tree in Newick form.
936 void Tree::createNewickFile(string f) {
937         try {
938                 int root = findRoot();
939         
940                 filename = f;
941
942                 m->openOutputFile(filename, out);
943                 
944                 printBranch(root, out, "branch");
945                 
946                 // you are at the end of the tree
947                 out << ";" << endl;
948                 out.close();
949         }
950         catch(exception& e) {
951                 m->errorOut(e, "Tree", "createNewickFile");
952                 exit(1);
953         }
954 }
955
956 /*****************************************************************/
957 //This function finds the index of the root node.
958
959 int Tree::findRoot() {
960         try {
961                 for (int i = 0; i < numNodes; i++) {
962                         //you found the root
963                         if (tree[i].getParent() == -1) { return i; }
964                         //cout << "i = " << i << endl;
965                         //cout << "i's parent = " << tree[i].getParent() << endl;  
966                 }
967                 return -1;
968         }
969         catch(exception& e) {
970                 m->errorOut(e, "Tree", "findRoot");
971                 exit(1);
972         }
973 }
974 /*****************************************************************/
975 void Tree::printBranch(int node, ostream& out, map<string, string> names) {
976 try {
977
978 // you are not a leaf
979                 if (tree[node].getLChild() != -1) {
980                         out << "(";
981                         printBranch(tree[node].getLChild(), out, names);
982                         out << ",";
983                         printBranch(tree[node].getRChild(), out, names);
984                         out << ")";
985                         
986             //if there is a branch length then print it
987             if (tree[node].getBranchLength() != -1) {
988                 out << ":" << tree[node].getBranchLength();
989             }
990                         
991                 }else { //you are a leaf
992             map<string, string>::iterator itNames = names.find(tree[node].getName());
993             
994             string outputString = "";
995             if (itNames != names.end()) { 
996                 
997                 vector<string> dupNames;
998                 m->splitAtComma((itNames->second), dupNames);
999                 
1000                 if (dupNames.size() == 1) {
1001                     outputString += tree[node].getName();
1002                     if (tree[node].getBranchLength() != -1) {
1003                         outputString += ":" + toString(tree[node].getBranchLength());
1004                     }
1005                 }else {
1006                     outputString += "(";
1007                     
1008                     for (int u = 0; u < dupNames.size()-1; u++) {
1009                         outputString += dupNames[u];
1010                         
1011                         if (tree[node].getBranchLength() != -1) {
1012                             outputString += ":" + toString(0.0);
1013                         }
1014                         outputString += ",";
1015                     }
1016                     
1017                     outputString += dupNames[dupNames.size()-1];
1018                     if (tree[node].getBranchLength() != -1) {
1019                         outputString += ":" + toString(0.0);
1020                     }
1021                     
1022                     outputString += ")";
1023                     if (tree[node].getBranchLength() != -1) {
1024                         outputString += ":" + toString(tree[node].getBranchLength());
1025                     }
1026                 }
1027             }else { 
1028                 outputString = tree[node].getName();
1029                 //if there is a branch length then print it
1030                 if (tree[node].getBranchLength() != -1) {
1031                     outputString += ":" + toString(tree[node].getBranchLength());
1032                 }
1033                 
1034                 m->mothurOut("[ERROR]: " + tree[node].getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); 
1035             }
1036                 
1037             out << outputString;
1038                 }
1039                 
1040         }
1041         catch(exception& e) {
1042                 m->errorOut(e, "Tree", "printBranch");
1043                 exit(1);
1044         }
1045 }
1046 /*****************************************************************/
1047 void Tree::printBranch(int node, ostream& out, string mode) {
1048     try {
1049         
1050         // you are not a leaf
1051                 if (tree[node].getLChild() != -1) {
1052                         out << "(";
1053                         printBranch(tree[node].getLChild(), out, mode);
1054                         out << ",";
1055                         printBranch(tree[node].getRChild(), out, mode);
1056                         out << ")";
1057                         if (mode == "branch") {
1058                                 //if there is a branch length then print it
1059                                 if (tree[node].getBranchLength() != -1) {
1060                                         out << ":" << tree[node].getBranchLength();
1061                                 }
1062                         }else if (mode == "boot") {
1063                                 //if there is a label then print it
1064                                 if (tree[node].getLabel() != -1) {
1065                                         out << tree[node].getLabel();
1066                                 }
1067                         }else if (mode == "both") {
1068                                 if (tree[node].getLabel() != -1) {
1069                                         out << tree[node].getLabel();
1070                                 }
1071                                 //if there is a branch length then print it
1072                                 if (tree[node].getBranchLength() != -1) {
1073                                         out << ":" << tree[node].getBranchLength();
1074                                 }
1075                         }
1076                 }else { //you are a leaf
1077                         string leafGroup = tmap->getGroup(tree[node].getName());
1078                         
1079                         if (mode == "branch") {
1080                                 out << leafGroup; 
1081                                 //if there is a branch length then print it
1082                                 if (tree[node].getBranchLength() != -1) {
1083                                         out << ":" << tree[node].getBranchLength();
1084                                 }
1085                         }else if (mode == "boot") {
1086                                 out << leafGroup; 
1087                                 //if there is a label then print it
1088                                 if (tree[node].getLabel() != -1) {
1089                                         out << tree[node].getLabel();
1090                                 }
1091                         }else if (mode == "both") {
1092                                 out << tree[node].getName();
1093                                 if (tree[node].getLabel() != -1) {
1094                                         out << tree[node].getLabel();
1095                                 }
1096                                 //if there is a branch length then print it
1097                                 if (tree[node].getBranchLength() != -1) {
1098                                         out << ":" << tree[node].getBranchLength();
1099                                 }
1100                         }
1101                 }
1102                 
1103         }
1104         catch(exception& e) {
1105                 m->errorOut(e, "Tree", "printBranch");
1106                 exit(1);
1107         }
1108 }
1109 /*****************************************************************/
1110 void Tree::printBranch(int node, ostream& out, string mode, vector<Node>& theseNodes) {
1111         try {
1112                 
1113                 // you are not a leaf
1114                 if (theseNodes[node].getLChild() != -1) {
1115                         out << "(";
1116                         printBranch(theseNodes[node].getLChild(), out, mode);
1117                         out << ",";
1118                         printBranch(theseNodes[node].getRChild(), out, mode);
1119                         out << ")";
1120                         if (mode == "branch") {
1121                                 //if there is a branch length then print it
1122                                 if (theseNodes[node].getBranchLength() != -1) {
1123                                         out << ":" << theseNodes[node].getBranchLength();
1124                                 }
1125                         }else if (mode == "boot") {
1126                                 //if there is a label then print it
1127                                 if (theseNodes[node].getLabel() != -1) {
1128                                         out << theseNodes[node].getLabel();
1129                                 }
1130                         }else if (mode == "both") {
1131                                 if (theseNodes[node].getLabel() != -1) {
1132                                         out << theseNodes[node].getLabel();
1133                                 }
1134                                 //if there is a branch length then print it
1135                                 if (theseNodes[node].getBranchLength() != -1) {
1136                                         out << ":" << theseNodes[node].getBranchLength();
1137                                 }
1138                         }
1139                 }else { //you are a leaf
1140                         string leafGroup = tmap->getGroup(theseNodes[node].getName());
1141                         
1142                         if (mode == "branch") {
1143                                 out << leafGroup; 
1144                                 //if there is a branch length then print it
1145                                 if (theseNodes[node].getBranchLength() != -1) {
1146                                         out << ":" << theseNodes[node].getBranchLength();
1147                                 }
1148                         }else if (mode == "boot") {
1149                                 out << leafGroup; 
1150                                 //if there is a label then print it
1151                                 if (theseNodes[node].getLabel() != -1) {
1152                                         out << theseNodes[node].getLabel();
1153                                 }
1154                         }else if (mode == "both") {
1155                                 out << theseNodes[node].getName();
1156                                 if (theseNodes[node].getLabel() != -1) {
1157                                         out << theseNodes[node].getLabel();
1158                                 }
1159                                 //if there is a branch length then print it
1160                                 if (theseNodes[node].getBranchLength() != -1) {
1161                                         out << ":" << theseNodes[node].getBranchLength();
1162                                 }
1163                         }
1164                 }
1165                 
1166         }
1167         catch(exception& e) {
1168                 m->errorOut(e, "Tree", "printBranch");
1169                 exit(1);
1170         }
1171 }
1172 /*****************************************************************/
1173
1174 void Tree::printTree() {
1175         
1176         for(int i=0;i<numNodes;i++){
1177                 cout << i << '\t';
1178                 tree[i].printNode();
1179         }
1180         
1181 }
1182
1183 /*****************************************************************/
1184 //this code is a mess and should be rethought...-slw
1185 void Tree::parseTreeFile() {
1186         
1187         //only takes names from the first tree and assumes that all trees use the same names.
1188         try {
1189                 string filename = m->getTreeFile();
1190                 ifstream filehandle;
1191                 m->openInputFile(filename, filehandle);
1192                 int c, comment;
1193                 comment = 0;
1194                 int done = 1;
1195                 
1196                 //ifyou are not a nexus file 
1197                 if((c = filehandle.peek()) != '#') {  
1198                         while((c = filehandle.peek()) != ';') { 
1199                                 while ((c = filehandle.peek()) != ';') {
1200                                         // get past comments
1201                                         if(c == '[') {
1202                                                 comment = 1;
1203                                         }
1204                                         if(c == ']'){
1205                                                 comment = 0;
1206                                         }
1207                                         if((c == '(') && (comment != 1)){ break; }
1208                                         filehandle.get();
1209                                 }
1210
1211                                 done = readTreeString(filehandle); 
1212                                 if (done == 0) { break; }
1213                         }
1214                 //ifyou are a nexus file
1215                 }else if((c = filehandle.peek()) == '#') {
1216                         string holder = "";
1217                                         
1218                         // get past comments
1219                         while(holder != "translate" && holder != "Translate"){  
1220                                 if(holder == "[" || holder == "[!"){
1221                                         comment = 1;
1222                                 }
1223                                 if(holder == "]"){
1224                                         comment = 0;
1225                                 }
1226                                 filehandle >> holder; 
1227
1228                                 //if there is no translate then you must read tree string otherwise use translate to get names
1229                                 if((holder == "tree") && (comment != 1)){       
1230                                         //pass over the "tree rep.6878900 = "
1231                                         while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF)) {;}
1232
1233                                         if(c == EOF) { break; }
1234                                         filehandle.putback(c);  //put back first ( of tree.
1235                                         done = readTreeString(filehandle);
1236         
1237                                         break;
1238                                 }
1239                         
1240                                 if (done == 0) { break;  }
1241                         }
1242                         
1243                         //use nexus translation rather than parsing tree to save time
1244                         if((holder == "translate") || (holder == "Translate")) {
1245
1246                                 string number, name, h;
1247                                 h = ""; // so it enters the loop the first time
1248                                 while((h != ";") && (number != ";")) { 
1249                                         filehandle >> number;
1250                                         filehandle >> name;
1251         
1252                                         //c = , until done with translation then c = ;
1253                                         h = name.substr(name.length()-1, name.length()); 
1254                                         name.erase(name.end()-1);  //erase the comma
1255                                         m->Treenames.push_back(number);
1256                                 }
1257                                 if(number == ";") { m->Treenames.pop_back(); }  //in case ';' from translation is on next line instead of next to last name
1258                         }
1259                 }
1260                 filehandle.close();
1261                 
1262                 //for (int i = 0; i < globaldata->Treenames.size(); i++) {
1263 //cout << globaldata->Treenames[i] << endl; }
1264 //cout << globaldata->Treenames.size() << endl;
1265         }
1266         catch(exception& e) {
1267                 m->errorOut(e, "Tree", "parseTreeFile");
1268                 exit(1);
1269         }
1270 }
1271 /*******************************************************/
1272
1273 /*******************************************************/
1274 int Tree::readTreeString(ifstream& filehandle)  {
1275         try {
1276                 int c;
1277                 string name;  //, k
1278                 
1279                 while((c = filehandle.peek()) != ';') { 
1280 //k = c;
1281 //cout << " at beginning of while " <<  k << endl;                      
1282                         if(c == ')')  {    
1283                                 //to pass over labels in trees
1284                                 c=filehandle.get();
1285                                 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
1286                                 filehandle.putback(c);
1287                         }
1288                         if(c == ';') { return 0; }
1289                         if(c == -1) { return 0; }
1290                         //if you are a name
1291                         if((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
1292                                 name = "";
1293                                 c = filehandle.get();
1294                         //k = c;
1295 //cout << k << endl;
1296                                 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {                       
1297                                         name += c;
1298                                         c = filehandle.get();
1299                         //k = c;
1300 //cout << " in name while " << k << endl;
1301                                 }
1302                                 
1303 //cout << "name = " << name << endl;
1304                                 m->Treenames.push_back(name);
1305                                 filehandle.putback(c);
1306 //k = c;
1307 //cout << " after putback" <<  k << endl;
1308                         } 
1309                         
1310                         if(c  == ':') { //read until you reach the end of the branch length
1311                                 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
1312                                         c = filehandle.get();
1313         //k = c;
1314         //cout << " in branch while " << k << endl;
1315                                 }
1316                                 filehandle.putback(c);
1317                         }
1318                 
1319                         c = filehandle.get();
1320 //k = c;
1321         //cout << " here after get " << k << endl;
1322                         if(c == ';') { return 0; }
1323                         if(c == ')') { filehandle.putback(c); }
1324         //k = c;
1325 //cout << k << endl;
1326
1327                 }
1328                 return 0;
1329         }
1330         catch(exception& e) {
1331                 m->errorOut(e, "Tree", "readTreeString");
1332                 exit(1);
1333         }
1334 }       
1335
1336 /*******************************************************/
1337
1338 /*******************************************************/
1339