-
Notifications
You must be signed in to change notification settings - Fork 1
/
getXIC.cpp
372 lines (285 loc) · 10.3 KB
/
getXIC.cpp
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
#ifdef _DEBUG
# pragma comment(lib, "comsuppwd.lib")
#else
# pragma comment(lib, "comsuppw.lib")
#endif
# pragma comment(lib, "wbemuuid.lib")
#include <iostream>
#include <string>
#include <fstream>
#include <wchar.h>
#include <time.h>
#include <numeric>
#include <comutil.h>
#include "getXIC.h"
std::vector< std::vector< std::pair<double, double> > > getXIC(std::string &fullFileName, double mz, float rt, int charge, bool boxCar, float mzTolerance){
IXRawfile5* pISL;
std::wstring wRawFile(fullFileName.begin(),fullFileName.end());
wRawFile.append(1, 0);
openRawFile(wRawFile, pISL);
pISL->SetCurrentController ( 0, 1 ); // first MS controller
BSTR filter = NULL;
long scanNumber = 1;
long nRet = pISL->GetFilterForScanNum(scanNumber,&filter);
//char* filterString = _com_util::ConvertBSTRToString(filter);
//std::cout << filterString << "\n";
std::vector<double> peakAreas;
float rtWindow = 1.5; // This should be user define as a parameter
std::vector< std::pair<double, double> > timeIntPairs;
std::vector< std::vector< std::pair<double, double> > > timeIntIsotopes;
// First isotope
double mzLower = mz - (mz * mzTolerance) / 1000000;
double mzUpper = mz + (mz * mzTolerance) / 1000000;
std::cout << "Isotope 1: " << mzLower << " - " << mzUpper << "\n";
std::wstring mzRange = std::to_wstring(mzLower);
mzRange=mzRange+std::wstring(L"-");
mzRange+=std::to_wstring(mzUpper);
mzRange.append(1, 0);
float rtLower = rt - rtWindow;
float rtUpper = rt + rtWindow;
getTimeIntPairs(timeIntPairs, filter, mzRange, rtLower, rtUpper, pISL, boxCar);
timeIntIsotopes.push_back(timeIntPairs);
timeIntPairs.clear();
// Second isotope
mz = mz + (1.0022 / charge);
mzLower = mz - (mz * mzTolerance) / 1000000;
mzUpper = mz + (mz * mzTolerance) / 1000000;
std::cout << "Isotope 2: " << mzLower << " - " << mzUpper << "\n";
mzRange = std::to_wstring(mzLower);
mzRange=mzRange+std::wstring(L"-");
mzRange+=std::to_wstring(mzUpper);
mzRange.append(1, 0);
getTimeIntPairs(timeIntPairs, filter, mzRange, rtLower, rtUpper, pISL, boxCar);
timeIntIsotopes.push_back(timeIntPairs);
timeIntPairs.clear();
// Third isotope
mz = mz + (1.0022 / charge);
mzLower = mz - (mz * mzTolerance) / 1000000;
mzUpper = mz + (mz * mzTolerance) / 1000000;
std::cout << "Isotope 3: " << mzLower << " - " << mzUpper << "\n";
mzRange = std::to_wstring(mzLower);
mzRange=mzRange+std::wstring(L"-");
mzRange+=std::to_wstring(mzUpper);
mzRange.append(1, 0);
getTimeIntPairs(timeIntPairs, filter, mzRange, rtLower, rtUpper, pISL, boxCar);
timeIntIsotopes.push_back(timeIntPairs);
peakInfo bestPeak;
peakHeight(timeIntIsotopes, bestPeak);
std::cout << "---------------- \n";
std::cout << "Retention time: " << timeIntIsotopes[0][bestPeak.index].first << "\n";
std::cout << "Peak area: " << bestPeak.area << "\n";
pISL->Close();
pISL->Release();
CoUninitialize();
return timeIntIsotopes;
}
void openRawFile(std::wstring wRawFile, IXRawfile5* &pISL){
HRESULT hr;
SetLastError(0);
wchar_t* const clsid_MSFileReader_str = (wchar_t *)L"{1D23188D-53FE-4C25-B032-DC70ACDBDC02}";
wchar_t* const iid_IXRawfile_str = (wchar_t *)L"{11B488A0-69B1-41FC-A660-FE8DF2A31F5B}";
CLSID clsid_MSFileReader;
CLSID clsid_IXRawfile;
CLSIDFromString(clsid_MSFileReader_str, &clsid_MSFileReader);
CLSIDFromString(iid_IXRawfile_str, &clsid_IXRawfile);
DWORD dw = GetLastError();
if(dw!=0){
std::cout << "CLSIDFromString error: " << dw << ". ";
getError();
SetLastError(0);
dw = 0;
}
//hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
hr=CoInitialize(NULL);
if(SUCCEEDED(hr)){
std::cout << "CoInitialize success" << std::endl;
hr = CoCreateInstance ( clsid_MSFileReader, // CLSID of coclass
NULL, // not used - aggregation
CLSCTX_INPROC_SERVER, // type of server
clsid_IXRawfile, // IID of interface
(void**) &pISL ); // Pointer to our interface pointer
if ( SUCCEEDED ( hr ) ){
std::cout << "CoCreateInstance success" << std::endl;
wchar_t* wcharRawFile = (wchar_t*)wRawFile.c_str();
std::wcout << wcharRawFile << std::endl;
long nRet = pISL->Open( wcharRawFile );
if(nRet != 0){
std::cout << "Open error : " << nRet << ". " << std::endl;
dw = GetLastError();
if(dw!=0){
getError();
SetLastError(0);
dw = 0;
}
}
std::cout << "Opened RAW file" << std::endl;
}
else{
std::cout << "CoCreateInstance failure" << std::endl;
dw = GetLastError();
if(dw!=0){
std::cout << "CoCreateInstance error: " << dw << ". ";
getError();
SetLastError(0);
dw = 0;
}
// Couldn't create the COM object. hr holds the error code.
}
} else {
std::cout << "CoInitialize failure" << std::endl;
dw = GetLastError();
if(dw!=0){
std::cout << "CoInitialize error: " << dw << ". ";
getError();
SetLastError(0);
dw = 0;
}
}
}
void getTimeIntPairs(std::vector<std::pair<double, double> > &timeIntPairs, BSTR &filter, std::wstring mzRange, float rtLower, float rtUpper, IXRawfile5* &pISL, bool boxCar){
wchar_t* wcharMzRange = &mzRange[0];
VARIANT varChroData;
VariantInit(&varChroData);
VARIANT varPeakFlags;
VariantInit(&varPeakFlags);
long nArraySize = 0;
double dStartTime = double(rtLower);
double dEndTime = double(rtUpper);
long smoothingType = 0;
long smoothingValue = 0;
if(boxCar){
smoothingValue = 9;
smoothingType = 1;
}
long nRet = pISL->GetChroData ( 0,
0,
0,
filter,
wcharMzRange,
NULL,
0.0,
&dStartTime,
&dEndTime,
smoothingType, //Boxcar chromatogram smoothing
smoothingValue, //Smoothing value between 3-15 (must be odd)
&varChroData,
&varPeakFlags,
&nArraySize );
if( nArraySize ){
// Get a pointer to the SafeArray
SAFEARRAY FAR* psa = varChroData.parray;
double* spectrumData;
SafeArrayAccessData( psa, (void**)(&spectrumData) );
std::pair<double, double> retrievedPair;
for( long j=0; j<2*nArraySize; j=j+2 ){
retrievedPair.first = spectrumData[j];
retrievedPair.second = spectrumData[j+1];
timeIntPairs.push_back(retrievedPair);
}
// Release the data handle
SafeArrayUnaccessData( psa );
SafeArrayDestroy(psa);
psa = NULL;
}
}
void peakHeight(std::vector< std::vector<std::pair<double, double> > > &timeIntIsotopes, peakInfo &bestPeak){
std::vector<std::pair<double, double> > &isotope1 = timeIntIsotopes[0];
int numIndices = isotope1.size();
std::vector<int> listOfPeaks;
for(int slidingWindowIndex=6; slidingWindowIndex<numIndices-6; slidingWindowIndex++){
//First let's do a basic check that this point is at least at a local maximum about 3 points
if(!( (isotope1[slidingWindowIndex].second>=isotope1[slidingWindowIndex-1].second) &&
(isotope1[slidingWindowIndex].second>=isotope1[slidingWindowIndex+1].second))){
continue;
}
int higherThan = 0;
for(int index=slidingWindowIndex-6; index<slidingWindowIndex; index++){
if(isotope1[slidingWindowIndex].second > isotope1[index].second){
higherThan++;
}
}
if(higherThan>2){
higherThan=0;
for(int index=slidingWindowIndex+1; index<=slidingWindowIndex+6; index++){
if(isotope1[slidingWindowIndex].second > isotope1[index].second){
higherThan++;
}
}
if(higherThan>2) listOfPeaks.push_back(slidingWindowIndex);
}
}
int numPeaks = listOfPeaks.size();
double bestArea = 0;
double bestIntensity = 0;
int bestIndex = 0;
for(int peakIndex=0; peakIndex<numPeaks; peakIndex++){
std::vector<double> peakAreas;
for(int isotopeIndex=0; isotopeIndex<3; isotopeIndex++){
int intensityIndex = listOfPeaks[peakIndex];
double peakIntensity = timeIntIsotopes[isotopeIndex][intensityIndex].second;
while(timeIntIsotopes[isotopeIndex][intensityIndex].second>peakIntensity/2 && intensityIndex>0){
intensityIndex--;
}
int lowerIndex = intensityIndex;
intensityIndex = listOfPeaks[peakIndex];
while(timeIntIsotopes[isotopeIndex][intensityIndex].second>peakIntensity/2 && intensityIndex<numIndices-1){
intensityIndex++;
}
int upperIndex = intensityIndex;
double peakArea = getPeakArea(upperIndex, lowerIndex, numIndices, peakIntensity, listOfPeaks[peakIndex]);
peakAreas.push_back(peakArea);
}
double totalArea = peakAreas[0] + peakAreas[1] + peakAreas[2];
if(totalArea>bestArea && (peakAreas[1] > (peakAreas[0]*0.2)) ){
bestArea=totalArea;
bestIntensity = isotope1[listOfPeaks[peakIndex]].second;
bestIndex = listOfPeaks[peakIndex];
}
}
bestPeak.intensity = bestIntensity;
bestPeak.area = bestArea;
bestPeak.index = bestIndex;
}
void outputTimeIntPairs(std::vector<std::pair<double, double> > &timeIntPairs, int isotopeNum){
std::ofstream datFile;
std::string csvFileName = "timeInt"+std::to_string(isotopeNum)+".csv";
const char *csvFileNameChar = csvFileName.c_str();
datFile.open (csvFileNameChar);
datFile << "time,int\n";
for(int i=0; i<timeIntPairs.size(); i++){
datFile << timeIntPairs[i].first << "," << timeIntPairs[i].second << "\n";
}
datFile.close();
}
void getError(){
//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if(errorMessageID == 0)
std::cout << "No error" << std::endl;
LPSTR messageBuffer = NULL;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
std::cout << message << std::endl;
}
double getPeakArea(int upperIndex, int lowerIndex, int numIndices, double peakIntensity, int peakIndex){
double peakWidth=0, peakArea=0;
//If the half maximum can't be found to the left, then we will use the half maximum to the right and multiple by 2, and visa-versa
//If the half maximum can't be found to the left AND the right, then this suggests a peak width wider than our XIC window - we can treat this as a bad peak and report its areas as zero
if(lowerIndex==0){
if(upperIndex==numIndices-1){
return peakArea;
} else{
peakWidth = (upperIndex-peakIndex)*2*60;
}
} else if(upperIndex==numIndices-1){
peakWidth = (peakIndex-lowerIndex)*2*60;
} else{
peakWidth = (upperIndex-lowerIndex)*60;
}
peakArea = peakWidth*peakIntensity;
return peakArea;
}