-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDF2xyzV2.py
157 lines (116 loc) · 2.84 KB
/
SDF2xyzV2.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
import sys, math
from numpy import *
import AtomInfo
def Read_sdf(infilename):
ifile = open(infilename, 'r') #open file for reading
count = 0
X = []
Y = []
Z = []
element_symbol = []
Bond_pair1 = []
Bond_pair2 = []
Bond_type = []
TotalCharge = 0
CHG_atom = []
CHG = []
for line in ifile:
if count == 0:
Header1 = line
count += 1
continue
if count == 1:
Header2 = line
count += 1
continue
if count == 2:
Header3 = line
count += 1
continue
if count == 3:
a = line.split()
N = int(a[0])
N_Bond = int(a[1])
count += 1
continue
if 3 < count <= N+4:
i_atom = line.split()
if len(i_atom) != 0:
X.append(float(i_atom[0]))
Y.append(float(i_atom[1]))
Z.append(float(i_atom[2]))
element_symbol.append(i_atom[3])
count += 1
continue
if N+4 < count <= N+N_Bond+3 :
bond_info = line.split()
#print (bond_info)
bond_info = line.split()
Bond_pair1.append(int(bond_info[0]))
Bond_pair2.append(int(bond_info[1]))
Bond_type.append(int(bond_info[2]))
count +=1
continue
if count > N+N_Bond+3:
mol_info = line.split()
#print (mol_info)
if (mol_info[0] == "M"):
if (mol_info[1] == "END"):
break
if (mol_info[1] == "CHG"):
Num_CHGInfo = int(mol_info[2])
for k in range(Num_CHGInfo):
CHG_atom.append(int(mol_info[3+2*k]))
CHG.append(int(mol_info[4+2*k]))
TotalCharge += int(mol_info[4+2*k])
else:
#print("The sdf file is invalid!")
sys.exit()
count +=1
#Copy to array of numpy###########################
Mol_atom = []
Mol_CartX = zeros(N)
Mol_CartY = zeros(N)
Mol_CartZ = zeros(N)
CHG_atom = array(CHG_atom)
CHG = array(CHG)
for j in range(N):
Mol_CartX[j] = X[j]
Mol_CartY[j] = Y[j]
Mol_CartZ[j] = Z[j]
Mol_atom.append(element_symbol[j])
#del element_symbol[N:TotalStep]
del element_symbol[:]
del X[:]
del Y[:]
del Z[:]
##################################################
#print (Mol_atom)
#print (N)
#print (len(Mol_CartX))
#print('Reading a sdf file has finished')
###Calculating the total number of electrons#################
TotalNum_electron = 0
for j in range(N):
if (len(CHG_atom) != 0):
Judge = CHG_atom-j
if (any(Judge) == 0):
TotalNum_electron += AtomInfo.AtomicNumElec(Mol_atom[j])-CHG[where(Judge == 0)]
else:
TotalNum_electron += AtomInfo.AtomicNumElec(Mol_atom[j])
else:
TotalNum_electron += AtomInfo.AtomicNumElec(Mol_atom[j])
print('Total number of electron: %7d ' % (TotalNum_electron))
if (TotalNum_electron%2==0):
#print ("This system is a closed shell!")
SpinMulti = 1
else:
#print ("This system is a open shell!")
SpinMulti = 0
#############################################################
return SpinMulti
#usage ='Usage; %s infile' % sys.argv[0]
#try:
# infilename = sys.argv[1]
#except:
# print (usage); sys.exit()