-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectraST_cramp.cpp
337 lines (278 loc) · 10.8 KB
/
SpectraST_cramp.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
/***************************************************************************
cramp.cpp
/***************************************************************************
cramp.hpp
A C++ wrapper for the RAMP code.
Use this library to parse an mzXML file in a non-sequential way, by
taking advantage of the index element.
(C) 2004 by Brian Pratt, Insilicos LLC
Based on mzXML2Other, which has this copyright:
-------------------
begin : Wed Apr 2
copyright : (C) 2002 by Pedrioli Patrick, ISB, Proteomics
email : ppatrick@student.ethz.ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library or "Lesser" General Public *
* License (LGPL) as published by the Free Software Foundation; *
* either version 2 of the License, or (at your option) any later *
* version. *
* *
***************************************************************************/
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "stdio.h"
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include "sys/errno.h"
#endif
#include "SpectraST_cramp.hpp"
/**
* This function performs a non-sequential parsing operation on an indexed
* msxml file.
*
* @param fileName: Name of the msxml file
* @param startSCan: Number of the scan we want to read from
* @param what: -HEADER will return num, msLevel and retentionTime
* -SCAN will return only the peaks
* -ALL will return everything found in scan, precursorMz and peaks
*
* @return pData is dynamically allocate and becomes property of the caller, who
* is responsible for its deallocation!!
*/
cRamp::cRamp( const char* fileName,bool declaredScansOnly ) :
m_filename(fileName), m_declaredScansOnly(declaredScansOnly), m_runInfo()
{
m_handle = rampOpenFile(fileName);
m_scanOffsets = NULL;
m_runInfo = NULL;
m_lastScan = 0;
if (!OK()) {
// HENRY -- I would prefer this to be silent, and let the caller deals with it
// cout << "Error: Could not open file " << fileName << ": " << strerror(errno) << endl;
// END HENRY
} else {
m_runInfo = getRunInfo();
// HENRY -- always read index to set scan count, since scan count
// declared at the top of the mzXML file is unreliable now that
// there are missing scans.
// This will also set the structs m_scanOffsets, and the value m_lastScan
// if (m_runInfo->m_data.scanCount < 0) { // undeclared scan count
// this will provoke reading of index, which sets scan count
rampScanInfo* tmp = getScanHeaderInfo ( 1 );
free(tmp);
// }
// END HENRY
}
}
cRamp::~cRamp() {
rampCloseFile(m_handle);
// NB: these pointers may be null on file open failure,
// but free/delete of NULL is OK per C++ standard
free(m_scanOffsets);
delete m_runInfo; // was free() - but allocated with new
}
//
// here are the private guts
//
rampInfo* cRamp::do_ramp( ramp_fileoffset_t arg , eWhatToRead what )
{
switch( what ) {
case RAMP_RUNINFO:
case RAMP_HEADER:
case RAMP_PEAKS:
case RAMP_INSTRUMENT:
break; // OK
default:
std::cerr << "unknown read type!\n";
return NULL;
break;
}
rampInfo* returnPtr=NULL;
if ((RAMP_RUNINFO != what) && (RAMP_INSTRUMENT != what) && !m_scanOffsets) {
int iLastScan = 0;
// we need the index to get anything besides the header
ramp_fileoffset_t indexOffset = getIndexOffset(m_handle);
m_scanOffsets = readIndex(m_handle, indexOffset, &iLastScan);
if (iLastScan >= m_runInfo->m_data.scanCount) {
if (!m_declaredScansOnly) {
m_runInfo->m_data.scanCount = iLastScan;
} else { // get rid of all the fake entries created
for (int n=1;n<=iLastScan;n++) { // ramp is 1 based
if (m_scanOffsets[n]==-1) {
// find a run of fakes
int m;
for (m=n+1;(m<=iLastScan)&&(m_scanOffsets[m]==-1);m++);
if (m<=iLastScan) {
memmove(m_scanOffsets+n,m_scanOffsets+m,
sizeof(ramp_fileoffset_t)*((iLastScan-m)+1));
}
iLastScan-=(m-n);
}
}
}
}
// HENRY - store last scan explicitly.
m_lastScan = iLastScan;
// END HENRY
}
// HENRY -- arg is out of bounds. instead of creating havoc in RAMP, let's just kill it here.
if (RAMP_RUNINFO != what && (RAMP_INSTRUMENT != what) && (arg > m_runInfo->m_data.scanCount || arg < 1)) {
return (NULL);
}
if (m_scanOffsets || (RAMP_RUNINFO == what) || (RAMP_INSTRUMENT == what)) {
ramp_fileoffset_t scanOffset=-1;
if (RAMP_RUNINFO == what || RAMP_INSTRUMENT == what) {
scanOffset = 0; // read from head of file
} else {
scanOffset = m_scanOffsets[arg]; // ramp is one-based
}
if (scanOffset >= 0) {
// -----------------------------------------------------------------------
// And now we can parse the info we were looking for
// -----------------------------------------------------------------------
// Ok now we have to copy everything in our structure
switch( what )
{
case RAMP_RUNINFO:
returnPtr = new rampRunInfo( m_handle );
break;
case RAMP_HEADER:
returnPtr = new rampScanInfo( m_handle, scanOffset, (int)arg );
if (returnPtr) {
#ifdef HAVE_PWIZ_MZML_LIB
if (!m_handle->mzML) // rampadapter already set this for us
#endif
((rampScanInfo *)returnPtr)->m_data.filePosition = scanOffset; // for future reference
// HENRY -- error checking here
if (((rampScanInfo*)returnPtr)->m_data.acquisitionNum < 0) {
// something failed in RAMP, possibly because it's a missing scan
delete ((rampScanInfo*)returnPtr);
returnPtr = NULL;
}
}
break;
case RAMP_PEAKS:
returnPtr = new rampPeakList( m_handle, scanOffset);
// HENRY -- error checking here
if (returnPtr && ((rampPeakList*)returnPtr)->getPeakCount() <= 0) {
// something failed in RAMP, possibly because it's a missing scan
delete ((rampPeakList*)returnPtr);
returnPtr = NULL;
}
break;
// HENRY -- add the instrument info reading functionality (present in RAMP, but not provided in cRAMP before)
case RAMP_INSTRUMENT:
returnPtr = new rampInstrumentInfo(m_handle);
if (((rampInstrumentInfo*)returnPtr)->m_instrumentStructPtr == NULL) {
delete ((rampInstrumentInfo*)returnPtr);
returnPtr = NULL;
}
break;
}
}
}
return returnPtr;
}
/**
* This function performs a non-sequential parsing operation on an indexed
* msxml file to obtain minimal info on the msRun contained in the file.
*
* @return rapRunInfo* is dynamically allocate and becomes property of the caller, who
* is responsible for its deallocation!!
*/
rampRunInfo* cRamp::getRunInfo ( ) {
rampRunInfo* result;
if (m_runInfo) { // did we derive this already?
result = new rampRunInfo(*m_runInfo);
} else {
result = (rampRunInfo*) do_ramp(0, RAMP_RUNINFO);
}
return result;
}
/**
* This function performs a non-sequential parsing operation on an indexed
* msxml file to obtain minimal header info for a numbered scan (thus minimizing parse time).
*
* @param fileName: Name of the msxml file
* @param startSCan: Number of the scan we want to read from
* @return rapHeaderInfo* is dynamically allocate and becomes property of the caller, who
* is responsible for its deallocation!! returns just the minimal header info num, msLevel and retentionTime
*/
rampScanInfo* cRamp::getScanHeaderInfo ( int whichScan ) {
return (rampScanInfo*) do_ramp((ramp_fileoffset_t)whichScan, RAMP_HEADER);
}
/**
* This function performs a non-sequential parsing operation on an indexed
* msxml file to obtain peak info for a numbered scan.
*
* @param fileName: Name of the msxml file
* @param startSCan: Number of the scan we want to read from
* @return rapPeakList* is dynamically allocate and becomes property of the caller, who
* is responsible for its deallocation!! returns everything found in scan, precursorMz and peaks
*/
rampPeakList* cRamp::getPeakList ( int whichScan ) {
return (rampPeakList*) do_ramp((ramp_fileoffset_t)whichScan, RAMP_PEAKS);
}
// HENRY - provides instrument info getting method
rampInstrumentInfo* cRamp::getInstrumentInfo () {
return (rampInstrumentInfo*) do_ramp(0, RAMP_INSTRUMENT);
}
// END HENRY
// HENRY - sequential access parser that skips over missing scans. This version only reads scan header.
bool cRampIterator::nextScan(rampScanInfo** scanInfo) {
while (++m_currentScan <= m_cramp.getLastScan() && m_cramp.getScanOffset(m_currentScan) <= 0);
if (m_currentScan > m_cramp.getLastScan()) {
return (false);
}
*scanInfo = (rampScanInfo*)m_cramp.do_ramp((ramp_fileoffset_t)(m_currentScan), RAMP_HEADER);
return (true);
}
// END HENRY
// HENRY - sequential access parser that skips over missing scans. This version reads both scan header and peak list.
bool cRampIterator::nextScan(rampScanInfo** scanInfo, rampPeakList** peakList) {
while (++m_currentScan <= m_cramp.getLastScan() && m_cramp.getScanOffset(m_currentScan) <= 0);
if (m_currentScan > m_cramp.getLastScan()) {
return (false);
}
*scanInfo = (rampScanInfo*)m_cramp.do_ramp((ramp_fileoffset_t)(m_currentScan), RAMP_HEADER);
*peakList = (rampPeakList*)m_cramp.do_ramp((ramp_fileoffset_t)(m_currentScan), RAMP_PEAKS);
return (true);
}
// END HENRY
// HENRY - resets the sequential access parser to the first scan.
void cRampIterator::reset() {
m_currentScan = 1;
}
// END HENRY
/**
* populate from a file handle
**/
rampPeakList::rampPeakList(RAMPFILE *handle, ramp_fileoffset_t index) {
init();
m_peaksCount = readPeaksCount(handle,index);
m_pPeaks = (rampPeakInfoStruct *)readPeaks(handle,index);
}
/**
* populate from a file handle
**/
rampScanInfo::rampScanInfo(RAMPFILE *handle, ramp_fileoffset_t index, int seqNum) {
init();
readHeader(handle,index,&m_data);
m_data.seqNum = seqNum;
}
/**
* populate from a file handle
**/
rampRunInfo::rampRunInfo(RAMPFILE *handle) {
init();
readMSRun(handle,&m_data);
}
// HENRY - provides instrument info reading functionality
rampInstrumentInfo::rampInstrumentInfo(RAMPFILE *handle) {
init();
m_instrumentStructPtr = getInstrumentStruct(handle);
}
// END HENRY