diff --git a/mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs b/mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs index 62422cc96..06f848376 100644 --- a/mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs +++ b/mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs @@ -498,7 +498,7 @@ public byte[] Get64BitYarray() public byte[] Get64BitXarray() { - return Get64Bitarray(XArray); + return Get64Bitarray(XArray.Select(x => Math.Round(x, 4, MidpointRounding.AwayFromZero))); } public override string ToString() diff --git a/mzLib/Test/FileReadingTests/TestMsDataFileToResultsAdapter.cs b/mzLib/Test/FileReadingTests/TestMsDataFileToResultsAdapter.cs index 85b9425c3..e5dfe6ae3 100644 --- a/mzLib/Test/FileReadingTests/TestMsDataFileToResultsAdapter.cs +++ b/mzLib/Test/FileReadingTests/TestMsDataFileToResultsAdapter.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Linq; using MassSpectrometry; using MzLibUtil; using NUnit.Framework; @@ -135,7 +136,8 @@ public void TestWriting(string filePath, string outfile, int loop) Assert.That(readInScan.OneBasedScanNumber.Equals(writtenScan.OneBasedScanNumber)); Assert.That(readInScan.MsnOrder.Equals(writtenScan.MsnOrder)); Assert.That(readInScan.IsCentroid.Equals(writtenScan.IsCentroid)); - Assert.That(readInScan.MassSpectrum.Equals(writtenScan.MassSpectrum)); + Assert.That(readInScan.MassSpectrum.YArray, Is.EquivalentTo(writtenScan.MassSpectrum.YArray)); + Assert.That(readInScan.MassSpectrum.XArray, Is.EquivalentTo(writtenScan.MassSpectrum.XArray.Select(x => Math.Round(x, 4, MidpointRounding.AwayFromZero)))); } File.Delete(outfile); diff --git a/mzLib/Test/FileReadingTests/TestMzML.cs b/mzLib/Test/FileReadingTests/TestMzML.cs index 826efca77..a43dd13ed 100644 --- a/mzLib/Test/FileReadingTests/TestMzML.cs +++ b/mzLib/Test/FileReadingTests/TestMzML.cs @@ -741,7 +741,8 @@ public void WriteMzmlTest() Assert.AreEqual(2, reader.GetClosestOneBasedSpectrumNumber(2)); var newFirstValue = reader.GetOneBasedScan(1).MassSpectrum.FirstX; - Assert.AreEqual(oldFirstValue.Value, newFirstValue.Value, 1e-9); + Assert.AreNotEqual(oldFirstValue.Value, newFirstValue.Value); + Assert.AreEqual(Math.Round((double)oldFirstValue, 4, MidpointRounding.AwayFromZero), newFirstValue.Value, 1e-6); var secondScan2 = reader.GetOneBasedScan(2); @@ -1451,6 +1452,32 @@ public void MzmlFindPrecursorReferenceScan() Assert.AreEqual(1, fakeMzml1.GetAllScansList().ElementAt(3).OneBasedPrecursorScanNumber); } + [Test] + public void TestMzmlWriterRounding() + { + // This test ensure the CreateAndWriteMzml method only writes m/z values out to 4 decimal places of precision + MsDataScan[] scans = new MsDataScan[1]; + + double[] intensities0 = new double[] { 1, 1, 1, 1 }; + double[] mz0 = new double[] { 50.00004, 50.00005, 50.0004, 50.0005 }; + MzSpectrum massSpec0 = new MzSpectrum(mz0, intensities0, false); + scans[0] = new MsDataScan(massSpec0, 1, 1, true, Polarity.Positive, 1, new MzRange(1, 100), "f", MZAnalyzerType.Orbitrap, massSpec0.SumOfAllY, null, null, "1"); + + FakeMsDataFile fakeFile = new FakeMsDataFile(scans); + MzmlMethods.CreateAndWriteMyMzmlWithCalibratedSpectra(fakeFile, Path.Combine(TestContext.CurrentContext.TestDirectory, "what.mzML"), false); + var fakeMzml = + MsDataFileReader.GetDataFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "what.mzML")); + fakeMzml.LoadAllStaticData(); + + var readSpectrum = fakeMzml.Scans[0].MassSpectrum; + + // Ensure that the spectrum was rounded to the fourth decimal place on write + Assert.That(readSpectrum.XArray[0], Is.EqualTo(50).Within(0.000001)); + Assert.That(readSpectrum.XArray[1], Is.EqualTo(50.0001).Within(0.000001)); + Assert.That(readSpectrum.XArray[2], Is.EqualTo(50.0004).Within(0.000001)); + Assert.That(readSpectrum.XArray[3], Is.EqualTo(50.0005).Within(0.000001)); + } + [Test] [TestCase("tester.mzml")] [TestCase("SmallCalibratibleYeast.mzml")] diff --git a/mzLib/Test/FileReadingTests/TestRawFileReader.cs b/mzLib/Test/FileReadingTests/TestRawFileReader.cs index 0b67c1508..717804807 100644 --- a/mzLib/Test/FileReadingTests/TestRawFileReader.cs +++ b/mzLib/Test/FileReadingTests/TestRawFileReader.cs @@ -171,13 +171,13 @@ public static void TestPeakFilteringRawFileReader(string infile) for (int j = 0; j < mzmlScan.MassSpectrum.XArray.Length; j++) { - double roundedMzmlMz = Math.Round(mzmlScan.MassSpectrum.XArray[j], 2); - double roundedRawMz = Math.Round(rawScan.MassSpectrum.XArray[j], 2); + double roundedRawMz = Math.Round(rawScan.MassSpectrum.XArray[j], 4, MidpointRounding.AwayFromZero); - Assert.AreEqual(roundedMzmlMz, roundedRawMz); + // XArray is rounded to the 4th digit during CreateAndWrite + Assert.AreEqual(mzmlScan.MassSpectrum.XArray[j], roundedRawMz); - double roundedMzmlIntensity = Math.Round(mzmlScan.MassSpectrum.XArray[j], 0); - double roundedRawIntensity = Math.Round(rawScan.MassSpectrum.XArray[j], 0); + double roundedMzmlIntensity = Math.Round(mzmlScan.MassSpectrum.XArray[j], 0, MidpointRounding.AwayFromZero); + double roundedRawIntensity = Math.Round(rawScan.MassSpectrum.XArray[j], 0, MidpointRounding.AwayFromZero); Assert.AreEqual(roundedMzmlIntensity, roundedRawIntensity); } diff --git a/mzLib/TestFlashLFQ/TestFlashLFQ.cs b/mzLib/TestFlashLFQ/TestFlashLFQ.cs index ed72ef077..9def997ce 100644 --- a/mzLib/TestFlashLFQ/TestFlashLFQ.cs +++ b/mzLib/TestFlashLFQ/TestFlashLFQ.cs @@ -1493,7 +1493,7 @@ public static void ProteoformPeakfindingTest() Assert.That((int)results.PeptideModifiedSequences[sequence].GetIntensity(file1) == 1386491); ChromatographicPeak peak = results.Peaks[file1].First(p => p.Identifications.First().ModifiedSequence == sequence); - Assert.That(Math.Round(peak.MassError, 3) == 0); + Assert.That(Math.Round(peak.MassError, 3), Is.EqualTo(0.006).Within(0.0001)); Assert.That(peak.IsotopicEnvelopes.Count == 10); }