Skip to content

Commit

Permalink
Corrected some IsoDec issues. Not passing tests yet, but getting corr…
Browse files Browse the repository at this point in the history
…ect charge at least.
  • Loading branch information
jgpavek committed Aug 6, 2024
2 parents 6ef4447 + 64d4a4b commit 1a0bd54
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 145 deletions.

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions mzLib/Development/Development.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
</ItemGroup>

<ItemGroup>
<None Update="Deconvolution\TestData\Averaged_221110_CytoOnly.mzML">
<None Update="DeconvolutionDevelopment\TestData\Averaged_221110_CytoOnly.mzML">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Deconvolution\TestData\Averaged_221110_HGHOnly.mzML">
<None Update="DeconvolutionDevelopment\TestData\Averaged_221110_HGHOnly.mzML">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Deconvolution\TestData\Averaged_221110_UbiqOnly.mzML">
<None Update="DeconvolutionDevelopment\TestData\Averaged_221110_UbiqOnly.mzML">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
33 changes: 3 additions & 30 deletions mzLib/MassSpectrometry/Deconvolution/Algorithms/IsoDecAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,10 @@ namespace MassSpectrometry
public class IsoDecAlgorithm(DeconvolutionParameters deconParameters)
: DeconvolutionAlgorithm(deconParameters)
{
public static string _isoDecDllPath;
private static string _libmmDllPath;
private static string _phaseModelPath;
private static string _scmlDispMdDllPath;
static IsoDecAlgorithm()
{

_isoDecDllPath = Path.Combine(new[]
{
AppDomain.CurrentDomain.BaseDirectory, "MassSpectrometry", "Deconvolution", "Resources", "isodeclib.dll"
});

_libmmDllPath = Path.Combine(new[]
{
AppDomain.CurrentDomain.BaseDirectory, "MassSpectrometry", "Deconvolution", "Resources", "libmm.dll"
});

_phaseModelPath = Path.Combine(new[]
{
AppDomain.CurrentDomain.BaseDirectory, "MassSpectrometry", "Deconvolution", "Resources",
"phase_model.bin"
});

_scmlDispMdDllPath = Path.Combine(new[]
{
AppDomain.CurrentDomain.BaseDirectory, "MassSpectrometry", "Deconvolution", "Resources",
"scml_disp_md.dll"
});
_phaseModelPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Deconvolution", "Algorithms", "IsoDecResources", "phase_model.bin");
}

[StructLayout(LayoutKind.Sequential, Pack =1)]
Expand All @@ -69,12 +45,9 @@ public struct MatchedPeak
public int endindex;
}



