Skip to content

Commit

Permalink
Fixed Equals and GetHashCode method for MatchedFragmentIon
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Sol committed Jan 2, 2024
1 parent b378eb8 commit ac3b2b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 17 additions & 4 deletions mzLib/Omics/Fragmentation/MatchedFragmentIon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,32 @@ public override string ToString()
return NeutralTheoreticalProduct.ProductType + "" + NeutralTheoreticalProduct.FragmentNumber + "+" + Charge + "\t;" + NeutralTheoreticalProduct.NeutralMass;
}

// Doubles are accurate to within 15-17 significant digits. Rounding to 10 decimal places ensures accurate comparison up to 100,000 m/z (non-inclusive)
internal const int MzDecimalDigits = 10;
// Rounding to 6 decimal places ensures accurate comparison up to 1,000,000,000 AU (non-inclusive)
internal const int IntensityDecimalDigits = 6;

public override bool Equals(object obj)

Check warning on line 80 in mzLib/Omics/Fragmentation/MatchedFragmentIon.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).

Check warning on line 80 in mzLib/Omics/Fragmentation/MatchedFragmentIon.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).
{
MatchedFragmentIon other = (MatchedFragmentIon)obj;
return obj is MatchedFragmentIon otherIon && this.Equals(otherIon);
}


public bool Equals(MatchedFragmentIon other)
{
return this.NeutralTheoreticalProduct.Equals(other.NeutralTheoreticalProduct)
&& this.Charge == other.Charge
&& Math.Abs(Mz - other.Mz) < 0.0001
&& Math.Abs(Intensity - other.Intensity) < 0.0001;
&& Math.Round(Mz, MzDecimalDigits) - Math.Round(other.Mz, MzDecimalDigits) == 0
&& Math.Round(Intensity, IntensityDecimalDigits) - Math.Round(other.Intensity, IntensityDecimalDigits) == 0;
}

public override int GetHashCode()
{
return Mz.GetHashCode();
return HashCode.Combine(
NeutralTheoreticalProduct.GetHashCode(),
Charge.GetHashCode(),
Math.Round(Mz, MzDecimalDigits).GetHashCode(),
Math.Round(Intensity, IntensityDecimalDigits).GetHashCode());
}
}
}
9 changes: 6 additions & 3 deletions mzLib/Test/TestFragments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,16 +883,19 @@ public static void Test_ProductMonoisotopicMass()
public static void Test_MatchedFragmentGetHashCode()
{
Product P = new Product(ProductType.b, FragmentationTerminus.N, 1, 1, 1, 0);
Product pPrime = new Product(ProductType.b, FragmentationTerminus.N, 1, 1, 1, 0);
MatchedFragmentIon m = new MatchedFragmentIon(P, 1, 1, 1);
Assert.AreEqual(1072693248, m.GetHashCode());
MatchedFragmentIon mPrime = new MatchedFragmentIon(pPrime, 1, 1, 1);
Assert.AreEqual(P.GetHashCode(), pPrime.GetHashCode());
Assert.AreEqual(mPrime.GetHashCode(), m.GetHashCode());
}

[Test]
public static void TestMatchedFragmentIonEquals()
{
Product P = new Product(ProductType.b, FragmentationTerminus.N, 1, 1, 1, 0);
MatchedFragmentIon ion1 = new MatchedFragmentIon(P, experMz: 150, experIntensity: 99.99999, charge: 2);
MatchedFragmentIon ion2 = new MatchedFragmentIon(P, experMz: 149.99999, experIntensity: 100, charge: 2);
MatchedFragmentIon ion1 = new MatchedFragmentIon(P, experMz: 150, experIntensity: 99.99999999999, charge: 2);
MatchedFragmentIon ion2 = new MatchedFragmentIon(P, experMz: 149.99999999999, experIntensity: 100, charge: 2);
Assert.AreEqual(ion1, ion2);
}

Expand Down

0 comments on commit ac3b2b1

Please sign in to comment.