masschroqWriter.cpp 10.3 KB
Newer Older
1

2
3
4
5
6
7
8
/**
 * \file masschroqWriter.cpp
 * \date January 03, 2011
 * \author Edlira Nano
 */

#include "masschroqWriter.h"
9
10
#include "../msrun/ms_run_hash_group.h"
#include "../peptides/peptide_isotope.h"
11
12
#include "../xic/xic_base.h"
#include "../../encode/decodeBinary.h"
13
14
#include "../mass_chroq.h"
#include "../share/utilities.h"
Olivier Langella's avatar
Olivier Langella committed
15
#include "../consoleout.h"
Olivier Langella's avatar
Olivier Langella committed
16
#include "../quanti_items/quantiItemBase.h"
17

18
#include <QXmlStreamReader>
19
20


21
MasschroqWriter::MasschroqWriter(
Olivier Langella's avatar
Olivier Langella committed
22
23
24
25
26
27
    const QString & in_filename,
    const QString & out_filename,
    const bool with_traces)
    :
    _quanti_item_counter(0),
    _input_filename(in_filename),
28
29
    _enc_precision(64),
    _enc_little_endian("true")
Olivier Langella's avatar
Olivier Langella committed
30
{
31
    QFileInfo outFileInfo(out_filename);
Olivier Langella's avatar
Olivier Langella committed
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
    QString outDir(outFileInfo.path());
    QFileInfo outDirInfo(outDir);
    if (! outDirInfo.exists()) {
        throw mcqError(QObject::tr("cannot find the output directory '%1' of output file '%2' \n").arg(outDir, out_filename));
    }

    QString complete_out_filename = out_filename;
    complete_out_filename.append(".xml");
    _output_file = new QFile(complete_out_filename);
    if (_output_file->exists())
    {
        mcqout() << "WARNING: XML output file '"
                 << complete_out_filename
                 << "' already exists, it will be overwrited."
                 << endl;
    }

    if (_output_file->open(QIODevice::WriteOnly))
    {
        _output_stream = new MCQXmlStreamWriter();
        _output_stream->setDevice(_output_file);
    } else
    {
        throw mcqError(QObject::tr("error : cannot open the XML output file : %1\n").arg(out_filename));
    }

    _output_stream->setAutoFormatting(true);
    writeInputParameters();
60
61
}

62
63
MasschroqWriter::~MasschroqWriter()
{
Olivier Langella's avatar
Olivier Langella committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    debriefing();
    if (_output_stream != 0)
    {
        _output_stream->writeEndDocument();
        delete _output_stream;
        _output_stream = 0;
    }

    if (_output_file != 0)
    {
        _output_file->close();
        delete _output_file;
        _output_file = 0;
    }
78
79
}

80

81
void
82
83
MasschroqWriter::writeInputParameters()
{
Olivier Langella's avatar
Olivier Langella committed
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
    // parse MassChroQ's input XML file
    QFile infile(_input_filename);

    if (!infile.open(QIODevice::ReadOnly))
    {
        throw mcqError(QObject::tr("MasschroqWriter : error reading MassChroQ XML input file '%1'\n").arg(_input_filename));
    }

    QXmlStreamReader reader(&infile);
    while (!reader.atEnd() && !reader.hasError())
    {
        QXmlStreamReader::TokenType token = reader.readNext();
        if (token == QXmlStreamReader::StartDocument)
        {
            _output_stream->writeStartDocument();
        }
        else if (token == QXmlStreamReader::StartElement)
        {
            QString name = reader.name().toString();
            _output_stream->writeStartElement(reader.namespaceUri().toString(), name);
            _output_stream->writeAttributes(reader.attributes());
            if (name =="masschroq")
            {
                QString mcq_xml_version(MASSCHROQ_SCHEMA_VERSION);
                _output_stream->writeAttribute("version", mcq_xml_version);
                QString type("output");
                _output_stream->writeAttribute("type", type);
            }
        }
        else if (token == QXmlStreamReader::Comment)
        {
            _output_stream->writeComment(reader.text().toString());
        }
        else if (token == QXmlStreamReader::Characters)
        {
            _output_stream->writeCharacters(reader.text().toString());
        }
        else if (token == QXmlStreamReader::EndElement &&
                 reader.name() != "masschroq")
        {
            _output_stream->writeEndElement();
        }
    }

    if (reader.hasError())
    {
        QString line, col;
        line.setNum(reader.lineNumber());
        col.setNum(reader.columnNumber());
        throw mcqError(QObject::tr("MasschroqWriter : error reading MassChroQ XML input file '%1':\n at line %2, column %3 : ").arg(_input_filename, line, col, reader.errorString()));
    }

    if (reader.atEnd()) {
        initialiseOutput();
    }
139
140
}

