libpappsomspp
Library for mass spectrometry
xtandemoutputsaxhandler.cpp
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/processing/tandemwrapper/xtandemoutputsaxhandler.cpp
3  * \date 01/02/2020
4  * \author Olivier Langella
5  * \brief rewrites tandem xml input file with temporary files
6  */
7 
8 /*******************************************************************************
9  * Copyright (c) 2020 Olivier Langella <Olivier.Langella@u-psud.fr>.
10  *
11  * This file is part of PAPPSOms-tools.
12  *
13  * PAPPSOms-tools is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * PAPPSOms-tools is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with PAPPSOms-tools. If not, see <http://www.gnu.org/licenses/>.
25  *
26  ******************************************************************************/
27 
28 
30 #include "../../../pappsomspp/pappsoexception.h"
31 #include <QFileInfo>
32 
33 namespace pappso
34 {
36  const QString &final_tandem_output, const QString &original_msdata_file_name)
37  : m_destinationTandemOutputFile(final_tandem_output)
38 {
39  qDebug() << final_tandem_output;
41  QFileInfo(original_msdata_file_name).absoluteFilePath();
42  if(!m_destinationTandemOutputFile.open(QIODevice::WriteOnly))
43  {
45  QObject::tr("ERROR: unable to open %1 to write XML output")
46  .arg(final_tandem_output));
47  }
48  p_writeXmlTandemOutput = new QXmlStreamWriter();
49  p_writeXmlTandemOutput->setAutoFormatting(true);
51 }
52 
54 {
55  if(p_writeXmlTandemOutput != nullptr)
56  {
59  }
60 }
61 
62 void
64  const QXmlAttributes &attributes)
65 {
66  p_writeXmlTandemOutput->writeStartElement(qName);
67  for(int i = 0; i < attributes.length(); i++)
68  {
69  p_writeXmlTandemOutput->writeAttribute(attributes.qName(i),
70  attributes.value(i));
71  }
72 }
73 
74 bool
76  [[maybe_unused]] const QString &namespaceURI,
77  [[maybe_unused]] const QString &localName,
78  const QString &qName,
79  const QXmlAttributes &attributes)
80 {
81  m_tagStack.push_back(qName);
82  bool is_ok = true;
83 
84  try
85  {
86  m_currentText.clear();
87  //<bioml label="example api document">
88  if(m_tagStack.size() == 1)
89  {
90  if(qName != "bioml")
91  {
93  QObject::tr(
94  "ERROR in XtandemOutputSaxHandler::startElement "
95  "root tag %1 is not <bioml>")
96  .arg(qName);
97  m_isTandemParameter = false;
98  return false;
99  }
100  else
101  {
102 
103  m_isTandemParameter = true;
104 
105  // <bioml xmlns:GAML="http://www.bioml.com/gaml/" label="models
106  // from '/tmp/msdata.mzxml'">
107  p_writeXmlTandemOutput->writeStartElement("bioml");
108  p_writeXmlTandemOutput->writeAttribute(
109  "xmlns:GAML", attributes.value("xmlns:GAML"));
110  p_writeXmlTandemOutput->writeAttribute(
111  "label",
112  QString("models from '%1'").arg(m_originalMsDataFileName));
113  return true;
114  }
115  }
116  // startElement_group
117 
118  if(qName == "note")
119  {
120  is_ok = startElement_note(attributes);
121  }
122  else
123  {
124  writeOpenTag(qName, attributes);
125  }
126  }
127  catch(const pappso::PappsoException &exception_pappso)
128  {
129  m_errorString = QObject::tr(
130  "ERROR in XtandemOutputSaxHandler::startElement "
131  "tag %1, PAPPSO exception:\n%2")
132  .arg(qName)
133  .arg(exception_pappso.qwhat());
134  return false;
135  }
136  catch(const std::exception &exception_std)
137  {
138  m_errorString = QObject::tr(
139  "ERROR in XtandemOutputSaxHandler::startElement "
140  "tag %1, std exception:\n%2")
141  .arg(qName)
142  .arg(exception_std.what());
143  return false;
144  }
145  return is_ok;
146 }
147 
148 bool
150  [[maybe_unused]] const QString &namespaceURI,
151  [[maybe_unused]] const QString &localName,
152  const QString &qName)
153 {
154 
155  bool is_ok = true;
156  // endElement_peptide_list
157  try
158  {
159 
160  if(qName == "note")
161  {
162  is_ok = endElement_note();
163  }
164  else
165  {
166  p_writeXmlTandemOutput->writeEndElement();
167  }
168  }
169  catch(const pappso::PappsoException &exception_pappso)
170  {
171  m_errorString = QObject::tr(
172  "ERROR in XtandemOutputSaxHandler::endElement tag "
173  "%1, PAPPSO exception:\n%2")
174  .arg(qName)
175  .arg(exception_pappso.qwhat());
176  return false;
177  }
178  catch(const std::exception &exception_std)
179  {
180  m_errorString = QObject::tr(
181  "ERROR in XtandemOutputSaxHandler::endElement tag "
182  "%1, std exception:\n%2")
183  .arg(qName)
184  .arg(exception_std.what());
185  return false;
186  }
187 
188  m_currentText.clear();
189  m_tagStack.pop_back();
190 
191  return is_ok;
192 }
193 
194 bool
196 {
197 
198  p_writeXmlTandemOutput->setAutoFormatting(true);
199  p_writeXmlTandemOutput->writeStartDocument("1.0");
200  return true;
201 }
202 
203 bool
205 {
206  p_writeXmlTandemOutput->writeEndDocument();
207 
209  delete p_writeXmlTandemOutput;
210  p_writeXmlTandemOutput = nullptr;
211  return true;
212 }
213 
214 bool
216 {
217  m_currentText += str;
218  if(m_tagStack.back() != "note")
219  {
220  p_writeXmlTandemOutput->writeCharacters(str);
221  }
222  return true;
223 }
224 
225 
226 bool
227 XtandemOutputSaxHandler::error(const QXmlParseException &exception)
228 {
229  m_errorString = QObject::tr(
230  "Parse error at line %1, column %2 :\n"
231  "%3")
232  .arg(exception.lineNumber())
233  .arg(exception.columnNumber())
234  .arg(exception.message());
235  qDebug() << m_errorString;
236  return false;
237 }
238 
239 
240 bool
241 XtandemOutputSaxHandler::fatalError(const QXmlParseException &exception)
242 {
243  m_errorString = QObject::tr(
244  "Parse error at line %1, column %2 :\n"
245  "%3")
246  .arg(exception.lineNumber())
247  .arg(exception.columnNumber())
248  .arg(exception.message());
249  qDebug() << m_errorString;
250  return false;
251 }
252 
253 QString
255 {
256  return m_errorString;
257 }
258 
259 
260 bool
262 {
263  // qDebug() << "XtandemParamSaxHandler::startElement_note begin " <<
264  // <note type="input"
265  // label="output,path">/gorgone/pappso/jouy/users/Celine/2019_Lumos/20191222_107_Juste_APD/metapappso_condor/test_run/20191222_18_EF1_third_step_test_condor_22janv.xml</note>
266 
267  writeOpenTag("note", attributes);
268  m_currentLabel = "";
269 
270  if(attributes.value("type") == "input")
271  {
272  m_currentLabel = attributes.value("label");
273  }
274 
275  // qDebug() << "XtandemParamSaxHandler::startElement_note _current_label " <<
276  // _current_label;
277  return true;
278 }
279 
280 bool
282 {
283  // qDebug() << "XtandemParamSaxHandler::endElement_note begin " <<
284  // <note type="input" label="spectrum,
285  // path">/tmp/tandemwrapper-IehrEL/msdata.mzxml</note>
286 
287  if(m_currentLabel == "spectrum, path")
288  {
289  //<note type="input"
290  // label="spectrum,path">/gorgone/pappso/jouy/raw/2019_Lumos/20191222_107_Juste/20191222_18_EF1.mzXML</note>
291  // m_originMzDataFileName = m_currentText;
292  // p_writeXmlTandemOutput->writeCharacters(m_destinationMzXmlFileName);
294  p_writeXmlTandemOutput->writeEndElement();
295 
296 
297  for(auto pair_input : m_mapTandemInputParameters)
298  {
299  p_writeXmlTandemOutput->writeStartElement("note");
300  p_writeXmlTandemOutput->writeAttribute("type", "input");
301  p_writeXmlTandemOutput->writeAttribute("label", pair_input.first);
302  p_writeXmlTandemOutput->writeCharacters(pair_input.second);
303  p_writeXmlTandemOutput->writeEndElement();
304  }
305  }
306  else if(m_currentLabel == "output, path")
307  {
308  //<note type="input" label="output,
309  // path">/tmp/tandemwrapper-sSGxtE/output_tandem.xml</note>
310  p_writeXmlTandemOutput->writeCharacters(
311  QFileInfo(m_destinationTandemOutputFile).absoluteFilePath());
312  p_writeXmlTandemOutput->writeEndElement();
313  }
314  else
315  {
316  p_writeXmlTandemOutput->writeCharacters(m_currentText);
317  p_writeXmlTandemOutput->writeEndElement();
318  }
319  return true;
320 }
321 
322 void
323 XtandemOutputSaxHandler::setInputParameters(const QString &label_name_attribute,
324  const QString &input_value)
325 {
327  std::pair<QString, QString>(label_name_attribute, input_value));
328 }
329 } // namespace pappso
virtual const QString & qwhat() const
XtandemOutputSaxHandler(const QString &final_tandem_output, const QString &original_msdata_file_name)
bool error(const QXmlParseException &exception) override
void setInputParameters(const QString &label_name_attribute, const QString &input_value)
void writeOpenTag(const QString &qName, const QXmlAttributes &attributes)
bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &attributes) override
bool fatalError(const QXmlParseException &exception) override
bool startElement_note(QXmlAttributes attributes)
std::map< QString, QString > m_mapTandemInputParameters
bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName) override
bool characters(const QString &str) override
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
rewrites tandem xml output file with temporary files