-
Notifications
You must be signed in to change notification settings - Fork 104
/
NMR.py
executable file
·624 lines (398 loc) · 17.8 KB
/
NMR.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
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 12 14:42:47 2015
@author: ke291
Takes care of all the NMR description interpretation, equivalent atom
averaging, Boltzmann averaging and DP4 input preparation and running DP4.py. Called by PyDP4.py
FUNCTIONS AFTER REWRITE:
Calculation of NMR shifts based on TMS reference
Equivalent atom averaging
NMR description parsing
NMR raw data interpretation top level organization
"""
import re
import os
import math
import copy
import pickle
from Proton_processing import process_proton
from Carbon_processing import process_carbon
import shutil
from pathlib import Path
import shutil
gasConstant = 8.3145
temperature = 298.15
hartreeEnergy = 2625.499629554010
# Data structure for loading and keeping all of experimental NMR data in one place.
class NMRData:
def __init__(self,settings):
self.cwd = Path(os.getcwd())
self.InputPath = settings.NMRsource # Initial structure input file
self.Type = 'desc' # desc or fid, depending on whether the description or raw data used
self.Atoms = [] # Element labels
self.Cshifts = [] # Experimental C NMR shifts
self.Clabels = [] # Experimental C NMR labels, if any
self.Hshifts = [] # Experimental H NMR shifts
self.Hlabels = [] # Experimental H NMR labels, if any
self.Equivalents = [] # Atoms assumed to be NMR equivalent in computational data
self.Omits = []
self.protondata = {}
self.carbondata = {}
print(self.InputPath)
#print(self.InputPath.split('.'))
#quit()
if len(self.InputPath) == 0:
print('No NMR Data Added, quitting...')
quit()
else:
for ind1 , p in enumerate(self.InputPath):
if p.exists():
if p.is_dir():
self.Type = 'fid'
if p.parts[-1] == "Proton" or p.parts[-1] == "proton":
self.ProcessProton(settings,ind1)
elif p.parts[-1] == "Carbon" or p.parts[-1] == "carbon":
self.ProcessCarbon(settings,ind1)
elif p.parts[-1] == "Proton.dx" or p.parts[-1] == "proton.dx":
self.Type = 'jcamp'
self.ProcessProton(settings,ind1)
elif p.parts[-1] == "Carbon.dx" or p.parts[-1] == "carbon.dx":
self.Type = 'jcamp'
self.ProcessCarbon(settings,ind1)
else:
self.Type = 'desc'
self.ExpNMRFromDesc()
else:
print('NMR data path does not exist, quitting...')
quit()
def ExpNMRFromDesc(self):
print('Loading NMR data from ' + str(self.InputPath))
# Reads the experimental NMR data from the file
ExpNMR_file = open(self.InputPath[0], 'r')
Cexp = ExpNMR_file.readline()
ExpNMR_file.readline()
Hexp = ExpNMR_file.readline()
# Check if exp NMR file contains info about equivalent atoms and read it
# into an array
# Also reads a list of atoms to omit from analysis
equivalents = []
omits = []
ExpNMR_file.readline()
for line in ExpNMR_file:
if not 'OMIT' in line and len(line) > 1:
equivalents.append(line[:-1].split(','))
elif 'OMIT' in line:
omits.extend(line[5:-1].split(','))
ExpNMR_file.close()
self.Clabels, self.Cshifts = self.ParseExp(Cexp)
self.Hlabels, self.Hshifts = self.ParseExp(Hexp)
self.Equivalents = equivalents
self.Omits = omits
def ParseExp(self, exp):
if len(exp)>0:
# Replace all 'or' and 'OR' with ',', remove all spaces and 'any'
texp = re.sub(r"or|OR", ',', exp, flags=re.DOTALL)
texp = re.sub(r" |any", '', texp, flags=re.DOTALL)
# Get all assignments, split mulitassignments
expLabels = re.findall(r"(?<=\().*?(?=\)|;)", texp, flags=re.DOTALL)
expLabels = [x.split(',') for x in expLabels]
# Remove assignments and get shifts
ShiftData = (re.sub(r"\(.*?\)", "", exp.strip(), flags=re.DOTALL)).split(',')
print(ShiftData)
expShifts = [float(x) for x in ShiftData]
else:
expLabels = []
expShifts=[]
return expLabels, expShifts
def ProcessProton(self, settings,ind):
if settings.OutputFolder == '':
pdir = self.cwd / "Pickles"
gdir = self.cwd / "Graphs"
else:
pdir = settings.OutputFolder / "Pickles"
gdir = settings.OutputFolder / "Graphs"
NMR_file = settings.NMRsource[ind]
if not Path(gdir).exists():
os.mkdir(gdir)
os.mkdir(gdir / settings.InputFiles[0])
else:
if not Path(gdir / settings.InputFiles[0]).exists():
os.mkdir(gdir / settings.InputFiles[0])
if not pdir.exists():
os.mkdir(pdir)
os.mkdir(pdir / settings.InputFiles[0])
else:
if not Path(pdir / settings.InputFiles[0]).exists():
os.mkdir(pdir / settings.InputFiles[0])
if Path(pdir / settings.InputFiles[0] / "protondata").exists():
self.protondata = pickle.load(open(pdir / settings.InputFiles[0] / "protondata", "rb"))
self.Hshifts = self.protondata["exppeaks"]
else:
protondata = {}
protondata["exppeaks"], protondata["xdata"], protondata["ydata"], protondata["integrals"], protondata[
"peakregions"], protondata["centres"], \
protondata["cummulativevectors"], protondata["integralsum"], protondata["picked_peaks"], protondata[
"params"], protondata["sim_regions"] \
= process_proton(NMR_file, settings,self.Type)
pickle.dump(protondata, Path(pdir / settings.InputFiles[0] / "protondata").open(mode = "wb+"))
self.Hshifts = protondata["exppeaks"]
self.protondata = protondata
def ProcessCarbon(self, settings,ind):
if settings.OutputFolder == '':
pdir = self.cwd / "Pickles"
gdir = self.cwd / "Graphs"
else:
pdir = settings.OutputFolder / "Pickles"
gdir = settings.OutputFolder / "Graphs"
NMR_file = settings.NMRsource[ind]
if not Path(gdir).exists():
os.mkdir(gdir)
os.mkdir(gdir / settings.InputFiles[0])
else:
if not Path(gdir / settings.InputFiles[0]).exists():
os.mkdir(gdir / settings.InputFiles[0])
if not pdir.exists():
os.mkdir(pdir)
os.mkdir(pdir / settings.InputFiles[0])
else:
if not Path(pdir / settings.InputFiles[0]).exists():
os.mkdir(pdir / settings.InputFiles[0])
if Path(pdir / settings.InputFiles[0] / "carbondata").exists():
self.carbondata = pickle.load(open(pdir / settings.InputFiles[0] / "carbondata", "rb"))
self.Cshifts = self.carbondata["exppeaks"]
else:
carbondata = {}
carbondata["ydata"], carbondata["xdata"], carbondata["corrdistance"], carbondata["uc"], \
carbondata["exppeaks"], carbondata["simulated_ydata"], carbondata["removed"] = process_carbon(
NMR_file, settings,self.Type)
pickle.dump(carbondata, Path(pdir / settings.InputFiles[0] / "carbondata").open(mode = "wb+"))
#pickle.dump(a, Path("/Users/Maidenhair/Desktop/text.txt").open(mode="wb+"))
self.carbondata = carbondata
self.Cshifts = carbondata["exppeaks"]
def CalcBoltzmannWeightedShieldings(Isomers):
energies = []
for i, iso in enumerate(Isomers):
# Calculate rel. energies in kJ/mol
minE = min(iso.DFTEnergies)
relEs = []
for e in iso.DFTEnergies:
relEs.append((e - minE) * hartreeEnergy)
Isomers[i].Energies = relEs
populations = []
# Calculate Boltzmann populations
for e in relEs:
populations.append(math.exp(-e * 1000 / (gasConstant * temperature)))
q = sum(populations)
for p in range(0, len(populations)):
populations[p] = populations[p] / q
Isomers[i].Populations = populations
# Calculate Boltzmann weighed shielding constants
# by summing the shifts multiplied by the isomers population
BoltzmannShieldings = []
for atom in range(len(iso.Atoms)):
shielding = 0
c = 1
for population, shieldings in zip(iso.Populations, iso.ConformerShieldings):
c+=1
shielding = shielding + shieldings[atom] * population
BoltzmannShieldings.append(shielding)
Isomers[i].BoltzmannShieldings = BoltzmannShieldings
return Isomers
def GetTMSConstants(settings):
TMSfile = open(settings.ScriptDir + '/TMSdata', 'r')
TMSdata = TMSfile.readlines()
TMSfile.close()
for i, line in enumerate(TMSdata):
buf = line.split(' ')
if len(buf) > 1:
if settings.Solvent != '':
if buf[0].lower() == settings.nFunctional.lower() and \
buf[1].lower() == settings.nBasisSet.lower() and \
buf[2].lower() == settings.Solvent.lower():
print("Setting TMS references to " + buf[3] + " and " + \
buf[4] + "\n")
TMS_SC_C13 = float(buf[3])
TMS_SC_H1 = float(buf[4])
return TMS_SC_C13, TMS_SC_H1
else:
if buf[0].lower() == settings.nFunctional.lower() and \
buf[1].lower() == settings.nBasisSet.lower() and \
buf[2].lower() == 'none':
print("Setting TMS references to " + buf[3] + " and " + \
buf[4] + "\n")
TMS_SC_C13 = float(buf[3])
TMS_SC_H1 = float(buf[4])
return TMS_SC_C13, TMS_SC_H1
print("No TMS reference data found for these conditions, using defaults\n")
print("Unscaled shifts might be inaccurate, use of unscaled models is" + \
" not recommended.")
return settings.TMS_SC_C13, settings.TMS_SC_H1
def NMRDataValid(Isomers):
for isomer in Isomers:
if (len(isomer.ConformerShieldings) == 0):
return False
return True
def CalcNMRShifts(Isomers, settings):
print('WARNING: NMR shift calculation currently ignores the instruction to exclude atoms from analysis')
for i, iso in enumerate(Isomers):
BShieldings = iso.BoltzmannShieldings
Cvalues = []
Hvalues = []
Clabels = []
Hlabels = []
for a, atom in enumerate(iso.Atoms):
if atom == 'C':
shift = (settings.TMS_SC_C13-BShieldings[a]) / (1-(settings.TMS_SC_C13/10**6))
Cvalues.append(shift)
Clabels.append('C' + str(a + 1))
if atom == 'H':
shift = (settings.TMS_SC_H1-BShieldings[a]) / (1-(settings.TMS_SC_H1/10**6))
Hvalues.append(shift)
Hlabels.append('H' + str(a + 1))
Isomers[i].Cshifts = Cvalues
Isomers[i].Hshifts = Hvalues
Isomers[i].Clabels = Clabels
Isomers[i].Hlabels = Hlabels
print('C shifts for isomer ' + str(i) + ": ")
print(', '.join(['{0:.3f}'.format(x) for x in Isomers[i].Cshifts]))
print('H shifts for isomer ' + str(i) + ": ")
print(', '.join(['{0:.3f}'.format(x) for x in Isomers[i].Hshifts]))
for conf in iso.ConformerShieldings:
Cconfshifts = []
Hconfshifts = []
for a, atom in enumerate(iso.Atoms):
if atom == 'C':
shift = (settings.TMS_SC_C13-conf[a]) / (1-(settings.TMS_SC_C13/10**6))
Cconfshifts.append(shift)
if atom == 'H':
shift = (settings.TMS_SC_H1 - conf[a]) / (1 - (settings.TMS_SC_H1 / 10 ** 6))
Hconfshifts.append(shift)
Isomers[i].ConformerCShifts.append(Cconfshifts)
Isomers[i].ConformerHShifts.append(Hconfshifts)
return Isomers
def PrintConformationData(AllSigConfs):
# Make a list of populations and corresponding files for reporting
# significant conformations
"""from operator import itemgetter
ConfsPops = [list(x) for x in zip(args, populations)]
ConfsPops.sort(key=itemgetter(1), reverse=True)
totpop = 0
i = 0
while totpop < 0.8:
totpop += ConfsPops[i][1]
i += 1
SigConfs = ConfsPops[:i]"""
for Es, pops in zip(RelEs, populations):
print('\nConformer relative energies (kJ/mol): ' + \
', '.join(["{:5.2f}".format(float(x)) for x in Es]))
print('\nPopulations (%): ' + \
', '.join(["{:4.1f}".format(float(x)*100) for x in pops]))
for i, SigConfs in enumerate(AllSigConfs):
print("\nNumber of significant conformers for isomer "\
+ str(i+1) + ": " + str(len(SigConfs)) + "\n(pop, filename)")
for conf in SigConfs:
print(" " + format(conf[1]*100, "4.2f") + "% " + conf[0])
print('----------------')
print(" " + format(100*sum([x[1] for x in SigConfs]), "4.2f") +\
"% in total")
def RemoveEquivalents(Noutp, equivs, OldCval, OldHval, OldClabels, OldHlabels):
Cvalues = list(OldCval)
Hvalues = list(OldHval)
Clabels = list(OldClabels)
Hlabels = list(OldHlabels)
for eqAtoms in equivs:
eqSums = [0.0]*Noutp
eqAvgs = [0.0]*Noutp
if eqAtoms[0][0] == 'H':
#print eqAtoms, Hlabels
for atom in eqAtoms:
eqIndex = Hlabels.index(atom)
for ds in range(0, Noutp):
eqSums[ds] = eqSums[ds] + Hvalues[ds][eqIndex]
for ds in range(0, Noutp):
eqAvgs[ds] = eqSums[ds]/len(eqAtoms)
#Place the new average value in the first atom shifts place
target_index = Hlabels.index(eqAtoms[0])
for ds in range(0, Noutp):
Hvalues[ds][target_index] = eqAvgs[ds]
#Delete the redundant atoms from the computed list
#start with second atom - e.g. don't delete the original one
for atom in range(1, len(eqAtoms)):
del_index = Hlabels.index(eqAtoms[atom])
del Hlabels[del_index]
for ds in range(0, Noutp):
del Hvalues[ds][del_index]
if eqAtoms[0][0] == 'C':
for atom in eqAtoms:
eqIndex = Clabels.index(atom)
for ds in range(0, Noutp):
eqSums[ds] = eqSums[ds] + Cvalues[ds][eqIndex]
for ds in range(0, Noutp):
eqAvgs[ds] = eqSums[ds]/len(eqAtoms)
#Place the new average value in the first atom shifts place
target_index = Clabels.index(eqAtoms[0])
for ds in range(0, Noutp):
Cvalues[ds][target_index] = eqAvgs[ds]
#Delete the redundant atoms from the computed list
#start with second atom - e.g. don't delete the original one
for atom in range(1, len(eqAtoms)):
del_index = Clabels.index(eqAtoms[atom])
del Clabels[del_index]
for ds in range(0, Noutp):
del Cvalues[ds][del_index]
return Cvalues, Hvalues, Clabels, Hlabels
def MAE(L1, L2):
if len(L1) != len(L2):
return -1
else:
L = []
for i in range(0, len(L1)):
L.append(abs(L1[i]-L2[i]))
return sum(L)/len(L)
def RMSE(L1, L2):
if len(L1) != len(L2):
return -1
else:
L = []
for i in range(0, len(L1)):
L.append((L1[i]-L2[i])**2)
return math.sqrt(sum(L)/len(L))
def PairwiseAssignment(Isomers,NMRData):
# for each isomer sort the experimental and calculated shifts
for iso in Isomers:
sortedCCalc = sorted(iso.Cshifts, reverse=True)
sortedClabels =[ '' for i in iso.Clabels]
for ind_1 , shift in enumerate(iso.Cshifts):
ind_2 = sortedCCalc.index(shift)
sortedClabels[ind_2] = iso.Clabels[ind_1]
sortedHCalc = sorted(iso.Hshifts, reverse=True)
sortedHlabels = ['' for i in iso.Hlabels]
for ind_1, shift in enumerate(iso.Hshifts):
ind_2 = sortedHCalc.index(shift)
sortedHlabels[ind_2] = iso.Hlabels[ind_1]
sortedCExp = sorted(NMRData.Cshifts, reverse=True)
sortedHExp = sorted(NMRData.Hshifts, reverse=True)
assignedCExp = [''] * len(sortedCCalc)
assignedHExp = [''] * len(sortedHCalc)
tempCCalcs = list(iso.Cshifts)
tempHCalcs = list(iso.Hshifts)
# do the assignment in order of chemical shift starting with the largest
# Carbon
exp_ind = 0
for shift ,label in zip( sortedCCalc , sortedClabels):
if label not in NMRData.Omits:
ind = tempCCalcs.index(shift)
assignedCExp[ind] = sortedCExp[exp_ind]
tempCCalcs[ind] = ''
exp_ind += 1
# Proton
exp_ind = 0
for shift,label in zip( sortedHCalc,sortedHlabels):
if label not in NMRData.Omits:
ind = tempHCalcs.index(shift)
assignedHExp[ind] = sortedHExp[exp_ind]
tempHCalcs[ind] = ''
# update isomers class
iso.Cexp = assignedCExp
iso.Hexp = assignedHExp
return Isomers