-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility_functions.py
1042 lines (848 loc) · 39 KB
/
utility_functions.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
#!/bin/python3
import re
import numpy as np
import pandas as pd
import scipy
import jenkspy
from pyclustering.cluster.xmeans import xmeans
from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer
from sklearn.cluster import AffinityPropagation
from pyclustering.cluster.gmeans import gmeans
from sklearn.cluster import DBSCAN
from sklearn.cluster import KMeans
from sklearn.neighbors import kneighbors_graph
from scipy.sparse import csgraph
from scipy.spatial.distance import pdist, squareform
from sklearn import preprocessing
from scipy.cluster.hierarchy import linkage
from scipy.cluster.hierarchy import fcluster
import spectral_clustering as SC
'''
define functions
'''
'''
preprocessing steps:
1. convert vcf to SNP matrix (sum of two haplotype matrix)
2. LD measurement
3. independent LD block partition
'''
def vcf2hapmatrix(vcf):
hap_matrix_d1 = {} #haplotype 1 of individuals, key as chromosome number
hap_matrix_d2 = {} #haplotype 2 of individuals, key as chromosome number
variant_names = {}
variant_positions = {} #key as chromosome number
chromosome = [] #key as chromosome number and value as number of SNPs per chromosome
with open(vcf,"r") as VCF:
for line in VCF:
if re.search("^##",line): ## skip the first annotation lines
continue
elif re.search("^#CHROM",line): ## acquire the sample name information
line = line.strip("\n")
ind_names = line.split("\t")[9:]
else:
line = line.strip("\n")
items = line.split("\t")
ch = items[0]
if ch not in chromosome:
chromosome.append(ch)
variant_names[ch] = [items[2]]
variant_positions[ch] = [int(items[1])]
hap_matrix_d1[ch] = []
hap_matrix_d2[ch] = []
genotype = items[9:]
for i in range(len(genotype)):
m = re.search('([0-9])\|([0-9])',genotype[i])
hap_matrix_d1[ch].append(int(m.group(1)))
hap_matrix_d2[ch].append(int(m.group(2)))
else:
variant_names[ch].append(items[2])
variant_positions[ch].append(int(items[1]))
genotype = items[9:]
for i in range(len(genotype)):
m = re.search('([0-9])\|([0-9])',genotype[i])
hap_matrix_d1[ch].append(int(m.group(1)))
hap_matrix_d2[ch].append(int(m.group(2)))
for ch in chromosome:
hap_matrix_d1[ch] = np.reshape(np.asarray(hap_matrix_d1[ch],dtype=int),(len(variant_names[ch]),len(ind_names)))
hap_matrix_d2[ch] = np.reshape(np.asarray(hap_matrix_d2[ch],dtype=int),(len(variant_names[ch]),len(ind_names)))
return(hap_matrix_d1,hap_matrix_d2,variant_names,variant_positions,chromosome)
def Standardize(X):
r,c = X.shape
mean = np.mean(X, axis=0)
std = np.std(X,axis = 0)
X_sd = (X - mean)/std
return(X_sd)
def remove_duplicates(duplicated_list):
final_list = []
for num in duplicated_list:
if num not in final_list:
final_list.append(int(num)) # change the data type into integers
return(final_list)
def sortFirst(val):
return val[0] # sort by the first element
def find_blocks(joints_array,master_array):
for i in range(len(joints_array)-1):
index1 = master_array.index(joints_array[i])
index2 = master_array.index(joints_array[i+1])
del master_array[index1:index2]
return(master_array)
def find_joints(joints_array):
joints_dedup = remove_duplicates(joints_array)
joints_dedup.sort()
return(joints_dedup)
def find_index(joints_array,master_array):
index = []
for i in joints_array:
index.append(master_array.index(i))
return(index)
def haplotype_matrix(block_index,num_hap,hap):
columns = [str(block_index)+'_'+str(i) for i in range(num_hap)]
n,m = hap.shape
haplotype_matrix = np.zeros((n,num_hap),dtype=int)
unique_haplotype = []
for i in range(n):
haplotype = "".join(map(str,hap.values[i,:]))
if haplotype not in unique_haplotype:
unique_haplotype.append(haplotype)
j = unique_haplotype.index(haplotype)
haplotype_matrix[i,j] += 1
return(haplotype_matrix)
def haplotype_distance_calculation(dedup,weights):
r,c = dedup.shape
distance_matrix = np.zeros(shape = (r,r))
for i in range(r):
for j in range(i+1,r):
distance_matrix[i,j] = np.dot(np.abs(dedup[i,:] - dedup[j,:]), weights) / np.sum(weights)
distance_matrix[j,i] = distance_matrix[i,j]
return(distance_matrix)
def xmeans_clustering(array):
# Prepare initial centers - amount of initial centers defines amount of clusters from which X-Means will
# start analysis.
amount_initial_centers = 2
initial_centers = kmeans_plusplus_initializer(array, amount_initial_centers).initialize()
# Create instance of X-Means algorithm. The algorithm will start analysis from 2 clusters, the maximum
# number of clusters that can be allocated is 20.
xmeans_instance = xmeans(array, initial_centers, 10)
xmeans_instance.process()
# Extract clustering results: clusters and their centers
clusters_ = xmeans_instance.get_clusters()
clusters = [0]*len(array)
for i in range(len(clusters_)):
for j in clusters_[i]:
clusters[j] = i
return(clusters)
def gmeans_clustering(array):
gmeans_instance = gmeans(array, repeat=10).process()
clusters_ = gmeans_instance.get_clusters()
clusters = [0]*len(array)
for i in range(len(clusters_)):
for j in clusters_[i]:
clusters[j] = i
return(clusters)
def affinity_propagation(np_array):
AP = AffinityPropagation(random_state=None).fit(np_array)
labels = AP.labels_
return(labels)
def DBSCAN_clustering(np_array):
r,c = np_array.shape
clusters = DBSCAN(eps=0.1,min_samples=2,p=2).fit(np_array)
labels = clusters.labels_
return(labels)
def local_scale_Spectral(np_array):
r,c =np_array.shape
k = max(int(r/10),5)
dists = squareform(pdist((np_array)))
knn_distances = np.sort(dists, axis=0)[k]
knn_distances = knn_distances[np.newaxis].T
local_scale = knn_distances.dot(knn_distances.T)
affinity_matrix = - dists * dists / local_scale
affinity_matrix[np.where(np.isnan(affinity_matrix))] = 0.0
affinity_matrix = np.exp(affinity_matrix)
np.fill_diagonal(affinity_matrix, 0)
L = csgraph.laplacian(affinity_matrix,normed = True)
eig_val, eig_vec = np.linalg.eig(L)
eig_val = np.real(eig_val)
eig_vec = np.real(eig_vec)
eig_vec = eig_vec[:,np.argsort(eig_val)]
eig_val = eig_val[np.argsort(eig_val)]
if sum(np.iscomplex(eig_val)) > 0:
print("Spectral Clustering failed. Clusters are assigned by affinity_propagation.")
print(np_array.shape)
labels = affinity_propagation(np_array)
print(max(labels))
if labels[0] == -1 or max(labels) == 0:
labels = np.arange(np_array.shape[0])
print("Affinity propagation failed")
else:
index_largest_gap = np.argsort(np.diff(eig_val))[::-1][0]
#print(index_largest_gap)
n_clusters = index_largest_gap + 2
V = eig_vec[:,:n_clusters]
Z = linkage(V, 'ward')
labels = fcluster(Z, n_clusters, criterion='maxclust') - 1
return(labels)
def Spectral_clustering(np_array):
r,c =np_array.shape
print(r,c)
dists = squareform(pdist((np_array)))
W = np.zeros(shape=(r,r))
for i in range(r):
for j in range(i+1,r):
W[i,j] = np.exp(-(dists[i,j]**2))
W[j,i] = W[i,j]
D = np.diag(np.sum(W,axis=0))
D_inv = np.linalg.inv(D)
L_rw = np.identity(r) - np.matmul(D_inv,W)
eig_val, eig_vec = np.linalg.eig(L_rw)
eig_vec = eig_vec[:,np.argsort(eig_val)]
eig_val = eig_val[np.argsort(eig_val)]
if sum(np.iscomplex(eig_val)) > 0:
print("Spectral Clustering failed. Clusters are assigned by affinity_propagation.")
print(np_array.shape)
labels = affinity_propagation(np_array,weights)
if labels[0] == -1 or max(labels) == 0:
labels = np.arange(np_array.shape[0])
print("Affinity propagation failed")
else:
index_eigen_gap = np.argmax(np.diff(eig_val))
n_clusters = index_eigen_gap + 2
V = eig_vec[:,:n_clusters]
Z = linkage(V, 'ward')
labels = fcluster(Z, n_clusters, criterion='maxclust') - 1
return(labels)
# def local_scale_Spectral(np_array,weights):
# r,c =np_array.shape
# k = max(int(r/10),5)
# dists = haplotype_distance_calculation(np_array,weights)
# W = np.zeros(shape=(r,r))
# for i in range(r):
# i_neighbors = np.argsort(dists[i,:])[:k]
# for j in range(i+1,r):
# j_neighbors = np.argsort(dists[j,:])[:k]
# W[i,j] = np.exp(-(dists[i,j]**2/(dists[i,i_neighbors[-1]]*dists[j,j_neighbors[-1]])))
# W[j,i] = W[i,j]
# D = np.diag(np.sum(W,axis=0))
# D_inv = np.linalg.inv(D)
# #L_rw = np.identity(r) - np.matmul(D_inv,W)
# L_rw = csgraph.laplacian(W,normed = True)
# eig_val, eig_vec = np.linalg.eig(L_rw)
# eig_vec = eig_vec[:,np.argsort(eig_val)]
# eig_val = eig_val[np.argsort(eig_val)]
# print(eig_val)
# if sum(np.iscomplex(eig_val)) > 0:
# print("Spectral Clustering failed. Clusters are assigned by affinity_propagation.")
# print(np_array.shape)
# labels = affinity_propagation(np_array,weights)
# print(max(labels))
# if labels[0] == -1 or max(labels) == 0:
# labels = np.arange(np_array.shape[0])
# print("Affinity propagation failed")
# else:
# index_eigen_gap = np.argmax(np.diff(eig_val))
# n_clusters = index_eigen_gap + 2
# V = eig_vec[:,:n_clusters]
# Z = linkage(V, 'ward')
# labels = fcluster(Z, n_clusters, criterion='maxclust') - 1
# return(labels)
# def KNN_Spectral(np_array,weights):
# distance = haplotype_distance_calculation(np_array,weights)
# r,c =np_array.shape
# k = max(int(r/10),5)
# W = np.zeros(shape=(r,r))
# for i in range(r):
# i_neighbors = np.argsort(distance[i,:])[:k]
# for j in range(i+1,r):
# j_neighbors = np.argsort(distance[j,:])[:k]
# if i in j_neighbors or j in i_neighbors:
# W[i,j] = 1 - distance[i,j]
# W[j,i] = W[i,j]
# D = np.diag(np.sum(W,axis=0))
# D_inv = np.linalg.inv(D)
# L_rw = np.identity(r) - np.matmul(D_inv,W)
# eig_val, eig_vec = np.linalg.eig(L_rw)
# eig_vec = eig_vec[:,np.argsort(eig_val)]
# eig_val = eig_val[np.argsort(eig_val)]
# if sum(np.iscomplex(eig_val)) > 0:
# print("Spectral Clustering failed. Clusters are assigned by affinity_propagation.")
# labels = affinity_propagation(np_array,weights)
# print(max(labels))
# if labels[0] == -1 or max(labels) == 0:
# labels = np.arange(df.shape[0])
# print("Affinity propagation failed")
# else:
# index_eigen_gap = np.argmax(np.diff(eig_val))
# n_clusters = index_eigen_gap + 2
# V = eig_vec[:,:n_clusters]
# Z = linkage(V, 'ward')
# labels = fcluster(Z, n_clusters, criterion='maxclust') - 1
# return(labels)
def KNN_Spectral(df):
r,c =df.shape
k = max(int(r/10),5)
connectivity = kneighbors_graph(X=df, n_neighbors=k, mode='distance')
A = (1/2)*(connectivity + connectivity.T)
L = csgraph.laplacian(A,normed = True)
L = L.toarray()
eig_val, eig_vec = np.linalg.eig(L)
eig_vec = eig_vec[:,np.argsort(eig_val)]
eig_val = eig_val[np.argsort(eig_val)]
#print(eig_val)
if sum(np.iscomplex(eig_val)) > 0:
print("Spectral Clustering failed. Clusters are assigned by affinity_propagation.")
labels = affinity_propagation(df)
print(max(labels))
if labels[0] == -1 or max(labels) == 0:
labels = np.arange(df.shape[0])
print("Affinity propagation failed")
else:
index_eigen_gap = np.argmax(np.diff(eig_val))
n_clusters = index_eigen_gap + 2
V = eig_vec[:,:n_clusters]
Z = linkage(V, 'ward')
labels = fcluster(Z, n_clusters, criterion='maxclust') - 1
return(labels)
# def haplotype_DM_generator(block_index,clustering,haplotypes,n_clusters,breakpoints,positions,ch):
# dictionary = {}
# r,c = haplotypes.shape
# dedup_haplotypes = haplotypes.drop_duplicates(keep = 'first')
# d_r,d_c = dedup_haplotypes.shape
# if d_r <= n_clusters: # The situation no.1 no clustering is necessary
# for i in range(d_r):
# haplotype_ = "".join(map(str,dedup_haplotypes.values[i,:]))
# dictionary[haplotype_] = i
# name = [ch+"@"+str(positions[breakpoints[block_index]])+"-"+str(positions[breakpoints[block_index+1]])+'_'+str(i) for i in range(d_r)]
# DM_matrix_1 = np.zeros((int(r/2),d_r),dtype=int)
# DM_matrix_2 = np.zeros((int(r/2),d_r),dtype=int)
# else: # The situation no.2 Too many haplotyps, clustering is needed.
# # choose the clustering function
# if clustering == 'xmeans':
# clusters = xmeans_clustering(list(dedup_haplotypes.values))
# elif clustering == 'affinity_propagation':
# clusters = affinity_propagation(dedup_haplotypes.values)
# if clusters[0] == -1:
# clusters = np.arange(d_r)
# print("Affinity propagation failed")
# # generate the dictionary. key: haplotype (str), values (cluster index)
# for j in range(d_r):
# haplotype_ = "".join(map(str,dedup_haplotypes.values[j,:]))
# dictionary[haplotype_] = clusters[j]
# # the column names of the dataframe
# name = [ch+"@"+str(positions[breakpoints[block_index]])+"-"+str(positions[breakpoints[block_index+1]])+'_'+str(i) for i in range(max(clusters)+1)]
# DM_matrix_1 = np.zeros((int(r/2),max(clusters)+1),dtype=int)
# DM_matrix_2 = np.zeros((int(r/2),max(clusters)+1),dtype=int)
# # generate the haplotype design matrix
# for k in range(int(r/2)):
# tmp_1 = "".join(map(str,haplotypes.values[k,:]))
# tmp_2 = "".join(map(str,haplotypes.values[k+int(r/2),:]))
# l_1 = dictionary[tmp_1]
# l_2 = dictionary[tmp_2]
# DM_matrix_1[k,l_1] += 1
# DM_matrix_2[k,l_2] += 1
# haplotype_DM = DM_matrix_1 + DM_matrix_2
# haplotype_DM_pd = pd.DataFrame(haplotype_DM,columns=name)
# return(haplotype_DM_pd,name)
# def haplotype_DM_generator_1(block_index,clutering_algorithm,haplotypes,n_clusters,breakpoints,positions,ch):
# dictionary = {}
# r,c = haplotypes.shape
# dedup_haplotypes = haplotypes.drop_duplicates(keep = 'first')
# d_r,d_c = dedup_haplotypes.shape
# if d_r <= n_clusters: # The situation no.1 no clutering_algorithm is necessary
# for i in range(d_r):
# haplotype_ = "".join(map(str,dedup_haplotypes.values[i,:]))
# dictionary[haplotype_] = i
# name = [ch+"@"+str(positions[breakpoints[block_index][0]])+"-"+str(positions[breakpoints[block_index][1]])+'_'+str(i) for i in range(d_r)]
# DM_matrix_1 = np.zeros((int(r/2),d_r),dtype=int)
# DM_matrix_2 = np.zeros((int(r/2),d_r),dtype=int)
# else: # The situation no.2 Too many haplotyps, clutering_algorithm is needed.
# # choose the clustering function
# if clutering_algorithm == 'xmeans':
# clusters = xmeans_clustering(list(dedup_haplotypes.values))
# elif clutering_algorithm == 'affinity_propagation':
# clusters = affinity_propagation(dedup_haplotypes.values)
# if clusters[0] == -1:
# clusters = np.arange(d_r)
# print("Affinity propagation failed")
# elif clutering_algorithm == 'gmeans':
# clusters = gmeans_clustering(list(haplotypes.values))
# elif clutering_algorithm == 'DBSCAN':
# clusters = DBSCAN_clustering(dedup_haplotypes.values)
# if len(np.unique(clusters)) == 1:
# clusters = affinity_propagation(dedup_haplotypes.values)
# if clusters[0] == -1:
# clusters = np.arange(d_r)
# print("Affinity propagation failed")
# elif clutering_algorithm == 'KNN':
# #print(dedup_haplotypes.values)
# #clusters = SC.Spectral_Clustering(dedup_haplotypes.values)
# clusters = KNN_Spec(dedup_haplotypes.values)
# # generate the dictionary. key: haplotype (str), values (cluster index)
# for j in range(d_r):
# haplotype_ = "".join(map(str,dedup_haplotypes.values[j,:]))
# dictionary[haplotype_] = clusters[j]
# # the column names of the dataframe
# name = [ch+"@"+str(positions[breakpoints[block_index][0]])+"-"+str(positions[breakpoints[block_index][1]])+'_'+str(i) for i in range(max(clusters)+1)]
# DM_matrix_1 = np.zeros((int(r/2),max(clusters)+1),dtype=int)
# DM_matrix_2 = np.zeros((int(r/2),max(clusters)+1),dtype=int)
# # generate the haplotype design matrix
# for k in range(int(r/2)):
# tmp_1 = "".join(map(str,haplotypes.values[k,:]))
# tmp_2 = "".join(map(str,haplotypes.values[k+int(r/2),:]))
# l_1 = dictionary[tmp_1]
# l_2 = dictionary[tmp_2]
# DM_matrix_1[k,l_1] += 1
# DM_matrix_2[k,l_2] += 1
# haplotype_DM = DM_matrix_1 + DM_matrix_2
# haplotype_DM_pd = pd.DataFrame(haplotype_DM,columns=name)
# return(haplotype_DM_pd,name)
def haplotype_DM_generator_1_singleton(block_index,clutering_algorithm,haplotypes,n_clusters,breakpoints,positions,ch):
dictionary = {}
r,c = haplotypes.shape
dedup_haplotypes = haplotypes.drop_duplicates(keep = 'first')
d_r,d_c = dedup_haplotypes.shape
#print(block_index,dedup_haplotypes.values,dedup_haplotypes.shape,haplotypes.shape)
if d_r <= n_clusters: # The situation no.1 no clutering_algorithm is necessary
for i in range(d_r):
haplotype_ = "".join(map(str,dedup_haplotypes.values[i,:]))
dictionary[haplotype_] = i
if breakpoints[block_index][0] == 0:
block_name = [ch+"@"+str(0)+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(l) for l in range(d_r)]
else:
block_name = [ch+"@"+str(positions[breakpoints[block_index][0]-1])+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(l) for l in range(d_r)]
marker_name = [ch+"@"+str(positions[breakpoints[block_index][0]])+"-"+str(positions[breakpoints[block_index][1]])+'_'+str(l) for l in range(d_r)]
DM_matrix_1 = np.zeros((int(r/2),d_r),dtype=int)
DM_matrix_2 = np.zeros((int(r/2),d_r),dtype=int)
else: # The situation no.2 Too many haplotyps, clutering_algorithm is needed.
# choose the clustering function
if clutering_algorithm == 'xmeans':
clusters = xmeans_clustering(list(dedup_haplotypes.values))
elif clutering_algorithm == 'affinity_propagation':
clusters = affinity_propagation(dedup_haplotypes.values)
if clusters[0] == -1 or max(clusters) == 0:
clusters = np.arange(d_r)
print("Affinity propagation failed")
elif clutering_algorithm == 'gmeans':
clusters = gmeans_clustering(list(haplotypes.values))
elif clutering_algorithm == 'DBSCAN':
clusters = DBSCAN_clustering(dedup_haplotypes.values)
if len(np.unique(clusters)) == 1:
clusters = affinity_propagation(dedup_haplotypes.values)
if clusters[0] == -1:
clusters = np.arange(d_r)
print("Affinity propagation failed")
elif clutering_algorithm == 'KNN':
clusters = KNN_Spec(haplotypes.values)
print("KNN",max(clusters))
clusters = local_scale_Spectral(dedup_haplotypes.values)
print("local",max(clusters))
# generate the dictionary. key: haplotype (str), values (cluster index)
for j in range(d_r):
haplotype_ = "".join(map(str,dedup_haplotypes.values[j,:]))
dictionary[haplotype_] = clusters[j]
#the column names of the dataframe
if breakpoints[block_index][0] == 0:
block_name = [ch+"@"+str(0)+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(l) for l in range(max(clusters)+1)]
else:
block_name = [ch+"@"+str(positions[breakpoints[block_index][0]-1])+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(i) for i in range(max(clusters)+1)]
marker_name = [ch+"@"+str(positions[breakpoints[block_index][0]])+"-"+str(positions[breakpoints[block_index][1]])+'_'+str(l) for l in range(max(clusters)+1)]
DM_matrix_1 = np.zeros((int(r/2),max(clusters)+1),dtype=int)
DM_matrix_2 = np.zeros((int(r/2),max(clusters)+1),dtype=int)
# generate the haplotype design matrix
for k in range(int(r/2)):
tmp_1 = "".join(map(str,haplotypes.values[k,:]))
tmp_2 = "".join(map(str,haplotypes.values[k+int(r/2),:]))
l_1 = dictionary[tmp_1]
l_2 = dictionary[tmp_2]
DM_matrix_1[k,l_1] += 1
DM_matrix_2[k,l_2] += 1
haplotype_DM = DM_matrix_1 + DM_matrix_2
hap_freq = np.sum(haplotype_DM,0) / (haplotype_DM.shape[0]*2)
#print(hap_freq)
hap_freq_order = np.argsort(hap_freq)
#print(hap_freq_order)
haplotype_DM_minus1 = haplotype_DM[:,hap_freq_order[1:]]
block_name = block_name[1:]
marker_name = marker_name[1:]
# haplotype_DM_minus1 = haplotype_DM ### for Pan-genome reference selection
# block_name = block_name
# marker_name = marker_name
haplotype_DM_minus1 = pd.DataFrame(haplotype_DM_minus1,columns=marker_name)
return(haplotype_DM_minus1,marker_name,block_name)
def HaploDM_1_singleton(ch,r,hap_matrix_d1,hap_matrix_d2,geno_matrix,variant_names,variant_positions,fine_breakpoints,HaploBlock_matrix,haplotype_cluster_marker_name,haplotype_cluster_block_name,clutering_algorithm):
hap_matrix_d1_pd = pd.DataFrame(np.transpose(hap_matrix_d1[ch]),columns=variant_names[ch])
hap_matrix_d2_pd = pd.DataFrame(np.transpose(hap_matrix_d2[ch]),columns=variant_names[ch])
HaploBlock_matrix_container = {}
haplotype_cluster_marker_name_container =[]
haplotype_cluster_block_name_container =[]
for key in fine_breakpoints[ch]:
l = len(fine_breakpoints[ch][key])
columns = []
MARKER_NAMES = []
haplotype_Dmatrix = pd.DataFrame(index=range(r),columns=columns)
for i in range(l):
if len(fine_breakpoints[ch][key][i]) == 1:
fine_index = fine_breakpoints[ch][key][i][0]
haplotype_DM_ = pd.DataFrame(geno_matrix[ch][:,fine_index])
if fine_index == 0:
block_name_ = [ch+"@"+str(0)+"-"+str(variant_positions[ch][fine_index+1])+"_0"]
else:
block_name_ = [ch+"@"+str(variant_positions[ch][fine_index-1])+"-"+str(variant_positions[ch][fine_index+1])+"_0"]
marker_name_ = [ch+"@"+str(variant_positions[ch][fine_index])+"_0"]
else:
fine_index1 = fine_breakpoints[ch][key][i][0]
fine_index2 = fine_breakpoints[ch][key][i][1]
hap1 = hap_matrix_d1_pd[variant_names[ch][fine_index1:fine_index2+1]]
hap2 = hap_matrix_d2_pd[variant_names[ch][fine_index1:fine_index2+1]]
haplotypes = pd.concat([hap1,hap2],ignore_index=True)
haplotype_DM_,marker_name_,block_name_ = haplotype_DM_generator_1_singleton(block_index=i,
clutering_algorithm =clutering_algorithm,
haplotypes = haplotypes,
n_clusters = 7,
breakpoints = fine_breakpoints[ch][key],
positions = variant_positions[ch],
ch = ch)
haplotype_Dmatrix = pd.concat([haplotype_Dmatrix,haplotype_DM_],axis=1,ignore_index=True)
MARKER_NAMES.extend(marker_name_)
haplotype_cluster_marker_name_container.extend(marker_name_)
haplotype_cluster_block_name_container.extend(block_name_)
haplotype_Dmatrix.columns = MARKER_NAMES
HaploBlock_matrix_container[key] = haplotype_Dmatrix
HaploBlock_matrix[ch] = HaploBlock_matrix_container
haplotype_cluster_marker_name[ch] = haplotype_cluster_marker_name_container
haplotype_cluster_block_name[ch] = haplotype_cluster_block_name_container
def haplotype_DM_generator(block_index,clutering_algorithm,haplotypes,n_clusters,breakpoints,positions,ch):
dictionary = {}
r,c = haplotypes.shape
dedup_haplotypes = haplotypes.drop_duplicates(keep = 'first')
d_r,d_c = dedup_haplotypes.shape
#print(block_index,dedup_haplotypes.values,dedup_haplotypes.shape,haplotypes.shape)
if d_r < n_clusters: # The situation no.1 no clutering_algorithm is necessary
for i in range(d_r):
haplotype_ = "".join(map(str,dedup_haplotypes.values[i,:]))
dictionary[haplotype_] = i
if breakpoints[block_index][0] == 0:
haplotype_names = [ch+"@"+str(0)+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(l) for l in range(d_r)]
block_name = ch+"@"+str(0)+"-"+str(positions[breakpoints[block_index][1]+1])
marker_name = ch+"@"+str(positions[breakpoints[block_index][0]]) + "-" + str(positions[breakpoints[block_index][1]])
else:
haplotype_names = [ch+"@"+str(positions[breakpoints[block_index][0]-1])+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(l) for l in range(d_r)]
block_name = ch+"@"+str(positions[breakpoints[block_index][0]-1])+"-"+str(positions[breakpoints[block_index][1]+1])
marker_name = ch+"@"+str(positions[breakpoints[block_index][0]])+"-"+str(positions[breakpoints[block_index][1]])
DM_matrix_1 = np.zeros((int(r/2),d_r),dtype=int)
DM_matrix_2 = np.zeros((int(r/2),d_r),dtype=int)
else: # The situation no.2 Too many haplotyps, clutering_algorithm is needed.
# choose the clustering function
if clutering_algorithm == 'xmeans':
clusters = xmeans_clustering(list(dedup_haplotypes.values))
elif clutering_algorithm == 'affinity_propagation':
clusters = affinity_propagation(dedup_haplotypes.values)
if clusters[0] == -1 or max(clusters) == 0:
clusters = np.arange(d_r)
print("Affinity propagation failed",clusters)
elif clutering_algorithm == 'gmeans':
clusters = gmeans_clustering(list(haplotypes.values))
elif clutering_algorithm == 'DBSCAN':
clusters = DBSCAN_clustering(dedup_haplotypes.values)
if len(np.unique(clusters)) == 1:
print("failed")
clusters = np.arange(d_r)
elif clutering_algorithm == 'KNN':
clusters = KNN_Spectral(dedup_haplotypes.values)
elif clutering_algorithm == 'local':
clusters = local_scale_Spectral(dedup_haplotypes.values)
# generate the dictionary. key: haplotype (str), values (cluster index)
for j in range(d_r):
haplotype_ = "".join(map(str,dedup_haplotypes.values[j,:]))
dictionary[haplotype_] = clusters[j]
#the column names of the dataframe
if breakpoints[block_index][0] == 0:
haplotype_names = [ch+"@"+str(0)+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(l) for l in range(max(clusters)+1)]
block_name = ch+"@"+str(0)+"-"+str(positions[breakpoints[block_index][1]+1])
marker_name = ch+"@"+str(positions[breakpoints[block_index][0]])+"-"+ str(positions[breakpoints[block_index][1]])
else:
haplotype_names = [ch+"@"+str(positions[breakpoints[block_index][0]-1])+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(i) for i in range(max(clusters)+1)]
block_name = ch+"@"+str(positions[breakpoints[block_index][0]-1])+"-"+str(positions[breakpoints[block_index][1]+1])
marker_name = ch+"@"+str(positions[breakpoints[block_index][0]])+"-"+str(positions[breakpoints[block_index][1]])
DM_matrix_1 = np.zeros((int(r/2),max(clusters)+1),dtype=int)
DM_matrix_2 = np.zeros((int(r/2),max(clusters)+1),dtype=int)
# generate the haplotype design matrix
for k in range(int(r/2)):
tmp_1 = "".join(map(str,haplotypes.values[k,:]))
tmp_2 = "".join(map(str,haplotypes.values[k+int(r/2),:]))
l_1 = dictionary[tmp_1]
l_2 = dictionary[tmp_2]
DM_matrix_1[k,l_1] += 1
DM_matrix_2[k,l_2] += 1
haplotype_DM = DM_matrix_1 + DM_matrix_2
if haplotype_DM.shape[1] == 2:
haplotype_DM_minus1 = haplotype_DM[:,1]
haplotype_names = [haplotype_names[1]]
else:
hap_freq = np.sum(haplotype_DM,0) / (haplotype_DM.shape[0]*2)
hap_freq_order = np.argsort(hap_freq)
haplotype_DM_minus1 = haplotype_DM[:,hap_freq_order[1:]]
haplotype_names = haplotype_names[1:]
haplotype_DM_minus1 = pd.DataFrame(haplotype_DM_minus1,columns=haplotype_names)
#haplotype_DM = pd.DataFrame(haplotype_DM,columns=haplotype_names)
return(haplotype_DM_minus1,block_name,haplotype_names,marker_name)
def haplotype_DM_generator_nocluster(block_index,haplotypes,breakpoints,positions,ch):
dictionary = {}
r,c = haplotypes.shape
dedup_haplotypes = haplotypes.drop_duplicates(keep = 'first')
d_r,d_c = dedup_haplotypes.shape
#print(block_index,dedup_haplotypes.values,dedup_haplotypes.shape,haplotypes.shape)
for i in range(d_r):
haplotype_ = "".join(map(str,dedup_haplotypes.values[i,:]))
dictionary[haplotype_] = i
if breakpoints[block_index][0] == 0:
haplotype_names = [ch+"@"+str(0)+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(l) for l in range(d_r)]
block_name = ch+"@"+str(0)+"-"+str(positions[breakpoints[block_index][1]+1])
else:
haplotype_names = [ch+"@"+str(positions[breakpoints[block_index][0]-1])+"-"+str(positions[breakpoints[block_index][1]+1])+'_'+str(l) for l in range(d_r)]
block_name = ch+"@"+str(positions[breakpoints[block_index][0]-1])+"-"+str(positions[breakpoints[block_index][1]+1])
DM_matrix_1 = np.zeros((int(r/2),d_r),dtype=int)
DM_matrix_2 = np.zeros((int(r/2),d_r),dtype=int)
# generate the haplotype design matrix
for k in range(int(r/2)):
tmp_1 = "".join(map(str,haplotypes.values[k,:]))
tmp_2 = "".join(map(str,haplotypes.values[k+int(r/2),:]))
l_1 = dictionary[tmp_1]
l_2 = dictionary[tmp_2]
DM_matrix_1[k,l_1] += 1
DM_matrix_2[k,l_2] += 1
haplotype_DM = DM_matrix_1 + DM_matrix_2
haplotype_DM = pd.DataFrame(haplotype_DM,columns=haplotype_names)
return(haplotype_DM,block_name)
def haplotype_DM_cluster(haplotype_DM,y):
cluster_count = np.sum(haplotype_DM,axis = 0)
haplotype_IM = np.asarray(haplotype_DM > 0, dtype = int)
cluster_sum = np.dot(y,np.array(haplotype_IM))
cluster_avg = np.divide(cluster_sum, cluster_count)
breaks = jenkspy.jenks_breaks(cluster_avg, nb_class = 2)
index1 = np.where(cluster_avg > breaks[1])[0]
haplotype_DM_ = np.sum(haplotype_DM.iloc[:,index1],axis = 1)
return(haplotype_DM_)
def BlockDM_generation(ch,r,hap_matrix_d1,hap_matrix_d2,geno_matrix,variant_names,variant_positions,fine_breakpoints,HaploBlock_matrix,haplotype_block_name,haplotype_marker_name,clutering_algorithm):
hap_matrix_d1_pd = pd.DataFrame(np.transpose(hap_matrix_d1[ch]),columns=variant_names[ch])
hap_matrix_d2_pd = pd.DataFrame(np.transpose(hap_matrix_d2[ch]),columns=variant_names[ch])
HaploBlock_matrix_container = {}
haplotype_block_name_container =[]
haplotype_marker_name_container = []
for key in fine_breakpoints[ch]:
l = len(fine_breakpoints[ch][key])
columns = []
BLOCK_NAMES = []
block_Dmatrix = pd.DataFrame(index=range(r),columns=columns)
for i in range(l):
if len(fine_breakpoints[ch][key][i]) == 1:
fine_index = fine_breakpoints[ch][key][i][0]
haplotype_DM_ = pd.DataFrame(geno_matrix[ch][:,fine_index])
if fine_index == 0:
haplotype_names = [ch+"@"+str(0)+"-"+str(variant_positions[ch][fine_index+1])+'_0']
block_name = ch+"@"+str(0)+"-"+str(variant_positions[ch][fine_index+1])
marker_name = ch+"@"+str(variant_positions[ch][fine_index])
else:
haplotype_names = [ch+"@"+str(variant_positions[ch][fine_index-1])+"-"+str(variant_positions[ch][fine_index+1]) + '_0']
block_name = ch+"@"+str(variant_positions[ch][fine_index-1])+"-"+str(variant_positions[ch][fine_index+1])
marker_name = ch+"@"+str(variant_positions[ch][fine_index])
haplotype_DM_.columns = [haplotype_names]
else:
fine_index1 = fine_breakpoints[ch][key][i][0]
fine_index2 = fine_breakpoints[ch][key][i][1]
hap1 = hap_matrix_d1_pd[variant_names[ch][fine_index1:fine_index2+1]]
hap2 = hap_matrix_d2_pd[variant_names[ch][fine_index1:fine_index2+1]]
haplotypes = pd.concat([hap1,hap2],ignore_index=True)
haplotype_DM_,block_name,haplotype_names,marker_name = haplotype_DM_generator(block_index=i,
clutering_algorithm =clutering_algorithm,
haplotypes = haplotypes,
n_clusters = 7,
breakpoints = fine_breakpoints[ch][key],
positions = variant_positions[ch],
ch = ch)
block_Dmatrix = pd.concat([block_Dmatrix,haplotype_DM_],axis=1,ignore_index=True)
# BLOCK_NAMES.append(block_name)
# haplotype_block_name_container.append(block_name)
BLOCK_NAMES.extend(haplotype_names)
haplotype_block_name_container.append(block_name)
haplotype_marker_name_container.append(marker_name)
block_Dmatrix.columns = BLOCK_NAMES
HaploBlock_matrix_container[key] = block_Dmatrix
HaploBlock_matrix[ch] = HaploBlock_matrix_container
haplotype_block_name[ch] = haplotype_block_name_container
haplotype_marker_name[ch] = haplotype_marker_name_container
def BlockDM_generation_cluster(ch,r,hap_matrix_d1,hap_matrix_d2,geno_matrix,variant_names,variant_positions,fine_breakpoints,HaploBlock_matrix,haplotype_block_name,haplotype_marker_name,clutering_algorithm,y):
hap_matrix_d1_pd = pd.DataFrame(np.transpose(hap_matrix_d1[ch]),columns=variant_names[ch])
hap_matrix_d2_pd = pd.DataFrame(np.transpose(hap_matrix_d2[ch]),columns=variant_names[ch])
HaploBlock_matrix_container = {}
haplotype_block_name_container =[]
haplotype_marker_name_container = []
for key in fine_breakpoints[ch]:
l = len(fine_breakpoints[ch][key])
columns = []
BLOCK_NAMES = []
block_Dmatrix = pd.DataFrame(index=range(r),columns=columns)
for i in range(l):
if len(fine_breakpoints[ch][key][i]) == 1:
fine_index = fine_breakpoints[ch][key][i][0]
haplotype_DM_ = pd.DataFrame(geno_matrix[ch][:,fine_index])
# if fine_index == 0:
# block_name = ch+"@"+str(0)+"-"+str(variant_positions[ch][fine_index+1])
# else:
# block_name = ch+"@"+str(variant_positions[ch][fine_index-1])+"-"+str(variant_positions[ch][fine_index+1])
# haplotype_DM_.columns = [block_name]
if fine_index == 0:
#haplotype_names = [ch+"@"+str(0)+"-"+str(variant_positions[ch][fine_index+1])+'_1']
block_name = ch+"@"+str(0)+"-"+str(variant_positions[ch][fine_index+1])
marker_name = ch+"@"+str(variant_positions[ch][fine_index])
else:
#haplotype_names = [ch+"@"+str(variant_positions[ch][fine_index-1])+"-"+str(variant_positions[ch][fine_index+1]) + '_1']
block_name = ch+"@"+str(variant_positions[ch][fine_index-1])+"-"+str(variant_positions[ch][fine_index+1])
marker_name = ch+"@"+str(variant_positions[ch][fine_index])
haplotype_DM_.columns = [block_name]
else:
fine_index1 = fine_breakpoints[ch][key][i][0]
fine_index2 = fine_breakpoints[ch][key][i][1]
hap1 = hap_matrix_d1_pd[variant_names[ch][fine_index1:fine_index2+1]]
hap2 = hap_matrix_d2_pd[variant_names[ch][fine_index1:fine_index2+1]]
haplotypes = pd.concat([hap1,hap2],ignore_index=True)
haplotype_DM,block_name,haplotype_names,marker_name = haplotype_DM_generator(block_index=i,
clutering_algorithm =clutering_algorithm,
haplotypes = haplotypes,
n_clusters = 7,
breakpoints = fine_breakpoints[ch][key],
positions = variant_positions[ch],
ch = ch)
#print(haplotype_DM.shape)
if haplotype_DM.shape[1] > 2:
haplotype_DM_ = haplotype_DM_cluster(haplotype_DM,y)
else:
haplotype_DM_ = haplotype_DM.iloc[:,1]
#print("shape",haplotype_DM_.shape)
block_Dmatrix = pd.concat([block_Dmatrix,haplotype_DM_],axis=1,ignore_index=True)
# BLOCK_NAMES.append(block_name)
# haplotype_block_name_container.append(block_name)
BLOCK_NAMES.append(block_name)
haplotype_block_name_container.append(block_name)
haplotype_marker_name_container.append(marker_name)
block_Dmatrix.columns = BLOCK_NAMES
HaploBlock_matrix_container[key] = block_Dmatrix
HaploBlock_matrix[ch] = HaploBlock_matrix_container
haplotype_block_name[ch] = haplotype_block_name_container
haplotype_marker_name[ch] = haplotype_marker_name_container
# block_Dmatrix.columns = BLOCK_NAMES
# HaploBlock_matrix_container[key] = block_Dmatrix
# HaploBlock_matrix[ch] = HaploBlock_matrix_container
# haplotype_block_name[ch] = haplotype_block_name_container
def BlockDM_generation_residual(ch,r,hap_matrix_d1,hap_matrix_d2,geno_matrix,variant_names,variant_positions,fine_breakpoints,HaploBlock_matrix,haplotype_block_name,haplotype_marker_name,clutering_algorithm,y):
hap_matrix_d1_pd = pd.DataFrame(np.transpose(hap_matrix_d1[ch]),columns=variant_names[ch])
hap_matrix_d2_pd = pd.DataFrame(np.transpose(hap_matrix_d2[ch]),columns=variant_names[ch])
HaploBlock_matrix_container = {}
haplotype_block_name_container =[]
haplotype_marker_name_container = []
for key in fine_breakpoints[ch]:
l = len(fine_breakpoints[ch][key])
columns = []
BLOCK_NAMES = []
block_Dmatrix = pd.DataFrame(index=range(r),columns=columns)
for i in range(l):
if len(fine_breakpoints[ch][key][i]) == 1:
fine_index = fine_breakpoints[ch][key][i][0]
haplotype_DM_ = pd.DataFrame(geno_matrix[ch][:,fine_index])
# if fine_index == 0:
# block_name = ch+"@"+str(0)+"-"+str(variant_positions[ch][fine_index+1])
# else:
# block_name = ch+"@"+str(variant_positions[ch][fine_index-1])+"-"+str(variant_positions[ch][fine_index+1])
# haplotype_DM_.columns = [block_name]
if fine_index == 0:
haplotype_names = [ch+"@"+str(0)+"-"+str(variant_positions[ch][fine_index+1])+'_1']
block_name = ch+"@"+str(0)+"-"+str(variant_positions[ch][fine_index+1])
marker_name = ch+"@"+str(variant_positions[ch][fine_index])
else:
haplotype_names = [ch+"@"+str(variant_positions[ch][fine_index-1])+"-"+str(variant_positions[ch][fine_index+1]) + '_1']
block_name = ch+"@"+str(variant_positions[ch][fine_index-1])+"-"+str(variant_positions[ch][fine_index+1])
marker_name = ch+"@"+str(variant_positions[ch][fine_index])
haplotype_DM_.columns = [block_name]
else:
fine_index1 = fine_breakpoints[ch][key][i][0]
fine_index2 = fine_breakpoints[ch][key][i][1]
hap1 = hap_matrix_d1_pd[variant_names[ch][fine_index1:fine_index2+1]]
hap2 = hap_matrix_d2_pd[variant_names[ch][fine_index1:fine_index2+1]]
haplotypes = pd.concat([hap1,hap2],ignore_index=True)
haplotype_DM,block_name,haplotype_names,marker_name = haplotype_DM_generator(block_index=i,
clutering_algorithm =clutering_algorithm,
haplotypes = haplotypes,
n_clusters = 6,
breakpoints = fine_breakpoints[ch][key],
positions = variant_positions[ch],
ch = ch)
#print(haplotype_DM.shape)
if haplotype_DM.shape[1] > 2:
haplotype_DM_ = haplotype_DM_cluster(haplotype_DM,y)
else:
haplotype_DM_ = haplotype_DM.iloc[:,1]
#print("shape",haplotype_DM_.shape)
block_Dmatrix = pd.concat([block_Dmatrix,haplotype_DM_],axis=1,ignore_index=True)
# BLOCK_NAMES.append(block_name)
# haplotype_block_name_container.append(block_name)
BLOCK_NAMES.append(block_name)
haplotype_block_name_container.append(block_name)
haplotype_marker_name_container.append(marker_name)
block_Dmatrix.columns = BLOCK_NAMES
HaploBlock_matrix_container[key] = block_Dmatrix
HaploBlock_matrix[ch] = HaploBlock_matrix_container
haplotype_block_name[ch] = haplotype_block_name_container
haplotype_marker_name[ch] = haplotype_marker_name_container
# block_Dmatrix.columns = BLOCK_NAMES
# HaploBlock_matrix_container[key] = block_Dmatrix
# HaploBlock_matrix[ch] = HaploBlock_matrix_container
# haplotype_block_name[ch] = haplotype_block_name_container
def format_bimbam(haplotype_Dmatrix,haplotype_names):
r,c = haplotype_Dmatrix.shape
l = len(haplotype_names)
if r != l:
print("haplotype matrix is not built right!!!!")
else:
columns = ['haplotype_names','hap','no-hap']
bimbam_ = pd.DataFrame(index=range(l),columns=columns)
bimbam_['haplotype_names'] = haplotype_names
bimbam_['hap'] = '-'
bimbam_['no-hap'] = '-'
bimbam = pd.concat([bimbam_,pd.DataFrame(haplotype_Dmatrix)],axis=1,ignore_index=True)
return(bimbam)
def cat(dictionary,keys):
dict_cat = []
for i in keys:
for j in dictionary[i]:
dict_cat.append(j)
return(dict_cat)
def pip_calculation_1(haplotype_burnt_gamma,block_haplotypes,block_positions):
nrow = haplotype_burnt_gamma.shape[0]
ncol = len(block_haplotypes)
block_gamma = np.zeros(shape = (nrow,ncol))
for i in range(len(block_positions)):
col_index = block_haplotypes[block_positions[i]]
x = np.sum(haplotype_burnt_gamma[:,col_index],axis = 1)
row_index = np.where(x >= 1)
block_gamma[row_index[0],i] = 1