Skip to content

Commit

Permalink
IsoDec incorporated!
Browse files Browse the repository at this point in the history
  • Loading branch information
jgpavek committed Aug 5, 2024
1 parent 6720fc5 commit b626f41
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 47 deletions.
96 changes: 96 additions & 0 deletions mzLib/MassSpectrometry/Deconvolution/Algorithms/IsoDecAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -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<IsotopicEnvelope> 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<result;i++)
{
matchedpeaks[i] = Marshal.PtrToStructure<MatchedPeak>(matchedPeaksPtr + i * Marshal.SizeOf(typeof(MatchedPeak)));
}
return ConvertToIsotopicEnvelopes(matchedpeaks, mzs, intensities);
}

else return new List<IsotopicEnvelope>();
}

private List<IsotopicEnvelope> ConvertToIsotopicEnvelopes(MatchedPeak[] matchedpeaks, float[] mzs, float[] intensities)
{
List<IsotopicEnvelope> result = new List<IsotopicEnvelope>();
foreach(MatchedPeak peak in matchedpeaks)
{
List<(double,double)> peaks = new List<(double,double)> ();
List<double> listofratios = new List<double>();
for(int i = 0;i<peak.matchedindsexp.Length;i++)
{
if (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;
}
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion mzLib/MassSpectrometry/Deconvolution/Deconvoluter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static IEnumerable<IsotopicEnvelope> 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")
};

Expand Down
2 changes: 1 addition & 1 deletion mzLib/MassSpectrometry/Deconvolution/DeconvolutionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public enum DeconvolutionType
{
ClassicDeconvolution,
ExampleNewDeconvolutionTemplate,
JohnnyDeconvolution,
IsoDecDeconvolution,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

Expand Down

0 comments on commit b626f41

Please sign in to comment.