Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mzml writer now rounds to 4 decimal places #821

Merged
merged 5 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@

public byte[] Get64BitXarray()
{
return Get64Bitarray(XArray);
return Get64Bitarray(XArray.Select(x => Math.Round(x, 4, MidpointRounding.AwayFromZero)));
}

public override string ToString()
Expand Down Expand Up @@ -782,14 +782,14 @@
return numerator / denominator;
}

public bool Equals(MzSpectrum? other)

Check warning on line 785 in mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 785 in mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 785 in mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs

View workflow job for this annotation

GitHub Actions / integration

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return (XArray.SequenceEqual(other.XArray) && YArray.SequenceEqual(other.YArray));
}

public override bool Equals(object? spectrumToCompare)

Check warning on line 792 in mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 792 in mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 792 in mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs

View workflow job for this annotation

GitHub Actions / integration

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (ReferenceEquals(null, spectrumToCompare)) return false;
if (ReferenceEquals(this, spectrumToCompare)) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using MassSpectrometry;
using MzLibUtil;
using NUnit.Framework;
Expand Down Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use CollectionAssert.IsEqualsTo() to if you want an exact match of each element. It is also much faster

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);
Expand Down
29 changes: 28 additions & 1 deletion mzLib/Test/FileReadingTests/TestMzML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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")]
Expand Down
10 changes: 5 additions & 5 deletions mzLib/Test/FileReadingTests/TestRawFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion mzLib/TestFlashLFQ/TestFlashLFQ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading