From b626f414996daa18d451af2fce2feab71f2b2873 Mon Sep 17 00:00:00 2001 From: jgpavek Date: Mon, 5 Aug 2024 13:44:15 -0700 Subject: [PATCH] IsoDec incorporated! --- .../Algorithms/IsoDecAlgorithm.cs | 96 +++++++++++++++++++ .../JohnnyDeconvolutionAlgorithm.cs | 42 -------- .../Deconvolution/Deconvoluter.cs | 2 +- .../Deconvolution/DeconvolutionType.cs | 2 +- ...rs.cs => IsoDecDeconvolutionParameters.cs} | 6 +- 5 files changed, 101 insertions(+), 47 deletions(-) create mode 100644 mzLib/MassSpectrometry/Deconvolution/Algorithms/IsoDecAlgorithm.cs delete mode 100644 mzLib/MassSpectrometry/Deconvolution/Algorithms/JohnnyDeconvolutionAlgorithm.cs rename mzLib/MassSpectrometry/Deconvolution/Parameters/{JohnnyDeconvolutionParameters.cs => IsoDecDeconvolutionParameters.cs} (65%) diff --git a/mzLib/MassSpectrometry/Deconvolution/Algorithms/IsoDecAlgorithm.cs b/mzLib/MassSpectrometry/Deconvolution/Algorithms/IsoDecAlgorithm.cs new file mode 100644 index 00000000..c9530deb --- /dev/null +++ b/mzLib/MassSpectrometry/Deconvolution/Algorithms/IsoDecAlgorithm.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using MassSpectrometry.MzSpectra; +using MzLibUtil; +using MathNet.Numerics.Statistics; + +namespace MassSpectrometry +{ + public class IsoDecAlgorithm(DeconvolutionParameters deconParameters) + : DeconvolutionAlgorithm(deconParameters) + { + [StructLayout(LayoutKind.Sequential, Pack =1)] + public struct MatchedPeak + { + public float mz; + public int z; + public float monoiso; + public float peakmass; + public float avgmass; + public float area; + public float peakint; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] + public int[] matchedindsiso; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] + public int[] matchedindsexp; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] + public float[] isomz; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] + public float[] isodist; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] + public float[] isomass; + public int startindex; + public int endindex; + } + + + [DllImport(@"C:\Python\UniDec3\unidec\IsoDec\src\isodec\x64\Release\isodeclib.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern int process_spectrum(float[] mz, float[] intensity, int len, string modelpath, IntPtr matchedpeaks); + + + public override IEnumerable Deconvolute(MzSpectrum spectrum, MzRange range) + { + var firstIndex = spectrum.GetClosestPeakIndex(range.Minimum); + var lastIndex = spectrum.GetClosestPeakIndex(range.Maximum); + + var mzs = spectrum.XArray[firstIndex..lastIndex] + .Select(p => (float)p) + .ToArray(); + var intensities = spectrum.YArray[firstIndex..lastIndex] + .Select(p => (float)p) + .ToArray(); + + IntPtr matchedPeaksPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MatchedPeak)) * intensities.Length); + + int result = process_spectrum(mzs, intensities, intensities.Length, @"C:\Python\UniDec3\unidec\IsoDec\phase_model.bin", matchedPeaksPtr); + if(result > 0) + { + MatchedPeak[] matchedpeaks = new MatchedPeak[result]; + for(int i = 0;i(matchedPeaksPtr + i * Marshal.SizeOf(typeof(MatchedPeak))); + } + return ConvertToIsotopicEnvelopes(matchedpeaks, mzs, intensities); + } + + else return new List(); + } + + private List ConvertToIsotopicEnvelopes(MatchedPeak[] matchedpeaks, float[] mzs, float[] intensities) + { + List result = new List(); + foreach(MatchedPeak peak in matchedpeaks) + { + List<(double,double)> peaks = new List<(double,double)> (); + List listofratios = new List(); + for(int i = 0;i 0 && peak.matchedindsexp[i] == 0) + break; + else + { + listofratios.Add(peak.isodist[i] / intensities[peak.startindex + i]); + peaks.Add((mzs[peak.startindex + i], intensities[peak.startindex + i])); + } + } + result.Add(new IsotopicEnvelope(peaks, peak.monoiso, peak.z, peak.peakint, Statistics.StandardDeviation(listofratios), 0)); + } + return result; + } + } +} diff --git a/mzLib/MassSpectrometry/Deconvolution/Algorithms/JohnnyDeconvolutionAlgorithm.cs b/mzLib/MassSpectrometry/Deconvolution/Algorithms/JohnnyDeconvolutionAlgorithm.cs deleted file mode 100644 index 11dcb84b..00000000 --- a/mzLib/MassSpectrometry/Deconvolution/Algorithms/JohnnyDeconvolutionAlgorithm.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using MzLibUtil; - -namespace MassSpectrometry -{ - public class JohnnyDeconvolutionAlgorithm(DeconvolutionParameters deconParameters) - : DeconvolutionAlgorithm(deconParameters) - { - public override IEnumerable Deconvolute(MzSpectrum spectrum, MzRange range) - { - var firstIndex = spectrum.GetClosestPeakIndex(range.Minimum); - var lastIndex = spectrum.GetClosestPeakIndex(range.Maximum); - - var mzs = spectrum.XArray[firstIndex..lastIndex] - .Select(p => (float)p) - .ToArray(); - var intensities = spectrum.YArray[firstIndex..lastIndex] - .Select(p => (float)p) - .ToArray(); - - foreach (var johnnyDeconObjectType in ReplaceMeWithDllCall()) - { - // pull johnny decon object type info out - - IsotopicEnvelope isotopicEnvelope = null; - yield return isotopicEnvelope; - } - } - - private IEnumerable ReplaceMeWithDllCall() - { - for (int i = 0; i < 10; i++) - { - yield return new object(); - } - } - } -} diff --git a/mzLib/MassSpectrometry/Deconvolution/Deconvoluter.cs b/mzLib/MassSpectrometry/Deconvolution/Deconvoluter.cs index 3799c2d8..3e28b1cf 100644 --- a/mzLib/MassSpectrometry/Deconvolution/Deconvoluter.cs +++ b/mzLib/MassSpectrometry/Deconvolution/Deconvoluter.cs @@ -65,7 +65,7 @@ public static IEnumerable Deconvolute(MzSpectrum spectrum, { DeconvolutionType.ClassicDeconvolution => new ClassicDeconvolutionAlgorithm(deconvolutionParameters), DeconvolutionType.ExampleNewDeconvolutionTemplate => new ExampleNewDeconvolutionAlgorithmTemplate(deconvolutionParameters), - DeconvolutionType.JohnnyDeconvolution => new JohnnyDeconvolutionAlgorithm(deconvolutionParameters), + DeconvolutionType.IsoDecDeconvolution => new IsoDecAlgorithm(deconvolutionParameters), _ => throw new MzLibException("DeconvolutionType not yet supported") }; diff --git a/mzLib/MassSpectrometry/Deconvolution/DeconvolutionType.cs b/mzLib/MassSpectrometry/Deconvolution/DeconvolutionType.cs index ed208bc4..f2fece52 100644 --- a/mzLib/MassSpectrometry/Deconvolution/DeconvolutionType.cs +++ b/mzLib/MassSpectrometry/Deconvolution/DeconvolutionType.cs @@ -10,6 +10,6 @@ public enum DeconvolutionType { ClassicDeconvolution, ExampleNewDeconvolutionTemplate, - JohnnyDeconvolution, + IsoDecDeconvolution, } } diff --git a/mzLib/MassSpectrometry/Deconvolution/Parameters/JohnnyDeconvolutionParameters.cs b/mzLib/MassSpectrometry/Deconvolution/Parameters/IsoDecDeconvolutionParameters.cs similarity index 65% rename from mzLib/MassSpectrometry/Deconvolution/Parameters/JohnnyDeconvolutionParameters.cs rename to mzLib/MassSpectrometry/Deconvolution/Parameters/IsoDecDeconvolutionParameters.cs index 22254863..787cc6f5 100644 --- a/mzLib/MassSpectrometry/Deconvolution/Parameters/JohnnyDeconvolutionParameters.cs +++ b/mzLib/MassSpectrometry/Deconvolution/Parameters/IsoDecDeconvolutionParameters.cs @@ -6,12 +6,12 @@ namespace MassSpectrometry { - public class JohnnyDeconvolutionParameters : DeconvolutionParameters + public class IsoDecDeconvolutionParameters : DeconvolutionParameters { public override DeconvolutionType DeconvolutionType { get; protected set; } = - DeconvolutionType.JohnnyDeconvolution; + DeconvolutionType.IsoDecDeconvolution; - public JohnnyDeconvolutionParameters(Polarity polarity = Polarity.Positive) + public IsoDecDeconvolutionParameters(Polarity polarity = Polarity.Positive) : base(1, 100, polarity) {