-
Notifications
You must be signed in to change notification settings - Fork 5
/
TreeOPE.cpp
1875 lines (1679 loc) · 45 KB
/
TreeOPE.cpp
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
//##########################################################################
//# This software is part of the Treescaper i
//# -- Version 0.1
//# Copyright (C) 2010 Wen Huang
//#
//# This program is free software; you can redistribute it and/or
//# modify it under the terms of the GNU General Public License
//# as published by the Free Software Foundation; either version 2
//# of the License, or (at your option) any later version.
//#
//# This program is distributed in the hope that it will be useful,
//# but WITHOUT ANY WARRANTY; without even the implied warranty of
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//# GNU General Public License for more details.
//# http://www.gnu.org/copyleft/gpl.html
//##########################################################################
// TreeOPE.cpp
// Member function definitions for class TreeOPE.h
// by whuang gzhou
#ifndef TREEOPE_CPP
#define TREEOPE_CPP
#include "TreeOPE.h"
#define __HALF_MAX_SIGNED(type) ((type)1 << (sizeof(type)*8-2))
#define __MAX_SIGNED(type) (__HALF_MAX_SIGNED(type) - 1 + __HALF_MAX_SIGNED(type))
#define __MIN_SIGNED(type) (-1 - __MAX_SIGNED(type))
#define __MIN(type) ((type)-1 < 1?__MIN_SIGNED(type):(type)0)
#define __MAX(type) ((type)~__MIN(type))
//#ifndef _WIN32
#define assign(dest,src) ({ \
typeof(src) __x=(src); \
typeof(dest) __y=__x; \
(__x==__y && ((__x<1) == (__y<1)) ? (void)((dest)=__y),0:1); \
})
#define add_of(c,a,b) ({ \
typeof(a) __a=a; \
typeof(b) __b=b; \
(__b)<1 ? \
((__MIN(typeof(c))-(__b)<=(__a)) ? assign(c,__a+__b):1) : \
((__MAX(typeof(c))-(__b)>=(__a)) ? assign(c,__a+__b):1); \
})
//#else
//// This is not correct and need to be rewitten.
// template<typename T1, typename T2, typename T3>
// T3 assign(T1 dest, T2 src)
// {
// return dest;
// }
// template<typename T1, typename T2, typename T3>
// T1 add_of(T1 c, T2 a, T3 b)
// {
// return a+b;
// }
// #define assign(dest,src) ({ \
// typeid(src) __x=(src); \
// typeid(dest) __y=__x; \
// (__x==__y && ((__x<1) == (__y<1)) ? (void)((dest)=__y),0:1); \
// })
// #define add_of(c,a,b) ({ \
// typeid(a) __a=a; \
// typeid(b) __b=b; \
// (__b)<1 ? \
// ((__MIN(typeid(c))-(__b)<=(__a)) ? assign(c,__a+__b):1) : \
// ((__MAX(typeid(c))-(__b)>=(__a)) ? assign(c,__a+__b):1); \
// })
//#endif
/*
load a newick tree from a file
Params: fname - path to file
error - return for error code
Returns: the loaded object on success, 0 on fail
Error codes: -1 out of memory
-2 parse error
-3 can't open file
*/
NEWICKTREE *TreeOPE::loadnewicktree(char *fname, int *error)
{
NEWICKTREE *answer = NULL;
FILE *fp = NULL;
int err;
fp = fopen(fname, "r");
if(!fp)
{
err = -3;
goto error_exit;
}
answer = floadnewicktree(fp, &err);
if(!answer)
goto error_exit;
if(error)
*error = 0;
return answer;
error_exit:
if(error)
*error = err;
return 0;
}
NEWICKTREE *TreeOPE::loadnewicktree2(FILE *fp, int *error)
{
NEWICKTREE *answer = NULL;
// FILE *fp;
int err;
// fp = fopen(fname, "r");
// if(!fp)
// {
// err = -3;
// goto error_exit;
// }
answer = floadnewicktree(fp, &err);
if(!answer)
goto error_exit;
if(error)
*error = 0;
return answer;
error_exit:
if(error)
*error = err;
return 0;
}
/*
load newick tree from an opened stream
Params: fp - input stream
error - return for error
Returns: pointer to object, 0 on fail
Error codes -1 out of memory
-2 parse error
Notes: format allows several trees to be stored in a file
*/
NEWICKTREE *TreeOPE::floadnewicktree(FILE *fp, int *error)
{
NEWICKTREE *answer = NULL;
int err;
int ch;
answer = (NEWICKTREE *) malloc(sizeof(NEWICKTREE));
if(!answer)
{
err = -1;
goto error_exit;
}
skipspace(fp);
ch = fgetc(fp);
if(ch == '(' )
{
answer->root = loadnode(fp, &err);
if(!answer->root || err != 0)
goto error_exit;
}
skipspace(fp);
ch = fgetc(fp);
if(ch != ';')
{
err = -2;
goto error_exit;
}
if(error)
*error = 0;
return answer;
error_exit:
killnewicktree(answer);
if(error)
*error = err;
return 0;
}
/*
newick tree destructor
*/
void TreeOPE::killnewicktree(NEWICKTREE *tree)
{
// rewind(stdin);
// setbuf(stdout, NULL);
if(tree)
{
killnoder(tree->root);
free(tree);
}
}
/*
node destructor (recursive)
*/
void TreeOPE::killnoder(NEWICKNODE *node)
{
if(!node)
return;
for(int i = 0;i < node->Nchildren; i++)
{
killnoder(node->child[i]);
}
setbuf(stdout, NULL);
free(node->label);
if(node->bitstr != NULL)
{
delete node->bitstr;
node->bitstr = NULL;
}
free(node->child);
free(node);
}
void TreeOPE::killnewicktreeBitstr(NEWICKTREE *tree)
{
if(tree)
{
killnoderBitstr(tree->root);
}
}
/*
node bitstr destructor (recursive)
*/
void TreeOPE::killnoderBitstr(NEWICKNODE *node)
{
for(int i = 0;i < node->Nchildren; i++)
{
killnoderBitstr(node->child[i]);
}
if(node->bitstr != NULL)
{
delete node->bitstr;
node->bitstr = NULL;
}
}
/*
turns a string into a label suitable for use in the tree
Params: str - the string
Returns: modified string, 0 on out of memory
Notes: strings containing spaces have them replaced by underscores
strings contianing illegal characters are quoted
null pointer is returned as the empty string ""
*/
char *TreeOPE::makenewicklabel(const char *str)
{
const char *cptr = NULL;
char *vptr = NULL;
int needsquote = 0;
char *answer = NULL;
if(!str)
return mystrdup("");
cptr = str;
while(*cptr)
{
if(strchr("\'()[]:;,", *cptr))
needsquote = 1;
if(isspace(*cptr) && *cptr != ' ')
needsquote = 1;
cptr++;
}
if(needsquote)
{
answer = (char *) malloc(strlen(str) + 2 + mystrcount(str, '\''));
vptr = answer;
*vptr++ = '\'';
while(*str)
{
*vptr = *str;
if(*str == '\'')
{
vptr++;
*vptr = '\'';
}
vptr++;
str++;
}
*vptr++ = '\'';
*vptr = 0;
}
else
{
answer = mystrdup(str);
vptr = answer;
while(*vptr)
{
if(*vptr == ' ' )
*vptr = '_';
vptr++;
}
}
delete cptr;
free(vptr);
return answer;
}
/*
load a node from the file
Params: fp - the input stream
error - return for error
Returns: node loaded.
Notes: recursive. Expects the opening parenthesis to have been eaten
*/
NEWICKNODE *TreeOPE::loadnode(FILE *fp, int *error)
{
NEWICKNODE *answer = NULL;
int err;
NEWICKNODE *child = 0;
int ch;
answer = (NEWICKNODE *) malloc(sizeof(NEWICKNODE));
//printf("sizeof(NEWICKNODE) = %d\n", sizeof(NEWICKNODE));
if(!answer)
{
err = -1;
goto error_exit;
}
answer->Nchildren = 0;
answer->label = 0;
answer->child = 0;
answer->hv1 = 0;
answer->hv2 = 0;
answer->bitstr = NULL;
skipspace(fp);
do
{
ch = fgetc(fp);
if(ch == '(')
{
child = loadnode(fp, &err);
if(!child)
goto error_exit;
if(addchild(answer, child ) == -1)
{
err = -1;
goto error_exit;
}
child = 0;
}
else
{
ungetc(ch, fp);
child = loadleaf(fp, &err);
if(!child)
goto error_exit;
if(addchild(answer, child) == -1)
{
err = -1;
goto error_exit;
}
child = 0;
}
skipspace(fp);
ch = fgetc(fp);
} while(ch == ',');
if(ch == ')')
{
answer->label = readlabelandweight(fp, &answer->weight, &err);
if(err)
goto error_exit;
}
else
{
err = -2;
goto error_exit;
}
if(error)
*error = 0;
return answer;
error_exit:
if(child)
killnoder(child);
killnoder(answer);
if(error)
*error = err;
return 0;
}
/*
add a child to a node
Params: parent - parent node
child - child to add
Returns: 0 on success, -1 on fail
*/
int TreeOPE::addchild(NEWICKNODE *parent, NEWICKNODE *child)
{
NEWICKNODE **temp = NULL;
temp = (NEWICKNODE **) realloc(parent->child, (parent->Nchildren + 1) * sizeof(NEWICKNODE *));
// std::cout << "addchild:" << (long) temp << std::endl;//-- WHtest
// if(child->label != '\0')
// std::cout << "cname:" << child->label << std::endl;//--- WHtest
if(!temp)
return -1;
parent->child = temp;
parent->child[parent->Nchildren] = child;
parent->Nchildren++;
return 0;
}
/*
load a leaf node
Params: fp - the input stream
error - return for error
Returns: node object
*/
NEWICKNODE *TreeOPE::loadleaf(FILE *fp, int *error)
{
NEWICKNODE *answer = NULL;
int err;
answer = (NEWICKNODE *) malloc(sizeof(NEWICKNODE));
if(!answer)
{
err = -1;
goto error_exit;
}
answer->Nchildren = 0;
answer->child = 0;
answer->label = readlabelandweight(fp, &answer->weight, &err);
answer->bitstr = NULL;
if(err)
goto error_exit;
return answer;
error_exit:
if(error)
*error = err;
if(answer)
{
free(answer->label);
free(answer);
}
return 0;
}
/*
read label, colon and associated weight
Params: fp - the input stream
weight - return for weight
error - return for error
Returns: the label
Notes: a null label is not an error
*/
char *TreeOPE::readlabelandweight(FILE *fp, double *weight, int *error)
{
char *answer = NULL;
int ch;
*weight = 0;
answer = readlabel(fp);
if(!answer)
{
*error = -1;
return 0;
}
skipspace(fp);
ch = fgetc(fp);
if(ch == ':')
{
if( fscanf(fp, "%lf", weight) != 1)
{
*error = -2;
free(answer);
return 0;
}
}
else
ungetc(ch, fp);
if(*answer == 0)
{
free(answer);
answer = 0;
}
*error = 0;
return answer;
}
/*
readlabel - read a label from stream
Params: fp - the input stream
Returns: allocarted label
Notes: null label is the null string ""
*/
char *TreeOPE::readlabel(FILE *fp)
{
char *answer = NULL;
int ch;
skipspace(fp);
ch = fgetc(fp);
ungetc(ch, fp);
if(ch == '\'')
answer = readquotedlabel(fp);
else
answer = readplainlabel(fp);
return answer;
}
/*
read a quoted label from stream
Params: fp - the input stream
Returns: the label. null string is ""
*/
char *TreeOPE::readquotedlabel(FILE *fp)
{
char *answer = NULL;
char *temp = NULL;
int capacity = 32 * sizeof(char);
int len = 0;
int ch;
answer = (char *) malloc(capacity);
if(!answer)
return 0;
ch = fgetc(fp);
while( (ch = fgetc(fp)) != EOF)
{
if(ch == '\'')
{
ch = fgetc(fp);
if(ch == '\'')
answer[len++] = (char) ch;
else
{
ungetc(ch, fp);
answer[len] = 0;
return answer;
}
}
else
answer[len++] = (char) ch;
if(len == capacity - 1)
{
temp = (char *) realloc(answer, capacity * 2);
if(!temp)
{
free(answer);
return 0;
}
answer = temp;
capacity = capacity * 2;
}
}
answer[len] = 0;
return answer;
}
/*
read aplian (unquoted) label
Params: fp - the open file
Returns: the label, null string is ""
*/
char *TreeOPE::readplainlabel(FILE *fp)
{
char *answer = NULL;
char *temp = NULL;
int len = 0;
int capacity = 32 * sizeof(char);
int ch;
answer = (char *) malloc(capacity);
if(!answer)
return 0;
while( (ch = fgetc(fp)) != EOF)
{
if(isspace(ch) || strchr(" ()[]\':;,", ch))
{
ungetc(ch, fp);
answer[len] = 0;
return answer;
}
if(ch == '_')
ch = '_'; // ssj
answer[len++] = (char) ch;
if(len == capacity - 1)
{
temp = (char *) realloc(answer, capacity * 2);
if(!temp)
{
free(answer);
return 0;
}
answer = temp;
capacity = capacity * 2;
}
}
answer[len] = 0;
return answer;
}
/*
consume space in input stream.
Params: fp - the input stream
*/
void TreeOPE::skipspace(FILE *fp)
{
int ch;
while( (ch = fgetc(fp)) != EOF )
if(!isspace(ch))
{
ungetc(ch, fp);
break;
}
}
/*
strduo implemetation
*/
char *TreeOPE::mystrdup(const char *str)
{
char *answer = NULL;
answer = (char *) malloc(strlen(str) + 1);
if(answer)
strcpy(answer, str);
return answer;
}
/*
count the number of instances of ch in the string
Params: str - serach string
ch - character.
Returns: number of times ch appears in str.
*/
int TreeOPE::mystrcount(const char *str, int ch)
{
int answer = 0;
while(*str)
if(*str++ == ch)
answer++;
return answer;
}
/*
print out a node (test function)
Parmas: node - node to print
indent - indent level
Notes: recursive
*/
void TreeOPE::printnewicknode(NEWICKNODE *node, int indent)
{
int i;
for(i=0;i<indent;i++)
cout << " ";
cout << (long) node << ":";
if(node->label == NULL)
cout << "NULL";
else
cout << node->label;
cout << " : " << node->weight << " : " << node->Nchildren << endl;
for(i=0;i<node->Nchildren;i++)
printnewicknode(node->child[i], indent + 5);
}
void TreeOPE::printnewicktree(const NEWICKTREE *tree)
{
printnewicknode(tree->root, 0);
}
int TreeOPE::newickmain(int argc, char **argv)
{
NEWICKTREE *tree = NULL;
int err;
char *label = NULL;
if(argc != 2)
fprintf(stderr, "Newick tree loader\n");
else
{
tree = loadnewicktree(argv[1], &err);
if(!tree)
{
switch(err)
{
case -1: printf("Out of memory\n"); break;
case -2: printf("parse error\n"); break;
case -3: printf("Can't load file\n"); break;
default:
printf("Error %d\n", err);
}
}
else
{
printf("Loaded\n");
printnewicktree(tree);
}
killnewicktree(tree);
}
label = makenewicklabel("My name is \'Fred()\'");
printf("***%s***\n", label);
free(label);
return 0;
}
void TreeOPE::GetTaxaLabels(NEWICKNODE *node, LabelMap &lm)
{
if (node->Nchildren == 0)
{
std::string temp(node->label);
lm.push(temp);
}
else
for (int i = 0; i<node->Nchildren; i++)
GetTaxaLabels(node->child[i], lm);
};
void TreeOPE::dfs_compute_hash(
NEWICKNODE* startNode,
LabelMap &lm,
HashRFMap &vec_hashrf,
unsigned treeIdx,
unsigned &numBitstr,
unsigned long long m1,
unsigned long long m2,
bool WEIGHTED,
unsigned int NUM_Taxa,
map<unsigned long long, Array<char> *> &hash2bitstr,
int numofbipartions)
{
// If the node is leaf node, just set the place of the taxon name in the bit string to '1'
// push the bit string into stack
/*cout << "output test========" << endl;//---
for(int i = 0; i < 16; i++)
{
Array<char> *arr = new Array<char> (16);
arr->SetBitArray(i+1);
arr->printbits(16);
cout << endl;//---
delete arr;
}*/
int btlength = (int) ((NUM_Taxa + 7) / 8);
if (startNode->Nchildren == 0)
{
// leaf node
// string temp(startNode->label);
// unsigned int idx = lm[temp];
unsigned int idx = atoi(startNode->label) - 1;
// Implicit BPS
// Set the hash value for each leaf node
startNode->hv1 = vec_hashrf._HF.getA1(idx);
startNode->hv2 = vec_hashrf._HF.getA2(idx);
// std::cout << "l:" << startNode->bitstr.get_length() << ", idx:" << idx << std::endl;//--
startNode->bitstr = new Array<char> (btlength);
// for(int i = 0; i < btlength; i++)
// {
// std::cout << "s i:" << i << std::endl;//--
// startNode->bitstr[i] = NULL;
// }
// cout << "ith:" << idx + 1 << endl;//----
if(! startNode->bitstr->SetBitArray(idx + 1))
std::cout << "Warning: Errors in setting bit string" << std::endl;
// startNode->bitstr->printbits(12);//--
// cout << endl;//---
}
else
{
for (int i = 0; i < startNode->Nchildren; ++i)
{
dfs_compute_hash(startNode->child[i], lm, vec_hashrf,
treeIdx, numBitstr, m1, m2,WEIGHTED,NUM_Taxa, hash2bitstr, numofbipartions);
}
// For weighted RF
float dist = 0.0;
if (WEIGHTED)
dist = startNode->weight;
else
dist = 1;
++ numBitstr;
// Implicit BPS
// After an internal node is found, compute hv1 and hv2
unsigned long long temphv1 = 0;
unsigned long long temphv2 = 0;
startNode->bitstr = new Array<char> (btlength);
for (int i = 0; i < startNode->Nchildren; ++i)
{
// cout << "i:" << i << ",child:" << endl;///---
// startNode->child[i]->bitstr->printbits(12);//---
Array<char> bitstrleft = *(startNode->bitstr);
Array<char> bitstrright = *(startNode->child[i]->bitstr);
startNode->bitstr->ORbitOPE(bitstrleft, bitstrright);
// cout << endl;//--
// cout << "results:";
// startNode->bitstr->printbits(12);//----
// cout << endl;//----
unsigned long long t1 = temphv1;
unsigned long long t2 = temphv2;
unsigned long long h1 = startNode->child[i]->hv1;
unsigned long long h2 = startNode->child[i]->hv2;
if (add_of(temphv1, t1, h1))
{
cout << "ERROR : ullong add overflow !\n\n";
cout << "t1 = " << t1 << "h1 = " << h1 << "t1 + h1 = " << t1+h1 << endl;
exit(0);
}
if (add_of(temphv2, t2, h2))
{
cout << "ERROR : ullong add overflow !\n";
cout << "t2 = " << t2 << "h2 = " << h2 << "t2 + h2 = " << t2+h2 << endl;
exit(0);
}
}
// startNode->bitstr->printbits(12);//---
// cout << endl;//---
// Check overflow
unsigned long long temp1 = temphv1 % m1;
unsigned long long temp2 = temphv2 % m2;
startNode->hv1 = temp1;
startNode->hv2 = temp2;
Array<char> *btpt = NULL;
btpt = new Array<char> (*startNode->bitstr);
if(hash2bitstr[startNode->hv2] != NULL)
{
delete hash2bitstr[startNode->hv2];
hash2bitstr[startNode->hv2] = btpt;
}
else
{
hash2bitstr[startNode->hv2] = btpt;
}
// Store bitstring in hash table
if (numBitstr <= numofbipartions)
{
vec_hashrf.hashing_bs_without_type2_nbits(treeIdx, NUM_Taxa, startNode->hv1, startNode->hv2, dist, WEIGHTED);
}
}
};
void TreeOPE::bipart(NEWICKNODE * const startnode, unsigned int &treeIdx,
unsigned long long *matrix_hv,
unsigned int *matrix_treeIdx,
double *matrix_weight, int &idx, int depth, bool isrooted)
{
depth++;
if (isrooted)
{
if(depth > 1 && startnode->Nchildren > 0)
{
matrix_hv[idx] = startnode->hv2;
matrix_treeIdx[idx] = treeIdx+1;
matrix_weight[idx] = startnode->weight;
idx++;
}
}
else
{ if(depth > 2 && startnode->Nchildren > 0)
{
matrix_hv[idx] = startnode->hv2;
matrix_treeIdx[idx] = treeIdx+1;
matrix_weight[idx] = startnode->weight;
idx++;
}
}
for (int i = 0; i < startnode->Nchildren; i++)
if(startnode->child[i]->Nchildren > 0)
bipart(startnode->child[i], treeIdx, matrix_hv, matrix_treeIdx, matrix_weight, idx, depth, isrooted);
};
NEWICKTREE *TreeOPE::parsetree(char *str, int *error, NEWICKTREE *testtree)
{
NEWICKTREE *answer = NULL;
int err;
char *ch = NULL;
int i = 0, idx = 0;
answer = (NEWICKTREE *) malloc(sizeof(NEWICKTREE));
// skip space
char *str_copy = (char *) malloc(sizeof(char) * (strlen(str) + 2));
for(i = 0; i < strlen(str) + 1; i++)
{
if((*(str + i)) != ' ')
{
*(str_copy + idx) = *(str + i);
idx++;
}
if(*(str + i) == '\0')
break;
}
*(str_copy + idx) = '\0';
if (!answer)
{
err = -1;
goto error_exit;
}
ch = str_copy;
if (*ch == '(')
{
ch++;
answer->root = parsenode(&ch, &err);
if (!answer->root || err != 0)
goto error_exit;
}
if(testtree != NULL)
{
cout << "in2-----" << endl;
TreeOPE::printnewicktree(testtree);
}
if (*ch != ';')
{
err = -2; // parse error
goto error_exit;
}
if (error)
*error = 0;
free(str_copy);
// std::cout << "root->child:" << (long) answer->root->child << std::endl;//-- WHtest
return answer;
error_exit:
cout << "Wrong in parsetree()!\n\n";//----
free(str_copy);
killnewicktree(answer);
if (error)
*error = err;
return NULL;
}
NEWICKNODE *TreeOPE::parsenode(char **str, int *error)
{
NEWICKNODE *answer = NULL;
int err;
NEWICKNODE *child = NULL;
answer = (NEWICKNODE *) malloc(sizeof(NEWICKNODE));
if (!answer)
{
err = -1;
goto error_exit;
}
answer->Nchildren = 0;
answer->label = NULL;
answer->child = NULL;
answer->hv1 = 0;
answer->hv2 = 0;
answer->bitstr = NULL;
if (**str == '(')
{
(*str)++;
child = parsenode(str, &err);
if (!child)