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

Mz spectrum weighted merge within mz tolerance #752

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a6b1639
correct Within calculation
Nov 18, 2021
fa4da8b
update unit tests
Nov 18, 2021
3246567
conflicts resolved back to upstream
Feb 4, 2022
a018d4d
Merge remote-tracking branch 'upstream/master'
Feb 15, 2022
15a37d0
Merge remote-tracking branch 'upstream/master'
Feb 17, 2022
892fa45
this is the spot
Feb 18, 2022
211013c
Merge remote-tracking branch 'upstream/master'
Feb 25, 2022
68104ee
Merge branch 'master' of https://github.com/trishorts/mzLib
trishorts Mar 9, 2022
d715a08
Merge remote-tracking branch 'upstream/master'
Mar 16, 2022
3565522
Merge remote-tracking branch 'upstream/master'
Mar 23, 2022
72e7b53
Merge remote-tracking branch 'upstream/master'
Mar 29, 2022
593872a
Merge remote-tracking branch 'upstream/master'
trishorts Apr 13, 2022
42dd034
Merge branch 'master' of https://github.com/trishorts/mzLib
trishorts Apr 13, 2022
fbeaec0
Merge remote-tracking branch 'upstream/master'
trishorts Jun 1, 2022
614ded7
Merge remote-tracking branch 'upstream/master'
Jun 14, 2022
47307c8
Merge branch 'master' of https://github.com/trishorts/mzLib
Jun 14, 2022
28e05ae
Merge remote-tracking branch 'upstream/master'
Jul 6, 2022
0a7c609
Merge remote-tracking branch 'upstream/master'
Jul 26, 2022
630d8c7
Merge remote-tracking branch 'upstream/master'
trishorts Jul 27, 2022
f6a386b
Merge branch 'master' of https://github.com/trishorts/mzLib
trishorts Jul 27, 2022
d673800
Merge remote-tracking branch 'upstream/master'
Sep 11, 2022
675a0ae
Merge branch 'master' of https://github.com/trishorts/mzLib
Sep 11, 2022
15d4baf
Merge remote-tracking branch 'upstream/master'
Sep 27, 2022
03ca9f7
Merge remote-tracking branch 'upstream/master'
Oct 4, 2022
d0a4c79
Merge remote-tracking branch 'upstream/master'
Jan 30, 2023
894b998
Merge remote-tracking branch 'upstream/master'
Mar 15, 2023
88269a1
Merge remote-tracking branch 'upstream/master'
trishorts Apr 24, 2023
9a9b24a
Merge remote-tracking branch 'upstream/master'
trishorts Jun 29, 2023
b4ad231
add space
trishorts Jun 29, 2023
bc59b38
Merge remote-tracking branch 'upstream/master'
trishorts Oct 10, 2023
2981dc5
Merge remote-tracking branch 'upstream/master'
trishorts Dec 12, 2023
df3581c
first pass
trishorts Dec 28, 2023
2b1e91e
works
trishorts Dec 29, 2023
4fe1016
peak merge and sorted array merge
trishorts Jan 2, 2024
431e9f0
yo
trishorts Jan 3, 2024
203a5e4
yoyo
trishorts Jan 3, 2024
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
102 changes: 102 additions & 0 deletions mzLib/MassSpectrometry/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MzLibUtil;

