Files
nmrproject/ILP/Vanilla/nmrSimilarityVanillien.py
T

280 lines
8.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import math
import numpy as np
["CINNAMICACID", "PCOUMARICACID", "MCOUMARICACID", "BENZALDEHYD", "CAFFEICACID", "3HYDROXYBENZALDEHYD", "4HYDROXYBENZALDEHYD", "34DIHYDROXYBENZALDEHYD"]
#H noch machen: Problem, Multiplett. Cinnamic acid voll Katastrophe, kombiniertmit einem Mangel an Informationen zu peak ppm in der Literatur
likelihood = []
#Cinnamicacid
HCINNAMICACID = {
1: ([5.78], [1]), #s
2: ([6.55, 6.34], [1]), #d
3: ([7.79, 7.57], [1]), #d
4: ([7.63, 7.55], [2]), #d
5: ([7.50, 7.47, 7.38, 7.21], [2]), #dd
6: ([7.32 , 7.32 ], [1]), #t
}
CCINNAMICACID = {
1: ([171.56], [1]),
2: ([120.33], [1]),
3: ([151.41], [1]),
4: ([140.47], [1]),
5: ([134.20], [2]),
6: ([133.48], [2]),
7: ([135.91], [1]),
}
#p-Coumaricacid
HPCOUMARICACID = {
}
CPCOUMARICACID = {
1: ([171.61], [1]),
2: ([117.18], [1]),
3: ([149.67], [1]),
4: ([132.76], [1]),
5: ([135.75], [2]),
6: ([118.65], [2]),
7: ([166.09], [1]),
}
#m-Coumaricacid
HMCOUMARICACID = {
}
CMCOUMARICACID = {
1: ([172.55], [1]),
2: ([118.60], [1]),
3: ([153.69], [1]),
4: ([141.29], [1]),
5: ([121.09], [1]),
6: ([134.46], [1]),
7: ([121.09], [1]),
8: ([164.15], [1]),
9: ([122.36], [1]),
}
#Benzaldehyd
HBENZALDEHYD = {
}
CBENZALDEHYD= {
1: ([195.09], [1]),
2: ([141.47], [1]),
3: ([135.86], [2]),
4: ([133.45], [2]),
5: ([139.15], [1]),
}
#Caffeicacid
HCAFFEICACID = {
}
CCAFFEICACID = {
1: ([171.66], [1]),
2: ([116.16], [1]),
3: ([149.88], [1]),
4: ([131.94], [1]),
5: ([132.99], [1]),
6: ([118.15], [1]),
7: ([155.79], [1]),
8: ([149.28], [1]),
9: ([110.95], [1]),
}
#3-Hydroxybenzaldehyd
H3HYDROXYBENZALDEHYD = {
}
C3HYDROXYBENZALDEHYD = {
1: ([164.39], [1]),
2: ([122.95], [1]),
3: ([133.96], [1]),
4: ([123.33], [1]),
5: ([143.41], [1]),
6: ([195.16], [1]),
7: ([124.61], [1]),
}
#4-Hydroxybenzaldehyd
H4HYDROXYBENZALDEHYD = {
1: ([9.96], [1]), #s
2: ([7.85, 7.77], [2]), #d
3: ([6.72, 6.66], [2]), #d
4: ([4.75], [1]), #s
}
C4HYDROXYBENZALDEHYD = {
1: ([191.53], [1]),
2: ([135.08], [1]),
3: ([137.69], [2]),
4: ([118.46], [2]),
5: ([168.28], [1]),
}
#3,4-Dihydroxybenzaldehyd
H34DIHYDROXYBENZALDEHYD = {
}
C34DIHYDROXYBENZALDEHYD = {
1: ([158.08], [1]),
2: ([117.79], [1]),
3: ([133.40], [1]),
4: ([134.66], [1]),
5: ([191.41], [1]),
6: ([113.10], [1]),
7: ([149.32], [1]),
}
#Experimental p-Coumaricacid
HNMR1= {
1: ([12.13], [1]),
2: ([7.49], [2]),
3: ([6.79], [2]),
4: ([9.96], [1]),
5: ([6.29], [1]),
6: ([7.52], [1]),
}
CNMR1= {
1: ([125.36], [1]),
2: ([130.17], [2]),
3: ([115.83], [2]),
4: ([159.67], [1]),
5: ([168.05], [1]),
6: ([115.41], [1]),
7: ([144.27], [1]),
}
#Experimental 4-Hydroxybenzaldehyd
HNMR2 = {
1: ([7.05], [2]),
2: ([7.58], [2]),
3: ([8.44], [1]),
4: ([10.15], [1]),
}
#No Intesities mentioned
CNMR2 = {
1: ([116.5], [2]),
2: ([130.3], [1]),
3: ([132.7], [2]),
4: ([163.8], [1]),
5: ([191.0], [1]),
}
#Experimental Caffeicacid
HNMR3 = {
1: ([6.99], [1]), #d
2: ([6.84], [1]), #dd
3: ([6.73], [1]), #d
4: ([7.27], [1]), #d
5: ([6.28], [1]), #d
}
CNMR3 = {
1: ([147.9], [1]),
2: ([146.5], [1]),
3: ([129.3], [1]),
4: ([123.1], [1]),
5: ([121.7], [1]),
6: ([116.4], [1]),
7: ([141.5], [1]),
8: ([114.6], [1]),
9: ([176.2], [1]),
}
#Experimental 3,4-Dihydroxybenzaldehyd
HNMR4 = {
1: ([7.44], [1]),
2: ([7.42], [1]),
3: ([7.00], [1]),
}
CNMR4 = {
1: ([124.59], [1]),
2: ([115.21], [1]),
3: ([145.44], [1]),
4: ([151.27], [1]),
5: ([130.13], [1]),
6: ([114.22], [1]),
7: ([190.26], [1]),
}
def overlap(listref, listnew):
twoleft = np.sum(np.multiply(np.concatenate((listref, [0, 0])), np.concatenate(([0, 0], listnew))))
oneleft = np.sum(np.multiply(np.concatenate((listref, [0])), np.concatenate(([0], listnew))))
neutral = np.sum(np.multiply(listref,listnew))
oneright = np.sum(np.multiply(np.concatenate(([0], listref)), np.concatenate((listnew, [0]))))
tworight = np.sum(np.multiply(np.concatenate(([0, 0], listref)), np.concatenate((listnew, [0, 0]))))
overlap = (oneleft + oneright)* 0.5 + neutral
return overlap
def bin_array(spectra, highest_ppm, lowest_ppm, bin_width):
binnumber = math.ceil((highest_ppm - lowest_ppm)/bin_width)
bin = [0] * binnumber
for peak in spectra:
(shift, height) = spectra[peak]
binindex = math.floor((shift[0] - lowest_ppm) / bin_width)
bin[binindex] += height[0]
normalizedbin = np.divide(bin, np.sum(bin))
return normalizedbin
def define_border_values(spectraref, spectranew, bin_width):
shifts = []
for _,(shift,_) in spectraref.items():
shifts.append(shift[0])
for _,(shift,_) in spectranew.items():
shifts.append(shift[0])
highest_ppm = math.ceil(max(shifts)) + bin_width
lowest_ppm = math.floor(min(shifts)) - bin_width
#lowest_ppm = min(shifts) - bin_width/2 #Worse result. None of the previously wrong (except 0.6) become right
return (lowest_ppm, highest_ppm)
def similarity_nmr(spectraref, spectranew, bin_width):
#Maximize likelihood or minimize Deviation
#Values for two spectra and optimize largest for both different?
#Spectra in Nodes to allow maximize overlapp with both spectra or one spectra.
#5.4.2 Eliminating XH signals from 1H NMR spectra
lowest_ppm, highest_ppm = define_border_values(spectraref, spectranew, bin_width)
binref = bin_array(spectraref, highest_ppm, lowest_ppm, bin_width)
binnew = bin_array(spectranew, highest_ppm, lowest_ppm, bin_width)
crosscorr = overlap(binref, binnew)
refselfcorr = overlap(binref, binref)
newselfcorr = overlap(binnew, binnew)
simidx = crosscorr / math.sqrt(refselfcorr * newselfcorr)
return(simidx)
def correction(spectra, corretionppm):
newspectra = {}
for id, (shift, height) in spectra.items():
shiftvalue = shift[0]
adjustedshift = shiftvalue + corretionppm
newspectra[id] = ([adjustedshift], height)
return newspectra
def main():
spectrumref = CNMR2
#1H-NMR Spectra ignoriert, da meiste H sauer, da an N gebunden
#spectra = [HCINNAMICACID, HPCOUMARICACID, HMCOUMARICACID, HBENZALDEHYD, HCAFFEICACID, H3HYDROXYBENZALDEHYD, H4HYDROXYBENZALDEHYD, H34DIHYDROXYBENZALDEHYD]
spectra = [CCINNAMICACID, CPCOUMARICACID, CMCOUMARICACID, CBENZALDEHYD, CCAFFEICACID, C3HYDROXYBENZALDEHYD, C4HYDROXYBENZALDEHYD, C34DIHYDROXYBENZALDEHYD]
spectranames = ["CINNAMICACID", "PCOUMARICACID", "MCOUMARICACID", "BENZALDEHYD", "CAFFEICACID", "3HYDROXYBENZALDEHYD", "4HYDROXYBENZALDEHYD", "34DIHYDROXYBENZALDEHYD"]
likelihood = []
for spectrumtrue in spectra:
similaritybycorrection = []
#Paper Chemical reviews Carbons bound to Heavy atoms (TMS) to high -> this could be reason for too high values.
correctionvalues = [2.5] #np.arange(0, 1.51, 0.01) #for C tested np.arange(-0.37, 7.64, 0.1) 0, 2.63, 1 (for CNMR3), 1.5 (true for all ref, 8.37 + 1.5 for the precision), 1 (good for first, ok for second, third because only 7 better/equal but for first much higher) np.arange(-1.5, 1.49, 0.01) (only for first), for H 0 (not good), np.arange(-0.26, 0.34, 0.01), -0.06 for first, second never first either 17 or caf higher np.arange(-0.15, 0.16, 0.01) good measure
for correctionvalue in correctionvalues:
spectrumrefcorrected = correction(spectrumref, correctionvalue) #CCAFFEINE 11 (klappt hier sehr gut) CCAFFEINE2 12 CPARAXANTHINE 10 CNMR1 9, 10 o 11 (sehr gut) CNMR2 10 o 11
similaritylist = []
binwidthlist = np.arange(0.1, 3.9, 0.1)
for i in binwidthlist:
similaritylist.append(similarity_nmr(spectrumtrue, spectrumrefcorrected, i))
similaritymean = sum(similaritylist) / len(similaritylist)
similaritybycorrection.append(similaritymean)
likelihood.append(round(sum(similaritybycorrection)/len(similaritybycorrection), 2))
print(likelihood)
if __name__ == "__main__":
main()