-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcHierarchical.cls
1446 lines (1227 loc) · 44.3 KB
/
cHierarchical.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "cHierarchical"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'author: Pong Yue Hin - https://github.com/yhpong
Option Explicit
Private pn_raw As Long
Private pZ() As Long
Private pZ_height() As Double
Private plabel As Variant
Private pleaf_order() As Long
Private psizes() As Long
Private pxy_plot() As Double
Private pparents() As Long
Private pDistance() As Double
Private plink_type As String
Private pswapped_Z() As Long
Private pswapped_Z_height() As Double
Private pclustered_items() As Variant
Private pclusters() As Variant
Private Sub Class_Initialize()
Call Reset
End Sub
Sub Reset()
Erase pZ, pZ_height, pleaf_order, psizes, pxy_plot ' plabel
Erase pparents, pDistance, pswapped_Z, pswapped_Z_height
Erase pclustered_items, pclusters
End Sub
Public Property Get z() As Long()
z = pZ
End Property
Public Property Get Z_height() As Double()
Z_height = pZ_height
End Property
Public Property Get leaf_order() As Long()
leaf_order = pleaf_order
End Property
Public Property Get sizes() As Long()
sizes = psizes
End Property
Public Property Get xy_plot() As Double()
xy_plot = pxy_plot
End Property
Public Property Get link_type() As String
link_type = plink_type
End Property
Public Property Get parents() As Long()
'Added by Daniel Grass
parents = pparents
End Property
Public Property Get clustered_items() As Variant
'Added by Daniel Grass
'Return an array with all labels and the corresponding clusters
Call cluster_items
clustered_items = pclustered_items
End Property
Public Property Get clusters() As Variant
'Added by Daniel Grass
'Return an array with all clusters and the corresponding labels
Dim i As Long
Dim i_no_of_enties As Long
Dim i_max_depth As Long
Dim i_col_in As Long
Dim i_row_in As Long
Dim i_row_out As Long: i_row_out = 1
Dim i_no_of_clusters As Long
Dim tmp_clusters() As Long
'make sure we have the matrix of clustered items
If IsEmpty(pclustered_items) Then
Call cluster_items
End If
'calculate the number of entries required to list all clusters with all lables
For i = LBound(psizes) To UBound(psizes)
If psizes(i) > 1 Then
'do not count single item clusters as we do not
'return them in clustered_items array
i_no_of_enties = i_no_of_enties + psizes(i)
End If
Next i
'size the output array
ReDim pclusters(0 To 0, 0 To 0)
ReDim pclusters(1 To i_no_of_enties, 1 To 2)
'get the number of clusters and size the tmp_clusters array so we can store clusters we already worked on
i_no_of_clusters = UBound(pparents) - LBound(pparents) + 1
ReDim tmp_clusters(1 To i_no_of_clusters)
i_max_depth = UBound(pclustered_items, 2)
'fill the tmp_clusters array with the max number of rows
For i = 1 To i_no_of_clusters
tmp_clusters(i) = i_max_depth
Next i
'walk through the clustered_items array and create the output
For i_col_in = 2 To UBound(pclustered_items, 2)
For i_row_in = 1 To UBound(pclustered_items)
'only process clusters if the are not yet processed in a previous column (i.e. already complete)
If tmp_clusters(pclustered_items(i_row_in, i_col_in)) >= i_col_in Then
pclusters(i_row_out, 1) = pclustered_items(i_row_in, i_col_in)
pclusters(i_row_out, 2) = pclustered_items(i_row_in, 1)
tmp_clusters(pclustered_items(i_row_in, i_col_in)) = i_col_in
i_row_out = i_row_out + 1
End If
Next i_row_in
Next i_col_in
clusters = pclusters
End Property
Private Sub cluster_items()
'Added by Daniel Grass
'crates an array with all labels and the corresponding clusters
Dim i As Long
Dim ii As Long
Dim y As Long
Dim z As Long
Dim i_next As Long
Dim i_max_cl As Long
Dim i_max_depth As Long
Dim i_steps As Long
Dim i_max As Long
Dim i_con_sum As Long
Dim s_path As String
Dim a_fwd_star_idx() As Long
Dim a_fwd_star_dat() As Long
Dim i_fwd_star_ptr As Long
i_max = 2 * pn_raw - 1
'max possible number of elements for the forward star array
i_con_sum = pn_raw * (pn_raw + 1) / 2
'size the forward star arrays
ReDim a_fwd_star_idx(1 To pn_raw + 1)
ReDim a_fwd_star_dat(1 To i_con_sum)
'create a forward star representation of the clustering path
For i = 1 To pn_raw
s_path = "Item " & i & ": "
i_steps = 0
i_next = pparents(i)
i_fwd_star_ptr = i_fwd_star_ptr + 1
a_fwd_star_idx(i) = i_fwd_star_ptr
a_fwd_star_dat(i_fwd_star_ptr) = i_next
s_path = s_path & " > " & i_next
i_steps = i_steps + 1
Do While i_next < i_max
i_next = pparents(i_next)
i_fwd_star_ptr = i_fwd_star_ptr + 1
a_fwd_star_dat(i_fwd_star_ptr) = i_next
s_path = s_path & " > " & i_next
i_steps = i_steps + 1
If i_steps > i_max_depth Then
i_max_depth = i_steps
End If
Loop
'Debug.Print s_path
Next i
'create the sentinel
a_fwd_star_idx(pn_raw + 1) = i_fwd_star_ptr + 1
'Debug.Print "Max Depth: " & i_max_depth
'size the array to the number of clusters + 1 additional column for the labels
ReDim pclustered_items(1 To pn_raw, 1 To i_max_depth + 1)
'populate the information
For i = 1 To pn_raw
'add the label
pclustered_items(i, 1) = plabel(i)
'add the cluster info - step 1: add the clusters
ii = 2
For z = a_fwd_star_idx(i + 1) - 1 To a_fwd_star_idx(i) Step -1
pclustered_items(i, ii) = a_fwd_star_dat(z)
ii = ii + 1
Next z
'add the cluster info - step 1: fill in the empty ones
z = z + 1
For y = ii To i_max_depth + 1
pclustered_items(i, y) = a_fwd_star_dat(z)
Next y
Next i
End Sub
'=== Direct Implementation of agglomoerative clustering
'Input: label() is the string labels of the N data points
'Input: x() is the distance matrix(N by N), symmetric and zero diagonals
'Input: linkage can be "AVERAGE","COMPLETE","SINGLE","WARD"
Sub linkage(x() As Double, Optional link_type As String = "AVERAGE", Optional label As Variant)
Dim i As Long, j As Long, m As Long, n As Long, k As Long
Dim n_raw As Long, iterate As Long, n_size As Long
Dim distance() As Double
Dim temp_min As Double, temp_max As Double, temp As Double
Dim temp_x As Double, temp_y As Double
Dim u As Long, v As Long, w As Long, new_node As Long
Dim temp_u As Long, temp_v As Long
n_raw = UBound(x, 1)
pn_raw = n_raw
pDistance = x
plink_type = UCase(link_type)
ReDim plabel(1 To n_raw)
If IsMissing(label) = True Then
For i = 1 To n_raw
plabel(i) = i
Next i
Else
For i = 1 To n_raw
plabel(i) = label(i)
Next i
End If
ReDim pZ(1 To n_raw - 1, 1 To 3)
ReDim pZ_height(1 To n_raw - 1)
ReDim distance(1 To 2 * n_raw - 1, 1 To 2 * n_raw - 1)
For i = 1 To n_raw - 1
For j = i + 1 To n_raw
distance(i, j) = x(i, j)
distance(j, i) = x(i, j)
Next j
Next i
Dim parent() As Long
Dim height() As Double
Dim Size() As Long
ReDim Size(1 To 2 * n_raw - 1)
ReDim parent(1 To 2 * n_raw - 1)
ReDim height(1 To 2 * n_raw - 1)
For i = 1 To n_raw
Size(i) = 1
Next i
'=== Direct Implementation of agglomoerative clustering
n_size = n_raw
new_node = n_raw
For iterate = 1 To n_raw - 1
temp_min = 999999999
For i = 1 To n_size - 1
For j = i + 1 To n_size
If parent(i) = 0 And parent(j) = 0 Then
If distance(i, j) < temp_min Then
temp_min = distance(i, j)
u = i
v = j
End If
End If
Next j
Next i
'=== Attributes of the new vertex
new_node = new_node + 1
parent(u) = new_node
parent(v) = new_node
height(new_node) = temp_min
Size(new_node) = Size(u) + Size(v)
pZ_height(iterate) = temp_min
pZ(iterate, 1) = new_node
If height(u) < height(v) Then
pZ(iterate, 2) = v
pZ(iterate, 3) = u
Else
pZ(iterate, 2) = u
pZ(iterate, 3) = v
End If
'===========================================
'=== Calculate the distance of the new vertex to other vertices
Select Case UCase(link_type)
Case "AVERAGE" 'Average Linkage
m = Size(u)
n = Size(v)
For w = 1 To n_size
temp_x = distance(u, w)
temp_y = distance(v, w)
distance(new_node, w) = (m * temp_x + n * temp_y) / (m + n)
distance(w, new_node) = distance(new_node, w)
Next w
Case "COMPLETE" 'Complete Linkage
For w = 1 To n_size
distance(new_node, w) = distance(u, w)
temp_max = distance(v, w)
If temp_max > distance(new_node, w) Then distance(new_node, w) = temp_max
distance(w, new_node) = distance(new_node, w)
Next w
Case "SINGLE" 'Single Linkage
For w = 1 To n_size
distance(new_node, w) = distance(u, w)
temp_min = distance(v, w)
If temp_min < distance(new_node, w) Then distance(new_node, w) = temp_min
distance(w, new_node) = distance(new_node, w)
Next w
Case "WARD" 'Ward's Method
m = Size(u)
n = Size(v)
temp = distance(u, v)
For w = 1 To n_size
k = Size(w)
temp_x = distance(u, w)
temp_y = distance(v, w)
distance(w, new_node) = Sqr(((m + k) * (temp_x ^ 2) + (n + k) * (temp_y ^ 2) - k * (temp ^ 2)) / (m + n + k))
distance(new_node, w) = distance(w, new_node)
Next w
End Select
'===========================================
n_size = n_size + 1
If iterate Mod 5 = 0 Then
DoEvents
Application.StatusBar = n_raw - n_size & " nodes remaining..."
End If
Next iterate
Erase distance
Call Find_Parents 'find parents of each node
Call Find_Sizes 'find sizes of each node
Call Calc_Leaf_Order 'get leafs ordering
Application.StatusBar = False
End Sub
'=== Using Nearest Neighbour Chain algorithm to speed up clustering
'Input: label() is the string labels of the N data points
'Input: x() is the distance matrix(N by N), symmetric and zero diagonals
'Input: linkage can be "AVERAGE","COMPLETE","SINGLE","WARD"
Sub NNChainLinkage(x() As Double, Optional link_type As String = "AVERAGE", Optional label As Variant)
Dim i As Long, j As Long, m As Long, n As Long, k As Long
Dim n_raw As Long, iterate As Long
Dim distance() As Double
Dim temp_min As Double, temp_max As Double, temp As Double
Dim temp_x As Double, temp_y As Double
Dim u As Long, v As Long, w As Long, new_node As Long
Dim temp_u As Long, temp_v As Long
n_raw = UBound(x, 1)
pn_raw = n_raw
pDistance = x
plink_type = UCase(link_type)
ReDim plabel(1 To n_raw)
If IsMissing(label) = True Then
For i = 1 To n_raw
plabel(i) = i
Next i
Else
For i = 1 To n_raw
plabel(i) = label(i)
Next i
End If
ReDim pZ(1 To n_raw - 1, 1 To 3)
ReDim pZ_height(1 To n_raw - 1)
ReDim distance(1 To 2 * n_raw - 1, 1 To 2 * n_raw - 1)
For i = 1 To n_raw - 1
For j = i + 1 To n_raw
distance(i, j) = x(i, j)
distance(j, i) = x(i, j)
Next j
Next i
Dim parent() As Long
Dim height() As Double
Dim Size() As Long
ReDim Size(1 To 2 * n_raw - 1)
ReDim parent(1 To 2 * n_raw - 1)
ReDim height(1 To 2 * n_raw - 1)
'=== Using Nearest Neighbour Chain algorithm to speed up clustering
'=== Start Adding internal nodes for most similar pairs
Dim iStack() As Long, iChain() As Long
ReDim iStack(0 To 0)
ReDim iChain(0 To 0)
For i = 1 To n_raw
Call Array_Push(iStack, i)
Size(i) = 1
Next i
iterate = 0
new_node = n_raw
Do While UBound(iStack) > 1
iterate = iterate + 1
If UBound(iChain) <= 3 Then
ReDim iChain(0 To 0)
u = iStack(1)
v = iStack(2)
Call Array_Push(iChain, u)
Else
u = iChain(UBound(iChain) - 3)
v = iChain(UBound(iChain) - 2)
For i = 1 To 3
Call Array_Pop(iChain)
Next i
End If
k = 0
Do
'=== Find pair of minimum dissimilarity
temp_min = 99999999
If parent(v) = 0 Then
w = v
temp_min = distance(u, v)
End If
For i = 1 To UBound(iStack)
temp_u = iStack(i)
If temp_u <> u Then
If distance(u, temp_u) < temp_min Then
temp_min = distance(u, temp_u)
w = temp_u
End If
End If
Next i
'==============================================
v = u
u = w
Call Array_Push(iChain, u)
If UBound(iChain) >= 3 Then
If u = iChain(UBound(iChain) - 2) Then k = 1
End If
Loop Until k = 1
'=== Attributes of the new vertex
new_node = new_node + 1
parent(u) = new_node
parent(v) = new_node
height(new_node) = temp_min
Size(new_node) = Size(u) + Size(v)
pZ_height(iterate) = temp_min
pZ(iterate, 1) = new_node
If height(u) < height(v) Then
pZ(iterate, 2) = v
pZ(iterate, 3) = u
Else
pZ(iterate, 2) = u
pZ(iterate, 3) = v
End If
'===========================================
Call Array_Remove(iStack, u)
Call Array_Remove(iStack, v)
'=== Calculate the distance of the new vertex to other vertices
Select Case UCase(link_type)
Case "AVERAGE" 'Average Linkage
m = Size(u)
n = Size(v)
For i = 1 To UBound(iStack)
w = iStack(i)
temp_x = distance(u, w)
temp_y = distance(v, w)
distance(new_node, w) = (m * temp_x + n * temp_y) / (m + n)
distance(w, new_node) = distance(new_node, w)
Next i
Case "COMPLETE" 'Complete Linkage
For i = 1 To UBound(iStack)
w = iStack(i)
distance(new_node, w) = distance(u, w)
temp_max = distance(v, w)
If temp_max > distance(new_node, w) Then distance(new_node, w) = temp_max
distance(w, new_node) = distance(new_node, w)
Next i
Case "SINGLE" 'Single Linkage
For i = 1 To UBound(iStack)
w = iStack(i)
distance(new_node, w) = distance(u, w)
temp_min = distance(v, w)
If temp_min < distance(new_node, w) Then distance(new_node, w) = temp_min
distance(w, new_node) = distance(new_node, w)
Next i
Case "WARD" 'Ward's Method
m = Size(u)
n = Size(v)
temp = distance(u, v)
For i = 1 To UBound(iStack)
w = iStack(i)
k = Size(w)
temp_x = distance(u, w)
temp_y = distance(v, w)
distance(w, new_node) = Sqr(((m + k) * (temp_x ^ 2) + (n + k) * (temp_y ^ 2) - k * (temp ^ 2)) / (m + n + k))
distance(new_node, w) = distance(w, new_node)
Next i
End Select
'===========================================
Call Array_Push(iStack, new_node)
If iterate Mod 5 = 0 Then
DoEvents
Application.StatusBar = UBound(iStack) & " nodes remaining..."
End If
Loop
Erase distance
Call sort_tree 're-index Z() in order of increasing height
Call Find_Parents 'find parents of each node
Call Find_Sizes 'find sizes of each node
Call Calc_Leaf_Order 'get leafs ordering
Application.StatusBar = False
End Sub
're-index Z() in order of increasing height
Private Sub sort_tree()
Dim i As Long, j As Long, n As Long, u As Long, v As Long
Dim sort_index() As Long, Z_sorted() As Long, parent() As Long
Call modMath.Sort_Bubble_A(pZ_height, sort_index)
ReDim Z_sorted(1 To pn_raw - 1, 1 To 3)
For i = 1 To pn_raw - 1
j = sort_index(i)
Z_sorted(i, 1) = pZ(j, 1)
Z_sorted(i, 2) = pZ(j, 2)
Z_sorted(i, 3) = pZ(j, 3)
Next i
ReDim parent(1 To 2 * pn_raw - 1)
For i = 1 To pn_raw - 1
Z_sorted(i, 1) = pn_raw + i
u = Z_sorted(i, 2)
v = Z_sorted(i, 3)
If u > pn_raw Then
Z_sorted(i, 2) = parent(pZ(u - pn_raw, 2))
Else
Z_sorted(i, 2) = u
End If
If v > pn_raw Then
Z_sorted(i, 3) = parent(pZ(v - pn_raw, 2))
Else
Z_sorted(i, 3) = v
End If
parent(u) = pn_raw + i
parent(v) = pn_raw + i
Next i
pZ = Z_sorted
End Sub
'=== Print Matrix
'Input: d(), NXN matrix to be visualize
'Input: when reverse is FALSE, higher value is blue, lower value is red
Sub Print_Matrix(vRng As Range, d() As Double, Optional reverse As Boolean = False)
Dim i As Long, j As Long, m As Long, n As Long, n_raw As Long
Dim vR() As Long, vG() As Long, vB() As Long
Dim temp As Double, temp_min As Double, temp_max As Double
n_raw = UBound(d, 1)
If n_raw <> pn_raw Then vRng.value = "Matrix size does not match tree"
ReDim vR(1 To n_raw, 1 To n_raw)
ReDim vG(1 To n_raw, 1 To n_raw)
ReDim vB(1 To n_raw, 1 To n_raw)
temp_min = 999999
temp_max = -999999
For i = 1 To n_raw - 1
For j = i + 1 To n_raw
If reverse = False Then
temp = d(i, j)
Else
temp = -d(i, j)
End If
If temp < temp_min Then temp_min = temp
If temp > temp_max Then temp_max = temp
Next j
Next i
For i = 1 To n_raw - 1
For j = i + 1 To n_raw
If reverse = False Then
temp = (d(i, j) - temp_min) / (temp_max - temp_min)
Else
temp = (-d(i, j) - temp_min) / (temp_max - temp_min)
End If
Call Color_Scale(temp, vR(i, j), vG(i, j), vB(i, j))
vR(j, i) = vR(i, j)
vG(j, i) = vG(i, j)
vB(j, i) = vB(i, j)
Next j
Next i
With vRng
For i = 1 To n_raw
If i Mod 20 = 0 Then Application.StatusBar = "Printing matrix..." & i & "/" & n_raw
m = pleaf_order(i)
.Offset(m, 0) = plabel(i)
For j = 1 To n_raw
n = pleaf_order(j)
.Offset(0, n) = plabel(j)
If i <> j Then
.Offset(m, n).Interior.Color = RGB(vR(i, j), vG(i, j), vB(i, j))
End If
Next j
Next i
End With
Application.StatusBar = False
End Sub
'Input: x is a real number between 0 and 1
'Output: vR,vG,vB are integers from 0 to 255
Private Sub Color_Scale(x As Double, vR As Long, vG As Long, vB As Long)
'Dim i As Long
'i = 255 - 200 * x
'vR = i
'vG = i
'vB = i
If x <= 0.5 Then
vR = 255
vG = Round(510 * x, 0)
vB = 0
Else
vR = Round(-510 * (x - 1), 0)
vG = vR
vB = Round(510 * x - 255, 0)
End If
End Sub
'=== Print tree
Sub Print_Tree(vRng As Range, Optional skew As Double = 1, Optional circular As Boolean = False)
Dim i As Long, j As Long, u As Long, v As Long, w As Long
Call get_xy_plot(skew, circular) 'get (x,y)-coordinates for visualization
With vRng
For i = 1 To pn_raw
j = pleaf_order(i)
.Offset(j - 1, 0).value = plabel(i)
.Offset(j - 1, 1).value = pxy_plot(i, 1)
.Offset(j - 1, 2).value = pxy_plot(i, 2)
Next i
If circular = False Then
j = 0
For i = 1 To pn_raw - 1
w = pn_raw + i
u = pZ(i, 2)
v = pZ(i, 3)
.Offset(i + j - 1, 3).value = pxy_plot(u, 1)
.Offset(i + j - 1, 4).value = pxy_plot(u, 2)
.Offset(i + j, 3).value = pxy_plot(w, 1)
.Offset(i + j, 4).value = pxy_plot(u, 2)
.Offset(i + j + 1, 3).value = pxy_plot(w, 1)
.Offset(i + j + 1, 4).value = pxy_plot(v, 2)
.Offset(i + j + 2, 3).value = pxy_plot(v, 1)
.Offset(i + j + 2, 4).value = pxy_plot(v, 2)
j = j + 4
Next i
Else
j = 0
For i = 1 To pn_raw - 1
w = pn_raw + i
u = pZ(i, 2)
v = pZ(i, 3)
.Offset(i + j - 1, 3).value = pxy_plot(u, 1)
.Offset(i + j - 1, 4).value = pxy_plot(u, 2)
.Offset(i + j, 3).value = pxy_plot(w, 1)
.Offset(i + j, 4).value = pxy_plot(w, 2)
.Offset(i + j + 1, 3).value = pxy_plot(v, 1)
.Offset(i + j + 1, 4).value = pxy_plot(v, 2)
j = j + 3
Next i
End If
End With
End Sub
Private Sub get_xy_plot(skew As Double, Optional circular As Boolean = False)
Dim i As Long
Dim theta() As Double, tmp_x As Double
ReDim pxy_plot(1 To 2 * pn_raw - 1, 1 To 2)
If circular = False Then
For i = 1 To pn_raw
pxy_plot(i, 1) = 1
pxy_plot(i, 2) = pleaf_order(i)
Next i
For i = 1 To pn_raw - 1
pxy_plot(i + pn_raw, 1) = 1 + pZ_height(i) ^ skew
pxy_plot(i + pn_raw, 2) = (pxy_plot(pZ(i, 2), 2) + pxy_plot(pZ(i, 3), 2)) / 2
Next i
Else
ReDim theta(1 To 2 * pn_raw - 1)
ReDim radius(1 To pn_raw - 1)
For i = 1 To pn_raw
theta(i) = 6.28318530717959 * pleaf_order(i) / pn_raw
pxy_plot(i, 1) = Cos(theta(i))
pxy_plot(i, 2) = Sin(theta(i))
Next i
For i = 1 To pn_raw - 1
tmp_x = (1 - (pZ_height(i) / pZ_height(pn_raw - 1))) ^ skew
theta(i + pn_raw) = (theta(pZ(i, 2)) + theta(pZ(i, 3))) / 2
pxy_plot(i + pn_raw, 1) = tmp_x * Cos(theta(i + pn_raw))
pxy_plot(i + pn_raw, 2) = tmp_x * Sin(theta(i + pn_raw))
Next i
End If
End Sub
Private Sub Find_Parents()
Dim i As Long
pn_raw = UBound(pZ, 1) + 1
ReDim pparents(1 To 2 * pn_raw - 1)
For i = 1 To pn_raw - 1
pparents(pZ(i, 2)) = pZ(i, 1)
pparents(pZ(i, 3)) = pZ(i, 1)
Next i
End Sub
Private Sub Find_Sizes()
Dim i As Long
ReDim psizes(1 To 2 * pn_raw - 1)
For i = 1 To pn_raw
psizes(i) = 1
Next i
For i = 1 To pn_raw - 1
psizes(pn_raw + i) = psizes(pZ(i, 2)) + psizes(pZ(i, 3))
Next i
End Sub
'Get leaves order based on current Z()
Private Sub Calc_Leaf_Order()
Dim u As Long, v As Long, k As Long
Dim visited() As Long
ReDim pleaf_order(1 To pn_raw)
ReDim visited(1 To 2 * pn_raw - 1)
k = 0
u = 2 * pn_raw - 1
visited(u) = 1
Do While k < pn_raw
If k Mod 50 = 0 Then Application.StatusBar = "Ordering leaves..." & k & "/" & pn_raw
If visited(pZ(u - pn_raw, 2)) = 0 Then
u = pZ(u - pn_raw, 2)
visited(u) = 1
If u <= pn_raw Then
k = k + 1
pleaf_order(u) = k
u = pparents(u)
End If
ElseIf visited(pZ(u - pn_raw, 3)) = 0 Then
u = pZ(u - pn_raw, 3)
visited(u) = 1
If u <= pn_raw Then
k = k + 1
pleaf_order(u) = k
u = pparents(u)
End If
Else
u = pparents(u)
End If
Loop
Application.StatusBar = False
End Sub
'get original distance and cophenetic distance side by side in condensed form
Function CopheneticDist() As Double()
Dim i As Long, j As Long, k As Long
Dim cd() As Double, x() As Double
cd = Cophenetic_Matrix()
ReDim x(1 To pn_raw * (pn_raw - 1) / 2, 1 To 2)
For i = 1 To pn_raw - 1
For j = i + 1 To pn_raw
k = k + 1
x(k, 1) = pDistance(i, j)
x(k, 2) = cd(i, j)
Next j
Next i
CopheneticDist = x
End Function
'Calculate pairwise cophenetic distance
Private Function Cophenetic_Matrix() As Double()
Dim i As Long, j As Long, w As Long
Dim y() As Double
ReDim y(1 To pn_raw, 1 To pn_raw)
For i = 1 To pn_raw - 1
DoEvents
If i Mod 10 = 0 Then Application.StatusBar = "calculating cophenetic distance..." & i & "/" & pn_raw
For j = i + 1 To pn_raw
w = LeastCommonAncestor(i, j)
y(i, j) = pZ_height(w - pn_raw)
y(j, i) = y(i, j)
Next j
Next i
Cophenetic_Matrix = y
Application.StatusBar = False
End Function
Private Function LeastCommonAncestor(u As Long, v As Long) As Long
Dim path_u() As Long, path_v() As Long
Dim i As Long, j As Long, k As Long
ReDim path_u(0 To 0)
ReDim path_v(0 To 0)
i = u
Do
i = pparents(i)
Call Enqueue(path_u, i)
Loop Until pparents(i) = 0
i = v
Do
i = pparents(i)
Call Enqueue(path_v, i)
Loop Until pparents(i) = 0
i = 1
Do While i <= UBound(path_u) And i <= UBound(path_v)
If path_u(i) <> path_v(i) Then Exit Do
i = i + 1
Loop
LeastCommonAncestor = path_u(i - 1)
End Function
'=== Re-order pZ() and pleaf_order() by optimization ==========================================
'Ziv Bar-Joseph "Fast optimal leaf ordering for hierarchical clustering", 2001
'Note: Simiarlity matrix is used in the paper, here we use distances, so the logic is reversed
'they maximize the cost function M, but we minimize it here
'======================================================================
Sub optOrdering()
Dim i As Long, j As Long, u As Long, v As Long, w As Long, v_L As Long, v_R As Long
Dim m As Long, n As Long
Dim sorted_Z() As Long, sorted_D() As Double, sorted_leaves() As Long
Dim cluster_ranges() As Long
Dim must_swap() As Long
Application.StatusBar = "Optimizing leaf ordering..."
're-index leaves according to their order in original tree
ReDim sorted_leaves(1 To pn_raw)
ReDim sorted_Z(1 To pn_raw - 1, 1 To 2)
ReDim sorted_D(1 To pn_raw, 1 To pn_raw)
For i = 1 To pn_raw
sorted_leaves(pleaf_order(i)) = i
Next i
For i = 1 To pn_raw - 1
v_L = pZ(i, 2)
v_R = pZ(i, 3)
If v_L <= pn_raw Then v_L = pleaf_order(v_L)
If v_R <= pn_raw Then v_R = pleaf_order(v_R)
sorted_Z(i, 1) = v_L
sorted_Z(i, 2) = v_R
Next i
For i = 1 To pn_raw - 1
For j = i + 1 To pn_raw
m = pleaf_order(i)
n = pleaf_order(j)
sorted_D(m, n) = pDistance(i, j)
sorted_D(n, m) = sorted_D(m, n)
Next j
Next i
ReDim cluster_ranges(1 To 2 * pn_raw - 1, 1 To 2)
For i = 1 To pn_raw
cluster_ranges(i, 1) = i
cluster_ranges(i, 2) = i
Next i
For i = 1 To pn_raw - 1
cluster_ranges(pn_raw + i, 1) = cluster_ranges(sorted_Z(i, 1), 1)
cluster_ranges(pn_raw + i, 2) = cluster_ranges(sorted_Z(i, 2), 2)
Next i
Dim M_order As Variant
Dim M_inner() As Double
ReDim M_order(1 To 2 * pn_raw - 1)
For i = 1 To 2 * pn_raw - 1
ReDim M_inner(cluster_ranges(i, 1) To cluster_ranges(i, 2), cluster_ranges(i, 1) To cluster_ranges(i, 2))
M_order(i) = M_inner
Next i
Call optOrdering_FindOrder(2 * pn_raw - 1, M_order, sorted_Z, sorted_D, cluster_ranges)
Call optOrdering_backtrack(2 * pn_raw - 1, M_order, sorted_Z, sorted_D, cluster_ranges)
For v = 1 To pn_raw - 1
i = sorted_Z(v, 1)
j = sorted_Z(v, 2)
If i <= pn_raw Then i = sorted_leaves(i)
If j <= pn_raw Then j = sorted_leaves(j)
pZ(v, 2) = i
pZ(v, 3) = j
Next v
Erase sorted_Z, sorted_D, cluster_ranges, M_order
Call Calc_Leaf_Order
Application.StatusBar = False
End Sub
Private Sub optOrdering_FindOrder(v As Long, M_order As Variant, sorted_Z() As Long, sorted_D() As Double, cluster_ranges() As Long)
Dim i As Long, j As Long, k As Long, m As Long, u As Long, w As Long
Dim v_L As Long, v_R As Long, v_LL As Long, v_LR As Long, v_RL As Long, v_RR As Long
Dim tmp_x As Double, tmp_y As Double, tmp_z As Double, tmp_min As Double
If v > pn_raw Then
DoEvents
v_L = sorted_Z(v - pn_raw, 1)
v_R = sorted_Z(v - pn_raw, 2)
If v_L > pn_raw Then
v_LL = sorted_Z(v_L - pn_raw, 1)
v_LR = sorted_Z(v_L - pn_raw, 2)
Else
v_LL = v_L
v_LR = v_L
End If
If v_R > pn_raw Then
v_RL = sorted_Z(v_R - pn_raw, 1)
v_RR = sorted_Z(v_R - pn_raw, 2)
Else
v_RL = v_R
v_RR = v_R
End If
Call optOrdering_FindOrder(v_L, M_order, sorted_Z, sorted_D, cluster_ranges)
Call optOrdering_FindOrder(v_R, M_order, sorted_Z, sorted_D, cluster_ranges)
For u = cluster_ranges(v_L, 1) To cluster_ranges(v_L, 2)
If u <= cluster_ranges(v_LL, 2) Then
i = v_LR
Else
i = v_LL
End If
For w = cluster_ranges(v_R, 1) To cluster_ranges(v_R, 2)
If w <= cluster_ranges(v_RL, 2) Then
j = v_RR
Else
j = v_RL
End If
tmp_min = 1073741824
For m = cluster_ranges(i, 1) To cluster_ranges(i, 2)
For k = cluster_ranges(j, 1) To cluster_ranges(j, 2)
tmp_x = M_order(v_L)(u, m) + M_order(v_R)(w, k) + sorted_D(m, k)
If tmp_x < tmp_min Then tmp_min = tmp_x
Next k
Next m
M_order(v)(u, w) = tmp_min
M_order(v)(w, u) = tmp_min
Next w
Next u
If (v - pn_raw) Mod 50 = 0 Then Application.StatusBar = "Optimizing leaf ordering..." & v - pn_raw & "/" & pn_raw
End If
End Sub