[DllImport(@"C:\Python\UniDec3\unidec\IsoDec\src\isodec\x64\Release\isodeclib.dll", CallingConvention = CallingConvention.Cdecl)]
[DllImport("Deconvolution/Algorithms/IsoDecResources/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);
Expand All @@ -88,7 +61,7 @@ public override IEnumerable<IsotopicEnvelope> Deconvolute(MzSpectrum spectrum, M
.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);
int result = process_spectrum(mzs, intensities, intensities.Length, _phaseModelPath , matchedPeaksPtr);
if(result > 0)
{
MatchedPeak[] matchedpeaks = new MatchedPeak[result];
Expand Down
37 changes: 23 additions & 14 deletions mzLib/MassSpectrometry/MassSpectrometry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>



<ItemGroup>
<PackageReference Include="CsvHelper" Version="32.0.3" />
Expand All @@ -20,19 +22,26 @@
<ProjectReference Include="..\MzLibUtil\MzLibUtil.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="isodeclib.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Deconvolution\Resources\libmmd.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Deconvolution\Resources\phase_model.bin">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Deconvolution\Resources\svml_dispmd.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Remove="Deconvolution\Algorithms\isodeclib.dll" />
<None Remove="Deconvolution\Algorithms\libmmd.dll" />
<None Remove="Deconvolution\Algorithms\phase_model.bin" />
<None Remove="Deconvolution\Algorithms\svml_dispmd.dll" />
</ItemGroup>

<ItemGroup>
<Content Include="Deconvolution\Algorithms\IsoDecResources\isodeclib.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Deconvolution\Algorithms\IsoDecResources\libmmd.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Deconvolution\Algorithms\IsoDecResources\phase_model.bin">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Deconvolution\Algorithms\IsoDecResources\svml_dispmd.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Binary file removed mzLib/MassSpectrometry/isodeclib.dll
Binary file not shown.
75 changes: 52 additions & 23 deletions mzLib/Test/TestDeconvolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void TestDeconvolutionProteoformMultiChargeState(double selectedIonMz, in

//The primary monoisotopic mass should be the same regardless of which peak in which charge state was selected for isolation.
//this case is interesting because other monoisotopic mass may have a sodium adduct. The unit test could be expanded to consider this.
Assert.That(monoIsotopicMasses[0], Is.EqualTo(14037.926829).Within(.0005));
NUnit.Framework.Assert.That(monoIsotopicMasses[0], Is.EqualTo(14037.926829).Within(.0005));
}

[Test]
Expand Down Expand Up @@ -94,7 +94,7 @@ public static void CheckGetMostAbundantObservedIsotopicMass(string peptide, stri
//check assigned correctly
List<IsotopicEnvelope> lie2 = singlespec.Deconvolute(singleRange, minAssumedChargeState, maxAssumedChargeState, deconvolutionTolerancePpm, intensityRatioLimit).ToList();
List<IsotopicEnvelope> lie2_charge = lie2.Where(p => p.Charge == charge).ToList();
Assert.That(lie2_charge[0].MostAbundantObservedIsotopicMass / charge, Is.EqualTo(m).Within(0.1));
NUnit.Framework.Assert.That(lie2_charge[0].MostAbundantObservedIsotopicMass / charge, Is.EqualTo(m).Within(0.1));

//check that if already assigned, skips assignment and just recalls same value
List<IsotopicEnvelope> lie3 = singlespec.Deconvolute(singleRange, minAssumedChargeState, maxAssumedChargeState, deconvolutionTolerancePpm, intensityRatioLimit).ToList();
Expand Down Expand Up @@ -149,8 +149,8 @@ public void TestClassicDeconvolutionProteoformMultiChargeState(double selectedIo

//The primary monoisotopic mass should be the same regardless of which peak in which charge state was selected for isolation.
//this case is interesting because other monoisotopic mass may have a sodium adduct. The unit test could be expanded to consider this.
Assert.That(monoIsotopicMasses[0], Is.EqualTo(14037.926829).Within(.0005));
Assert.That(monoIsotopicMasses2[0], Is.EqualTo(14037.926829).Within(.0005));
NUnit.Framework.Assert.That(monoIsotopicMasses[0], Is.EqualTo(14037.926829).Within(.0005));
NUnit.Framework.Assert.That(monoIsotopicMasses2[0], Is.EqualTo(14037.926829).Within(.0005));
}

[Test]
Expand Down Expand Up @@ -184,13 +184,42 @@ public static void CheckClassicGetMostAbundantObservedIsotopicMass(string peptid

List<IsotopicEnvelope> lie2 = Deconvoluter.Deconvolute(singlespec, deconParameters, singleRange).ToList();
List<IsotopicEnvelope> lie2_charge = lie2.Where(p => p.Charge == charge).ToList();
Assert.That(lie2_charge[0].MostAbundantObservedIsotopicMass / charge, Is.EqualTo(m).Within(0.1));
NUnit.Framework.Assert.That(lie2_charge[0].MostAbundantObservedIsotopicMass / charge, Is.EqualTo(m).Within(0.1));

//check that if already assigned, skips assignment and just recalls same value
List<IsotopicEnvelope> lie3 = Deconvoluter.Deconvolute(singlespec, deconParameters, singleRange).ToList();
Assert.AreEqual(lie2.Select(p => p.MostAbundantObservedIsotopicMass), lie3.Select(p => p.MostAbundantObservedIsotopicMass));
}

[Test]
[TestCase(373.85, -5, 1874.28)] // GUAGUC -5
[TestCase(467.57, -4, 1874.28)] // GUAGUC -4
[TestCase(623.75, -3, 1874.28)] // GUAGUC -3
[TestCase(936.13, -2, 1874.28)] // GUAGUC -2
[TestCase(473.05, -4, 1896.26)] // GUAGUC +Na -H -4
[TestCase(631.07, -3, 1896.26)] // GUAGUC +Na -H -3
[TestCase(947.121, -2, 1896.26)] // GUAGUC +Na -H -2
public void TestNegativeModeClassicDeconvolution(double expectedMz, int expectedCharge, double expectedMonoMass)
{
// get scan
string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "DataFiles",
"GUACUG_NegativeMode_Sliced.mzML");
var scan = MsDataFileReader.GetDataFile(filePath).GetAllScansList().First();
var tolerance = new PpmTolerance(20);

// set up deconvolution
DeconvolutionParameters deconParams = new ClassicDeconvolutionParameters(-10, -1, 20, 3, Polarity.Negative);

List<IsotopicEnvelope> deconvolutionResults = Deconvoluter.Deconvolute(scan, deconParams).ToList();
// ensure each expected result is found, with correct mz, charge, and monoisotopic mass
var resultsWithPeakOfInterest = deconvolutionResults.FirstOrDefault(envelope =>
envelope.Peaks.Any(peak => tolerance.Within(peak.mz, expectedMz)));
if (resultsWithPeakOfInterest is null) NUnit.Framework.Assert.Fail();

NUnit.Framework.Assert.That(expectedMonoMass, Is.EqualTo(resultsWithPeakOfInterest.MonoisotopicMass).Within(0.01));
NUnit.Framework.Assert.That(expectedCharge, Is.EqualTo(resultsWithPeakOfInterest.Charge));
}

#endregion

#region IsoDec Deconvolution
Expand Down Expand Up @@ -242,8 +271,8 @@ public void TestIsoDecDeconvolutionProteoformMultiChargeState(double selectedIon

//The primary monoisotopic mass should be the same regardless of which peak in which charge state was selected for isolation.
//this case is interesting because other monoisotopic mass may have a sodium adduct. The unit test could be expanded to consider this.
Assert.That(monoIsotopicMasses[0], Is.EqualTo(14037.926829).Within(.0005));
Assert.That(monoIsotopicMasses2[0], Is.EqualTo(14037.926829).Within(.0005));
NUnit.Framework.Assert.That(monoIsotopicMasses[0], Is.EqualTo(14037.926829).Within(.0005));
NUnit.Framework.Assert.That(monoIsotopicMasses2[0], Is.EqualTo(14037.926829).Within(.0005));
}

[Test]
Expand All @@ -269,15 +298,13 @@ public static void CheckIsoDecGetMostAbundantObservedIsotopicMass(string peptide
//check assigned correctly
List<IsotopicEnvelope> lie2 = Deconvoluter.Deconvolute(singlespec, deconParameters, singleRange).ToList();
List<IsotopicEnvelope> lie2_charge = lie2.Where(p => p.Charge == charge).ToList();
Assert.That(lie2_charge[0].MostAbundantObservedIsotopicMass / charge, Is.EqualTo(m).Within(0.1));
NUnit.Framework.Assert.That(lie2_charge[0].MostAbundantObservedIsotopicMass / charge, Is.EqualTo(m).Within(0.1));

//check that if already assigned, skips assignment and just recalls same value
List<IsotopicEnvelope> lie3 = Deconvoluter.Deconvolute(singlespec, deconParameters, singleRange).ToList();
Assert.AreEqual(lie2.Select(p => p.MostAbundantObservedIsotopicMass), lie3.Select(p => p.MostAbundantObservedIsotopicMass));
}

#endregion

[Test]
[TestCase(373.85, -5, 1874.28)] // GUAGUC -5
[TestCase(467.57, -4, 1874.28)] // GUAGUC -4
Expand All @@ -286,7 +313,7 @@ public static void CheckIsoDecGetMostAbundantObservedIsotopicMass(string peptide
[TestCase(473.05, -4, 1896.26)] // GUAGUC +Na -H -4
[TestCase(631.07, -3, 1896.26)] // GUAGUC +Na -H -3
[TestCase(947.121, -2, 1896.26)] // GUAGUC +Na -H -2
public void TestNegativeModeClassicDeconvolution(double expectedMz, int expectedCharge, double expectedMonoMass)
public void TestNegativeModeIsoDecDeconvolution(double expectedMz, int expectedCharge, double expectedMonoMass)
{
// get scan
string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "DataFiles",
Expand All @@ -295,18 +322,20 @@ public void TestNegativeModeClassicDeconvolution(double expectedMz, int expected
var tolerance = new PpmTolerance(20);

// set up deconvolution
DeconvolutionParameters deconParams = new ClassicDeconvolutionParameters(-10, -1, 20, 3, Polarity.Negative);
DeconvolutionParameters deconParams = new IsoDecDeconvolutionParameters(Polarity.Negative);

List<IsotopicEnvelope> deconvolutionResults = Deconvoluter.Deconvolute(scan, deconParams).ToList();
// ensure each expected result is found, with correct mz, charge, and monoisotopic mass
var resultsWithPeakOfInterest = deconvolutionResults.FirstOrDefault(envelope =>
envelope.Peaks.Any(peak => tolerance.Within(peak.mz, expectedMz)));
if (resultsWithPeakOfInterest is null) Assert.Fail();
if (resultsWithPeakOfInterest is null) NUnit.Framework.Assert.Fail();

Assert.That(tolerance.Within(expectedMonoMass, resultsWithPeakOfInterest.MonoisotopicMass));
Assert.That(expectedCharge, Is.EqualTo(resultsWithPeakOfInterest.Charge));
NUnit.Framework.Assert.That(expectedMonoMass, Is.EqualTo(resultsWithPeakOfInterest.MonoisotopicMass).Within(0.01));
NUnit.Framework.Assert.That(expectedCharge, Is.EqualTo(resultsWithPeakOfInterest.Charge));
}

#endregion

[Test]
public static void TestExampleNewDeconvolutionInDeconvoluter()
{
Expand All @@ -318,14 +347,14 @@ public static void TestExampleNewDeconvolutionInDeconvoluter()
dataFile.CloseDynamicConnection();

// test switch statements in Deconvoluter
Assert.Throws<NotImplementedException>(() => Deconvoluter.Deconvolute(spectrum, deconParams));
Assert.Throws<NotImplementedException>(() => Deconvoluter.Deconvolute(scan, deconParams));
NUnit.Framework.Assert.Throws<NotImplementedException>(() => Deconvoluter.Deconvolute(spectrum, deconParams));
NUnit.Framework.Assert.Throws<NotImplementedException>(() => Deconvoluter.Deconvolute(scan, deconParams));

// test default exceptions in deconvoluter
var badEnumValue = (DeconvolutionType)Int32.MaxValue;
deconParams.GetType().GetProperty("DeconvolutionType")!.SetValue(deconParams, badEnumValue);
Assert.Throws<MzLibException>(() => Deconvoluter.Deconvolute(spectrum, deconParams));
Assert.Throws<MzLibException>(() => Deconvoluter.Deconvolute(scan, deconParams));
NUnit.Framework.Assert.Throws<MzLibException>(() => Deconvoluter.Deconvolute(spectrum, deconParams));
NUnit.Framework.Assert.Throws<MzLibException>(() => Deconvoluter.Deconvolute(scan, deconParams));
}


Expand All @@ -343,15 +372,15 @@ public static void Test_MsDataScan_GetIsolatedMassesAndCharges()

// get isolated masses and charges on an MS1 scan. This means the isolation window is null.
var ms1Result = precursorScan.GetIsolatedMassesAndCharges(precursorScan.MassSpectrum, deconParams).ToList();
Assert.That(ms1Result.Count, Is.EqualTo(0));
NUnit.Framework.Assert.That(ms1Result.Count, Is.EqualTo(0));
ms1Result = precursorScan.GetIsolatedMassesAndCharges(precursorScan, deconParams).ToList();
Assert.That(ms1Result.Count, Is.EqualTo(0));
NUnit.Framework.Assert.That(ms1Result.Count, Is.EqualTo(0));

// get isolated masses and charges on an MS2 scan. This should work correctly
var ms2Result = fragmentationScan.GetIsolatedMassesAndCharges(precursorScan.MassSpectrum, deconParams).ToList();
Assert.That(ms2Result.Count, Is.EqualTo(1));
NUnit.Framework.Assert.That(ms2Result.Count, Is.EqualTo(1));
ms2Result = fragmentationScan.GetIsolatedMassesAndCharges(precursorScan, deconParams).ToList();
Assert.That(ms2Result.Count, Is.EqualTo(1));
NUnit.Framework.Assert.That(ms2Result.Count, Is.EqualTo(1));
}
}
}
8 changes: 8 additions & 0 deletions mzLib/mzLib.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<file src="Chemistry\bin\x64\Release\net8.0\Chemistry.dll" target="lib\net8.0" />
<file src="FlashLFQ\bin\x64\Release\net8.0\FlashLFQ.dll" target="lib\net8.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\MassSpectrometry.dll" target="lib\net8.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\Deconvolution\Algorithms\IsoDecResources\isodeclib.dll" target="lib\net8.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\Deconvolution\Algorithms\IsoDecResources\libmmd.dll" target="lib\net8.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\Deconvolution\Algorithms\IsoDecResources\svml_dispmd.dll.dll" target="lib\net8.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\Deconvolution\Algorithms\IsoDecResources\phase_mode.bin" target="lib\net8.0" />
<file src="MzIdentML\bin\x64\Release\net8.0\MzIdentML.dll" target="lib\net8.0" />
<file src="SpectralAveraging\bin\x64\Release\net8.0\SpectralAveraging.dll" target="lib\net8.0" />
<file src="MzLibUtil\bin\x64\Release\net8.0\MzLibUtil.dll" target="lib\net8.0" />
Expand All @@ -64,6 +68,10 @@
<file src="Chemistry\bin\x64\Release\net8.0\Chemistry.dll" target="lib\net8.0-windows7.0" />
<file src="FlashLFQ\bin\x64\Release\net8.0\FlashLFQ.dll" target="lib\net8.0-windows7.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\MassSpectrometry.dll" target="lib\net8.0-windows7.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\Deconvolution\Algorithms\IsoDecResources\isodeclib.dll" target="lib\net8.0-windows7.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\Deconvolution\Algorithms\IsoDecResources\libmmd.dll" target="lib\net8.0-windows7.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\Deconvolution\Algorithms\IsoDecResources\svml_dispmd.dll.dll" target="lib\net8.0-windows7.0" />
<file src="MassSpectrometry\bin\x64\Release\net8.0\Deconvolution\Algorithms\IsoDecResources\phase_mode.bin" target="lib\net8.0-windows7.0" />
<file src="MzIdentML\bin\x64\Release\net8.0\MzIdentML.dll" target="lib\net8.0-windows7.0" />
<file src="SpectralAveraging\bin\x64\Release\net8.0\SpectralAveraging.dll" target="lib\net8.0-windows7.0" />
<file src="MzLibUtil\bin\x64\Release\net8.0\MzLibUtil.dll" target="lib\net8.0-windows7.0" />
Expand Down

0 comments on commit 1a0bd54

Please sign in to comment.