namespace MassSpectrometry
{
public class ExtensionMethods
{
public static void MergeMzAndIntensityValuesForAnyMzsWithinTolerance(ref double[] mzArray, ref double[] intensityArray, PpmTolerance ppmTolerance)
{
List<double> newMzArray = new List<double>();
List<double> newIntensityArray = new List<double>();

List<int> indiciesToMerge = new List<int>();
for (int i = 0; i < mzArray.Length; i++)
{
if (i < (mzArray.Length - 1) && ppmTolerance.Within(mzArray[i], mzArray[i + 1])) // two mzValues are close enough to merge
{
indiciesToMerge.Add(i);
indiciesToMerge.Add(i + 1);
}
else // mzValues are NOT close enough to merge so we merge any that we've collected so far.
{
if (indiciesToMerge.Any())
{
newMzArray.Add(GetWeightedMz(indiciesToMerge.DistinctBy(i => i).ToList(), mzArray.ToList(), intensityArray.ToList()));
newIntensityArray.Add(GetIntensityAverage(indiciesToMerge, mzArray, intensityArray));
indiciesToMerge.Clear(); //we clear this array because we have merged a contiguous group and can begin again with the next group
}
else
{
newMzArray.Add(mzArray[i]);
newIntensityArray.Add(intensityArray[i]);
}
}
}

mzArray = newMzArray.ToArray();
intensityArray = newIntensityArray.ToArray();
}
private static double GetIntensityAverage(List<int> indiciesToMerge, double[] mzArray, double[] intensityArray)
{
double intensitySum = 0;
foreach (var index in indiciesToMerge)
{
intensitySum += intensityArray[index];
}
return (intensitySum / (double)indiciesToMerge.Count);
}

private static double GetWeightedMz(List<int> indiciesToMerge, List<double> mzArray, List<double> intensityArray)
{
double weightedMz = 0;
double intensitySum = 0;
foreach (var index in indiciesToMerge)
{
weightedMz += mzArray[index] * intensityArray[index];
intensitySum += intensityArray[index];
}
return (weightedMz / intensitySum);
}

public static double[] MergeTwoSortedArraysIntoSortedArray(double[] sortedArrayOne, double[] sortedArrayTwo)
{
double[] mergedSortedArray = new double[sortedArrayOne.Length + sortedArrayTwo.Length];
int oneIndex = 0;
int twoIndex = 0;

for (int i = 0; i < mergedSortedArray.Length; i++)
{
if (oneIndex<sortedArrayOne.Length && sortedArrayOne[oneIndex] < sortedArrayTwo[twoIndex])
{
mergedSortedArray[i] = sortedArrayOne[oneIndex];
oneIndex++;
}
else
{
if (twoIndex < sortedArrayTwo.Length)
{
mergedSortedArray[i] = sortedArrayTwo[twoIndex];
twoIndex++;
}
}
}
return mergedSortedArray;
}

public (T[], U[]) MergeSortedArrays<T, U>(T[][] arraysToBeSorted, U[][] valuesToBePreserved) where T : IComparable
{
int[] minPointerArray = new int[arraysToBeSorted.Length];
int sumOfAllArrayLengths = 0;
for (int i = 0; i < arraysToBeSorted.Length; i++)
{
sumOfAllArrayLengths += arraysToBeSorted[i].Length;
}
T[] = new T[sumOfAllArrayLengths];

Check failure on line 97 in mzLib/MassSpectrometry/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / build

Identifier expected

Check failure on line 97 in mzLib/MassSpectrometry/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / build

Identifier expected
U[] = new U[sumOfAllArrayLengths];

Check failure on line 98 in mzLib/MassSpectrometry/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / build

Identifier expected

Check failure on line 98 in mzLib/MassSpectrometry/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / build

Identifier expected
Array<T> t = new int()

Check failure on line 99 in mzLib/MassSpectrometry/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / build

; expected

Check failure on line 99 in mzLib/MassSpectrometry/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / build

; expected
}
}
}
19 changes: 19 additions & 0 deletions mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using MassSpectrometry.MzSpectra;
using ClassExtensions = Chemistry.ClassExtensions;

namespace MassSpectrometry
{
Expand Down Expand Up @@ -800,5 +803,21 @@ private MzPeak GeneratePeak(int index)
{
return new MzPeak(XArray[index], YArray[index]);
}
/// <summary>
/// Method takes a list of mz and intensity values from a spectrum and merges those pairs of mzs and intensities when the difference in the mzs is less than specified tolerance
/// </summary>
/// <param name="mzArray"></param>
/// <param name="intensityArray"></param>
/// <param name="ppmTolerance"></param>
/// <returns></returns>
public void MergeMzAndIntensityValuesForAnyMzsWithinTolerance(PpmTolerance ppmTolerance)
{
var mzArray = this.XArray;
var intensityArray= this.YArray;
ExtensionMethods.MergeMzAndIntensityValuesForAnyMzsWithinTolerance(ref mzArray, ref intensityArray, ppmTolerance);
this.XArray = mzArray;
this.YArray = intensityArray;
}

}
}
2 changes: 1 addition & 1 deletion mzLib/Omics/Fragmentation/MatchedFragmentIon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public override bool Equals(object obj)
{
MatchedFragmentIon other = (MatchedFragmentIon)obj;

return this.NeutralTheoreticalProduct.Equals(other.NeutralTheoreticalProduct)
return this.NeutralTheoreticalProduct.Equals(other.NeutralTheoreticalProduct as Product)
&& this.Charge == other.Charge
&& this.Mz == other.Mz
&& this.Intensity == other.Intensity;
Expand Down
31 changes: 31 additions & 0 deletions mzLib/Test/TestMzSpectrum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using MassSpectrometry;
using MzLibUtil;
using NUnit.Framework;
using TopDownProteomics.IO.MzIdentMl;

namespace Test
{
[TestFixture]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public sealed class TestMzSpectrum
{

[Test]
public void Bubba()
{
double[] mzs = new double[3] { 1,1.0000001, 1.0000002 };
double[] intensities = new double[3] { 1,2,3 };
bool shouldCopy = false;
MzSpectrum s = new MzSpectrum(mzs,intensities,shouldCopy);

s.MergeMzAndIntensityValuesForAnyMzsWithinTolerance(new PpmTolerance(0.01));
}

[Test]
public void Bubba2()
{
var j = ExtensionMethods.MergeTwoSortedArraysIntoSortedArray(new double[] { 2, 4, 6, }, new double[] { 1, 3, 5, 7, 8, 9, 10, 11, 12 });
CollectionAssert.AreEquivalent(new double[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, j);
}
}
}
Loading