-
Notifications
You must be signed in to change notification settings - Fork 4
/
ScanTracking.cs
379 lines (324 loc) · 15.9 KB
/
ScanTracking.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.Collections.Generic;
using MASIC.Data;
using MASIC.DatasetStats;
using MASIC.Options;
using MASICPeakFinder;
namespace MASIC
{
/// <summary>
/// Class for tracking scan metadata
/// </summary>
public class ScanTracking : MasicEventNotifier
{
// Ignore Spelling: MASIC
/// <summary>
/// Absolute maximum number of ions that will be tracked for a mass spectrum
/// </summary>
private const int MAX_ALLOWABLE_ION_COUNT = 50000;
/// <summary>
/// Scan Stats list
/// </summary>
public List<ScanStatsEntry> ScanStats { get; }
private readonly ReporterIons mReporterIons;
private readonly clsMASICPeakFinder mPeakFinder;
private int mSpectraFoundExceedingMaxIonCount;
private int mMaxIonCountReported;
/// <summary>
/// Constructor
/// </summary>
/// <param name="reporterIons"></param>
/// <param name="peakFinder"></param>
public ScanTracking(ReporterIons reporterIons, clsMASICPeakFinder peakFinder)
{
mReporterIons = reporterIons;
mPeakFinder = peakFinder;
ScanStats = new List<ScanStatsEntry>();
}
/// <summary>
/// Check whether the scan number is within the range specified by sicOptions
/// </summary>
/// <param name="scanNumber"></param>
/// <param name="sicOptions"></param>
/// <returns>True if filtering is disabled, or if scanNumber is within the limits</returns>
public bool CheckScanInRange(
int scanNumber,
SICOptions sicOptions)
{
// ReSharper disable once InvertIf
if (sicOptions.ScanRangeStart >= 0 && sicOptions.ScanRangeEnd > sicOptions.ScanRangeStart)
{
if (scanNumber < sicOptions.ScanRangeStart || scanNumber > sicOptions.ScanRangeEnd)
{
return false;
}
}
return true;
}
/// <summary>
/// Check whether the scan number and elution time are within the ranges specified by sicOptions
/// </summary>
/// <param name="scanNumber"></param>
/// <param name="elutionTime"></param>
/// <param name="sicOptions"></param>
/// <returns>True if filtering is disabled, or if scanNumber and elutionTime are within the limits</returns>
public bool CheckScanInRange(
int scanNumber,
double elutionTime,
SICOptions sicOptions)
{
if (!CheckScanInRange(scanNumber, sicOptions))
{
return false;
}
if (sicOptions.RTRangeStart >= 0 && sicOptions.RTRangeEnd > sicOptions.RTRangeStart)
{
if (elutionTime < sicOptions.RTRangeStart || elutionTime > sicOptions.RTRangeEnd)
{
return false;
}
}
return true;
}
/// <summary>
/// Resizes the lists to a set capacity (unless there are existing contents larger than <paramref name="scanCount"/>)
/// </summary>
/// <param name="scanCount"></param>
public void ReserveListCapacity(int scanCount)
{
ScanStats.Capacity = Math.Max(ScanStats.Count, scanCount);
}
/// <summary>
/// Reduces memory usage by resizing the list to the contents
/// </summary>
public void SetListCapacityToCount()
{
ScanStats.Capacity = ScanStats.Count;
}
private void CompressSpectraData(
MSSpectrum msSpectrum,
double msDataResolution,
double mzIgnoreRangeStart,
double mzIgnoreRangeEnd)
{
// First, look for blocks of data points that consecutively have an intensity value of 0
// For each block of data found, reduce the data to only retain the first data point and last data point in the block
//
// Next, look for data points in msSpectrum that are within msDataResolution units of one another (m/z units)
// If found, combine into just one data point, keeping the largest intensity and the m/z value corresponding to the largest intensity
if (msSpectrum.IonCount <= 1)
{
return;
}
// Look for blocks of data points that all have an intensity value of 0
var targetIndex = 0;
var index = 0;
while (index < msSpectrum.IonCount)
{
if (msSpectrum.IonsIntensity[index] < float.Epsilon)
{
var countCombined = 0;
for (var comparisonIndex = index + 1; comparisonIndex < msSpectrum.IonCount; comparisonIndex++)
{
if (msSpectrum.IonsIntensity[comparisonIndex] < float.Epsilon)
{
countCombined++;
}
else
{
break;
}
}
if (countCombined > 1)
{
// Only keep the first and last data point in the block
msSpectrum.IonsMZ[targetIndex] = msSpectrum.IonsMZ[index];
msSpectrum.IonsIntensity[targetIndex] = msSpectrum.IonsIntensity[index];
targetIndex++;
msSpectrum.IonsMZ[targetIndex] = msSpectrum.IonsMZ[index + countCombined];
msSpectrum.IonsIntensity[targetIndex] = msSpectrum.IonsIntensity[index + countCombined];
index += countCombined;
}
// Keep this data point since a single zero
else if (targetIndex != index)
{
msSpectrum.IonsMZ[targetIndex] = msSpectrum.IonsMZ[index];
msSpectrum.IonsIntensity[targetIndex] = msSpectrum.IonsIntensity[index];
}
}
// Note: targetIndex will be the same as index until the first time that data is combined (countCombined > 0)
// After that, targetIndex will always be less than index and we will thus always need to copy data
else if (targetIndex != index)
{
msSpectrum.IonsMZ[targetIndex] = msSpectrum.IonsMZ[index];
msSpectrum.IonsIntensity[targetIndex] = msSpectrum.IonsIntensity[index];
}
index++;
targetIndex++;
}
// Update .IonCount with the new data count
msSpectrum.ShrinkArrays(targetIndex);
// Step through the data, consolidating data within msDataResolution
// Note that we're copying in place rather than making a new, duplicate array
// If the m/z value is between mzIgnoreRangeStart and mzIgnoreRangeEnd, we will not compress the data
targetIndex = 0;
index = 0;
while (index < msSpectrum.IonCount)
{
var countCombined = 0;
var bestMz = msSpectrum.IonsMZ[index];
// Only combine data if the first data point has a positive intensity value
if (msSpectrum.IonsIntensity[index] > 0)
{
var pointInIgnoreRange = Utilities.CheckPointInMZIgnoreRange(msSpectrum.IonsMZ[index], mzIgnoreRangeStart, mzIgnoreRangeEnd);
if (!pointInIgnoreRange)
{
for (var comparisonIndex = index + 1; comparisonIndex < msSpectrum.IonCount; comparisonIndex++)
{
if (Utilities.CheckPointInMZIgnoreRange(msSpectrum.IonsMZ[comparisonIndex], mzIgnoreRangeStart, mzIgnoreRangeEnd))
{
// Reached the ignore range; do not allow to be combined with the current data point
break;
}
if (msSpectrum.IonsMZ[comparisonIndex] - msSpectrum.IonsMZ[index] < msDataResolution)
{
if (msSpectrum.IonsIntensity[comparisonIndex] > msSpectrum.IonsIntensity[index])
{
msSpectrum.IonsIntensity[index] = msSpectrum.IonsIntensity[comparisonIndex];
bestMz = msSpectrum.IonsMZ[comparisonIndex];
}
countCombined++;
}
else
{
break;
}
}
}
}
// Note: targetIndex will be the same as index until the first time that data is combined (countCombined > 0)
// After that, targetIndex will always be less than index and we will thus always need to copy data
if (targetIndex != index || countCombined > 0)
{
msSpectrum.IonsMZ[targetIndex] = bestMz;
msSpectrum.IonsIntensity[targetIndex] = msSpectrum.IonsIntensity[index];
index += countCombined;
}
index++;
targetIndex++;
}
// Update .IonCount with the new data count
msSpectrum.ShrinkArrays(targetIndex);
}
private void ComputeNoiseLevelForMassSpectrum(
ScanInfo scanInfo,
MSSpectrum msSpectrum,
BaselineNoiseOptions noiseThresholdOptions)
{
const bool IGNORE_NON_POSITIVE_DATA = true;
scanInfo.BaselineNoiseStats = clsMASICPeakFinder.InitializeBaselineNoiseStats(0, noiseThresholdOptions.BaselineNoiseMode);
if (noiseThresholdOptions.BaselineNoiseMode == clsMASICPeakFinder.NoiseThresholdModes.AbsoluteThreshold)
{
scanInfo.BaselineNoiseStats.NoiseLevel = noiseThresholdOptions.BaselineNoiseLevelAbsolute;
scanInfo.BaselineNoiseStats.PointsUsed = 1;
}
else if (msSpectrum.IonCount > 0)
{
mPeakFinder.ComputeTrimmedNoiseLevel(
msSpectrum.IonsIntensity, 0, msSpectrum.IonCount - 1,
noiseThresholdOptions, IGNORE_NON_POSITIVE_DATA,
out var newBaselineNoiseStats);
scanInfo.BaselineNoiseStats = newBaselineNoiseStats;
}
}
/// <summary>
/// Process and store a spectrum
/// </summary>
/// <param name="scanInfo"></param>
/// <param name="dataImportUtilities"></param>
/// <param name="spectraCache"></param>
/// <param name="msSpectrum"></param>
/// <param name="noiseThresholdOptions"></param>
/// <param name="discardLowIntensityData"></param>
/// <param name="compressData"></param>
/// <param name="msDataResolution"></param>
/// <param name="keepRawSpectrum"></param>
/// <returns>True if success false if an error</returns>
public bool ProcessAndStoreSpectrum(
ScanInfo scanInfo,
DataInput.DataImport dataImportUtilities,
SpectraCache spectraCache,
MSSpectrum msSpectrum,
BaselineNoiseOptions noiseThresholdOptions,
bool discardLowIntensityData,
bool compressData,
double msDataResolution,
bool keepRawSpectrum)
{
var lastKnownLocation = "Start";
try
{
// Determine the noise threshold intensity for this spectrum
// Stored in scanInfo.BaselineNoiseStats
lastKnownLocation = "Call ComputeNoiseLevelForMassSpectrum";
ComputeNoiseLevelForMassSpectrum(scanInfo, msSpectrum, noiseThresholdOptions);
if (!keepRawSpectrum)
{
return true;
}
// Discard low intensity data, but not for MRM scans
if (discardLowIntensityData && scanInfo.MRMScanType == ThermoRawFileReader.MRMScanTypeConstants.NotMRM)
{
// Discard data below the noise level or below the minimum S/N level
// If we are searching for Reporter ions, it is important to not discard any of the ions in the region of the reporter ion m/z values
lastKnownLocation = "Call DiscardDataBelowNoiseThreshold";
dataImportUtilities.DiscardDataBelowNoiseThreshold(
msSpectrum,
scanInfo.BaselineNoiseStats.NoiseLevel,
mReporterIons.MZIntensityFilterIgnoreRangeStart,
mReporterIons.MZIntensityFilterIgnoreRangeEnd,
noiseThresholdOptions);
scanInfo.IonCount = msSpectrum.IonCount;
}
if (compressData)
{
lastKnownLocation = "Call CompressSpectraData";
// Again, if we are searching for Reporter ions, it is important to not discard any of the ions in the region of the reporter ion m/z values
CompressSpectraData(msSpectrum, msDataResolution,
mReporterIons.MZIntensityFilterIgnoreRangeStart,
mReporterIons.MZIntensityFilterIgnoreRangeEnd);
}
if (msSpectrum.IonCount > MAX_ALLOWABLE_ION_COUNT)
{
// Do not keep more than 50,000 ions
lastKnownLocation = "Call DiscardDataToLimitIonCount";
mSpectraFoundExceedingMaxIonCount++;
// Display a message at the console the first 10 times we encounter spectra with over MAX_ALLOWABLE_ION_COUNT ions
// In addition, display a new message every time a new max value is encountered
if (mSpectraFoundExceedingMaxIonCount <= 10 || msSpectrum.IonCount > mMaxIonCountReported)
{
Console.WriteLine();
Console.WriteLine(
"Note: Scan " + scanInfo.ScanNumber + " has " + msSpectrum.IonCount + " ions; " +
"will only retain " + MAX_ALLOWABLE_ION_COUNT + " (trimmed " +
mSpectraFoundExceedingMaxIonCount + " spectra)");
mMaxIonCountReported = msSpectrum.IonCount;
}
dataImportUtilities.DiscardDataToLimitIonCount(
msSpectrum,
mReporterIons.MZIntensityFilterIgnoreRangeStart,
mReporterIons.MZIntensityFilterIgnoreRangeEnd,
MAX_ALLOWABLE_ION_COUNT);
scanInfo.IonCount = msSpectrum.IonCount;
}
lastKnownLocation = "Call AddSpectrumToPool";
return spectraCache.AddSpectrumToPool(msSpectrum, scanInfo.ScanNumber);
}
catch (Exception ex)
{
ReportError("Error in ProcessAndStoreSpectrum (LastKnownLocation: " + lastKnownLocation + ")", ex, clsMASIC.MasicErrorCodes.InputFileDataReadError);
return false;
}
}
}
}