-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClusterAndDraw.py
1410 lines (1005 loc) · 65.6 KB
/
ClusterAndDraw.py
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
# -*- coding: utf-8 -*-
import wx
#numpy
import numpy as np
from scipy.cluster.vq import *
import copy
import string
import os, sys
from copy import deepcopy
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
class Matrix_creator:
def __init__ (self, params, data, post):
#create output directory for generated data
self.params = params
self.data = data
self.post = post
def computeMatrix(self):
if rank == 0:
#use superclass method to filter acceptable solutions
self.log=self.post.select_solutions(self.params) # -> the result is in fact the self.filter_log already
print ">> %s solutions filtered"%len(self.log[:,1])
if len(self.log[:,1])==0:
return
self.coordinateArray = deepcopy(self.log) #[:, 0:len(self.log[0,:])].astype(float)
self.dummyMatrix = np.empty(len(self.coordinateArray)**2)
self.dummyMatrix.fill(100)
self.distanceMatrix = self.dummyMatrix.reshape(len(self.coordinateArray),len(self.coordinateArray))
self.dummyMatrix = []
total_size = (len(self.coordinateArray)**2)/2
binNo = size
indexes_per_bin = total_size / binNo
soustractor = 1
indexBinHash = {}
accumulator = 0
rankIterator = 0
lowBoundary = 0
# getting the sliced indexes
for i in xrange(0, len(self.distanceMatrix),1):
array_len = len(self.distanceMatrix[i]) - soustractor
accumulator += array_len
if accumulator > indexes_per_bin:
indexBinHash[rankIterator] = [lowBoundary, i]
# change the parameters
rankIterator += 1
lowBoundary = i
# empty the accumulator
accumulator = 0
soustractor += 1
if lowBoundary < i:
indexBinHash[binNo-1] = [lowBoundary, i]
print ">> Starting distance matrix creation:\n"
print ">> clustering best solutions..."
else:
self.distanceMatrix = None
self.coordinateArray = None
indexBinHash = None
#synchronize all processors
comm.Barrier()
self.distanceMatrix=comm.bcast(self.distanceMatrix,root=0)
self.coordinateArray=comm.bcast(self.coordinateArray,root=0)
indexBinHash=comm.bcast(indexBinHash,root=0)
comm.Barrier()
#exec 'import %s as constraint'%(self.post.constraint)
# ---------------------------- Depending on the number of solutions, use all the processors or just one of them
if len(self.coordinateArray) > (size *3):
## creating variables to check for status of clustering of process 0
if rank == 0:
repetitions = indexBinHash[rank][1] - indexBinHash[rank][0]
totalIterations = len(self.coordinateArray) * repetitions
counter = 0
printresent = 1 # those are used not to repeat the state of the clustering
printPast = 0
counter = 0
pieceOfCoordinateArray = np.array([])
if rank in indexBinHash.keys():
#Starting the creation with 2 loops
for n in xrange(indexBinHash[rank][0],len(self.coordinateArray),1):
#print ">> computing distances for solution no: "+str(n+1)
if n == indexBinHash[rank][1]:
break
for m in xrange (n,len(self.coordinateArray),1):
# make sure you are not using the same structures against themselves
if n == m:
pass
else:
self.distanceMatrix[n][m] = self.post.computeDistance(self.coordinateArray[n],self.coordinateArray[m])
if rank == 0:
counter += 1.0
printPresent = int((counter / totalIterations) * 100)
if (printPresent%10) == 0 and printPresent != printPast:
print "> ~"+str( printPresent )+" % structures clustered "
printPast = printPresent
pieceOfCoordinateArray = self.distanceMatrix[indexBinHash[rank][0]:indexBinHash[rank][1],:]
#print "process "+str(rank)+" finished"
comm.Barrier()
pieces = comm.gather(pieceOfCoordinateArray,root=0)
comm.Barrier()
if rank == 0:
if int( (counter / totalIterations) * 100.00) != 100:
print "> ~100 % structures clustered "
self.distanceMatrix = []
for elem in pieces:
if len(elem) < 2:
pass
else:
for arrays in elem:
self.distanceMatrix.append(arrays)
lastRow = np.empty(len(self.coordinateArray))
lastRow.fill(100)
self.distanceMatrix.append(lastRow)
self.distanceMatrix = np.array(self.distanceMatrix)
np.transpose(self.distanceMatrix)
# np.savetxt('coordinateArray.txt', self.coordinateArray) # coordinateArray[0:50,0:50]
# np.savetxt('np_matrix.txt', self.distanceMatrix) # distanceMatrix[0:50]
else:
if rank == 0:
print ">> less than "+str(size*3)+" solutions, proceeding ..."
for n in xrange(0,len(self.coordinateArray),1):
#print ">> computing distances for solution no: "+str(n+1)
#if n == indexBinHash[rank][1]:
#break
for m in xrange (n,len(self.coordinateArray),1):
# make sure you are not using the same structures against themselves
if n == m:
pass
else:
# create the first multimer
self.distanceMatrix[n][m] = self.post.computeDistance(self.coordinateArray[n],self.coordinateArray[m])
if rank == 0:
np.savetxt('coordinateArray.txt', self.coordinateArray)
np.savetxt('np_matrix.txt', self.distanceMatrix)
class App(wx.App):
def OnInit(self):
self.frame = MainFrame(None, "distance Viewer")
self.frame.Show()
self.SetTopWindow(self.frame)
return True
class MainFrame (wx.Frame):
def __init__(self, parent, title, outputDir, params, data, post): # _CHANGED_
wx.Frame.__init__(self, parent, title=title, size=(1000,600))
self.post = post
self.outputDir = outputDir # _CHANGED_
self.params = params # _CHANGED_
self.data = data # _CHANGED_
# changing color of backgroundl
self.SetBackgroundColour("black")
# inserting the panels:
self.treePanel = treeDisplay(self)
self.distancePanel = MakeWork(self)
# adding the panels to the main frame
box = wx.BoxSizer(wx.VERTICAL)
box.Add(self.distancePanel, 0, wx.EXPAND | wx.ALL, 3 )
box.Add(self.treePanel, 0, wx.EXPAND | wx.ALL, 3 ) # the 3 is the padding compared to the frame!
self.SetSizer(box)
self.Centre()
# ------------------------------------------------------------- WORKING CLASS
class MakeWork (wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent,style=wx.SIMPLE_BORDER)
font1 = wx.Font(10, wx.NORMAL, wx.ITALIC, wx.BOLD)
self.title = wx.StaticText(self, label="Draw and Select Centroids")
self.title.SetFont(font1)
# ---------- INPUT FIELDS AND BUTTONS
self.lbldistance=wx.StaticText(self, label="distance threshold:")
self.fielddistance=wx.TextCtrl(self, value="")
self.buttonDrawTree=wx.Button(self, label="Draw Tree")
self.buttonShowdistance=wx.Button(self, label="Select distance")
self.buttonExportFile=wx.Button(self, label="Dump Output")
self.buttonSavePicture=wx.Button(self, label="Save as Png")
self.Bind(wx.EVT_BUTTON, self.selectdistance, self.buttonShowdistance)
self.Bind(wx.EVT_BUTTON, self.clickToConvert, self.buttonExportFile)
self.Bind(wx.EVT_BUTTON, self.DrawdistanceTree, self.buttonDrawTree)
self.Bind(wx.EVT_BUTTON, self.saveSnapshot, self.buttonSavePicture)
# ---------- MATRIX CONTAINER
distanceContainer = wx.FlexGridSizer(1, 6, 3, 3) # flexGridSzer (self, rows, cols, vgap, hgap) info at http://wxpython.org/docs/api/wx.FlexGridSizer-class.html
distanceContainer.SetFlexibleDirection(wx.HORIZONTAL) # specifies that rows are resized dynamically
distanceContainer.AddGrowableCol(4,1) # specifies that the column 1(starting from 0) can be regrown, ids specified below!
distanceContainer.AddMany([ # here insert them in order plaase
(self.buttonDrawTree, 0,wx.ALIGN_LEFT | wx.EXPAND, 3),
(self.buttonSavePicture, 0,wx.ALIGN_LEFT | wx.EXPAND, 3),
(self.buttonExportFile, 0,wx.ALIGN_LEFT | wx.EXPAND, 3),
(self.lbldistance, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT,3),
(self.fielddistance, 0,wx.ALIGN_LEFT | wx.EXPAND, 3),
(self.buttonShowdistance, 0,wx.ALIGN_LEFT | wx.EXPAND, 3),
])
# ----------- BOX CONTAINER
BigdistanceContainer = wx.BoxSizer(wx.VERTICAL) # more info about box sizing at http://zetcode.com/wxpython/layout/
BigdistanceContainer.Add(self.title, 0, wx.EXPAND | wx.ALL, 3 )
BigdistanceContainer.Add(distanceContainer, 1, wx.EXPAND | wx.ALL, 3 )
self.SetSizer(BigdistanceContainer)
self.Centre()
def DrawdistanceTree (self, event):
self.Parent.treePanel.plotFigure(self.arrayReadyforDrawing)
def selectdistance (self, event):
self.selecteddistance = self.fielddistance.GetValue()
self.Parent.treePanel.plotdistanceLine(self.selecteddistance)
def saveSnapshot(self, event):
# based largely on code posted to wxpython-users by Andrea Gavana 2006-11-08
dcSource = wx.PaintDC(self.Parent.treePanel)
size = dcSource.Size
# Create a Bitmap that will later on hold the screenshot image
# Note that the Bitmap must have a size big enough to hold the screenshot
# -1 means using the current default colour depth
bmp = wx.EmptyBitmap(size.width, size.height)
# Create a memory DC that will be used for actually taking the screenshot
memDC = wx.MemoryDC()
# Tell the memory DC to use our Bitmap
# all drawing action on the memory DC will go to the Bitmap now
memDC.SelectObject(bmp)
# Blit (in this case copy) the actual screen on the memory DC
# and thus the Bitmap
memDC.Blit( 0, # Copy to this X coordinate
0, # Copy to this Y coordinate
size.width, # Copy this width
size.height, # Copy this height
dcSource, # From where do we copy?
0, # What's the X offset in the original DC?
0 # What's the Y offset in the original DC?
)
# Select the Bitmap out of the memory DC by selecting a new
# uninitialized Bitmap
memDC.SelectObject(wx.NullBitmap)
img = bmp.ConvertToImage()
img.SaveFile("%s/tree.png"%(self.Parent.outputDir), wx.BITMAP_TYPE_PNG)
print ">> saved snapshot of dendrogram tree"
def clickToConvert(self, event):
self.convertCoordsAndExport(self.selecteddistance)
def convertCoordsAndExport (self, selecteddistance):
print ">> Exporting coordinates of selected centroids"
centroidArray = [] # this array will hold the indexes of the centroids
average_distance_ARRAY = [] # this one will hold the average distance values of clusters in the same order as the centroidArray
clusteredIndexArray = []
sorteddistanceArray = copy.deepcopy(self.Parent.distancePanel.distanceThresholdHash.keys())# this contains the sorted distances of the distance threshold hash containing the RMDS and the corresponding centroid
sorteddistanceArray.sort()
# parse the sorted array and return an array of the centroids and within distance values with distances below threshold
for e in range(len(sorteddistanceArray)):
if float(selecteddistance) < float(sorteddistanceArray[e]):
centroidArray = copy.deepcopy(self.Parent.distancePanel.distanceThresholdHash[sorteddistanceArray[e-1]][0])
average_distance_ARRAY = copy.deepcopy(self.Parent.distancePanel.distanceThresholdHash[sorteddistanceArray[e-1]][1])
clusteredIndexArray = copy.deepcopy(self.Parent.distancePanel.distanceThresholdHash[sorteddistanceArray[e-1]][2])
break
# in case the distance value is higher than the distances in the sorted arrays of distances:
if len(centroidArray) == 0:
centroidArray = copy.deepcopy(self.Parent.distancePanel.distanceThresholdHash[sorteddistanceArray[-1]][0])
average_distance_ARRAY = copy.deepcopy(self.Parent.distancePanel.distanceThresholdHash[sorteddistanceArray[-1]][1])
clusteredIndexArray = copy.deepcopy(self.Parent.distancePanel.distanceThresholdHash[sorteddistanceArray[-1]][2])
####################UPDATED PART############################
for i in xrange(0,len(self.coordinateArray),1):
if not i in centroidArray and not i in clusteredIndexArray:
centroidArray.append(i)
average_distance_ARRAY.append(0) #add zeros to RMSD array in a quantity equal to the elements added to centridArray
coordinates=self.coordinateArray[centroidArray,:] #SLICE HERE coordinateArray, to extract only data we're interested about
self.Parent.post.make_output(coordinates, average_distance_ARRAY) #make_output just needs 2 parameters
#self.Parent.post.make_output( centroidArray,average_distance_ARRAY, self.coordinateArray, clusteredIndexArray) #OLD CALL
def computeCluster (self):
#self.Parent.treePanel.Center()
# import the files into arrays using numpy
distanceMatrix = np.loadtxt('np_matrix.txt')
coordinateArray = np.loadtxt('coordinateArray.txt')
# distanceMatrix = self.Parent.post.distanceMatrix
# coordinateArray = self.Parent.post.coordinateArray
self.distanceMatrix = distanceMatrix#[0:50]
self.coordinateArray = coordinateArray#[0:50,0:50]
# create a dummy of the distanceMatrix
# self.distanceMatrixDummy = self.distanceMatrix[:,0:len(self.distanceMatrix[0,:])]
self.distanceMatrixDummy = copy.deepcopy(self.distanceMatrix)
# create a hash to be sorted that will contain the distance values as well as the pairwise coodinates
self.unSortedHash = {}
self.clusterHash = {} # the first value of the self.clusterHash will be the centroid of the cluster and the second will be the distanceMatrix index of structures
# keep track of all the index that have already been added in a cluster
self.clusteredIndex = []
# ------------------------------------------------------ CREATING CLUSTERS
# self.sortedHashKeys = self.unSortedHash.keys()
# self.sortedHashKeys.sort()
foundStructureInCluster = False # a boolean that lets you know whether you have either of struture in one of the clusters
NewAddedIndex = '' # the index of the structure found when iterating over all the clusters
clusterCount = 1
nonCentroids = [] # this array will be used to remove the non centroid elements in the sorted hash
ArrayeddistanceIndexes = []
valuesToBeRemoved = []
self.centroidArray = [] # this will contain an array of indexes which are the centroids of all the clusters
ClusterMergingSwitch = False
centroidIndexForMerge = 0 # used to know which index is the centroid when one cluster merges with a smaller one
self.maximaldistance = 0.00 # was used to stop clustering at the certain point but is now used to compute the graduation bar in the dendogram
self.iterant = 0 #### THIS IS THE ITERANT VARIABLE USED TO DRAW THE DENDOGRAM ####
self.DrawingArray = [] # this array will host all the small arrays to be drawn
arraylist1copy = []
arraylist2copy = [] # this is used to transfer the data between the first and second merged array when drawing
self.distanceMutliplier = 0.00
topdistanceBeforeRemoval = 0 # this is to store the distance value before being removed and using it to make functions
self.withinBetween = True # this value will help you to know whether the within distance is always smaller than the between distance
indexAway = 0
# TAKING data FROM distance THRESHOLD
self.distanceThresholdHash = {} # this dictionary will contain the centroids as well as their clustered indexes corresponding to each passed distance threshold
# Numpy matrix shortcut variables:
self.minimumIndex = 0
self.firstMinimumIndex = 0
self.secondMinimumIndex = 0
# as long as the self.sortedHash.Keys is no empty continue assigning clusters
while self.distanceMatrixDummy.min() != 100:
self.updataMinimumValue()
# self.distanceMatrixDummy[np.where(self.distanceMatrixDummy == self.distanceMatrixDummy.min())])
#for lolo in range(30):
# ======================================== WHEN CLUSTER IS EMPTY ===================================================================
# if there is nothing in the self.clusterHash, create your first cluster
if len(self.clusterHash.keys()) == 0:
self.clusterHash[clusterCount] = [None , [ self.firstMinimumIndex, self.secondMinimumIndex ]]
# adding them to the clustered index hash
self.clusteredIndex += [self.firstMinimumIndex , self.secondMinimumIndex]
#print "removing first element "+ str(self.clusterHash[clusterCount][1]) +" from list "
# self.DrawingArray += [ [clusterCount,[ self.makeSingleArray(self.distanceMatrixDummy.min())]] ]
self.DrawingArray += [ [clusterCount, [["ini", self.makeSingleArray(self.distanceMatrixDummy.min())]] ] ]
# remove value in the Matrix
self.distanceMatrixDummy[self.minimumIndex] = 100
#print "cluster so far -> "
#print self.clusterHash[clusterCount]
self.updataMinimumValue()
# Xx DRAWING xX
#print self.DrawingArray
# ======================================= WHEN TWO CLUSTERS ARE CLOSE ==============================================================
else:
# iterate over all the cluster hash values and see whether the two next closest structures are already in one of the clusters
for ki in self.clusterHash.keys():
# ---------------------------------------- TWO BIG CLUSTERS MERGER --------------------------------------------------------------
# if both the index in the sorted hash belong to a cluster, merge the two
if ( ( (self.firstMinimumIndex in self.centroidArray) and (self.secondMinimumIndex in self.centroidArray) ) ):
# print "Two Big Clusters found at proximity! proceeding to merge"
topdistanceBeforeRemoval = self.distanceMatrixDummy.min()
# getting the cluster numbers
for cluster1No in self.clusterHash.keys():
if self.firstMinimumIndex == self.clusterHash[cluster1No][0]:
break
for cluster2No in self.clusterHash.keys():
if self.secondMinimumIndex == self.clusterHash[cluster2No][0]:
break
if True:
#print "Two clusters to be merged are "+str(cluster1No)+" with centroid "+str(self.clusterHash[cluster1No][0])+" and "+str(cluster2No)+" with centroid "+str(self.clusterHash[cluster2No][0])
# getting elements from first cluster and transfer it to next one
# THE FIRST CLUSTER IS THE ONE TO ADD THE INDEXES TO!!
for ClusIdx2 in self.clusterHash[cluster2No][1]:
self.clusterHash[cluster1No][1] += [ClusIdx2]
# ----------------------------- ASSIGN A CLUSTER
self.clusterHash[cluster1No][0] = self.getCentroid(self.clusterHash[cluster1No][1], self.clusterHash[cluster1No][0])
# ----------------------------- DELETING OLD CENTROID OF OTHER CLUSTER FROM LIST
#print ">> Deleting old cluster indexes from the Dummy matrix"
if self.clusterHash[cluster1No][0] != self.clusterHash[cluster2No][0]:
for ind in self.clusterHash[cluster2No][1]:
# print "index "+str(ind)+" in dummy matrix deleted (replaced by 100s)"
if ind != self.clusterHash[cluster1No][0]:
self.distanceMatrixDummy[ind, :] = 100
self.distanceMatrixDummy[:,ind] = 100
#print ">> Deleting centroid of old cluster: "+str(self.clusterHash[cluster2No][0])
# -------------------------- ADDING OLD CENTROID VALUE TO LIST OF INDEXES
self.clusteredIndex += [self.clusterHash[cluster2No][0]]
# --------------------------- REMOVING OLD CENTROID FROM CENTROID ARRAY
self.centroidArray.pop(self.centroidArray.index(self.clusterHash[cluster2No][0]))
#print "centroid array: "+str(self.centroidArray)
# --------------------------- ADDING NEW CENTROID TO CENTROID ARRAY
if self.clusterHash[cluster1No][0] not in self.centroidArray:
self.centroidArray += [self.clusterHash[cluster1No][0]]
# --------------------------- DELETING SECOND CLUSTER
del self.clusterHash[cluster2No]
#print "deleted cluster number "+str(cluster2No)+" permanently!"
#self.CheckIfClusterFull(cluster1No, self.clusterHash[cluster1No][0], self.clusterHash[cluster1No][1])
ClusterMergingSwitch = True
# =================================== Xx DRAWING xX ===========================================
# those clusters are TWO BIG ONES SO THREAD CAREFULLY #TODO: try see if height difference matters
# locate first cluster:
for arraylist1 in self.DrawingArray:
if arraylist1[0] == cluster1No:
break
for arraylist2 in self.DrawingArray:
if arraylist2[0] == cluster2No:
break
# add all the content of arraylist 2 into arraylist 1
arraylist1copy = copy.deepcopy(arraylist1[1])
arraylist2copy = copy.deepcopy(arraylist2[1])
# dealing with size now!
if len(arraylist1copy) > len(arraylist2copy):
indexAway = 0
for elem in arraylist2[1]:
arraylist1[1] += [elem]
indexAway += 1
# merge the two arrays
# arraylist1[1] += [self.joinTwoArrays(arraylist1copy[-1] ,arraylist2copy[-1],topdistanceBeforeRemoval )]
arraylist1[1].append( [ "self.joinTwoArrays",[ indexAway, topdistanceBeforeRemoval ] ])
# destroy the scond array permanently:
self.DrawingArray.pop(self.DrawingArray.index(arraylist2))
# in case the second one is bigger than the first one just reverse the addition of elements
else:
indexAway = 0
for elem in arraylist1[1]:
arraylist2[1] += [elem]
indexAway += 1
# merge the two arrays
# arraylist2[1] += [self.joinTwoArrays(arraylist2copy[-1] ,arraylist1copy[-1],topdistanceBeforeRemoval )]
arraylist2[1].append( [ "self.joinTwoArrays",[ indexAway, topdistanceBeforeRemoval ] ])
# destroy the first array permanently:
self.DrawingArray.pop(self.DrawingArray.index(arraylist1))
# rename the number label of the second array so that the others can still merge with it
arraylist2[0] = cluster1No
#print "MERGED 2 BIG CLUSTERS AND DELETED ELEMENT "+str(cluster2No)
# print self.DrawingArray
# save the centroids of that distance
self.SaveCentroidsAtdistance(topdistanceBeforeRemoval)
self.updataMinimumValue()
break
# ---------------------------------------- ONE SMALL AND ONE BIG CLUSTER MERGER -------------------------------------------------
elif ( ((self.firstMinimumIndex in self.centroidArray) and (self.secondMinimumIndex in self.clusterHash[ki][1]) ) \
or ( (self.secondMinimumIndex in self.centroidArray) and (self.firstMinimumIndex in self.clusterHash[ki][1]) ) ):
# print "Bigger cluster will absord smaller one"
# getting which index is centroid
if self.firstMinimumIndex in self.centroidArray:
centroidIndexForMerge = self.firstMinimumIndex # -> 21
else:
centroidIndexForMerge = self.secondMinimumIndex
topdistanceBeforeRemoval = self.distanceMatrixDummy.min()
# getting all the indexes from one cluster to another
# locate the cluster having for centroid centroidIndexForMerge
for clusterKey in self.clusterHash.keys():
if centroidIndexForMerge == self.clusterHash[clusterKey][0]:
#self.checkWithinBetween(self.clusterHash[clusterKey], self.clusterHash[ki], topdistanceBeforeRemoval )
for ind in self.clusterHash[ki][1]:
self.clusterHash[clusterKey][1] += [ind]
break
# ---------------- CREATING CENTROID
# create a new centroid index if there is more than 3 indexes in the cluster array
self.clusterHash[clusterKey][0] = self.getCentroid(self.clusterHash[clusterKey][1], self.clusterHash[clusterKey][0])
#print "Centroid assigned for newly merged cluster "+str(clusterKey)+" -> "+str(self.clusterHash[clusterKey][0])
# add the new centroid value to the centroid array:
if (self.clusterHash[clusterKey][0] not in self.centroidArray):
self.centroidArray += [self.clusterHash[clusterKey][0]]
# delete the centroid index from the self.clusteredIndex
if self.clusterHash[clusterKey][0] in self.clusteredIndex:
self.clusteredIndex.pop(self.clusteredIndex.index(self.clusterHash[clusterKey][0]))
# ---------------- REMOVING THE NON CENTROID ELEMENTS OF NEW CLUSTER FROM LIST
#print ">> deleting elements "+str(nonCentroids)+" from the distance lists "
for indexValues in self.clusterHash[ki][1]:
# print "index "+str(indexValues)+" in dummy matrix deleted (replaced by 100s)"
self.distanceMatrixDummy[indexValues, :] = 100
self.distanceMatrixDummy[:,indexValues] = 100
# ---------------- DELETING THE MERGED CLUSTER
#print "deleting cluster: "+str(ki)
del self.clusterHash[ki]
#self.CheckIfClusterFull(clusterKey, self.clusterHash[clusterKey][0], self.clusterHash[clusterKey][1])
# self.PrintSortedListState()
ClusterMergingSwitch = True
# Xx DRAWING xX
# those clusters are small and of the same height normally #TODO: try see if height difference matters
# locate first cluster:
for arraylist1 in self.DrawingArray:
if arraylist1[0] == clusterKey:
break
for arraylist2 in self.DrawingArray:
if arraylist2[0] == ki:
break
indexAway = 0
for elem in arraylist2[1]:
arraylist1[1] += [elem]
indexAway += 1
# merge the two arrays
# arraylist1[1] += [self.joinTwoArrays(arraylist1copy[-1] ,arraylist2copy[-1],topdistanceBeforeRemoval )]
arraylist1[1].append( ["self.joinTwoArrays", [indexAway, topdistanceBeforeRemoval ]] )
# destroy the scond array permanently:
self.DrawingArray.pop(self.DrawingArray.index(arraylist2))
#print "MERGED 1 SMALL AND 1 BIG CLUSTERS AND DELETED ELEMENT "
# print self.DrawingArray
# save the centroids of that distance
self.SaveCentroidsAtdistance(topdistanceBeforeRemoval)
self.updataMinimumValue()
break
# --------------------------------------------- MERGING 2 SMALL CLUSTERS ---------------------------------------
elif ((self.firstMinimumIndex in self.clusteredIndex) and (self.secondMinimumIndex in self.clusteredIndex)):
# print "merging two small clusters !!"
topdistanceBeforeRemoval = self.distanceMatrixDummy.min()
#getting the cluster numbers
for cluster1No in self.clusterHash.keys():
if self.firstMinimumIndex in self.clusterHash[cluster1No][1]:
break
for cluster2No in self.clusterHash.keys():
if self.secondMinimumIndex in self.clusterHash[cluster2No][1]:
break
# print "Two clusters to be merged are "+str(cluster1No)+" with centroid "+str(self.clusterHash[cluster1No][0])+" and "+str(cluster2No)+" with centroid "+str(self.clusterHash[cluster2No][0])
# getting elements from first cluster and transfer it to next one
# THE FIRST CLUSTER IS THE ONE TO ADD THE INDEXES TO!!
for ClusIdx2 in self.clusterHash[cluster2No][1]:
self.clusterHash[cluster1No][1] += [ClusIdx2]
# ----------------------------- ASSIGN A CLUSTER
self.clusterHash[cluster1No][0] = self.getCentroid(self.clusterHash[cluster1No][1], self.clusterHash[cluster1No][0])
# ----------------------------- DELETING FROM SORTED LIST ALL THE INDEXES 1ST CLUSTER ARRAY
# get the non centroid elements:
nonCentroids = copy.deepcopy(self.clusterHash[cluster1No][1])
nonCentroids.pop(nonCentroids.index(self.clusterHash[cluster1No][0]))
#print ">> deleting elements "+str(nonCentroids)+" from the distance lists, not get centroid function "
for indexValues in nonCentroids:
# print "index "+str(indexValues)+" in dummy matrix deleted (replaced by 100s)"
self.distanceMatrixDummy[indexValues, :] = 100
#print self.distanceMatrixDummy[indexValues]
self.distanceMatrixDummy[:,indexValues] = 100
#print self.distanceMatrixDummy[:,indexValues]
# ----------------------------- DELETE NEW CENTROID FROM INDEX ARRAY
if self.clusterHash[cluster1No][0] in self.clusteredIndex:
self.clusteredIndex.pop(self.clusteredIndex.index(self.clusterHash[cluster1No][0]))
#print "deleted "+str(self.clusterHash[cluster1No][0])+" from centroid array"
# ----------------------------- ADDING NEW CENTROID TO CENTROID ARRAY
if self.clusterHash[cluster1No][0] not in self.centroidArray:
self.centroidArray += [self.clusterHash[cluster1No][0]]
# --------------------------- DELETING SECOND CLUSTER
del self.clusterHash[cluster2No]
#print "deleted cluster number "+str(cluster2No)+" permanently!"
ClusterMergingSwitch = True
# Xx DRAWING xX
# those clusters are small and of the same height normally
# locate first cluster:
for arraylist1 in self.DrawingArray:
if arraylist1[0] == cluster1No:
break
for arraylist2 in self.DrawingArray:
if arraylist2[0] == cluster2No:
break
indexAway = 0 # counts the number of indexes away from arraylist1 arraylist2 is
for elem in arraylist2[1]:
arraylist1[1].append(elem)
indexAway += 1
# merge the two arrays
# arraylist1[1] += [self.joinTwoArrays(arraylist1copy[-1] ,arraylist2copy[-1], topdistanceBeforeRemoval )]
arraylist1[1] += [ ["self.joinTwoArrays", [ indexAway , topdistanceBeforeRemoval] ] ]
# destroy the scond array permanently:
self.DrawingArray.pop(self.DrawingArray.index(arraylist2))
# print "MERGED 2 SMALL CLUSTERS " #+str(cluster2No)
#print self.DrawingArray
# save the centroids of that distance
self.SaveCentroidsAtdistance(topdistanceBeforeRemoval)
self.updataMinimumValue()
break
# ======================================= WHEN TWO CLUSTERS ARE FAR AWAY BUT SINGLE INDEXES ARE CLOSE =====================================
if ClusterMergingSwitch == False:
for i in self.clusterHash.keys():
# if one of the structure index is already present in a cluster, add the other one
if ((self.firstMinimumIndex in self.clusterHash[i][1]) or (self.secondMinimumIndex in self.clusterHash[i][1])):
# adding either of the numbers in the array of the found cluster
if (self.firstMinimumIndex in self.clusterHash[i][1]):
self.clusterHash[i][1] += [self.secondMinimumIndex] # adding the index to appropriate cluster
self.clusteredIndex += [self.secondMinimumIndex] # adding the index to the list of added indexes
NewAddedIndex = self.secondMinimumIndex
elif (self.secondMinimumIndex in self.clusterHash[i][1]):
self.clusterHash[i][1] += [self.firstMinimumIndex] # adding the index to appropriate cluster
self.clusteredIndex += [self.firstMinimumIndex] # adding the index to the list of added indexes
NewAddedIndex = self.firstMinimumIndex
# print "new index to be added to cluster "+str(i)+" : "+str(NewAddedIndex)
# Xx DRAWING xX
for arraylist in self.DrawingArray:
if arraylist[0] == i:
self.iterant += 1
# arraylist[1] += [self.addSIngleIndex (arraylist[1][-1], self.distanceMatrixDummy.min()) ]
arraylist[1] += [ ["addSingleIndex", "x"+str(self.iterant) ,self.distanceMatrixDummy.min()] ]
break
# save the centroids of that distance
self.SaveCentroidsAtdistance(self.distanceMatrixDummy.min())
# remove value in the Matrix
self.distanceMatrixDummy[self.minimumIndex] = 100
# switch back the found in structure boolean
foundStructureInCluster = True
# -------------------------------- ASSIGNING NEW CENTROID
# create a new centroid index if there is more than 3 indexes in the cluster array
self.clusterHash[i][0] = self.getCentroid(self.clusterHash[i][1], self.clusterHash[i][0])
#print "Centroid assigned for cluster "+str(i)+" -> "+str(self.clusterHash[i][0])
# add the new centroid value to the centroid array:
if (self.clusterHash[i][0] not in self.centroidArray):
self.centroidArray += [self.clusterHash[i][0]]
# delete the centroid index from the self.clusteredIndex
if self.clusterHash[i][0] in self.clusteredIndex:
self.clusteredIndex.pop(self.clusteredIndex.index(self.clusterHash[i][0]))
# ---------------------------- REMOVING NON CENTROID PARTS
if len(self.clusterHash[i][1]) == 3:
# get the non centroid elements:
nonCentroids = copy.deepcopy(self.clusterHash[i][1])
nonCentroids.pop(nonCentroids.index(self.clusterHash[i][0]))
#print ">> deleting elements "+str(nonCentroids)+" from the distance lists "
for indexValues in nonCentroids:
# print "index "+str(indexValues)+" in dummy matrix deleted (replaced by 100s)"
self.distanceMatrixDummy[indexValues, :] = 100
self.distanceMatrixDummy[:,indexValues] = 100
elif len(self.clusterHash[i][1]) > 3:
# get the non centroid elements:
nonCentroids = copy.deepcopy(self.clusterHash[i][1])
nonCentroids.pop(nonCentroids.index(self.clusterHash[i][0]))
#print ">> deleting element "+str(NewAddedIndex)+" from the distance lists "
for indexValues in nonCentroids:
# print "index "+str(indexValues)+" in dummy matrix deleted (replaced by 100s)"
self.distanceMatrixDummy[indexValues, :] = 100
self.distanceMatrixDummy[:,indexValues] = 100
self.updataMinimumValue()
break
# ---------------------------------- CREATING NEW CLUSTERS ---------------------------------
if foundStructureInCluster == False:
clusterCount += 1
# create a new cluster with the following data
self.clusterHash[clusterCount] = [None , [ self.firstMinimumIndex , self.secondMinimumIndex ]]
# add the new elements to the list of already added elements
for newElem in self.clusterHash[clusterCount][1]:
self.clusteredIndex += [newElem]
self.DrawingArray.append( [clusterCount, [ [ "ini" , self.makeSingleArray(self.distanceMatrixDummy.min()) ] ]] )
# remove value in the Matrix
self.distanceMatrixDummy[self.minimumIndex] = 100
self.updataMinimumValue()
# Xx DRAWING xX
#print self.DrawingArray
# switch back the foundStructureInCluster boolean:
foundStructureInCluster = False
self.withinBetween = True
self.clusteredIndex.sort()
#print "indexes assiged to clusters so far:"+ str(self.clusteredIndex)
print " > distances remaining: "+str(len(self.distanceMatrixDummy[self.distanceMatrixDummy < 100]))+" | number of clusters: "+str(len(self.clusterHash.keys()))
# restart the clusterSwitch
ClusterMergingSwitch = False
self.PrintClusterState()
# self.PrintSortedListState()
#print self.clusteredIndex
# ==================================================================================================================================================== #
# =========================================================== END OF WHILE LOOP ====================================================================== #
# ==================================================================================================================================================== #
print ">>> PREPARING TO DRAW NOW"
# print self.DrawingArray
#print ">>> distance mutliplier: "+str(self.distanceMutliplier)
# first need to extract the x data into a dictio to make the arrays real
print ">>> initialising Drawing Array"
self.unSeparatedArray = []
# counter = 0.00
for clusters in self.DrawingArray:
for clusterElement in clusters[1]:
self.unSeparatedArray += [clusterElement]
# counter += 1.00
# print str(float(counter/ len(self.DrawingArray))*100.00)+" %"
xdictio = {}
xiterator = 900.0/float(len(self.distanceMatrix)) # len matrix because there as many x as indexes
# secondCounter = 0.00
print ">>> Creating coordinate array for drawing"
for coordArray in self.unSeparatedArray:
if coordArray[0] == "ini":
xdictio[coordArray[1][0][0]] = xiterator
xiterator += 900.0/float(len(self.distanceMatrix))
xdictio[coordArray[1][1][0]] = xiterator
xiterator += 900.0/float(len(self.distanceMatrix))
if coordArray[0] == "addSingleIndex":
xdictio[coordArray[1]] = xiterator
xiterator += 900.0/float(len(self.distanceMatrix))
# secondCounter += 1.00
# print str(float(secondCounter/ len(unSeparatedArray))*100.00)+" %"
self.arrayReadyforDrawing = []
thirdCounter = 0.00
print ">>> Converting virtual coordinates into actual ones "
# MAKE THE UNSEPARATED DRAWING ARRAY REAL!!!!!
index = 0
distance = 0
indexOfFirst = 0
indexOfSecond= 0
for bluePrints in self.unSeparatedArray:
# self.arrayReadyforDrawing += [self.makeArrayReal( bluePrints , xdictio)]
if bluePrints[0] == "ini":
self.arrayReadyforDrawing += [self.makeArrayReal( bluePrints[1] , xdictio)]
# print self.arrayReadyforDrawing
if bluePrints[0] == "addSingleIndex" :
# getting the distance
if self.unSeparatedArray[index - 1][0] == "ini" or self.unSeparatedArray[index - 1][0] == "self.joinTwoArrays":
distance = self.unSeparatedArray[index - 1][-1][-1]
else:
distance = self.unSeparatedArray[index - 1][-1]
self.arrayReadyforDrawing += [self.addSingleIndex(bluePrints[1], bluePrints[2], xdictio, distance)]
if bluePrints[0] == "self.joinTwoArrays":
# get the unseparated array index of the 2nd array to join:
indexOfFirst = self.unSeparatedArray.index(bluePrints)
indexOfSecond = indexOfFirst - bluePrints[1][0] - 1
# getting the distance
if self.unSeparatedArray[index - 1][0] == "ini" or self.unSeparatedArray[index - 1][0] == "self.joinTwoArrays":
distance = self.unSeparatedArray[index - 1][-1][-1]
else:
distance = self.unSeparatedArray[index - 1][-1]
self.arrayReadyforDrawing += [self.joinTwoArrays(indexOfSecond ,bluePrints[1][1],distance)]
index += 1
thirdCounter += 1.00
percentage = float(thirdCounter/len(self.unSeparatedArray))*100.00
if percentage == 20 or percentage == 40 or percentage == 60 or percentage == 80 or (float(thirdCounter/len(self.unSeparatedArray))*100.00) == 100:
print ">>> Drawing %.2f"%(percentage)+ " %"
#print arrayReadyforDrawing
print ">>> Done!"
# -------------------------------------------------------------------------------------------------------------------------------------------------
# FUNCTIONS FOR CLUSTER CREATION
# -------------------------------------------------------------------------------------------------------------------------------------------------
def updataMinimumValue(self):
if self.distanceMatrixDummy.min() != 100:
self.minimumIndex = np.where(self.distanceMatrixDummy == self.distanceMatrixDummy.min())
# print self.distanceMatrixDummy.min()
# print "error ?: "+str(self.minimumIndex[0])
self.firstMinimumIndex = int(self.minimumIndex[0][0])
self.secondMinimumIndex = int(self.minimumIndex[1][0])
# saving the last distance value so as to compute the dendogram tree according to that height
self.maximaldistance = self.distanceMatrixDummy.min()
self.distanceMutliplier = (self.Parent.treePanel.GetSize()[1] - 50)/(self.distanceMatrixDummy.min())
def getCentroid (self, array, oldCentroid):
distanceHash = {} # contains the average distance as key and index as value
distanceArray = [] # to hold the different distance to calculate mean value
newCentroid = 0
# first you need to get all distances relative of one to another
for i in array: # -> the array contains the indexes of the proteins: 0,1,3, ...
for j in array:
if i == j:
pass # as it does not make sense to look for the distance value for self.distanceMatrix[0][0]