-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmBUD.py
More file actions
1285 lines (1028 loc) · 43.8 KB
/
Copy pathmBUD.py
File metadata and controls
1285 lines (1028 loc) · 43.8 KB
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
import os
import pandas as pd
import numpy as np
from numpy import sin,cos
import math
import argparse
import cif2cell
import networkx as nx
import datetime
class LIBRARY:
def __init__ (self):
# Library dir
self.dir = os.getcwd() + "/lib/"
# General infor
self.dim = 3
self.a2r = np.pi / 180.0
self.cifextension = '.cif'
self.xyzextension = '.xyz'
# Metal info
self.METALcsv = self.dir + "metal.csv"
self.dfmetal = pd.read_csv(self.METALcsv)
self.metalsymbol = self.dfmetal.iloc[:,1]
# Bond info
self.ATRcsv = self.dir + "atr.csv"
self.dfatr = pd.read_csv(self.ATRcsv)
class ATOM(LIBRARY):
dim = 3
lib = LIBRARY()
def __init__ (self, data, index):
#self.label = data[0]
self.label = data[1] + str(index + 1)
self.symbol = data[1]
self.index = index
self.neighborlist = []
self.nneighbor = 0
self.gridindex = 0
self.ismetal = False
self.x = []
self.writex = []
for idim in range(self.dim):
self.x.append(data[idim+3])
self.writex.append(data[idim+3])
self.atr = self.get_atr()
def destroy(self, array):
del array
array = []
return array
def destroy_all(self):
del self.label
del self.symbol
del self.neighborlist
del self.x
del self.writex
def get_atr(self):
try:
atr = self.lib.dfatr.iloc[self.lib.dfatr[(self.lib.dfatr['Symbol'] == self.symbol)].index[0]]['atr']
except:
raise ValueError('ATR not found')
return atr
def print_info(self):
print(self.index, end = ' ')
print(self.label, end = ' ')
print(self.symbol, end = ' ')
print(self.x, end = ' ')
print(self.atr)
def setnneighborzero(self):
self.nneighbor = 0
def check_neighbor(self, iindex):
check = False
for ineighbor in self.neighborlist:
if ineighbor == iindex:
check = True
return check
def add_neighbor(self, iindex):
check = False
if len(self.neighborlist) > 0:
check = self.check_neighbor(iindex)
if check == False:
self.neighborlist.append(iindex)
def remove_neighbor(self, iindex):
currentneighborlist = []
currentneighborlist = self.neighborlist
self.neighborlist = self.destroy(self.neighborlist)
for ineighbor in currentneighborlist:
if ineighbor != iindex:
self.add_neighbor(ineighbor)
del currentneighborlist
class MOF(ATOM,LIBRARY):
lib = LIBRARY()
dim = 3
def __init__ (self, name):
self.ciffile = name
self.G = nx.Graph(name = 'mbud')
self.fragG = nx.Graph(name = 'mbud')
self.compG = nx.Graph(name = 'mbud')
self.detH = 1.0
self.lx = []
self.ar = []
self.atom = []
self.h = []
self.hinv = []
self.atomtypelist = []
self.metaltypelist = []
#SBU lists (frag)
self.linkerlist = []
self.funcgrouplist = []
self.solventlist = []
self.metalnodelist = []
self.capairlist = []
#SBU lists (comp)
self.complinkerlist = []
self.compmetalnodelist = []
self.compcapairlist = []
#Grid Informations
self.skin = 0.18
self.gridlxmax = self.skin
#self.minbondlength2 = 0.63*0.63
self.totgrid = 1
self.gridlx = []
self.ngrid = []
self.neighgrid = []
self.gridatomlist = []
def destroy(self, array):
del array
array = []
return array
def destroy_all(self):
del self.ciffile
self.G.clear()
self.fragG.clear()
self.compG.clear()
del self.lx
del self.ar
for iatom in self.atom:
iatom.destroy_all()
del self.atom
del self.h
del self.hinv
del self.atomtypelist
del self.metaltypelist
#SBU lists (frag)
del self.linkerlist
del self.funcgrouplist
del self.solventlist
del self.metalnodelist
del self.capairlist
#SBU lists (comp)
del self.complinkerlist
del self.compmetalnodelist
del self.compcapairlist
del self.gridlx
del self.ngrid
del self.neighgrid
del self.gridatomlist
def get_boxinfo(self):
self.loop = False
self.atom = self.destroy(self.atom)
self.lx = self.destroy(self.lx)
self.ar = self.destroy(self.ar)
index = 0
f = open (self.ciffile,'r')
lines = f.readlines()
f.close()
for line in lines:
list = line.split()
if len(list) > 0:
if list[0] == '_cell_length_a':
self.lx.append(float(list[1]))
if list[0] == '_cell_length_b':
self.lx.append(float(list[1]))
if list[0] == '_cell_length_c':
self.lx.append(float(list[1]))
if list[0] == '_cell_angle_alpha':
self.ar.append(self.lib.a2r*float(list[1]))
if list[0] == '_cell_angle_beta':
self.ar.append(self.lib.a2r*float(list[1]))
if list[0] == '_cell_angle_gamma':
self.ar.append(self.lib.a2r*float(list[1]))
if list[0] == 'loop_':
self.loop = True
if self.loop == True and len(list) > 5:
data = []
#label, symbol, symmetry, ux, uy, uz, occupancy
data.append(list[0])
data.append(list[1])
data.append(int(list[2]))
data.append(float(list[3]))
data.append(float(list[4]))
data.append(float(list[5]))
data.append(float(list[6]))
self.atom.append(ATOM(data,index))
del data
index += 1
del lines
def get_hmatrix(self):
self.h = self.destroy(self.h)
for idim in range(self.dim):
data = []
for jdim in range(self.dim):
data.append(0.0)
self.h.append(data)
del data
#get h-matrix
self.h[0][0] = self.lx[0]
self.h[0][1] = self.lx[1]*cos(self.ar[2])
self.h[0][2] = self.lx[2]*cos(self.ar[1])
self.h[1][0] = 0.0
self.h[1][1] = self.lx[1]*sin(self.ar[2])
local = cos(self.ar[0]) - cos(self.ar[1])*cos(self.ar[2])
local /= sin(self.ar[2])
self.h[1][2] = self.lx[2]*local
self.h[2][0] = 0.0
self.h[2][1] = 0.0
self.h[2][2] = self.lx[2]*np.sqrt(1 - cos(self.ar[1])*cos(self.ar[1]) - local*local)
for idim in range(self.dim):
self.detH *= self.h[idim][idim]
self.hinv = self.destroy(self.hinv)
ht = []
for idim in range(self.dim):
data = []
for jdim in range(self.dim):
data.append(0.0)
self.hinv.append(data)
ht.append(data)
del data
#get hinv-matrix
ht[0][0] = self.h[1][1]*self.h[2][2] - self.h[1][2]*self.h[2][1]
ht[0][1] = 0.0
ht[0][2] = 0.0
ht[1][0] = self.h[0][2]*self.h[2][1] - self.h[0][1]*self.h[2][2]
ht[1][1] = self.h[0][0]*self.h[2][2] - self.h[0][2]*self.h[2][0]
ht[1][2] = self.h[0][1]*self.h[2][0] - self.h[0][0]*self.h[2][1]
ht[2][0] = self.h[0][1]*self.h[1][2] - self.h[0][2]*self.h[1][1]
ht[2][1] = self.h[0][2]*self.h[1][0] - self.h[0][0]*self.h[1][2]
ht[2][2] = self.h[0][0]*self.h[1][1] - self.h[0][1]*self.h[1][0]
for idim in range(self.dim):
for jdim in range(self.dim):
self.hinv[idim][jdim] = ht[jdim][idim]
for idim in range(self.dim):
for jdim in range(self.dim):
self.hinv[idim][jdim] /= self.detH
del ht
def get_gridinfo(self):
self.gridlxmax = self.get_maxcovbl() + self.skin
#print('Minimum grid length (maximum covalent radius + skin/2):', self.gridlxmax)
if self.gridlxmax < self.skin:
raise NameError('Grid length is not right')
self.gridlx = self.destroy(self.gridlx)
self.ngrid = self.destroy(self.ngrid)
for idim in range(self.dim):
self.gridlx.append(0.0)
self.ngrid.append(1)
for idim in range(self.dim):
for jdim in range(self.dim):
if jdim == idim:
self.gridlx[jdim] = 1.0 / self.ngrid[idim]
else:
self.gridlx[jdim] = 0.0
self.gridlx = self.fractional_to_cartesian(self.gridlx)
while self.gridlx[idim] > self.gridlxmax:
self.ngrid[idim] += 1
for jdim in range(self.dim):
if jdim == idim:
self.gridlx[jdim] = 1.0 / self.ngrid[idim]
else:
self.gridlx[jdim] = 0.0
self.gridlx = self.fractional_to_cartesian(self.gridlx)
self.ngrid[idim] -= 1
for idim in range(self.dim):
if self.ngrid[idim] == 0:
self.ngrid[idim] = 1
for idim in range(self.dim):
self.gridlx[idim] = 1.0 / self.ngrid[idim]
#print('Grid dimension (updated fractional):', self.gridlx)
#print('Grid dimension (updated cartesian):', self.fractional_to_cartesian(self.gridlx))
#print('Number of grids in axis:', self.ngrid)
self.totgrid = 1
for idim in range(self.dim):
self.totgrid *= self.ngrid[idim]
#print('Total number of grids:', self.totgrid)
self.neighgrid = self.destroy(self.neighgrid)
for igrid in range(self.totgrid):
igz = math.floor(igrid/(self.ngrid[0]*self.ngrid[1]))
igy = math.floor((igrid - igz*self.ngrid[0]*self.ngrid[1])/self.ngrid[0])
igx = igrid - igz*self.ngrid[0]*self.ngrid[1] - igy*self.ngrid[0]
#print('\nCurrent grid:', igrid)
data = []
for iz in range(-1,2):
iggz = igz + iz
if iggz < 0:
iggz += self.ngrid[2]
elif iggz >= self.ngrid[2]:
iggz -= self.ngrid[2]
for iy in range(-1,2):
iggy = igy + iy
if iggy < 0:
iggy += self.ngrid[1]
elif iggy >= self.ngrid[1]:
iggy -= self.ngrid[1]
for ix in range(-1,2):
iggx = igx + ix
if iggx < 0:
iggx += self.ngrid[0]
elif iggx >= self.ngrid[0]:
iggx -= self.ngrid[0]
currgrid = iggx + iggy * self.ngrid[0] + iggz * self.ngrid[0] * self.ngrid[1]
if currgrid not in data:
data.append(currgrid)
#print(currgrid, end = ' ')
self.neighgrid.append(data)
del data
#print('Neighgrids', self.neighgrid)
def get_gridindex(self,x):
igrid = []
for i in range(self.dim):
if x[i] < 0.0:
x[i] += 1.0
if x[i] > 1.0:
x[i] -= 1.0
igrid.append(math.ceil(x[i] / self.gridlx[i]))
for idim in range(self.dim):
if igrid[idim] == 0:
igrid[idim] += 1
gridindex = 0
gridindex += self.ngrid[0] * self.ngrid[1] * (igrid[2] - 1)
gridindex += self.ngrid[0] * (igrid[1] - 1)
gridindex += igrid[0] - 1
#print(igrid, self.ngrid)
del igrid
found = True
if gridindex < 0:
found = False
return gridindex, found
def get_atomgridinfo(self):
self.get_gridinfo()
for iatom in self.atom:
iatom.gridindex, found = self.get_gridindex(iatom.x)
if found == False:
print(iatom.label,iatom.x, iatom.gridindex)
raise NameError('Wrong gridindex')
self.gridatomlist = self.destroy(self.gridatomlist)
for i in range(self.totgrid):
self.gridatomlist.append([])
#print('Length of gridatomlist:', len(self.gridatomlist))
for iatom in self.atom:
self.gridatomlist[iatom.gridindex].append(iatom.index)
#print(self.gridatomlist)
def get_atomtypelist(self):
self.atomtypelist = self.destroy(self.atomtypelist)
tempatomtypelist = []
for iatom in self.atom:
if iatom.symbol not in tempatomtypelist:
tempatomtypelist.append(iatom.symbol)
data = []
data.append(iatom.symbol)
data.append(self.lib.dfatr.iloc[self.lib.dfatr[(self.lib.dfatr['Symbol'] == iatom.symbol)].index[0]]['atr'])
self.atomtypelist.append(data)
del data
del tempatomtypelist
def get_metaltypelist(self):
self.metaltypelist = self.destroy(self.metaltypelist)
if len(self.atomtypelist) == 0:
raise NameError("Get atomtypelist first")
else:
for iatomtype in self.atomtypelist:
for imetalsymbol in self.lib.metalsymbol:
if iatomtype[0] == imetalsymbol:
data1 = []
for iatom in self.atom:
if iatom.symbol == imetalsymbol:
data1.append(iatom.index)
iatom.ismetal = True
data2 = []
data2.append(imetalsymbol)
data2.append(data1)
del data1
self.metaltypelist.append(data2)
del data2
def fractional_to_cartesian(self, dux):
dx = []
dx = self.destroy(dx)
for i in range(self.dim):
val = 0.0
for j in range(self.dim):
val += self.h[i][j] * dux[j]
dx.append(val)
return dx
def cartesian_to_fractional(self, dx):
dux = []
dux = self.destroy(dux)
for i in range(self.dim):
val = 0.0
for j in range(self.dim):
val += self.hinv[i][j] * dx[j]
dux.append(val)
return dux
def get_maxcovbl(self):
maxcovbl = self.skin
for iatomtype in self.atomtypelist:
for jatomtype in self.atomtypelist:
covbl = iatomtype[1] + jatomtype[1]
if covbl > maxcovbl:
maxcovbl = covbl
return maxcovbl
def check_bond(self, iatom, jatom):
'''1. Check this website: https://www.slideshare.net/NextMoveSoftware/rdkit-gems
Slide number: 20+
2. r_cov values are taken from openbabel elementtable.h
'''
maxdx2 = iatom.atr + jatom.atr + self.skin
maxdx2 *= maxdx2
x1 = iatom.x
x2 = jatom.x
dx = []
for idim in range(self.dim):
val = x1[idim] - x2[idim]
if val > 0.5:
val -= 1.0
if val < -0.5:
val += 1.0
dx.append(val)
dx = self.fractional_to_cartesian(dx)
val = 0.0
for idim in range(self.dim):
val += dx[idim]*dx[idim]
if val > maxdx2:
del dx
return False
del dx
minbondlength2 = (iatom.atr + jatom.atr)/2.0
minbondlength2 *= minbondlength2
if val < minbondlength2:
raise ValueError('Atom overlap detected')
return True
def get_distance(self, x1, x2):
""" Calculate the distance between two atoms, takes self.atom[index] as input, return distance in float"""
dx = []
for idim in range(self.dim):
val = x1[idim] - x2[idim]
if val > 0.5:
val -= 1.0
if val < -0.5:
val += 1.0
dx.append(val)
dx = self.fractional_to_cartesian(dx)
val = 0.0
for idim in range(self.dim):
val += dx[idim]*dx[idim]
val = np.sqrt(val)
del dx
return val
def check_repeat_atomlabel(self):
repeat = False
atomcount = len(self.atom)
for atomcount1 in range(0, atomcount - 1):
label1 = self.atom[atomcount1].label
for atomcount2 in range(atomcount1 + 1, atomcount):
label2 = self.atom[atomcount2].label
if label1 == label2:
repeat = True
return repeat
def get_neighborlist_without_grid(self):
self.G.clear()
self.G.add_nodes_from([i for i in range(len(self.atom))])
for iatom in self.atom:
#print(iatom.index, end = ' ')
for jatom in self.atom:
if iatom.index < jatom.index:
if self.check_bond(iatom,jatom) == True:
iatom.add_neighbor(jatom.index)
jatom.add_neighbor(iatom.index)
if self.G.has_edge(iatom.index, jatom.index) == False:
self.G.add_edge(iatom.index, jatom.index)
#print('Bond Calculation Time (s):\t', (end-start))
def get_neighborlist_with_grid(self):
self.G.clear()
self.G.add_nodes_from([i for i in range(len(self.atom))])
for iatom in self.atom:
#print(iatom.index, end = ' ')
for igrid in self.neighgrid[iatom.gridindex]:
for jindex in self.gridatomlist[igrid]:
if iatom.index < jindex:
jatom = self.atom[jindex]
if self.check_bond(iatom,jatom) == True:
iatom.add_neighbor(jatom.index)
jatom.add_neighbor(iatom.index)
if self.G.has_edge(iatom.index, jatom.index) == False:
self.G.add_edge(iatom.index, jatom.index)
#print('Bond Calculation Time (s):\t', (end-start))
def get_neighborlist(self, grid):
if grid:
self.get_neighborlist_with_grid()
else:
self.get_neighborlist_without_grid()
def clear_neighborlist(self):
for iatom in self.atom:
for ineighbor in iatom.neighborlist:
iatom.remove_neighbor(ineighbor)
def remove_neighbor(self, iindex, jindex):
self.atom[iindex].remove_neighbor(jindex)
self.atom[jindex].remove_neighbor(iindex)
def get_graph(self):
self.G.clear()
self.G.add_nodes_from([i for i in range(len(self.atom))])
for iatom in self.atom:
for ineighbor in iatom.neighborlist:
if self.G.has_edge(iatom.index, ineighbor) == False:
self.G.add_edge(iatom.index, ineighbor)
def get_solvent(self):
fragmentlist = nx.connected_components(self.G)
for ifragment in fragmentlist:
metal = False
for iindex in ifragment:
if self.atom[iindex].ismetal:
metal = True
break
if metal == False:
self.solventlist.append(list(ifragment))
#Remove solvent from graph
if len(self.solventlist) > 0:
print('Solvent presence:\t\t','True')
for isolvent in self.solventlist:
self.G.remove_nodes_from(isolvent)
'''
else:
print('Solvent presence:\t\t','False')
'''
def check_multimetal(self, iindex, imetalindex):
for ineighbor in self.atom[iindex].neighborlist:
if self.atom[ineighbor].ismetal == True:
if ineighbor != imetalindex:
return True
return False
def break_mof(self):
self.capairlist = self.destroy(self.capairlist)
#get capairlist
for imetaltype in self.metaltypelist:
for imetalindex in imetaltype[1]:
for ineighbor in self.atom[imetalindex].neighborlist:
#print('All Neigh',self.atom[imetalindex].label,self.atom[ineighbor].label)
if self.atom[ineighbor].ismetal == False:
count = 0
for jneighbor in self.atom[ineighbor].neighborlist:
if self.atom[jneighbor].ismetal == True:
count += 1
elif self.atom[jneighbor].symbol == 'H':
count += 1
if count != len(self.atom[ineighbor].neighborlist):
self.capairlist.append([imetalindex, ineighbor])
#print('Only CA',self.atom[imetalindex].label,self.atom[ineighbor].label)
############################################
#This part is for actual building block
############################################
self.fragG = self.G.copy()
#break capairlist
for inode, jnode in self.capairlist:
if self.fragG.has_edge(inode, jnode):
#print('Breaking path between', self.atom[inode].label, self.atom[jnode].label)
self.fragG.remove_edge(inode, jnode)
self.metalnodelist = self.destroy(self.metalnodelist)
self.funcgrouplist = self.destroy(self.funcgrouplist)
self.linkerlist = self.destroy(self.linkerlist)
fragmentlist = nx.connected_components(self.fragG)
for ifragment in fragmentlist:
found = False
count = 0
for iindex in ifragment:
if self.atom[iindex].ismetal:
found = True
if found:
break
else:
for inode, jnode in self.capairlist:
if iindex == jnode:
#print(iindex,inode,jnode)
count += 1
#print(len(ifragment),count)
if found:
self.metalnodelist.append(list(ifragment))
else:
if count == 1:
self.funcgrouplist.append(list(ifragment))
elif count > 1:
self.linkerlist.append(list(ifragment))
else:
raise ValueError('Wrong Breaking')
"""
Breaking of Building Block Critical Info:
Ref to: Figure 3 of (Cryst. Growth Des. 2017, 17, 5801−5810)
The buliding blocks can be differentiated into to types based on the breaking point.
We will take COO group for the example.
1. Building blocks consistent with chemical synthesis. In this case the breaking point
will be between metal atom and O atoms.
2. Building blocks useful for computational construction. In this case the breaking point
will be between C of COO group and the atom, part of the linker.
Update:
1. Currently this scenario is observered for COO group only.
2. We are searching for more cases.
"""
############################################
#This part is for computational construction
############################################
self.compG = self.G.copy()
if self.funcgrouplist:
for ifuncgroup in self.funcgrouplist:
self.compG.remove_nodes_from(ifuncgroup)
self.compcapairlist = self.destroy(self.compcapairlist)
self.compmetalnodelist = self.destroy(self.compmetalnodelist)
self.complinkerlist = self.destroy(self.complinkerlist)
allcalist = []
for inode, jnode in self.capairlist:
allcalist.append(jnode)
for imetalnode in self.metalnodelist:
calist = []
for iindex in imetalnode:
#print(self.atom[iindex].label)
for inode, jnode in self.capairlist:
if iindex == inode:
calist.append(jnode)
capairlist = []
removecalist = []
for k, ica in enumerate(calist):
for ineighbor in self.atom[ica].neighborlist:
for jca in calist[k+1:]:
for jneighbor in self.atom[jca].neighborlist:
if ineighbor == jneighbor:
if ineighbor not in imetalnode:
update = True
for kneighbor in self.atom[ica].neighborlist:
if kneighbor not in imetalnode:
if kneighbor != ineighbor:
if self.atom[kneighbor].symbol != 'H':
update = False
for kneighbor in self.atom[jca].neighborlist:
if kneighbor not in imetalnode:
if kneighbor != ineighbor:
if self.atom[kneighbor].symbol != 'H':
update = False
if update:
for kneighbor in self.atom[jneighbor].neighborlist:
if kneighbor not in allcalist:
if kneighbor not in imetalnode:
capairlist.append([jneighbor,kneighbor])
else:
removecalist.append(jneighbor)
if ica not in removecalist:
removecalist.append(ica)
if jca not in removecalist:
removecalist.append(jca)
for inode, jnode in self.capairlist:
if jnode in calist:
if inode in imetalnode:
if jnode not in removecalist:
self.compcapairlist.append([inode,jnode])
if capairlist:
for inode, jnode in capairlist:
self.compcapairlist.append([inode,jnode])
del calist
del capairlist
del removecalist
del allcalist
#break compcapairlist
for inode, jnode in self.compcapairlist:
if self.compG.has_edge(inode, jnode):
#print('Breaking path between', self.atom[inode].label, self.atom[jnode].label)
self.compG.remove_edge(inode, jnode)
fragmentlist = nx.connected_components(self.compG)
for ifragment in fragmentlist:
metal = False
for iindex in ifragment:
if self.atom[iindex].ismetal:
metal = True
break
if metal:
self.compmetalnodelist.append(list(ifragment))
else:
self.complinkerlist.append(list(ifragment))
#print(self.metalnodelist)
#print(self.compmetalnodelist)
count = 0
for imetalnode in self.compmetalnodelist:
addindex = []
for iindex in imetalnode:
for inode, jnode in self.compcapairlist:
if inode == iindex:
addindex.append(jnode)
for iindex in addindex:
self.compmetalnodelist[count].append(iindex)
del addindex
count += 1
#print(self.compmetalnodelist)
#print(self.linkerlist)
count = 0
for ilinker in self.complinkerlist:
addindex = []
for iindex in ilinker:
for inode, jnode in self.compcapairlist:
if jnode == iindex:
if inode not in addindex:
addindex.append(inode)
for iindex in addindex:
self.complinkerlist[count].append(iindex)
del addindex
count += 1
#print('Metal Node Count:\t\t', len(self.metalnodelist))
#print('Functional Group Count:\t\t', len(self.funcgrouplist))
#print('Organic Linker Counts:\t\t', len(self.linkerlist))
def get_fragment_data(self, fragment, case):
fragmentatomtypelist = []
fragmentatomtypelist = self.destroy(fragmentatomtypelist)
for iindex in fragment:
if self.atom[iindex].symbol not in fragmentatomtypelist:
fragmentatomtypelist.append(self.atom[iindex].symbol)
fragmentatomtypelist.sort()
fragmentatomtypecountlist = []
fragmentatomtypecountlist = self.destroy(fragmentatomtypecountlist)
for fragmentatomtype in fragmentatomtypelist:
fragmentatomtypecountlist.append(0)
for iindex in fragment:
count = 0
for fragmentatomtype in fragmentatomtypelist:
if fragmentatomtype != self.atom[iindex].symbol:
count += 1
else:
break
fragmentatomtypecountlist[count] += 1
fragmentnca = 0
if case == 0:
for iindex in fragment:
for inode, jnode in self.capairlist:
if inode == iindex:
fragmentnca += 1
elif case == 1:
for iindex in fragment:
for inode, jnode in self.capairlist:
if jnode == iindex:
fragmentnca += 1
return fragmentatomtypelist, fragmentatomtypecountlist, fragmentnca
def check_uniq_fragmentlist(self, fragment, uniqfragmentlist, case):
if len(uniqfragmentlist) == 0:
uniqfragmentlist.append(fragment)
return uniqfragmentlist
else:
uniq = True
for uniqfragment in uniqfragmentlist:
if len(uniqfragment) == len(fragment):
uniqfragmentatomtypelist, uniqfragmentatomtypecountlist, uniqfragmentnca = self.get_fragment_data(uniqfragment, case)
fragmentatomtypelist, fragmentatomtypecountlist, fragmentnca = self.get_fragment_data(fragment, case)
if uniqfragmentatomtypelist == fragmentatomtypelist:
if uniqfragmentatomtypecountlist == fragmentatomtypecountlist:
if case == 0:
if uniqfragmentnca == fragmentnca:
if fragmentnca != 0:
uniq = False
elif case == 1:
if uniqfragmentnca == fragmentnca:
if fragmentnca != 0:
uniq = False
else:
uniq = False
if uniq:
uniqfragmentlist.append(fragment)
return uniqfragmentlist
def get_uniq_fragmentlist(self, fragmentlist, case):
'''
Cases:
0 - ca connected to metalnode
1 - ca connected to linker
# - others
'''
uniqfragmentlist = []
uniqfragmentlist = self.destroy(uniqfragmentlist)
for fragment in fragmentlist:
uniqfragmentlist = self.check_uniq_fragmentlist(fragment, uniqfragmentlist, case)
return uniqfragmentlist
def wrap_fragment(self, fragment):
for iindex in fragment:
for idim in range(self.dim):
self.atom[iindex].writex[idim] = self.atom[iindex].x[idim]
atomshift = []
for i, iindex in enumerate(fragment):
atomshift.append([iindex,[False,False,False]])