Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into encyclopediaDIAmo…
Browse files Browse the repository at this point in the history
…difiedPeptidesFormatMassShift
  • Loading branch information
trishorts committed Aug 14, 2023
2 parents 9134d17 + 40dc604 commit 0919bc0
Show file tree
Hide file tree
Showing 65 changed files with 3,219 additions and 299 deletions.
75 changes: 75 additions & 0 deletions mzLib/Chemistry/ClassExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,74 @@ public static double ToMass(this double massToChargeRatio, int charge)
return myNumber;
}

/// <summary>
/// Finds the index of the array element closest to the target value.
/// </summary>
/// <param name="sortedArray"> A double array, sorted in ascending order </param>
/// <param name="target"> Target value for binary search </param>
/// <param name="searchType"> Determines whether the the closest, previous, or next index - relative to the target - is returned </param>
/// <returns> Index of the array element closest to the target value. If target is greater than the
/// maximum array value, this returns the index of the last array element (array.count - 1). If the target is less than
/// the minimum array value, returns 0. </returns>
public static int GetClosestIndex(this double[] sortedArray, double target,
ArraySearchOption searchType = ArraySearchOption.Closest)
{
int defaultImplementationIndex = Array.BinarySearch(sortedArray, target);
// Positive index == exact match
if (defaultImplementationIndex >= 0)
{
return defaultImplementationIndex;
}

// Negative indices mean no exact match. Taking there bitwise complement yield the index
// of the first element with a value greater than the target.
defaultImplementationIndex = ~defaultImplementationIndex;

if (defaultImplementationIndex == sortedArray.Length)
// This implies that the target value was greater than the largest element in the array
return defaultImplementationIndex - 1;
if (defaultImplementationIndex == 0)
// This implies that the target value was less than the smallest element in the array
return 0;


int closestIndex;
switch (searchType)
{
case ArraySearchOption.Previous:
closestIndex = defaultImplementationIndex - 1;
break;
case ArraySearchOption.Closest:
double backwardsDiff = target - sortedArray[defaultImplementationIndex - 1];
double forwardsDiff = sortedArray[defaultImplementationIndex] - target;
closestIndex = backwardsDiff < forwardsDiff
? defaultImplementationIndex - 1
: defaultImplementationIndex;
break;
case ArraySearchOption.Next: // Next falls through to default
default:
closestIndex = defaultImplementationIndex;
break;
}

return closestIndex;
}

/// <summary>
/// Finds array element closest to the target value.
/// </summary>
/// <param name="sortedArray"> A double array, sorted in ascending order </param>
/// <param name="target"> Target value for binary search </param>
/// <param name="searchType"> Determines whether the the closest, previous, or next value - relative to the target - is returned </param>
/// <returns> The array element closest to the target value. If target is greater than the
/// maximum array value, this returns the last element of the array. If the target is less than
/// the minimum array value, returns the minimum array value. </returns>
public static double GetClosestValue(this double[] sortedArray, double target,
ArraySearchOption searchType = ArraySearchOption.Closest)
{
return sortedArray[sortedArray.GetClosestIndex(target, searchType)];
}

public class TupleList<T1, T2> : List<Tuple<T1, T2>>
{
public void Add(T1 item, T2 item2)
Expand All @@ -65,4 +133,11 @@ public void Add(T1 item, T2 item2)
}
}
}

public enum ArraySearchOption
{
Previous,
Closest,
Next
}
}
16 changes: 14 additions & 2 deletions mzLib/FlashLFQ/ChromatographicPeak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,30 @@ public void CalculateIntensityForThisFeature(bool integrate)
}
}

/// <summary>
/// Merges ChromatographicPeaks by combining Identifications and IsotopicEnvelopes,
/// then recalculates feature intensity.
/// </summary>
/// <param name="otherFeature"> Peak to be merged in. This peak is not modified</param>
public void MergeFeatureWith(ChromatographicPeak otherFeature, bool integrate)
{
if (otherFeature != this)
{
var thisFeaturesPeaks = new HashSet<IndexedMassSpectralPeak>(IsotopicEnvelopes.Select(p => p.IndexedPeak));
this.Identifications = this.Identifications.Union(otherFeature.Identifications).Distinct().OrderBy(p => p.PosteriorErrorProbability).ToList();
this.Identifications = this.Identifications
.Union(otherFeature.Identifications)
.Distinct()
.OrderBy(p => p.PosteriorErrorProbability).ToList();
ResolveIdentifications();
this.IsotopicEnvelopes.AddRange(otherFeature.IsotopicEnvelopes.Where(p => !thisFeaturesPeaks.Contains(p.IndexedPeak)));
this.IsotopicEnvelopes.AddRange(otherFeature.IsotopicEnvelopes
.Where(p => !thisFeaturesPeaks.Contains(p.IndexedPeak)));
this.CalculateIntensityForThisFeature(integrate);
}
}

/// <summary>
/// Sets two ChromatographicPeak properties: NumIdentificationsByBaseSeq and NumIdentificationsByFullSeq
/// </summary>
public void ResolveIdentifications()
{
this.NumIdentificationsByBaseSeq = Identifications.Select(v => v.BaseSequence).Distinct().Count();
Expand Down
Loading

0 comments on commit 0919bc0

Please sign in to comment.