-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiamondEnergyII.py
2109 lines (1898 loc) · 108 KB
/
DiamondEnergyII.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
#################################################################
## Diamond Energy version 2.0
##
## Input of just an InChI will do a conformation search which
## lists all low-energy conformations and an estimate of their
## energies, approximately in kJ/mol.
##
## Input of an InChI and a conformation number will report the
## energy and generate an .sdf file of the structure of that
## conformation
##
#################################################################
import sys
import array
import time
from time import process_time
# from IPython.core.display import display
from rdkit import Chem
# from rdkit.Chem.Draw import IPythonConsole
# IPythonConsole.ipython_useSVG=True
from rdkit.Chem import AllChem, rdMolDescriptors
from rdkit.Chem import Draw
from rdkit.Chem import rdmolfiles
from rdkit.Chem import Descriptors
# from IPython.display import display, Markdown, HTML
import re
start_time = process_time()
def takeSecond(elem):
return elem[1]
def atom_counts(molecule, atom_symbol):
"""Return the count of a specific atom type in a molecule."""
return sum(1 for atom in molecule.GetAtoms() if atom.GetSymbol() == atom_symbol)
def get_alcohol_oxygens(molecule):
"""Return a list of oxygen atom indices that belong to alcohol groups."""
alcohols = []
for atom in molecule.GetAtoms():
if atom.GetSymbol() == 'O' and atom.GetDegree() == 2:
neighbors = [n.GetSymbol() for n in atom.GetNeighbors()]
if 'C' in neighbors and 'H' in neighbors:
alcohols.append(atom.GetIdx())
return alcohols
def extract_and_convert(s):
"""Extracts the numeric part of a string and converts it to an integer."""
match = re.search(r'(\d+)', s)
if match:
return int(match.group(1))
return None
def printatoms():
print(" I#,At,Di,Tw,Nd, x, y, z, Co ")
for i in range(number_of_skeleton):
print("%3d " % (i), atom[i * 9:i * 9 + 9])
def printxyzatoms():
# this is an XYZ file
print(number_of_skeleton)
print(sys.argv[1])
scalefactor = 0.85
for i in range(number_of_skeleton):
if list_of_connectivity_int[i] in oxygen_atom:
print("O %8.4f %8.4f %8.4f" % (float(atom[i * 9 + 5]) * 0.885, float(atom[i * 9 + 6]) * 0.885,
float(change_enantiomer * atom[i * 9 + 7]) * 0.885))
else:
print("C %8.4f %8.4f %8.4f" % (float(atom[i * 9 + 5]) * 0.885, float(atom[i * 9 + 6]) * 0.885,
float(change_enantiomer * atom[i * 9 + 7]) * 0.885))
def printmol():
# NB must be in .mol order, not InChI order, for a molfile
# this is .sdf file single molecule .mol format
with open("Mol_conf"+str(conf_num)+".sdf", "a+") as f:
f.write(sys.argv[1])
f.write("\n")
f.write("\n")
f.write("\n")
## Here the bond number info need to add the number of bonds connecting the first and final ring atom as well
f.write("%3d%3d 0 0 0 0 0 0 0 0999 V2000" % (
number_of_skeleton, number_of_skeleton - 1 + len(first_atom_InChI)))
f.write("\n")
# scalefactor = 0.85s
# write coordinates information
i = 0
while i < number_of_skeleton:
if list_of_connectivity_int[i] in oxygen_atom:
f.write("%10.4f%10.4f%10.4f O 0 0 0 0 0 0 0 0 0 0 0 0" % (
float(atom[i * 9 + 5]) * 0.885, float(atom[i * 9 + 6]) * 0.885, float(atom[i * 9 + 7]) * 0.885))
f.write("\n")
else:
f.write("%10.4f%10.4f%10.4f C 0 0 0 0 0 0 0 0 0 0 0 0" % (
float(atom[i * 9 + 5]) * 0.885, float(atom[i * 9 + 6]) * 0.885, float(atom[i * 9 + 7]) * 0.885))
f.write("\n")
i += 1
# write bond connectivity information
j = 0
bond = []
while j < number_of_skeleton:
if (atominfo(inchi2wn[int(list_of_connectivity_int[j])], 1) == 0 and list_of_connectivity_int[
j] not in first_atom_InChI):
pass
else:
if list_of_connectivity_int[j] in first_atom_InChI:
current_ring_index = first_atom_InChI.index(list_of_connectivity_int[j])
final_atom = final_atom_InChI[current_ring_index]
bond_value = []
bond_value.append(list_of_connectivity_int[j])
bond_value.append(atominfo(inchi2wn[list_of_connectivity_int[j]], 1))
bond_value.sort()
bond.append(bond_value)
bond_value_1 = []
bond_value_1.append(list_of_connectivity_int[j])
bond_value_1.append(final_atom)
bond_value_1.sort()
bond.append(bond_value_1)
else:
bond_value_2 = []
bond_value_2.append(list_of_connectivity_int[j])
bond_value_2.append(atominfo(inchi2wn[list_of_connectivity_int[j]], 1))
bond_value_2.sort()
bond.append(bond_value_2)
j += 1
# bond.sort()
bond_num = 0
while bond_num < len(bond):
if (inchi2wn[bond[bond_num][0]] + 1) != (inchi2wn[bond[bond_num][1]] + 1):
f.write("%3d%3d 1 0" % (inchi2wn[bond[bond_num][0]] + 1, inchi2wn[bond[bond_num][1]] + 1))
f.write("\n")
else:
pass
bond_num += 1
f.write("M END")
f.write("\n")
f.write("$$$$")
f.write("\n")
f.close()
def atominfo(atomnumber, information):
# Return data for atom 'atomnumber'
# 0: InchiNumber
# 1: Previous Attached Atom
# 2: Was Direction; now energy of atom
# 3: Twist
# 4: New Direction
# 5,6,7: x,y,z coordinates
# 8: conformation - number of the relevant torsion angle
# print("atominfo working",atomnumber,information,atomnumber*8+information)
return atom[atomnumber * 9 + information]
def setatominfo(atomnumber, information, datapoint):
atom[atomnumber * 9 + information] = datapoint
def GetRingSystems(mol, includeSpiro=False):
ri = mol.GetRingInfo()
systems = []
for ring in ri.AtomRings():
ringAts = set(ring)
nSystems = []
for system in systems:
nInCommon = len(ringAts.intersection(system))
if nInCommon and (includeSpiro or nInCommon > 1):
ringAts = ringAts.union(system)
else:
nSystems.append(system)
nSystems.append(ringAts)
systems = nSystems
return systems
def mol_with_atom_index(mol):
for atom in mol.GetAtoms():
print(atom.GetIdx())
atom.SetAtomMapNum(atom.GetIdx())
return mol
#################################################################
## From a conformation number, recalculate newdirection and
## calculate the x, y, z coordinates of the conformation
## The current conformation is define by list conformation[]
## NB: direction (atominfo(#, 2) is currently unused
## Rest of system is unchanged
## old_conformation[] has the list for the energies in the starting structure
## except first time around
def find_all_conf_number():
# energy:
# count number of overlaps, adjacent atoms, 1,5 diaxial, gauche
num_overlap = 0
num_adjacent = 0
num_15diaxial = 0
num_gauche = 0
num_axeq = 0
num_13connect = 0
energy_total = 0
##count the number of C_C interraction
num_15diaxial_C_C = 0
num_gauche_C_C = 0
num_axeq_C_C = 0
##count the number of C_O interraction
num_15diaxial_C_O = 0
num_gauche_C_O = 0
num_axeq_C_O = 0
##count the number of O_O interraction
num_15diaxial_O_O = 0
num_gauche_O_O = 0
num_axeq_O_O = 0
##count the number of C_OH interraction
num_15diaxial_C_OH = 0
num_gauche_C_OH = 0
num_axeq_C_OH = 0
##count the number of O_OH interraction
num_15diaxial_O_OH = 0
num_gauche_O_OH = 0
num_axeq_O_OH = 0
##count the number of OH_OH interraction
num_15diaxial_OH_OH = 0
num_gauche_OH_OH = 0
num_axeq_OH_OH = 0
# Original Energy components
energy_axeq = -2
energy_gauche = 2
energy_15diaxial = 20
energy_adjacent = 200
energy_overlap = energy_adjacent
# New Energy components
energy_axeq_C_O = -1
energy_gauche_C_O = -3
energy_15diaxial_C_O = 4
energy_axeq_O_O = -6
energy_gauche_O_O = -4
energy_15diaxial_O_O = 11
energy_axeq_C_OH = -1
energy_gauche_C_OH = -2
energy_15diaxial_C_OH = 2
energy_axeq_O_OH = -1
energy_gauche_O_OH = -4
energy_15diaxial_O_OH = -11
energy_axeq_OH_OH = -9
energy_gauche_OH_OH = -3
energy_15diaxial_OH_OH = -16
energy_adjacent_O_OH = -26
energy_adjacent_OH_OH = -29
energy_13connect_O_OH = -21
energy_13connect_OH_OH = -23
current_atom_energy = 0
current_atom_marker = 0
num_repeat_torsions = -1
nt = 0
while nt < number_rotatable_bonds:
if conformation[nt] == old_conformation[nt]:
num_repeat_torsions += 1
else:
nt = number_rotatable_bonds
nt += 1
# print("Number of repeated torsions",num_repeat_torsions+1,conformation,old_conformation)
# print("Conformation",conformation)
# do the cs process
ia = 0
while ia < number_of_skeleton:
current_atom_marker = ia
# print("current_atom_marker", current_atom_marker)
current_torsion = atominfo(ia, 8)
# print("TEST!!!current_torsion:", current_torsion)
# print("TEST!!!atom_info:")
# printatoms()
##if current_torsion > -1:
if current_torsion > -1 and current_torsion >= num_repeat_torsions:
# print("TEST!!!current_torsion:", current_torsion)
if atominfo(inchi2wn[atominfo(inchi2wn[atominfo(ia, 1)], 1)], 1) == 0:
# print("TEST!!!current_torsion:", current_torsion)
total_twist = 0
## Expect the first and the second atom on the ring,
## the rest atoms will maintain untwist status while rotating mol
## Consequently, the relative twist value of these atoms will not be changed
## Judge whether the current atom is a fourth ring atom onward
elif atominfo(ia, 0) in ringatoms:
# print("TEST!!!current_torsion:", current_torsion)
ring_index = -1
for ring in inchi_ring_order:
ring_index += 1
if atominfo(ia, 0) in ring:
atom_index = ring.index(atominfo(ia, 0))
break
#print("ring_index:", ring_index)
#print("atom_index:", atom_index)
# The first two ring atoms, are treated the same as other non-ring atoms
if atom_index < 2:
total_twist = (atominfo(ia, 3) + conformation[current_torsion]) % 3
else:
if ring_twist_statue[ring_index] == 0:
if conformation[current_torsion] == 0:
total_twist = atominfo(ia, 3)
if conformation[current_torsion] == 1:
total_twist = atominfo(ia, 3)
# ring flex
if conformation[current_torsion] == 2:
# since ring flex will affect the branch before the third ring atom
if atom_index == 2:
total_twist = conformation[current_torsion]
if atom_index == 3:
total_twist = (conformation[current_torsion] + ring_flip_list[0][0]) % 3
if atom_index == 4:
total_twist = (conformation[current_torsion] + ring_flip_list[0][1]) % 3
if atom_index == 5:
total_twist = (conformation[current_torsion] + ring_flip_list[0][2]) % 3
if ring_twist_statue[ring_index] == 1:
if conformation[current_torsion] == 0:
total_twist = atominfo(ia, 3)
# ring flex
if conformation[current_torsion] == 1:
# since ring flex will affect the branch before the third ring atom
if atom_index == 2:
total_twist = conformation[current_torsion]
if atom_index == 3:
total_twist = (conformation[current_torsion] + ring_flip_list[1][0]) % 3
if atom_index == 4:
total_twist = (conformation[current_torsion] + ring_flip_list[1][1]) % 3
if atom_index == 5:
total_twist = (conformation[current_torsion] + ring_flip_list[1][2]) % 3
if conformation[current_torsion] == 2:
total_twist = atominfo(ia, 3)
# If the attached atom on ring atom
# In this version code, do not consider their movement
elif (atominfo(ia, 0) not in ringatoms) and (atominfo(ia, 1) in ringatoms):
ring_index = -1
for ring in inchi_ring_order:
ring_index += 1
if atominfo(ia, 1) in ring:
attached_atom_index = ring.index(atominfo(ia, 1))
break
#print("ring_index:", ring_index)
#print("attached_atom_index:", attached_atom_index)
#print("ring_torsion:", ring_torsion)
current_ring=ring_torsion[ring_index]
if ring_twist_statue[ring_index] == 0:
if conformation[current_ring] == 0:
total_twist = atominfo(ia, 3)
if conformation[current_ring] == 1:
total_twist = atominfo(ia, 3)
if conformation[current_ring] == 2:
# test atom_index=3
if attached_atom_index==1:
total_twist = (atominfo(ia, 3)+2)%3
if attached_atom_index==2:
total_twist = (atominfo(ia, 3)+1)%3
if attached_atom_index==3:
total_twist = (atominfo(ia, 3)+2)%3
if attached_atom_index==4:
total_twist = (atominfo(ia, 3)+1)%3
if attached_atom_index==5:
total_twist = (atominfo(ia, 3)+2)%3
#print("current_ring:", current_ring)
#print("ring_twist_statue[ring_index]:", ring_twist_statue[ring_index])
#print("current_ring_chekcing_atominfo(ia, 3):", atominfo(ia, 3))
#print("current_ring_chekcing_total_twist:", total_twist)
#print("current_ring_conformation[current_ring]:", conformation[current_ring])
if ring_twist_statue[ring_index] == 1:
if conformation[current_ring] == 0:
total_twist = atominfo(ia, 3)
if conformation[current_ring] == 1:
# test atom_index=3
if attached_atom_index == 1:
total_twist = (atominfo(ia, 3)+1)%3
if attached_atom_index == 2:
total_twist = (atominfo(ia, 3)+2)%3
if attached_atom_index == 3:
total_twist = (atominfo(ia, 3)+1)%3
if attached_atom_index == 4:
total_twist = (atominfo(ia, 3)+2)%3
if attached_atom_index==5:
total_twist = (atominfo(ia, 3)+1)%3
#print("current_ring:", current_ring)
#print("ring_twist_statue[ring_index]:", ring_twist_statue[ring_index])
#print("current_ring_chekcing_atominfo(ia, 3):", atominfo(ia, 3))
#print("current_ring_chekcing_total_twist:", total_twist)
#print("current_ring_conformation[current_ring]:", conformation[current_ring])
if conformation[current_ring] == 2:
total_twist = atominfo(ia, 3)
else:
total_twist = (atominfo(ia, 3) + conformation[current_torsion]) % 3
# print("TEST!!!total_twist:", total_twist)
# print("### total_twist, line 119,total_twist, atominfo(ia,3), conformation[current_torsion], ia, current_torsion")
# print("### total_twist, line 119",total_twist, atominfo(ia,3), conformation[current_torsion], ia, current_torsion)
# find direction of penultimate atom
direction = atominfo(inchi2wn[atominfo(inchi2wn[atominfo(ia, 1)], 1)], 4)
# print("direction:", direction)
# find direction of attached atom
old_direction = atominfo(inchi2wn[atominfo(ia, 1)], 4)
# print("old_direction:", old_direction)
if twisting[total_twist * 64 + old_direction * 8 + direction] > 8:
# print("conf_num, conformation",conf_num,conformation)
# print("current_torsion",current_torsion,"num_repeat_torsions",num_repeat_torsions)
# print("atom, attach_atom",ia,atominfo(ia,0),atominfo(ia,3),inchi2wn[atominfo(ia,1)])
# print("t-1, t, t+1",twisting[total_twist*64+old_direction*8+direction-1],twisting[total_twist*64+old_direction*8+direction],twisting[total_twist*64+old_direction*8+direction+1])
# print("twisting",twisting[total_twist*64+old_direction*8+direction],"tt,od,d",total_twist,old_direction,direction,"combine",total_twist*64+old_direction*8+direction)
# printatoms()
# printxyzatoms()
print("****** twisting too big ****** ", "current_atom_marker:", current_atom_marker, "old_direction:",
old_direction, "direction:", direction, "total_twist:", total_twist)
print(twisting[total_twist * 64 + old_direction * 8 + direction])
newdirection = twisting[total_twist * 64 + old_direction * 8 + direction]
# print("newdirection:", newdirection)
iattach_atom = inchi2wn[atominfo(ia, 1)]
# print("starting on atom",ia)
# print(atominfo(ia,0),atominfo(ia,1),atominfo(ia,2),atominfo(ia,3),atominfo(ia,4),atominfo(ia,5),atominfo(ia,6),atominfo(ia,7),atominfo(ia,8))
# print("dir, old, new, iattach_atom",direction,old_direction,newdirection,iattach_atom,"tt",total_twist)
xcoord = atominfo(iattach_atom, 5) + directions[newdirection * 3]
ycoord = atominfo(iattach_atom, 6) + directions[newdirection * 3 + 1]
zcoord = atominfo(iattach_atom, 7) + directions[newdirection * 3 + 2]
# print("old x,y,z",atominfo(iattach_atom,5),atominfo(iattach_atom,6),atominfo(iattach_atom,7),"directions",newdirection,";",directions[newdirection*3],directions[newdirection*3+1],directions[newdirection*3+2])
# print("new x,y,z",xcoord,ycoord,zcoord)
setatominfo(ia, 4, newdirection)
setatominfo(ia, 5, xcoord)
setatominfo(ia, 6, ycoord)
setatominfo(ia, 7, zcoord)
if ia > 2:
# No energy contribution from first three atoms, which are joined to each other
# Now calculate the contributions to the energy
# compare distance with all previous atoms
# However, can we avoid Pythagoras, because distances so close?
# overlap - idential coordinates
# adjacent - one bond length (root 3) away, and not bonded: dx+dy+dz=3, but so does 0,0,3
# 1,5 - two bond lengths: 2root2, and not bonded: dx+dy+dz=4
# gauche - three bonds (does bonding matter? probably not) (0,0,0 via 2,2,0 to 1,3,-1) root11
# make a list of atoms sorted by x, y, z?
# Should butane and methylpropane have the same energy? Give an energy for methyl, methylene, methine, C ?
# Compile a list of connections (1,3; 1,4) for each atom?
# favourable non-bonded interactions?
# print("worrying about energy")
current_atom_energy = 0
ea = 0
while ea < ia:
# for ea in range(0, ia):
# if ea>=ia:
# print("ea>=ia",ea,ia)
xea = atominfo(ea, 5) - xcoord
yea = atominfo(ea, 6) - ycoord
zea = atominfo(ea, 7) - zcoord
distance2 = xea * xea + yea * yea + zea * zea
# print("distance2",ia,ea,"d2",distance2,"x,y,z",xea,yea,zea)
if distance2 == 0:
num_overlap = num_overlap + 1
# no point in going on - energy too high
# if it is overlap, then the total energy will be marked lower than -1000
energy_total = -1000 - ia
test_atom = current_atom_marker
while test_atom < number_of_skeleton:
if atominfo(test_atom, 8) < current_torsion:
ia = current_atom_marker
break
else:
ia = number_of_skeleton
ea = ia
test_atom += 1
current_atom_energy += energy_overlap
if distance2 == 3 and not inchi2wn[atominfo(ia, 1)] == ea:
# if this adjacent situation happens between the first and the final ring atom on the same ring,
# this situation will not be regarded as adjacent
# then need to skip this energy calculation round
if atominfo(ia, 0) in final_atom_InChI and atominfo(ea, 0) in first_atom_InChI:
ia_index = final_atom_InChI.index(atominfo(ia, 0))
ia_index = first_atom_InChI.index(atominfo(ea, 0))
if ia_index == ia_index:
ea += 1
continue
else:
# pass
# if it is an alcohol oxygen interact with the other oxygen
if (ia in oxygen_atom) and (ea in oxygen_atom):
if (wn2inchi[ia] in alcohol_oxygen_atom) and (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_adjacent_OH_OH
else:
current_atom_energy += energy_adjacent_O_OH
# else it is adjacent
else:
# print("adjacent",ia,ea,atominfo(ia,1),inchi2wn[atominfo(ia,1)])
num_adjacent = num_adjacent + 1
# if it is adjacent
# no point in going on - energy too high
# if it is adjacent, then the total energy will be marked lower than -1000
energy_total = -1000 - ia
test_atom = current_atom_marker
while test_atom < number_of_skeleton:
if atominfo(test_atom, 8) < current_torsion:
ia = current_atom_marker
break
else:
ia = number_of_skeleton
ea = ia
test_atom += 1
current_atom_energy += energy_adjacent
if distance2 == 8:
# two bonds away, or 1,5 diaxial?
# if two bonds away, atominfo(ia,1) must be attached to ea
if not (atominfo(ia, 1) == atominfo(ea, 1) or inchi2wn[
atominfo(inchi2wn[atominfo(ia, 1)], 1)] == ea):
# if they were to be bound to each other
# print("1,3 connection",ia,ea,";",atominfo(ia,1),atominfo(ea,1),";",inchi2wn[atominfo(inchi2wn[atominfo(ia,1)],1)],ea)
# Good moment to look for gauche interactions???
# What is attached to ea, other than atominfo(ia,1) ?
# Might be helpful to make a list in advance
num_15diaxial = num_15diaxial + 1
if (ia in oxygen_atom) or (ea in oxygen_atom):
if (ia in oxygen_atom) and (ea in oxygen_atom):
# num_15diaxial_O_O = num_15diaxial_O_O + 1
if (wn2inchi[ia] in alcohol_oxygen_atom) and (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_15diaxial_OH_OH
elif (wn2inchi[ia] in alcohol_oxygen_atom) or (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_15diaxial_O_OH
else:
current_atom_energy += energy_15diaxial_O_O
else:
# num_15diaxial_C_O = num_15diaxial_C_O + 1
if (wn2inchi[ia] in alcohol_oxygen_atom) and (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_15diaxial_C_OH
else:
current_atom_energy += energy_15diaxial_C_O
else:
num_15diaxial_C_C = num_15diaxial_C_C + 1
current_atom_energy += energy_15diaxial
# in 1,3 connection case, if they are alcohol oxygen interact with the other osygen, then need to add new motif
else:
num_13connect += 1
if (ia in oxygen_atom) and (ea in oxygen_atom):
# if both are alcohol oxygen
if (wn2inchi[ia] in alcohol_oxygen_atom) and (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_13connect_OH_OH
# else one of atoms is ether oxygen
else:
current_atom_energy += energy_13connect_O_OH
if distance2 == 11:
# possible gauche interaction; only if connected??? Rather unusual if not connected; Risk it!
num_gauche = num_gauche + 1
if (ia in oxygen_atom) or (ea in oxygen_atom):
if (ia in oxygen_atom) and (ea in oxygen_atom):
# num_gauche_O_O = num_gauche_O_O + 1
if (wn2inchi[ia] in alcohol_oxygen_atom) and (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_gauche_OH_OH
elif (wn2inchi[ia] in alcohol_oxygen_atom) or (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_gauche_O_OH
else:
current_atom_energy += energy_gauche_O_O
else:
# num_gauche_C_O = num_gauche_C_O + 1
if (wn2inchi[ia] in alcohol_oxygen_atom) or (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_gauche_C_OH
else:
current_atom_energy += energy_gauche_C_O
else:
num_gauche_C_C = num_gauche_C_C + 1
current_atom_energy += energy_gauche
if distance2 == 16:
num_axeq = num_axeq + 1
if (ia in oxygen_atom) or (ea in oxygen_atom):
if (ia in oxygen_atom) and (ea in oxygen_atom):
# num_axeq_O_O = num_axeq_O_O + 1
if (wn2inchi[ia] in alcohol_oxygen_atom) and (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_axeq_OH_OH
elif (wn2inchi[ia] in alcohol_oxygen_atom) or (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_axeq_O_OH
else:
current_atom_energy += energy_axeq_O_O
else:
# num_axeq_C_O = num_axeq_C_O + 1
if (wn2inchi[ia] in alcohol_oxygen_atom) or (wn2inchi[ea] in alcohol_oxygen_atom):
current_atom_energy += energy_axeq_C_OH
else:
current_atom_energy += energy_axeq_C_O
else:
num_axeq_C_C = num_axeq_C_C + 1
current_atom_energy += energy_axeq
ea += 1
# if the ring is broken after the searing process, then the structure need to be throw away
# if it is not overlap or adjacent, then the process will continue
if energy_total > -1000:
energy_total += current_atom_energy
# print("current_atom_marker,ia,current_atom_energy",current_atom_marker,ia,current_atom_energy,energy_total)
else:
return energy_total
setatominfo(current_atom_marker, 2, current_atom_energy)
else:
energy_total += atominfo(current_atom_marker, 2)
# print("Adding energy from previous structure",ia,current_atom_marker,atominfo(current_atom_marker,2),energy_total)
# printatoms()
# print("===")
ia += 1
# if energy_total > -1:
# gauche interaction is about 2 kJ/mol (two butane conformations)
# 1,5 diaxial is at least 20 kJ/mol - this is a relaxed diaxial cyclohexane, so should be more
# overlap and adjacent are very large - no more detail needed
# print("Energy_total",energy_total)
# energy_total=energy_gauche*num_gauche+energy_15diaxial*num_15diaxial+energy_adjacent*(num_overlap+num_adjacent)
# print("Energy of",conf_num,":",num_overlap,num_adjacent,num_15diaxial,num_gauche,num_axeq,"overall",energy_total,conformation)
# f1.write("\n")
# f1.write("Energy of conformer"+" "+str(conf_num)+":"+" "+str(num_overlap)+" "+str(num_adjacent)+" "+str(num_axeq_C_C)+" "+str(num_gauche_C_C)+" "+str(num_15diaxial_C_C)+" "+str(num_axeq_C_O)+" "+str(num_gauche_C_O)+" "+str(num_15diaxial_C_O)+" "+str(num_axeq_O_O)+" "+str(num_gauche_O_O)+" "+str(num_15diaxial_O_O)+" "+"overall_energy:"+" "+str(energy_total)+" "+str(conformation))
# f1.write("\n")
# printatoms()
# else:
# print("*** Energy overload ***",conf_num,energy_total,-1-energy_total,conformation)
print()
return energy_total
#################################################################
## Start and calculate preliminary quantities:
## Number of atoms, stereogenic centres, rotatable bonds, etc
#################################################################
print("###########################################")
print("############ Diamond Energy II ############")
print("###########################################")
print(sys.argv[1])
m = Chem.MolFromInchi(sys.argv[1])
number_of_skeleton = m.GetNumAtoms()
number_of_carbons = atom_counts(m, 'C')
number_of_oxygens = atom_counts(m, 'O')
print("Number of skeleton: ", number_of_skeleton)
print("Number of carbons:", number_of_carbons)
print("Number of oxygens:", number_of_oxygens)
ringatoms = [atom + 1 for ring in Chem.GetSymmSSSR(m) for atom in ring]
print("ringatoms:", ringatoms)
oxygen_atom = [atom.GetIdx() + 1 for atom in m.GetAtoms() if atom.GetSymbol() == 'O']
print("oxygen_atom:", oxygen_atom)
alcohol_oxygen_atom = get_alcohol_oxygens(m)
print("alcohol_oxygen_atom:", alcohol_oxygen_atom)
change_enantiomer = 1
len_list_numbers_stereogenic_centres = 0
if sys.argv[1].find("/t") > -1:
stereogenic_centres = sys.argv[1].split("/")[4].split("t")[1]
number_stereogenic_centres = rdMolDescriptors.CalcNumAtomStereoCenters(m)
print("number_stereogenic_centres:", number_stereogenic_centres)
list_stereogenic_centres = stereogenic_centres.split(",")
print("list_stereogenic_centres:", list_stereogenic_centres)
initial_stereoinfo=list_stereogenic_centres.copy()
list_numbers_stereogenic_centres = array.array("i")
for i in range(0, len(list_stereogenic_centres)):
list_numbers_stereogenic_centres.append(int(list_stereogenic_centres[i].replace("+", "").replace("-", "")))
len_list_numbers_stereogenic_centres = len(list_numbers_stereogenic_centres)
print(number_stereogenic_centres, "stereogenic_centres_initial:", stereogenic_centres)
print("list_numbers_stereogenic_centres_initial", list_numbers_stereogenic_centres)
print("len_list_numbers_stereogenic_centres", len(list_numbers_stereogenic_centres))
if sys.argv[1].find("/m1") > -1:
change_enantiomer = -1
else:
stereogenic_centres = ''
number_stereogenic_centres = 0
connectivity = sys.argv[1].split("/")[2].split("c")[1]
print("connectivity ", connectivity)
list_of_connectivity = connectivity.replace(")", ")-").replace("(", "-(").replace(",", "-,").split("-")
print("list_of_connectivity ", list_of_connectivity)
# remove duplicates since introduce ring and get all atoms InChI order
temp_list_of_connectivity = connectivity.replace(")", ")-").replace("(", "-(").replace(",", "-,").split("-")
# print("temp_list_of_connectivity ",temp_list_of_connectivity)
temp_all_atom_order = connectivity.replace(")", "-").replace("(", "-").replace(",", "-").split("-")
# print("temp_all_atom_order:", temp_all_atom_order)
all_atom_order = []
for atom in temp_all_atom_order:
if atom not in all_atom_order:
all_atom_order.append(atom)
print("all_atom_InChI_order:", all_atom_order)
list_of_connectivity_int = [extract_and_convert(x) for x in all_atom_order if extract_and_convert(x) is not None]
print("list_of_connectivity_int", list_of_connectivity_int)
if sys.argv[1].find("/t") > -1:
# sort the stereocenter order based on list of connectivity int order
sorted_indices = sorted(range(len(list_numbers_stereogenic_centres)), key=lambda k: list_of_connectivity_int.index(list_numbers_stereogenic_centres[k]))
list_numbers_stereogenic_centres = [list_numbers_stereogenic_centres[i] for i in sorted_indices]
list_stereogenic_centres = [list_stereogenic_centres[i] for i in sorted_indices]
print("list_stereogenic_centres_sort:", list_stereogenic_centres)
print("list_numbers_stereogenic_centres", list_numbers_stereogenic_centres)
else:
pass
# Remove the repeated first atom from list_of_connectivity
first_atom_InChI = []
time = 0
for atom_info in all_atom_order:
if int(atom_info) in ringatoms:
time += 1
if time % 6 == 1:
# print(time)
first_atom_InChI.append(int(atom_info))
print("first_atom_InChI:", first_atom_InChI)
# re-sort the order of the temp_inchi_ring_order based on the first_atom_InChI
inchi_ring_order = []
i = 0
num = 0
for atom_info in all_atom_order:
if int(atom_info) in ringatoms:
if num % 6 == 0:
inchi_ring_order.append([int(atom_info)])
num += 1
else:
inchi_ring_order[i].append(int(atom_info))
num += 1
if num % 6 == 0:
i += 1
# print("num:", num)
# print("i:", i)
print("inchi_ring_order:", inchi_ring_order)
# inchi2workingnumber translates the inchi atom number to the position in the array
# InChI numbers are 1 - n; working numbers are 0 to n-1
# The atoms are numbered as InChI numbers, and as the position in the atom list
# For example, if an InChI has c1-6(2)7(3,4)5
# Then: inchi2wn[6]=1
# and: wn2inchi[1]=6
# Then: inchi2wn[7]=3
# and: wn2inchi[3]=7
inchi2wn = array.array('i')
wn2inchi = array.array('i')
tmp_inchi2workingnumber = all_atom_order
for workingposition in range(0, number_of_skeleton):
if int(tmp_inchi2workingnumber[workingposition]) not in wn2inchi:
wn2inchi.append(int(tmp_inchi2workingnumber[workingposition]))
inchi2wn.append(0)
else:
pass
inchi2wn.append(0)
for workingposition in range(0, number_of_skeleton):
inchi2wn[wn2inchi[workingposition]] = workingposition
tmp_inchi2workingnumber.clear()
# print("wn2inchi", wn2inchi)
# print("inchi2wn", inchi2wn[1: number_of_carbons+1])
# Number of methyl groups; may not need this, but never know...
# number_methyl=int(sys.argv[1].split("/")[3].split("h")[1].split(",").pop().split("-").pop().split("H")[0])
# print(number_methyl,"methyls")
## using NumRotatableBonds function in rdkit to count rotatable bonds, which ignore bonds on ring automatically
## let the ring bond regarbe ded as a rotatable bond to do ring flex in this version
number_rotatable_bonds = Descriptors.NumRotatableBonds(m) + len(inchi_ring_order)
print("number_rotatable_bonds:", number_rotatable_bonds)
## number_rotatable_bonds=len(connectivity.replace(")","-").split("-"))-3(for alkanes)
number_conformations = 3 ** number_rotatable_bonds
print(number_rotatable_bonds, "rotatable bonds;", number_conformations, "conformations")
conformation = array.array("i")
old_conformation = array.array("i")
conf_number = 0
# print("len(sys.argv)",len(sys.argv),sys.argv)
if len(sys.argv) > 4:
conf_number = int(sys.argv[4])
tmp_conf_number = conf_number
if number_rotatable_bonds == 0:
conformation.append(0)
else:
for i in range(0, number_rotatable_bonds):
conformation.append(0)
old_conformation = conformation[:]
old_conformation[0] = -1
for i in range(0, number_rotatable_bonds):
# print("conf_sort_out_setup",number_rotatable_bonds,i,tmp_conf_number,conformation)
conformation[number_rotatable_bonds - i - 1] = tmp_conf_number % 3
tmp_conf_number = int(tmp_conf_number / 3)
# print("Conformation",conformation)
# The directions array gives the vector for each step between atoms
directions = array.array('i',
[ 1, 1, 1,
-1, -1, 1,
-1, 1, -1,
1, -1, -1,
-1, -1, -1,
1, 1, -1,
1, -1, 1,
-1, 1, 1])
# print(directions)
# The twistings array gives the twisted vectors
# The 9 should never occur
# With penultimate direction pendir
# and attached atom direction attdir
# the new direction will be pendir (untwisted)
# twisting[attdir*8+pendir] to twist up
# twisting[64+attdir*8+pendir] to twist down
# newdirection=twisting[twist*64+old_direction*8+direction]
twisting = array.array('i',
[9, 9, 9, 9, 9, 5, 6, 7,
9, 9, 9, 9, 4, 9, 6, 7,
9, 9, 9, 9, 4, 5, 9, 7,
9, 9, 9, 9, 4, 5, 6, 9,
9, 1, 2, 3, 9, 9, 9, 9,
0, 9, 2, 3, 9, 9, 9, 9,
0, 1, 9, 3, 9, 9, 9, 9,
0, 1, 2, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 7, 5, 6,
9, 9, 9, 9, 6, 9, 7, 4,
9, 9, 9, 9, 7, 4, 9, 5,
9, 9, 9, 9, 5, 6, 4, 9,
9, 2, 3, 1, 9, 9, 9, 9,
3, 9, 0, 2, 9, 9, 9, 9,
1, 3, 9, 0, 9, 9, 9, 9,
2, 0, 1, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 6, 7, 5,
9, 9, 9, 9, 7, 9, 4, 6,
9, 9, 9, 9, 5, 7, 9, 4,
9, 9, 9, 9, 6, 4, 5, 9,
9, 3, 1, 2, 9, 9, 9, 9,
2, 9, 3, 0, 9, 9, 9, 9,
3, 0, 9, 1, 9, 9, 9, 9,
1, 2, 0, 9, 9, 9, 9, 9])
# print(twisting)
# The ring atoms twist value should be store in lists in advance for building the ring structure
# Once the first three ring 'atoms confirm
# The ring atoms also confirm closing the ring
# ring_twist_list_1=[1, 2, 1] # beta
# ring_twist_list_2=[2, 1, 2] # alpha
ring_twist_list = [[1, 2, 1], [2, 1, 2]]
# Correspondingly, there will be two different ring flip relative twist values lists
# for ring_twist_list_1 and ring_twist_list_2 in their situation
ring_flip_list = [[0, 2, 0], [0, 1, 0]]
# Since ring flex will affect the branch before the third ring atom
# need to jump to rebuild the branch attached to the second ring atom
global affected_branch
affected_branch=[]
#################################################################
# Assemble molecule
# First three atoms are all connected as propane
# but need to distinguish ring atoms
# Each atom has the InChI atom number,
# the InChI# of the atom to which it is attached,
# the bond direction (from list of eight possibilities),
# twist (0,1,2) for multiple atoms on same last bond
# The overall new direction
# x,y,z coordinates (signed integers)
# NB: first atom is one, for easy array indexing
# I#,At,Di,Tw,Nd, x, y, z
#
# Need to index flexible torsion angles
# Each is uniquely defined by terminal atom, but need a list
#################################################################
atom1 = int(list_of_connectivity[0])
atom2 = int(list_of_connectivity[1])
# create a list named ring_twist_statue to record how the ring is built, alpha or beta
# The default statue is ring_twist_list_1, ring_twist_statue value should be 0
ring_twist_statue = []
ring_twist_statue.append(0)
firstatom_dir = 0
# create a list to store each ring bond's twist value,
# Record and store where the rotatable bond is ring flex indicator using ring_torsion list
global ring_torsion
ring_torsion = []
global ring_twist
ring_twist=[]
final_atom_InChI = []
# Record the first ring atom have been looked at
# avoid looking at it second time as there will be two first ring number shown in InChI string for ring system
processed_atoms = set()
attach = [atom1, atom2]
atom = array.array('i',
[int(list_of_connectivity[0]), 0, 0, 0, 5, 0, 0, 0, -1,
int(list_of_connectivity[1]), wn2inchi[0], 0, 0, 0, 1, 1, 1, -1])
# If the first two atoms are not in a ring, so could just re-use the previous method to build the propane
if (atom1 and atom2) not in inchi_ring_order[0]:
#print("Checking!")
if not list_of_connectivity[2].isnumeric():
if list_of_connectivity[3].find(",") > -1:
atom.extend([int(list_of_connectivity[2].replace("(", "").replace(")", "")), wn2inchi[1], 0, 1, 7, 0, 2, 2, -1])
atom.extend([int(list_of_connectivity[3].replace("(", "").replace(")", "").replace(",", "")), wn2inchi[1], 0, 2, 6, 2, 0, 2, -1])
if list_of_connectivity[3].find(")") == -1:
attach.append(int(list_of_connectivity[3].replace("(", "").replace(")", "")))
else:
# wait to test, atom 4 might be on torsion angle 0??
atom.extend([int(list_of_connectivity[4]), wn2inchi[1], 0, 0, 5, 2, 2, 0, -1])
attach.append(int(list_of_connectivity[4]))
else:
atom.extend([int(list_of_connectivity[2].replace("(", "").replace(")", "")), wn2inchi[1], 0, 1, 7, 0, 2, 2, -1])
if list_of_connectivity[2].find(")") > -1:
atom.extend([int(list_of_connectivity[3]), wn2inchi[1], 0, 0, 5, 2, 2, 0, -1])
attach.append(int(list_of_connectivity[3]))
if int(list_of_connectivity[3]) in ringatoms:
firstatom_dir = 5
twist = 0
current_twist = twist
ring_twist.append([current_twist])
# Mark the atom as processed
processed_atoms.add(int(list_of_connectivity[3]))
else:
attach.append("(")
attach.append(int(list_of_connectivity[2].replace("(", "").replace(")", "")))
else:
atom.extend([int(list_of_connectivity[2]), wn2inchi[1], 0, 0, 5, 2, 2, 0, -1])
attach.append(int(list_of_connectivity[2]))
# Once the first ring atom is recognized
# need to record the info of the current ring when the first ring atom is found
if int(list_of_connectivity[2]) in ringatoms:
firstatom_dir = 5
twist = 0
current_twist = twist
ring_twist.append([current_twist])
# Mark the atom as processed
processed_atoms.add(int(list_of_connectivity[2]))
# If the first three atoms involve a ring system, need to add ring information at the beginning
else:
if atom1 in ringatoms:
## If atom1 is the first atom of the ring, it represents this is a cyclohexane
## So the first three ring atoms are built as propane
## Here add the first atom's information, including direction/twist value/append ring_atom list
firstatom_dir = 5
twist = 0
current_twist = twist
# add first ring atom twist value
ring_twist.append([current_twist])
# Mark the atom as processed
processed_atoms.add(atom1)
## Here add the the second atom's information, including direction/twist value/append ring_atom list
secondatom_dir = 0
ring_twist[0].append(0)
## Here add the third atom's information, including direction/twist value/append ring_atom list/atom_list/attach_list
atom.extend([int(list_of_connectivity[2]), wn2inchi[1], 0, 0, 5, 2, 2, 0, -1])
attach.append(int(list_of_connectivity[2]))
ring_twist[0].append(0)
else:
## If atom2 is the first atom of the ring, it represents this is methyl-cyclohexane
## need to add its ring information
firstatom_dir = 0
twist = 0
current_twist = twist
ring_twist.append([current_twist])
# Mark the atom as processed
processed_atoms.add(atom2)
if not list_of_connectivity[2].isnumeric():
## means there are two methyl at the first ring atom
## So atom4 is the second ring atom, need to add its info
atom.extend([int(list_of_connectivity[2].replace("(", "").replace(")", "")), wn2inchi[1], 0, 0, 5, 2, 2, 0, -1])
if not list_of_connectivity[3].isnumeric():
pass
else: