Skip to content

Commit

Permalink
peak merge and sorted array merge
Browse files Browse the repository at this point in the history
  • Loading branch information
trishorts committed Jan 2, 2024
1 parent 2b1e91e commit 4fe1016
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 58 deletions.
88 changes: 88 additions & 0 deletions mzLib/MassSpectrometry/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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);
}

Check warning on line 21 in mzLib/MassSpectrometry/ExtensionMethods.cs

View check run for this annotation

Codecov / codecov/patch

mzLib/MassSpectrometry/ExtensionMethods.cs#L18-L21

Added lines #L18 - L21 were not covered by tests
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
}

Check warning on line 29 in mzLib/MassSpectrometry/ExtensionMethods.cs

View check run for this annotation

Codecov / codecov/patch

mzLib/MassSpectrometry/ExtensionMethods.cs#L25-L29

Added lines #L25 - L29 were not covered by tests
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;

Check warning on line 43 in mzLib/MassSpectrometry/ExtensionMethods.cs

View check run for this annotation

Codecov / codecov/patch

mzLib/MassSpectrometry/ExtensionMethods.cs#L42-L43

Added lines #L42 - L43 were not covered by tests
foreach (var index in indiciesToMerge)
{
intensitySum += intensityArray[index];
}
return (intensitySum / (double)indiciesToMerge.Count);
}

Check warning on line 49 in mzLib/MassSpectrometry/ExtensionMethods.cs

View check run for this annotation

Codecov / codecov/patch

mzLib/MassSpectrometry/ExtensionMethods.cs#L45-L49

Added lines #L45 - L49 were not covered by tests

private static double GetWeightedMz(List<int> indiciesToMerge, List<double> mzArray, List<double> intensityArray)
{
double weightedMz = 0;
double intensitySum = 0;

Check warning on line 54 in mzLib/MassSpectrometry/ExtensionMethods.cs

View check run for this annotation

Codecov / codecov/patch

mzLib/MassSpectrometry/ExtensionMethods.cs#L52-L54

Added lines #L52 - L54 were not covered by tests
foreach (var index in indiciesToMerge)
{
weightedMz += mzArray[index] * intensityArray[index];
intensitySum += intensityArray[index];
}
return (weightedMz / intensitySum);
}

Check warning on line 61 in mzLib/MassSpectrometry/ExtensionMethods.cs

View check run for this annotation

Codecov / codecov/patch

mzLib/MassSpectrometry/ExtensionMethods.cs#L56-L61

Added lines #L56 - L61 were not covered by tests

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;
}
}
}
64 changes: 14 additions & 50 deletions mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using MassSpectrometry.MzSpectra;
using ClassExtensions = Chemistry.ClassExtensions;

namespace MassSpectrometry
Expand Down Expand Up @@ -802,58 +803,21 @@ private MzPeak GeneratePeak(int index)
{
return new MzPeak(XArray[index], YArray[index]);
}

public (double[] mzArray, double[] intensityArray) Yoyo(List<double> mzArray, List<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.Count; i++)
{
if (i < (mzArray.Count - 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,intensityArray));
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]);
}
}
}
return (newMzArray.ToArray(), newIntensityArray.ToArray());
}

private double GetIntensityAverage(List<int> indiciesToMerge, List<double> mzArray, List<double> intensityArray)
/// <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)
{
double intensitySum = 0;
foreach (var index in indiciesToMerge)
{
intensitySum += intensityArray[index];
}
return (intensitySum / (double)indiciesToMerge.Count);
var mzArray = this.XArray;
var intensityArray= this.YArray;
ExtensionMethods.MergeMzAndIntensityValuesForAnyMzsWithinTolerance(ref mzArray, ref intensityArray, ppmTolerance);
this.XArray = mzArray;
this.YArray = intensityArray;
}

private 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);
}
}
}
18 changes: 10 additions & 8 deletions mzLib/Test/TestMzSpectrum.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MassSpectrometry;
using MassSpectrometry;
using MzLibUtil;
using NUnit.Framework;
using TopDownProteomics.IO.MzIdentMl;

namespace Test
{
Expand All @@ -22,8 +18,14 @@ public void Bubba()
bool shouldCopy = false;
MzSpectrum s = new MzSpectrum(mzs,intensities,shouldCopy);

(mzs,intensities) = s.Yoyo(mzs.ToList(), intensities.ToList(), new PpmTolerance(100));
Assert.IsTrue(false);
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);
}
}
}

0 comments on commit 4fe1016

Please sign in to comment.