Commit 8f9f70bd authored by mirandaa's avatar mirandaa
Browse files

feat: compare sim to ms and adjust

parent 1ee5c9e5
......@@ -189,6 +189,31 @@ def toCSVFile(fileBaseName, dictOrList):
def func(x, a, b):
return a*(x**-b)
def getFuncVariables(matchingSim):'curve fitting to generate projection')
from scipy.optimize import curve_fit
simMasses, simIntens, simHeads, msMasses, msIntens, msHeads = zip(*matchingSim)
sumSimIntens = sum(simIntens) # to normalize
simMsIntens = sum(msIntens)
simIntens_norm = [simInten / sumSimIntens for simInten in simIntens]
msIntens_norm = [msInten / simMsIntens for msInten in msIntens]
deviationFactors = [msIntens_norm[idx] / simIntens_norm[idx] for idx, _ in enumerate(msIntens_norm)]
popt, pconv = curve_fit(func, msMasses, deviationFactors)'function a*(x**-b) uses values: projection, %f, %f ', popt[0], popt[1])
return popt
def project_ms2sim(matchingSim, popt):
simMasses, simIntens, simHeads, msMasses, msIntens, msHeads = zip(*matchingSim)
correctionFactors = [func(simMass, *popt) for simMass in simMasses]
simIntens_Corrected = [simIntens[idx] * correctionFactors[idx] for idx, _ in enumerate(simIntens)]
return zip(simMasses, simIntens, simHeads, msMasses, msIntens, msHeads, simIntens_Corrected)
def simStitcher(filePath = None, doCompare = True):
......@@ -247,8 +272,8 @@ def simStitcher(filePath = None, doCompare = True):'\n'.join(scans_stitched.keys()))'writing to :'+outputFile(filePath))
write2templateMzXML(outputFile(filePath), scans_stitched)'writing to :'+outputStitchedFile(filePath))
write2templateMzXML(outputStitchedFile(filePath), scans_stitched)'finish stitching')
......@@ -269,17 +294,49 @@ def simStitcher(filePath = None, doCompare = True):'get matching ms peak')
matchingSim = getMatchingMS(sampleSimPeaks, scans_ms)
toCSVFile('matchingSim', matchingSim)'finished')
popt = getFuncVariables(matchingSim)
def outputFile(fileName):
projected = project_ms2sim(matchingSim, popt)
toCSVFile('matchingSim', projected)'finished comparing')'start sim intens adjustment')
for idx, scan in enumerate(scans_stitched):
(masses, intens) = scans_stitched[scan]
correctionFactors = [func(mass, *popt) for mass in masses]
intensAdjusted = [intens[idx] * correctionFactors[idx] for idx, _ in enumerate(intens)]
scans_adjusted[scan] = (masses, intensAdjusted)
toCSVFile('scans_stitched', scans_adjusted)'writing to :'+outputAdjustedFile(filePath))
write2templateMzXML(outputAdjustedFile(filePath), scans_adjusted)'finish adjustment')
def outputStitchedFile(fileName):
dir,file = os.path.split(fileName)
if not os.path.exists(dir+'\\stitched'):
newfilename = dir+'\\stitched\\'+file[:-6]+'-s'+file[-6:]
return newfilename
def outputAdjustedFile(fileName):
dir,file = os.path.split(fileName)
if not os.path.exists(dir+'\\adjusted'):
newfilename = dir+'\\adjusted\\'+file[:-6]+'-s'+file[-6:]
return newfilename
def getMZXMLEncondedScanRows(filePath):
return zip(*getMZXMLEncondedScans(filePath))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment