-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaxmzxmlhandler.cpp
193 lines (173 loc) · 5.27 KB
/
saxmzxmlhandler.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
/************************************************************
* SAXMzxmlHandler.cpp
*
* Premiere version janvier 2005
* Patrick Lacasse
*
* 3/11/2005 (Brendan MacLean): Use eXpat SAX parser, and create SAXSpectraHandler
*
* See http://sashimi.sourceforge.net/software_glossolalia.html for
* mzXML schema information.
*
* Inspired by DtaSAX2Handler.cpp
* copyright : (C) 2002 by Pedrioli Patrick, ISB, Proteomics
* email : [email protected]
* Artistic License granted 3/11/2005
*******************************************************/
#include "stdafx.h"
#include "saxmzxmlhandler.h"
SAXMzxmlHandler::SAXMzxmlHandler( vector<mspectrum>& _vS, mspectrumcondition& _sC, mscore& _m)
: SAXSpectraHandler(_vS, _sC, _m)
{
m_bInMsLevel2 = false;
m_bInPrecursorMz = false;
m_bInPeaks = false;
//added this (true by default)
m_bNetworkData = false;
}
SAXMzxmlHandler::~SAXMzxmlHandler()
{
}
void SAXMzxmlHandler::startElement(const XML_Char *el, const XML_Char **attr)
{
if(isElement("scan", el))
{
if((m_cidLevel = atoi(getAttrValue("msLevel", attr))) == 2)
{
m_bInMsLevel2 = true;
reset(); // Clean up for the next scan
m_scanNum = atoi(getAttrValue("num", attr));
m_tId = m_scanNum;
while(m_sId.find(m_tId) != m_sId.end()) {
m_tId++;
}
m_sId.insert(m_tId);
m_peaksCount = atoi(getAttrValue("peaksCount", attr));
m_strRt = getAttrValue("retentionTime", attr);
}
}
else if(isElement("peaks", el))
{
m_bInPeaks = true;
m_bLowPrecision = (strcmp("64", getAttrValue("precision", attr)) != 0);
m_bCompressed = false;
const char *compressionType = getAttrValue("compressionType", attr);
if ((*compressionType != '\0')&&strcmp(compressionType, "none"))
{
// TODO: Cause parse error with line number.
if (strcmp(compressionType, "zlib") != 0)
{
cerr << "Unsupported compression type '" << compressionType << "'.\n";
exit(EXIT_FAILURE);
}
m_bCompressed = true;
const char *compressedLen = getAttrValue("compressedLen", attr);
if (*compressedLen == '\0')
{
cerr << "Missing compressedLen attribute.\n";
exit(EXIT_FAILURE);
}
m_lenCompressed = atoi(compressedLen);
}
}
else if(isElement("precursorMz", el))
{
if (m_cidLevel < 3) {
//don't read precursor data if ms level >= 3
m_bInPrecursorMz = true;
m_precursorCharge = atoi(getAttrValue("precursorCharge", attr));
// test for the mzXML 3.1 possibleCharges attribute
// ex: "7,11,13"
string possibleCharges = getAttrValue("possibleCharges", attr);
if (possibleCharges != "") {
// parse the comma-separated list of additional possible precursor charges, as may come from ETD data.
string::size_type token_begin = 0;
string::size_type token_end = string::npos;
bool done=false;
while (!done) {
token_end=possibleCharges.find_first_of(',', token_begin);
string charge;
if (token_end == string::npos) {
charge=possibleCharges.substr(token_begin);
done = true;
}
else {
charge=possibleCharges.substr(token_begin, token_end-token_begin);
token_begin = token_end+1;
}
if (charge.size()>0) {
m_viPossiblePrecursorCharges.push_back(atoi(charge.c_str()));
}
}
}
}
}
}
void SAXMzxmlHandler::endElement(const XML_Char *el)
{
if(isElement("peaks", el))
{
processData();
m_bInPeaks = false;
}
else if(isElement("precursorMz", el))
{
processData();
m_bInPrecursorMz = false;
}
else if(isElement("scan", el) && m_bInMsLevel2 == true)
{
// only add a spectrum without charge (which will lead
// to internal xtandem charge state guessing) if there
// were no values parsed from *both* "precursorCharge"
// or "possibleCharges"
if ( (m_precursorCharge == 0) && (m_viPossiblePrecursorCharges.size() == 0) ) {
// add spectrum, with precursorMz charge
pushSpectrum();
}
else {
// add the spectrum with the m_precursorCharge value
int originalPrecursorMZ = m_precursorCharge; // do other pushSpectrum calls change this?
pushSpectrum(m_precursorCharge);
// are there any multiple precursor charges from mzXML 3.1's
// possibleCharges?
if (m_viPossiblePrecursorCharges.size() > 0) {
size_t originalId = m_tId;
for (vector<int>::iterator i = m_viPossiblePrecursorCharges.begin();
i != m_viPossiblePrecursorCharges.end();
++i) {
int z = *i;
if (z != originalPrecursorMZ) { // no need to duplicate if already added
m_tId += 100000000;
pushSpectrum(z);
}
}
m_tId = originalId;
}
}
m_bInMsLevel2 = false;
}
}
void SAXMzxmlHandler::characters(const XML_Char *s, int len)
{
if ((m_bInPeaks && m_cidLevel == 2) ||
(m_bInPrecursorMz))
{
m_strData.append(s, len);
}
}
void SAXMzxmlHandler::processData()
{
if( m_bInPeaks && m_cidLevel == 2)
{
pushPeaks();
}
else if (m_bInPrecursorMz)
{
if (m_cidLevel < 3) {
m_precursorMz = atof(m_strData.data());
}
}
m_strData.clear();
}