forked from PNNL-Comp-Mass-Spec/MASIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsScanNumScanTimeConversion.cs
275 lines (242 loc) · 12.2 KB
/
clsScanNumScanTimeConversion.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
using System;
using MASICPeakFinder;
using PRISM;
namespace MASIC
{
/// <summary>
/// Class for converting from scan number to acquisition time
/// </summary>
public class clsScanNumScanTimeConversion : EventNotifier
{
/// <summary>
/// Find the index of the scan closest to scanOrAcqTime (searching both Survey and Fragmentation Scans using the MasterScanList)
/// </summary>
/// <param name="scanList"></param>
/// <param name="scanOrAcqTime">can be absolute, relative, or AcquisitionTime</param>
/// <param name="scanType">Specifies what type of value scanOrAcqTime is; 0=absolute, 1=relative, 2=acquisition time (aka elution time)</param>
/// <returns>The index of the scan closest to scanOrAcqTime, or 0 if an error</returns>
private int FindNearestScanNumIndex(
clsScanList scanList,
float scanOrAcqTime,
clsCustomSICList.CustomSICScanTypeConstants scanType)
{
try
{
if (scanType is clsCustomSICList.CustomSICScanTypeConstants.Absolute or clsCustomSICList.CustomSICScanTypeConstants.Relative)
{
var absoluteScanNumber = ScanOrAcqTimeToAbsolute(scanList, scanOrAcqTime, scanType, false);
return clsBinarySearch.BinarySearchFindNearest(
scanList.MasterScanNumList,
absoluteScanNumber,
clsBinarySearch.eMissingDataModeConstants.ReturnClosestPoint);
}
// scanType = CustomSICScanTypeConstants.AcquisitionTime
// Find the closest match in scanList.MasterScanTimeList
return clsBinarySearch.BinarySearchFindNearest(
scanList.MasterScanTimeList,
scanOrAcqTime,
clsBinarySearch.eMissingDataModeConstants.ReturnClosestPoint);
}
catch (Exception ex)
{
OnErrorEvent("Error in FindNearestScanNumIndex", ex);
return 0;
}
}
/// <summary>
/// Finds the index of the survey scan closest to scanOrAcqTime
/// </summary>
/// <param name="scanList"></param>
/// <param name="scanOrAcqTime">Scan or acquisition time</param>
/// <param name="scanType">Type for scanOrAcqTime; should be Absolute, Relative, or AcquisitionTime</param>
public int FindNearestSurveyScanIndex(
clsScanList scanList,
float scanOrAcqTime,
clsCustomSICList.CustomSICScanTypeConstants scanType)
{
try
{
var surveyScanIndexMatch = -1;
var scanNumberToFind = ScanOrAcqTimeToAbsolute(scanList, scanOrAcqTime, scanType, false);
for (var index = 0; index < scanList.SurveyScans.Count; index++)
{
if (scanList.SurveyScans[index].ScanNumber >= scanNumberToFind)
{
surveyScanIndexMatch = index;
if (scanList.SurveyScans[index].ScanNumber != scanNumberToFind && index < scanList.SurveyScans.Count - 1)
{
// Didn't find an exact match; determine which survey scan is closer
if (Math.Abs(scanList.SurveyScans[index + 1].ScanNumber - scanNumberToFind) <
Math.Abs(scanList.SurveyScans[index].ScanNumber - scanNumberToFind))
{
surveyScanIndexMatch++;
}
}
break;
}
}
if (surveyScanIndexMatch < 0)
{
// Match not found; return either the first or the last survey scan
if (scanList.SurveyScans.Count > 0)
{
surveyScanIndexMatch = scanList.SurveyScans.Count - 1;
}
else
{
surveyScanIndexMatch = 0;
}
}
return surveyScanIndexMatch;
}
catch (Exception ex)
{
OnErrorEvent("Error in FindNearestSurveyScanIndex", ex);
return 0;
}
}
/// <summary>
/// Converts a scan number or acquisition time to an actual scan number
/// </summary>
/// <param name="scanList"></param>
/// <param name="scanOrAcqTime">Value to convert</param>
/// <param name="scanType">Type of the value to convert; 0=Absolute, 1=Relative, 2=Acquisition Time (aka elution time)</param>
/// <param name="convertingRangeOrTolerance">True when converting a range</param>
public int ScanOrAcqTimeToAbsolute(
clsScanList scanList,
float scanOrAcqTime,
clsCustomSICList.CustomSICScanTypeConstants scanType,
bool convertingRangeOrTolerance)
{
try
{
var absoluteScanNumber = 0;
switch (scanType)
{
case clsCustomSICList.CustomSICScanTypeConstants.Absolute:
// scanOrAcqTime is an absolute scan number (or range of scan numbers)
// No conversion needed; simply return the value
absoluteScanNumber = (int)Math.Round(scanOrAcqTime);
break;
case clsCustomSICList.CustomSICScanTypeConstants.Relative:
// scanOrAcqTime is a fraction of the total number of scans (for example, 0.5)
// Use the total range of scan numbers
if (scanList.MasterScanOrderCount > 0)
{
var totalScanRange = scanList.MasterScanNumList[scanList.MasterScanOrderCount - 1] - scanList.MasterScanNumList[0];
absoluteScanNumber = (int)Math.Round(scanOrAcqTime * totalScanRange + scanList.MasterScanNumList[0]);
}
else
{
absoluteScanNumber = 0;
}
break;
case clsCustomSICList.CustomSICScanTypeConstants.AcquisitionTime:
// scanOrAcqTime is an elution time value
// If convertingRangeOrTolerance = False, look for the scan that is nearest to scanOrAcqTime
// If convertingRangeOrTolerance = True, convert scanOrAcqTime to a relative scan range and then
// call this function again with that relative time
if (convertingRangeOrTolerance)
{
var totalRunTime = scanList.MasterScanTimeList[scanList.MasterScanOrderCount - 1] - scanList.MasterScanTimeList[0];
if (totalRunTime < 0.1)
{
totalRunTime = 1;
}
var relativeTime = scanOrAcqTime / totalRunTime;
absoluteScanNumber = ScanOrAcqTimeToAbsolute(scanList, relativeTime, clsCustomSICList.CustomSICScanTypeConstants.Relative, true);
}
else
{
var masterScanIndex = FindNearestScanNumIndex(scanList, scanOrAcqTime, scanType);
if (masterScanIndex >= 0 && scanList.MasterScanOrderCount > 0)
{
absoluteScanNumber = scanList.MasterScanNumList[masterScanIndex];
}
}
break;
default:
// Unknown type; assume absolute scan number
absoluteScanNumber = (int)Math.Round(scanOrAcqTime);
break;
}
return absoluteScanNumber;
}
catch (Exception ex)
{
OnErrorEvent("Error in clsMasic->ScanOrAcqTimeToAbsolute", ex);
return 0;
}
}
/// <summary>
/// Convert a relative scan or acquisition time to elution time
/// If scanType is CustomSICScanTypeConstants.AcquisitionTime, no conversion is required
/// </summary>
/// <param name="scanList"></param>
/// <param name="scanOrAcqTime"></param>
/// <param name="scanType"></param>
/// <param name="convertingRangeOrTolerance"></param>
public float ScanOrAcqTimeToScanTime(
clsScanList scanList,
float scanOrAcqTime,
clsCustomSICList.CustomSICScanTypeConstants scanType,
bool convertingRangeOrTolerance)
{
try
{
switch (scanType)
{
case clsCustomSICList.CustomSICScanTypeConstants.Absolute:
// scanOrAcqTime is an absolute scan number (or range of scan numbers)
// If convertingRangeOrTolerance = False, look for the scan that is nearest to scanOrAcqTime
// If convertingRangeOrTolerance = True, Convert scanOrAcqTime to a relative scan range,
// then call this function again with that relative time
if (convertingRangeOrTolerance)
{
var totalScans = scanList.MasterScanNumList[scanList.MasterScanOrderCount - 1] - scanList.MasterScanNumList[0];
if (totalScans < 1)
{
totalScans = 1;
}
var relativeTime = scanOrAcqTime / totalScans;
return ScanOrAcqTimeToScanTime(scanList, relativeTime, clsCustomSICList.CustomSICScanTypeConstants.Relative, true);
}
else
{
var masterScanIndex = FindNearestScanNumIndex(scanList, scanOrAcqTime, scanType);
if (masterScanIndex >= 0 && scanList.MasterScanOrderCount > 0)
{
return scanList.MasterScanTimeList[masterScanIndex];
}
}
break;
case clsCustomSICList.CustomSICScanTypeConstants.Relative:
// scanOrAcqTime is a fraction of the total number of scans (for example, 0.5)
// Use the total range of scan times
if (scanList.MasterScanOrderCount > 0)
{
var totalRunTime = scanList.MasterScanTimeList[scanList.MasterScanOrderCount - 1] - scanList.MasterScanTimeList[0];
return scanOrAcqTime * totalRunTime + scanList.MasterScanTimeList[0];
}
else
{
return 0;
}
case clsCustomSICList.CustomSICScanTypeConstants.AcquisitionTime:
// scanOrAcqTime is an elution time value (or elution time range)
// No conversion needed; simply return the value
return scanOrAcqTime;
default:
// Unknown type; assume already a scan time
return scanOrAcqTime;
}
return 0;
}
catch (Exception ex)
{
OnErrorEvent("Error in clsMasic->ScanOrAcqTimeToScanTime", ex);
return 0;
}
}
}
}