-
Notifications
You must be signed in to change notification settings - Fork 0
/
MInChI_script.py
318 lines (251 loc) · 10.8 KB
/
MInChI_script.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
@author: Nina Sachdev
@date created: 1/13/21 (modified from my original JavaScript code created on 1/5/21)
'''
import json, rdkit, os, shutil
from rdkit import Chem
'''
@param fileName: name of the file containing UVCB data
@return filepath: the path of the file (including .txt)
@return filename: the name of the file (not including .txt)
'''
def getFile(fileName):
filepath = fileName
filename= fileName.split('.')[0]
return filepath, filename
'''
Creates a list of dictionaries to hold all UVCB data
Each dictionary corresponds to one row (substance) in the file
Each key in the dictionary corresponds to a column in the file
Each value in the dictionary corresponds to the entry associated with that column in the file
Saves chem_dict as a JSON file
@param fileName: name of the file containing UVCB data (not including .txt)
@return chem_dict: list of dictionaries containing all UVCB data
@return chem_keys: list of column names in the file
'''
def createChemDict(fileName):
filepath, filename = getFile(fileName)
with open (filepath, 'r') as infile:
lines = infile.readlines()
chem_keys = lines[0].split('\t')
chem_dict = []
for i in range(1, len(lines)): #want to skip the first row of labels
current_dict = {}
for k in range(len(chem_keys)):
current_dict[chem_keys[k]] = lines[i].split('\t')[k]
chem_dict.append(current_dict)
infile.close()
if os.path.isdir(filename + '_MInChI'):
shutil.rmtree(filename + '_MInChI')
if os.path.isdir(filename + '_MInChI/mixtures'):
shutil.rmtree(filename + '_MInChI/mixtures')
os.mkdir(filename + '_MInChI')
os.chdir(filename + '_MInChI')
with open(filename + '_dict.json', 'w') as outfile:
json.dump(chem_dict, outfile)
return chem_dict, chem_keys
'''
Filters out all substances that do not have an InChI
@param chem_dict: list of dictionaries containing all UVCB data
@return filter_InChI: list of dictionaries containing all substances that have an InChI
'''
def filterInChI(chem_dict):
filter_InChI = []
for i in range(len(chem_dict)):
if len(chem_dict[i]['InChI_Final']) > 0:
filter_InChI.append(chem_dict[i])
return filter_InChI
'''
Extracts features needed to create the Mixfile and MInChI string
@param filter_InChI: list of dictionaries containing all substances that have an InChI
@return names: list of chemical names
@return InChI_list: list of InChI values
@return InChI_key_list: list of InChI keys
@return SMILES_list: list of SMILES values
'''
def getFeatures(filter_InChI):
names = []
InChI_list = []
InChI_key_list = []
SMILES_list = []
for i in range(len(filter_InChI)):
names.append(filter_InChI[i]['Chemical_Name_Final'])
InChI_list.append(filter_InChI[i]['InChI_Final'])
InChI_key_list.append(filter_InChI[i]['InChIKey_Final'])
SMILES_list.append(filter_InChI[i]['SMILES_Final'])
return names, InChI_list, InChI_key_list, SMILES_list
'''
Uses Chem module from rdkit library to convert a list of SMILES to their corresponding molfiles
@param SMILES_list: list of SMILES values
@return molfile_list: list of molfiles associated with the SMILES list
'''
def getMolfile(SMILES_list):
molfile_list = []
for i in range(len(SMILES_list)):
m = Chem.MolFromSmiles(SMILES_list[i])
molfile_string = Chem.MolToMolBlock(m)
molfile_list.append(molfile_string)
return molfile_list
'''
Finds the concentration ratio for each substance, if it exists
Matches the corresponding unit (vp) to the ratio
@param filter_InChI: list of dictionaries containing all substances that have an InChI
@param names: chemical names of all substances in filter_InChI
@return quantities: list of concentration ratios in filter_InChI, empty string if no ratio exists
@return units: list of units associated with concentration ratios, empty string if no ratio exists
'''
def getConcentration(filter_InChI, names):
quantities = ['' for i in range(len(names))]
units = ['' for i in range(len(names))]
for n in range(len(names)):
name = names[n]
for l in range(len(name)):
if name[l] == '(':
if name[l+2] == ':' and name[l+4] == ')':
quantities[n] = name[l:l+5]
units[n] = 'vp' #MInChI units for ratio
return quantities, units
'''
Constructs a Mixfile for each UVCB substance that has an InChI
Saves the Mixfile in JSON format in a new /mixfiles/ directory
@param filename: name of the file containing UVCB data (not including .txt)
@param InChI_list: list of InChI values
@param InChI_key_list: list of InChI keys
@param names: list of chemical names
@param molfiles: list of molfiles
@param SMILES: list of SMILES values
@param quantities: list of concentration ratios
@return mixfiles: list of dictionaries, where each dictionary corresponds to a substance's Mixfile
'''
def createMixfileDict(filename, InChI_list, InChI_key_list, names, molfiles, SMILES, quantities):
os.mkdir('mixfiles')
mixfiles = []
for i in range(len(InChI_list)):
mixfile_dict = {}
mixfile_dict['mixfileVersion'] = 0.01
mixfile_dict['name'] = names[i]
mixfile_dict['molfile'] = molfiles[i]
mixfile_dict['inchi'] = InChI_list[i]
mixfile_dict['inchiKey'] = InChI_key_list[i]
mixfile_dict['smiles'] = SMILES[i]
if len(quantities[i]) > 0:
ratio = '[' + str(quantities[i][1]) + ', ' + str(quantities[i][3] + ']')
mixfile_dict['ratio'] = ratio
with open('mixfiles/' + filename + '_mixture' + str(i) + '.mixfile', 'w') as outfile:
json.dump(mixfile_dict, outfile)
mixfiles.append(mixfile_dict)
return mixfiles
'''
Builds the MInChI string from the substance's corresponding Mixfile
@param mixfiles: list of dictionaries, where each dictionary corresponds to a substance's Mixfile
@return minchis: a dictionary of MinChI strings, where each key is the chemical name and each value is the MInChI string
'''
def createMInChIString(mixfiles):
minchis = {}
for mixfile in mixfiles:
header = 'MInChI=0.00.1S'
identifier = mixfile['inchi'][9:]
indexing = 'n1'
concentration = ''
if 'ratio' in mixfile.keys():
concentration = mixfile['ratio'][1] + ':' + mixfile['ratio'][3] + 'vp'
minchi = header + '/' + identifier + '/' + indexing + '/' + concentration
minchis[mixfile['name']] = minchi
return minchis
'''
Adds the MInChI strings back into the original UVCB data
Creates a new MInChI column
Saves the updated data in a new .txt file
@param minchis: dictionary of MInChI strings
@param chem_dict: list of dictionaries containing all UVCB data
@param chem_keys: list of column names in the original file
@param filename: name of file containing original UVCB data (not including .txt)
'''
def addMInChIFile(minchis, chem_dict, chem_keys, filename):
outfile = open(filename + '_MInChI.txt', 'w')
chem_keys[-1] = 'Iodo'
chem_keys.append('MInChI\n')
outfile.write('\t'.join(chem_keys))
for i in range(len(chem_dict)):
name = chem_dict[i]['Chemical_Name_Final']
elts = list(chem_dict[i].values())
elts[-1] = elts[-1].strip('\n')
if name in minchis:
elts.append(minchis[name])
else:
elts.append('')
outfile.write('\t'.join(elts) + '\n')
'''
Computes various summary statistics for a given UVCB dataset
Finds how many substances had values for InChI, SMILES, concentration ratio, and a combination of the three
Writes summary statistics to a new file
@param chem_dict: list of dictionaries containing all UVCB data
@param minchis: dictionary of MInChI strings
@param filename: name of file containing all UVCB data (not including .txt)
'''
def summary_stats(chem_dict, minchis, filename):
stats = {}
for i in range(len(chem_dict)):
keys = []
for key, val in chem_dict[i].items():
if len(chem_dict[i][key]) > 0 and (key == 'InChI_Final' or key == 'SMILES_Final'):
keys.append(key)
name = chem_dict[i]['Chemical_Name_Final']
for l in range(len(name)):
if name[l] == '(':
if name[l+2] == ':' and name[l+4] == ')':
keys.append('Ratio_Concentration')
stats[name] = keys
inchi_smiles_ratio = inchi_smiles = inchi_ratio = smiles_ratio = inchi = smiles = ratio = 0
for s in stats:
if stats[s] == ['InChI_Final', 'SMILES_Final', 'Ratio_Concentration']:
inchi_smiles_ratio += 1
elif stats[s] == ['InChI_Final', 'SMILES_Final']:
inchi_smiles += 1
elif stats[s] == ['InChI_Final', 'Ratio_Concentration']:
inchi_ratio += 1
elif stats[s] == ['SMILES_Final', 'Ratio_Concentration']:
smiles_ratio += 1
elif stats[s] == ['InChI_Final']:
inchi += 1
elif stats[s] == ['SMILES_Final']:
smiles += 1
elif stats[s] == ['Ratio_Concentration']:
ratio += 1
outfile = open(filename + 'summary_stats.txt', 'w')
labels = ['Num_MInChIs', 'InChI_SMILES_Ratio', 'InChI_SMILES', 'InChI_Ratio', 'SMILES_Ratio', 'InChI', 'SMILES', 'Ratio']
stats_list = [len(minchis), inchi_smiles_ratio, inchi_smiles, inchi_ratio, smiles_ratio, inchi, smiles, ratio]
for i in range(len(labels)):
outfile.write(labels[i] + ': ' + str(stats_list[i]) + ' UVCB substances')
outfile.write('\n')
'''
Executes all functions in this script
For each substance in the UVCB dataset that contains an InChI, generates its Mixfile and MInChI string
Adds Mixfile to /mixfiles/ directory
Adds all MInChI strings to the dataset
Computes summary statistics for the dataset
@param currentFile: file path of the UVCB dataset (including .txt)
'''
def main(currentFile):
filepath, filename = getFile(currentFile)
chem_dict, chem_keys = createChemDict(filepath)
filter_InChI = filterInChI(chem_dict)
names, InChI_list, InChI_key_list, SMILES = getFeatures(filter_InChI)
molfiles = getMolfile(SMILES)
quantities, units = getConcentration(filter_InChI, names)
mixfiles = createMixfileDict(filename, InChI_list, InChI_key_list, names, molfiles, SMILES, quantities)
minchis = createMInChIString(mixfiles)
addMInChIFile(minchis, chem_dict, chem_keys, filename)
summary_stats(chem_dict, minchis, filename)
########################################
if __name__=='__main__':
#List of UVCB datasets
mixtureFiles = ['EU_REACH.txt', 'mixtures.txt', 'random.txt']
#Current directory (should also contain UVCB datasets)
mainDir = os.getcwd()
#Run the main function on the list of UVCB datasets
for mixtureFile in mixtureFiles:
os.chdir(mainDir)
main(mixtureFile)