-
Notifications
You must be signed in to change notification settings - Fork 4
/
DataAggregation.cs
339 lines (305 loc) · 12.8 KB
/
DataAggregation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using MASIC.Data;
using PRISM;
namespace MASIC
{
/// <summary>
/// This class looks for ions in a given m/z range and combines their intensity values
/// (reporting either the summed value or the maximum value)
/// </summary>
public class DataAggregation : EventNotifier
{
// Ignore Spelling: MASIC, uncaching
/// <summary>
/// When returnMax is false, determine the sum of the data within the search mass tolerance
/// When returnMaxis true, determine the maximum of the data within the search mass tolerance
/// </summary>
/// <remarks>
/// Note that this function performs a recursive search of msSpectrum.IonsMZ
/// It is therefore very efficient regardless of the number of data points in the spectrum
/// For sparse spectra, you can alternatively use FindMaxValueInMZRange
/// </remarks>
/// <param name="msSpectrum"></param>
/// <param name="searchMZ"></param>
/// <param name="searchToleranceHalfWidth"></param>
/// <param name="ionMatchCount"></param>
/// <param name="closestMZ"></param>
/// <param name="returnMax"></param>
/// <returns>The sum or maximum of the matching data; 0 if no matches</returns>
public double AggregateIonsInRange(
MSSpectrum msSpectrum,
double searchMZ,
double searchToleranceHalfWidth,
out int ionMatchCount,
out double closestMZ,
bool returnMax)
{
ionMatchCount = 0;
closestMZ = 0;
try
{
var smallestDifference = double.MaxValue;
if (msSpectrum.IonsMZ == null || msSpectrum.IonCount == 0)
{
return 0;
}
if (!SumIonsFindValueInRange(msSpectrum.IonsMZ, searchMZ, searchToleranceHalfWidth, out var indexFirst, out var indexLast))
{
return 0;
}
double ionSumOrMax = 0;
for (var ionIndex = indexFirst; ionIndex <= indexLast; ionIndex++)
{
if (returnMax)
{
// Return max
if (msSpectrum.IonsIntensity[ionIndex] > ionSumOrMax)
{
ionSumOrMax = msSpectrum.IonsIntensity[ionIndex];
}
}
else
{
// Return sum
ionSumOrMax += msSpectrum.IonsIntensity[ionIndex];
}
var testDifference = Math.Abs(msSpectrum.IonsMZ[ionIndex] - searchMZ);
if (testDifference < smallestDifference)
{
smallestDifference = testDifference;
closestMZ = msSpectrum.IonsMZ[ionIndex];
}
}
ionMatchCount = indexLast - indexFirst + 1;
return ionSumOrMax;
}
catch (Exception)
{
ionMatchCount = 0;
return 0;
}
}
/// <summary>
/// Searches currentScan.IonsMZ for the maximum intensity value between mzStart and mzEnd
/// If a match is found, updates bestMatchMZ to the m/z of the match,
/// updates matchIntensity to its intensity, and returns True
/// </summary>
/// <remarks>
/// Note that this function performs a linear search of .IonsMZ
/// It is therefore good for spectra with less than 10 data points and bad for spectra with more than 10 data points
/// As an alternative to this function, use AggregateIonsInRange
/// </remarks>
/// <param name="spectraCache"></param>
/// <param name="currentScan"></param>
/// <param name="mzStart"></param>
/// <param name="mzEnd"></param>
/// <param name="bestMatchMZ">Output: m/z of the most intense ion in the given range</param>
/// <param name="matchIntensity">Output: intensity of the most intense ion in the given range</param>
/// <returns>True if a match is found, false if no data exists within the m/z range (or if the spectrum could not be obtained)</returns>
public bool FindMaxValueInMZRange(
SpectraCache spectraCache,
ScanInfo currentScan,
double mzStart,
double mzEnd,
out double bestMatchMZ,
out double matchIntensity)
{
bestMatchMZ = 0;
matchIntensity = 0;
try
{
if (!spectraCache.GetSpectrum(currentScan.ScanNumber, out var spectrum, true))
{
OnErrorEvent("Error uncaching scan " + currentScan.ScanNumber);
return false;
}
return FindMaxValueInMZRange(
spectrum.IonsMZ,
spectrum.IonsIntensity,
spectrum.IonCount,
mzStart, mzEnd,
out bestMatchMZ, out matchIntensity);
}
catch (Exception ex)
{
OnErrorEvent("Error in FindMaxValueInMZRange (A): " + ex.Message, ex);
return false;
}
}
/// <summary>
/// Searches mzList for the maximum intensity value between mzStart and mzEnd
/// If a match is found, updates bestMatchMZ to the m/z of the match,
/// updates matchIntensity to its intensity, and returns True
/// </summary>
/// <remarks>
/// Note that this function performs a linear search of .IonsMZ
/// It is therefore good for spectra with less than 10 data points and bad for spectra with more than 10 data points
/// As an alternative to this function, use AggregateIonsInRange
/// </remarks>
/// <param name="mzList"></param>
/// <param name="intensityList"></param>
/// <param name="ionCount"></param>
/// <param name="mzStart"></param>
/// <param name="mzEnd"></param>
/// <param name="bestMatchMZ">Output: m/z of the most intense ion in the given range</param>
/// <param name="matchIntensity">Output: intensity of the most intense ion in the given range</param>
/// <returns>True if a match is found, false if no data exists within the m/z range (or if the spectrum could not be obtained)</returns>
private bool FindMaxValueInMZRange(
IList<double> mzList,
IList<double> intensityList,
int ionCount,
double mzStart,
double mzEnd,
out double bestMatchMZ,
out double matchIntensity)
{
int closestMatchIndex;
var highestIntensity = 0.0;
try
{
closestMatchIndex = -1;
highestIntensity = 0;
for (var dataIndex = 0; dataIndex < ionCount; dataIndex++)
{
if (mzList[dataIndex] >= mzStart && mzList[dataIndex] <= mzEnd)
{
if (closestMatchIndex < 0)
{
closestMatchIndex = dataIndex;
highestIntensity = intensityList[dataIndex];
}
else if (intensityList[dataIndex] > highestIntensity)
{
closestMatchIndex = dataIndex;
highestIntensity = intensityList[dataIndex];
}
}
}
}
catch (Exception ex)
{
OnErrorEvent("Error in FindMaxValueInMZRange (B): " + ex.Message, ex);
closestMatchIndex = -1;
}
if (closestMatchIndex >= 0)
{
bestMatchMZ = mzList[closestMatchIndex];
matchIntensity = highestIntensity;
return true;
}
bestMatchMZ = 0;
matchIntensity = 0;
return false;
}
/// <summary>
/// Searches dataValues for searchValue with a tolerance of +/-toleranceHalfWidth
/// </summary>
/// <param name="dataValues"></param>
/// <param name="searchValue"></param>
/// <param name="toleranceHalfWidth"></param>
/// <param name="matchIndexStart">Output: start index of matching values</param>
/// <param name="matchIndexEnd">Output: end index of matching values</param>
/// <returns> True if a match is found, false if no match</returns>
private bool SumIonsFindValueInRange(
IReadOnlyList<double> dataValues,
double searchValue,
double toleranceHalfWidth,
out int matchIndexStart,
out int matchIndexEnd)
{
matchIndexStart = 0;
matchIndexEnd = dataValues.Count - 1;
if (dataValues.Count == 0)
{
matchIndexEnd = -1;
}
else if (dataValues.Count == 1)
{
if (Math.Abs(searchValue - dataValues[0]) > toleranceHalfWidth)
{
// Only one data point, and it is not within tolerance
matchIndexEnd = -1;
}
}
else
{
SumIonsBinarySearchRangeDbl(dataValues, searchValue, toleranceHalfWidth, ref matchIndexStart, ref matchIndexEnd);
}
if (matchIndexStart <= matchIndexEnd)
{
return true;
}
matchIndexStart = -1;
matchIndexEnd = -1;
return false;
}
private void SumIonsBinarySearchRangeDbl(
IReadOnlyList<double> dataValues,
double searchValue,
double toleranceHalfWidth,
ref int matchIndexStart,
ref int matchIndexEnd)
{
// Recursive search function
var leftDone = false;
var rightDone = false;
var indexMidpoint = (matchIndexStart + matchIndexEnd) / 2;
if (indexMidpoint == matchIndexStart)
{
// Min and Max are next to each other
if (Math.Abs(searchValue - dataValues[matchIndexStart]) > toleranceHalfWidth)
matchIndexStart = matchIndexEnd;
if (Math.Abs(searchValue - dataValues[matchIndexEnd]) > toleranceHalfWidth)
matchIndexEnd = indexMidpoint;
return;
}
if (dataValues[indexMidpoint] > searchValue + toleranceHalfWidth)
{
// Out of range on the right
matchIndexEnd = indexMidpoint;
SumIonsBinarySearchRangeDbl(dataValues, searchValue, toleranceHalfWidth, ref matchIndexStart, ref matchIndexEnd);
}
else if (dataValues[indexMidpoint] < searchValue - toleranceHalfWidth)
{
// Out of range on the left
matchIndexStart = indexMidpoint;
SumIonsBinarySearchRangeDbl(dataValues, searchValue, toleranceHalfWidth, ref matchIndexStart, ref matchIndexEnd);
}
else
{
// Inside range; figure out the borders
var leftIndex = indexMidpoint;
do
{
leftIndex--;
if (leftIndex < matchIndexStart)
{
leftDone = true;
}
else if (Math.Abs(searchValue - dataValues[leftIndex]) > toleranceHalfWidth)
{
leftDone = true;
}
}
while (!leftDone);
var rightIndex = indexMidpoint;
do
{
rightIndex++;
if (rightIndex > matchIndexEnd)
{
rightDone = true;
}
else if (Math.Abs(searchValue - dataValues[rightIndex]) > toleranceHalfWidth)
{
rightDone = true;
}
}
while (!rightDone);
matchIndexStart = leftIndex + 1;
matchIndexEnd = rightIndex - 1;
}
}
}
}