Olivier Langella's avatar
Olivier Langella committed
141
void
142
143
MasschroqWriter::initialiseOutput()
{
Olivier Langella's avatar
Olivier Langella committed
144
145
146
147
    _output_stream->writeStartElement("results");
    QDateTime date_time(QDateTime::currentDateTime());
    _output_stream->writeAttribute("date", date_time);
    _output_stream->writeAttribute("input_file", _input_filename);
148
149
150
151
152
}

void
MasschroqWriter::printAlignmentResults(const Msrun * msrun)
{
Olivier Langella's avatar
Olivier Langella committed
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    qDebug() << "MasschroqWriter::printAlignmentResults begin";
    //check  this was not already written
    bool already_written = (find(_written_align_results.begin(), _written_align_results.end(), msrun) != _written_align_results.end());
    if (already_written) {
    }
    else {

        _output_stream->writeStartElement("alignment_result");
        // get the time values for this msrun and encode them
        std::vector<mcq_double> v_time = msrun->getVectorOfTimeValues();
        QByteArray enc_time_array = DecodeBinary::base64_encode_doubles(v_time);
        const QString enc_time = QString::fromAscii(enc_time_array.constData());
        _output_stream->writeAttribute("precision", _enc_precision);
        _output_stream->writeAttribute("little_endian", _enc_little_endian);
        _output_stream->writeCharacters(enc_time);
        _output_stream->writeEndElement(); // </alignment_result>

        _written_align_results.push_back(msrun);
    }
    qDebug() << "MasschroqWriter::printAlignmentResults end";
174
175
176
}

void
Olivier Langella's avatar
Olivier Langella committed
177
178
MasschroqWriter::writeQuantifyBegin(const Quantificator * p_quantificator)
{
179
    qDebug() << "MasschroqWriter::writeQuantifyBegin begin";
Olivier Langella's avatar
Olivier Langella committed
180
181
182
183
184
185
    _p_quantificator = p_quantificator;
    _current_group_id = p_quantificator->getMsRunGroup()->getXmlId();

    _current_quantify_id = _p_quantificator->getXmlId();
    _output_stream->writeStartElement("result");
    _output_stream->writeAttribute("quantify_id", _p_quantificator->getXmlId());
186
    qDebug() << "MasschroqWriter::writeQuantifyBegin end";
Olivier Langella's avatar
Olivier Langella committed
187

188
189
190
}

void
Olivier Langella's avatar
Olivier Langella committed
191
MasschroqWriter::writeQuantifyEnd()
192
{
193
    qDebug() << "MasschroqWriter::writeQuantifyEnd begin";
Olivier Langella's avatar
Olivier Langella committed
194
    _current_group_id.clear();
195
196
197
198

    if (_p_msrun != nullptr) {
        setEndCurrentMsrun();
    }
Olivier Langella's avatar
Olivier Langella committed
199
    _output_stream->writeEndElement(); // </result>
200
201
202
    
    _written_align_results.clear();
    qDebug() << "MasschroqWriter::writeQuantifyEnd end";
203
}
204

Olivier Langella's avatar
Olivier Langella committed
205

Olivier Langella's avatar
Olivier Langella committed
206
void
207
208
MasschroqWriter::setCurrentMsrun(const Msrun * msrun)
{
Olivier Langella's avatar
Olivier Langella committed
209
210
211
212
213
214
215
216
217
218
219
220
    if (_p_msrun != msrun) {
        if (_p_msrun != nullptr) {
            setEndCurrentMsrun();
        }
        _p_msrun = msrun;
        _output_stream->writeStartElement("data");
        _output_stream->writeAttribute("id_ref", msrun->getXmlId());
        if (msrun->hasBeenAligned())
        {
            printAlignmentResults(msrun);
        }
    }
221
222
}

223

Olivier Langella's avatar
Olivier Langella committed
224
void
225
226
MasschroqWriter::setEndCurrentMsrun()
{
Olivier Langella's avatar
Olivier Langella committed
227
    _output_stream->writeEndElement(); // </data>
228
    _p_msrun = nullptr;
229
230
}

Olivier Langella's avatar
Olivier Langella committed
231
232
void MasschroqWriter::privWriteMatchedPeak(const Msrun * p_msrun, const QuantiItemBase * p_quanti_item, const AlignedXicPeak * peak) {

233
    qDebug() << "MasschroqWriter::privWriteMatchedPeak begin";
Olivier Langella's avatar
Olivier Langella committed
234
235
236
237
238
239
240
241
    setCurrentMsrun(p_msrun);
    _output_stream->writeStartElement("quanti_item");

    QString quanti_item_id("quanti_item"), item_counter;
    item_counter.setNum(_quanti_item_counter);
    quanti_item_id.append(item_counter);
    _output_stream->writeAttribute("id", quanti_item_id);

Olivier Langella's avatar
Olivier Langella committed
242
243
244
    p_quanti_item->writeCurrentSearchItem(_output_stream);


Olivier Langella's avatar
Olivier Langella committed
245
246
247
248
249
250
    _quanti_item_counter++;
    // if il y a des xics à tracer
    // writeStartElement("xic_traces");



251
    mcq_double currentMz = p_quanti_item->getMz();
Olivier Langella's avatar
Olivier Langella committed
252
253
254
255
256
    const Peptide * current_peptide(p_quanti_item->getPeptide());
    QString pepId, isotopeLabel;
    unsigned int currentZ;


257
258
    if (peak != nullptr) {
        _output_stream->writeStartElement("quantification_data");
Olivier Langella's avatar
Olivier Langella committed
259

260
261
262
263
264
265
        _output_stream->writeAttribute("mz", currentMz);
        _output_stream->writeAttribute("rt", peak->getMaxXicElement().rt);
        _output_stream->writeAttribute("max_intensity", peak->getMaxXicElement().intensity);
        _output_stream->writeAttribute("area", peak->getArea());
        _output_stream->writeAttribute("rt_begin", peak->getLeftBoundary().rt);
        _output_stream->writeAttribute("rt_end", peak->getRightBoundary().rt);
Olivier Langella's avatar
Olivier Langella committed
266

267
        if  (current_peptide != 0)
Olivier Langella's avatar
Olivier Langella committed
268
        {
269
270
            pepId = current_peptide->getXmlId();
            _output_stream->writeAttribute("peptide", pepId);
Olivier Langella's avatar
Olivier Langella committed
271

272
273
274
275
276
            if (current_peptide->getIsotopeLabel() != 0)
            {
                isotopeLabel = current_peptide->getIsotopeLabel()->getXmlId();
                _output_stream->writeAttribute("isotope", isotopeLabel);
            }
Olivier Langella's avatar
Olivier Langella committed
277

278
279
280
281
282
            currentZ = *p_quanti_item->getZ();
            _output_stream->writeAttribute("z", currentZ);
        }
        _output_stream->writeEndElement(); // "</quantification_data>"
    }
Olivier Langella's avatar
Olivier Langella committed
283
284


Olivier Langella's avatar
Olivier Langella committed
285
    _output_stream->writeEndElement(); // </quanti_item>
286
287

    qDebug() << "MasschroqWriter::privWriteMatchedPeak end";
288
}
289

290

Edlira Nano's avatar
Edlira Nano committed
291
292
293



Olivier Langella's avatar
Olivier Langella committed
294
void
295
296
MasschroqWriter::debriefing()
{
Olivier Langella's avatar
Olivier Langella committed
297
298
    // write protein results

299
300
    qDebug() << "MasschroqWriter::debriefing begin";

Olivier Langella's avatar
Olivier Langella committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    _output_stream->writeStartElement("summary");
    QString mcq_version(MASSCHROQ_VERSION);
    _output_stream->writeAttribute("masschroq_version", mcq_version);
    _output_stream->writeStartElement("execution_time");

    QDateTime dt_end_xml = QDateTime::currentDateTime();
    const unsigned int secs_elapsed = MassChroq::begin_date_time.secsTo(dt_end_xml);
    _output_stream->writeAttribute("seconds", secs_elapsed);
    const Duration dur =
        Utilities::getDurationFromDates(MassChroq::begin_date_time,
                                        dt_end_xml);
    QString days, hours, minutes, secs;
    days.setNum(Utilities::getDaysFromDuration(dur));
    hours.setNum(Utilities::getHoursFromDuration(dur));
    minutes.setNum(Utilities::getMinutesFromDuration(dur));
    secs.setNum(Utilities::getSecondsFromDuration(dur));
    QString xml_duration_format = QString("P0Y0M");
    xml_duration_format.append(days).append(QString("DT"));
    xml_duration_format.append(hours).append(QString("H"));
    xml_duration_format.append(minutes).append(QString("M"));
    xml_duration_format.append(secs).append(QString("S"));
    _output_stream->writeAttribute("duration", xml_duration_format);
    _output_stream->writeEndElement(); // </execution_time>
    _output_stream->writeEndElement(); // </summary>
325
    _output_stream->writeEndElement(); // </results>
Olivier Langella's avatar
Olivier Langella committed
326
327
    _output_stream->writeEndDocument();
    _output_file->close();
328
    qDebug() << "MasschroqWriter::debriefing end";
329
}