monitorodsoutput.cpp 11.9 KB
Newer Older
Olivier Langella's avatar
Olivier Langella committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

/*******************************************************************************
 * Copyright (c) 2016 Olivier Langella <Olivier.Langella@moulon.inra.fr>.
 *
 * This file is part of the MassChroQ software.
 *
 *     MassChroQ is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     MassChroQ is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with MassChroQ.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
Langella Olivier's avatar
Langella Olivier committed
21
22
 *     Olivier Langella <Olivier.Langella@moulon.inra.fr> - initial API and
 *implementation
Olivier Langella's avatar
Olivier Langella committed
23
24
25
26
 ******************************************************************************/

#include "monitorodsoutput.h"
#include "../peptides/peptide.h"
27
#include "../quantificator.h"
28
#include "../share/utilities.h"
Olivier Langella's avatar
Olivier Langella committed
29
#include <QDir>
Langella Olivier's avatar
Langella Olivier committed
30
#include <odsstream/writer/options/odstablesettings.h>
31
#include <pappsomspp/processing/detection/tracedetectionzivy.h>
Olivier Langella's avatar
Olivier Langella committed
32

Langella Olivier's avatar
Langella Olivier committed
33
34
35
36
37
MonitorTsvOutput::MonitorTsvOutput(const QString &output_directory)
{
  try
    {
      _p_tsv_directory_writer = new TsvDirectoryWriter(QDir(output_directory));
38
      _p_tsv_directory_writer->setFlushLines(true);
Langella Olivier's avatar
Langella Olivier committed
39
40
41
42
43
44
45
46
    }
  catch(OdsException &error)
    {
      throw mcqError(
        QObject::tr("Error writing TSV ouput in directory %1 :\n%2")
          .arg(output_directory)
          .arg(error.qwhat()));
    }
Langella Olivier's avatar
Langella Olivier committed
47
  _p_writer = _p_tsv_directory_writer;
Olivier Langella's avatar
Olivier Langella committed
48
}
49

Langella Olivier's avatar
Langella Olivier committed
50
51
MonitorTsvOutput::~MonitorTsvOutput()
{
52

53
  qDebug();
Olivier Langella's avatar
Olivier Langella committed
54

Langella Olivier's avatar
Langella Olivier committed
55
  _p_writer->close();
56

Langella Olivier's avatar
Langella Olivier committed
57
  delete _p_tsv_directory_writer;
Olivier Langella's avatar
Olivier Langella committed
58
59
}

Langella Olivier's avatar
Langella Olivier committed
60
61
62
63
64
65
66
67
68
MonitorOdsOutput::MonitorOdsOutput(const QString &ods_file)
{
  try
    {
      _p_ods_writer = new OdsDocWriter(ods_file);
    }
  catch(OdsException &error)
    {
      throw mcqError(QObject::tr("Error writing ODS file %1 :\n%2")
Langella Olivier's avatar
Langella Olivier committed
69
70
                       .arg(ods_file)
                       .arg(error.qwhat()));
Langella Olivier's avatar
Langella Olivier committed
71
    }
Langella Olivier's avatar
Langella Olivier committed
72
  _p_writer = _p_ods_writer;
73
74
}

Langella Olivier's avatar
Langella Olivier committed
75
76
MonitorOdsOutput::~MonitorOdsOutput()
{
77
  qDebug();
78

Langella Olivier's avatar
Langella Olivier committed
79
80
  _p_writer->close();
  delete _p_ods_writer;
81
82
}

Langella Olivier's avatar
Langella Olivier committed
83
84
85
MonitorOdsInterfaceOutput::MonitorOdsInterfaceOutput()
{
}
Olivier Langella's avatar
Olivier Langella committed
86

Langella Olivier's avatar
Langella Olivier committed
87
88
MonitorOdsInterfaceOutput::~MonitorOdsInterfaceOutput()
{
89
  qDebug();
90
91
}

Langella Olivier's avatar
Langella Olivier committed
92
93
94
95
void
MonitorOdsInterfaceOutput::writeQuantifyBegin(
  const Quantificator *p_quantificator)
{
96
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
97
  _current_group_id          = p_quantificator->getMsRunGroup()->getXmlId();
Langella Olivier's avatar
Langella Olivier committed
98
  _current_quantification_id = p_quantificator->getXmlId();
Langella Olivier's avatar
Langella Olivier committed
99
100
  try
    {
101

Langella Olivier's avatar
Langella Olivier committed
102
103
      _p_writer->writeSheet(
        QString("MassChroQ informations - %1").arg(_current_quantification_id));
104

105
      _p_writer->writeCell("MassChroQ version");
106
      _p_writer->writeCell(QString("%1").arg(MASSCHROQ_VERSION));
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
      _p_writer->writeLine();

      // alignment parameters
      // ms2_tendency_halfwindow \t 10
      // ms2_smoothing_halfwindow \t 15
      // ms1_smoothing_halfwindow \t 0

      // extraction parameters
      _p_writer->writeCell("extraction range lower limit");
      _p_writer->writeCell(p_quantificator->getQuantificationMethod()
                             ->getLowerPrecision()
                             ->toString());
      _p_writer->writeLine();
      _p_writer->writeCell("extraction range upper limit");
      _p_writer->writeCell(p_quantificator->getQuantificationMethod()
                             ->getUpperPrecision()
                             ->toString());
      _p_writer->writeLine();
      _p_writer->writeCell("natural isotope minimum abundance");
      _p_writer->writeCell(p_quantificator->getNiMinimumAbundance());
      _p_writer->writeLine();
      _p_writer->writeCell("matching mode");
      _p_writer->writeCell(
        Utilities::toString(p_quantificator->getMatchingMode()));
      _p_writer->writeLine();

      const pappso::FilterSuite &filters =
        p_quantificator->getQuantificationMethod()->getXicFilters();

      for(auto &&one_filter : filters)
        {
          const pappso::FilterNameInterface *p_filter =
            dynamic_cast<const pappso::FilterNameInterface *>(one_filter.get());
          if(p_filter != nullptr)
            {
              _p_writer->writeCell(p_filter->toString());
              _p_writer->writeLine();
            }
        }


      const pappso::TraceDetectionInterface *p_detection =
        p_quantificator->getQuantificationMethod()->getDetectionMethod().get();
      if(p_detection != nullptr)
        {
          const pappso::TraceDetectionZivy *p_detectionZivy =
            dynamic_cast<const pappso::TraceDetectionZivy *>(p_detection);
          if(p_detectionZivy != nullptr)
            {
              _p_writer->writeCell("detection Zivy");
              _p_writer->writeLine();
              _p_writer->writeCell("smoothing half edge window");
              _p_writer->writeCell(
                (std::size_t)p_detectionZivy->getSmoothingHalfEdgeWindows());
              _p_writer->writeLine();
              _p_writer->writeCell("maxmin half edge window");
              _p_writer->writeCell(
                (std::size_t)p_detectionZivy->getMaxMinHalfEdgeWindows());
              _p_writer->writeLine();
              _p_writer->writeCell("minmax half edge window");
              _p_writer->writeCell(
                (std::size_t)p_detectionZivy->getMinMaxHalfEdgeWindows());
              _p_writer->writeLine();
              _p_writer->writeCell("detection threshold on maxmin");
              _p_writer->writeCell(
                (std::size_t)p_detectionZivy->getDetectionThresholdOnMaxmin());
              _p_writer->writeLine();
              _p_writer->writeCell("detection threshold on minmax");
              _p_writer->writeCell(
                (std::size_t)p_detectionZivy->getDetectionThresholdOnMinmax());
              _p_writer->writeLine();
            }
        }


182
183
184
185
      _p_writer->writeLine();
      _p_writer->writeLine();


Langella Olivier's avatar
Langella Olivier committed
186
      const std::vector<const Peptide *> &peptide_list =
Langella Olivier's avatar
Langella Olivier committed
187
        p_quantificator->getPeptideList();
Langella Olivier's avatar
Langella Olivier committed
188
189
190
191
      if(!_peptide_list_written.contains(_current_group_id))
        {
          this->writePeptideList(peptide_list);
        }
Langella Olivier's avatar
Langella Olivier committed
192

Langella Olivier's avatar
Langella Olivier committed
193
194
      //_p_writer->writeLine();
      _p_writer->writeSheet(QString("peptides_%1_%2")
Langella Olivier's avatar
Langella Olivier committed
195
196
197
                              .arg(p_quantificator->getXmlId())
                              .arg(_current_group_id));

Langella Olivier's avatar
Langella Olivier committed
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
      OdsTableSettings settings;
      settings.setVerticalSplit(1);
      _p_writer->setCurrentOdsTableSettings(settings);

      _p_writer->setCellAnnotation("quantification XML id");
      _p_writer->writeCell("quantification");
      _p_writer->setCellAnnotation("group XML id (fraction name)");
      _p_writer->writeCell("group");
      _p_writer->setCellAnnotation("MS run XML id (sample id)");
      _p_writer->writeCell("msrun");
      _p_writer->setCellAnnotation("MS run file path");
      _p_writer->writeCell("msrunfile");
      _p_writer->setCellAnnotation("XIC m/z");
      _p_writer->writeCell("mz");
      _p_writer->setCellAnnotation("peak maximum intensity retention time");
      _p_writer->writeCell("rt");
      _p_writer->setCellAnnotation("peak maximum intensity");
      _p_writer->writeCell("maxintensity");
      _p_writer->setCellAnnotation("peak area");
      _p_writer->writeCell("area");
      _p_writer->setCellAnnotation("peak start retention time");
      _p_writer->writeCell("rtbegin");
      _p_writer->setCellAnnotation("peak stop retention time");
      _p_writer->writeCell("rtend");
      _p_writer->setCellAnnotation("peptide id");
      _p_writer->writeCell("peptide");
224
225
      _p_writer->setCellAnnotation("isotope label");
      _p_writer->writeCell("label");
Langella Olivier's avatar
Langella Olivier committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
      _p_writer->setCellAnnotation("peptide sequence");
      _p_writer->writeCell("sequence");
      _p_writer->setCellAnnotation("peptide charge");
      _p_writer->writeCell("z");
      _p_writer->setCellAnnotation("peptide modifications (free text)");
      _p_writer->writeCell("mods");
      if(p_quantificator->getNiMinimumAbundance() != 0)
        {
          _p_writer->setCellAnnotation("natural isotope number");
          _p_writer->writeCell("ninumber");
          _p_writer->setCellAnnotation("natural isotope rank");
          _p_writer->writeCell("nirank");
          _p_writer->setCellAnnotation("natural isotope theoretical ratio");
          _p_writer->writeCell("niratio");
        }
241
242
243


      _p_writer->writeLine();
244
    }
Langella Olivier's avatar
Langella Olivier committed
245
246
247
  catch(OdsException &ods_error)
    {
      throw mcqError(
Langella Olivier's avatar
Langella Olivier committed
248
        QObject::tr("error writing quantification for group %1 :\n%2")
Langella Olivier's avatar
Langella Olivier committed
249
250
251
          .arg(_current_group_id)
          .arg(ods_error.qwhat()));
    }
252
  qDebug();
Olivier Langella's avatar
Olivier Langella committed
253
254
}

Langella Olivier's avatar
Langella Olivier committed
255
256
257
258
void
MonitorOdsInterfaceOutput::writePeptideList(
  const std::vector<const Peptide *> &isotope_peptides)
{
259
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
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
  try
    {
      _peptide_list_written.append(_current_group_id);
      _p_writer->writeSheet(QString("%1_proteins").arg(_current_group_id));
      //_p_writer->writeLine();
      _p_writer->writeCell("peptide");
      _p_writer->writeCell("protein");
      _p_writer->writeCell("protein_description");

      OdsTableSettings settings;
      settings.setVerticalSplit(1);
      _p_writer->setCurrentOdsTableSettings(settings);

      std::vector<std::pair<QString, QString>> prot_pep_ids;

      auto itpep    = isotope_peptides.begin();
      auto itpepend = isotope_peptides.end();
      for(; itpep != itpepend; ++itpep)
        {
          auto itprot    = (*itpep)->getProteinList().begin();
          auto itprotend = (*itpep)->getProteinList().end();
          for(; itprot != itprotend; ++itprot)
            {
              std::pair<QString, QString> prot_pep = {(*itpep)->getXmlId(),
                                                      (*itprot)->getXmlId()};
              if(std::find_if(
                   prot_pep_ids.begin(),
                   prot_pep_ids.end(),
                   [prot_pep](std::pair<QString, QString> &pair_pep_prot) {
                     return ((pair_pep_prot.first == prot_pep.first) &&
                             (pair_pep_prot.second == prot_pep.second));
                   }

                   ) == prot_pep_ids.end())
                {
                  _p_writer->writeLine();
                  _p_writer->writeCell((*itpep)->getXmlId());
                  _p_writer->writeCell((*itprot)->getXmlId());
                  _p_writer->writeCell((*itprot)->getDescription());
                  prot_pep_ids.push_back(prot_pep);
300
                }
Langella Olivier's avatar
Langella Olivier committed
301
            }
302
        }
303
    }
Langella Olivier's avatar
Langella Olivier committed
304
305
306
307
308
309
310
  catch(OdsException &ods_error)
    {
      throw mcqError(
        QObject::tr("error writing peptide list for group %1 :\n%2")
          .arg(_current_group_id)
          .arg(ods_error.qwhat()));
    }
Olivier Langella's avatar
Olivier Langella committed
311

312
  qDebug();
313
}
Olivier Langella's avatar
Olivier Langella committed
314

Langella Olivier's avatar
Langella Olivier committed
315
316
317
318
void
MonitorOdsInterfaceOutput::writeQuantifyEnd()
{
}
Olivier Langella's avatar
Olivier Langella committed
319
320

// print results
Langella Olivier's avatar
Langella Olivier committed
321
322
323
324
325
326
void
MonitorOdsInterfaceOutput::privWriteMatchedPeak(
  const Msrun *p_msrun,
  const QuantiItemBase *p_quanti_item,
  const AlignedXicPeak *peak)
{
Langella Olivier's avatar
Langella Olivier committed
327
  try
Langella Olivier's avatar
Langella Olivier committed
328
    {
Langella Olivier's avatar
Langella Olivier committed
329
330
331
332
333
334
335
336
337
338
      if(peak != nullptr)
        {
          _p_writer->writeCell(_current_quantification_id);
          _p_writer->writeCell(_current_group_id);
          _p_writer->writeCell(p_msrun->getMsRunIdCstSPtr()->getXmlId());
          _p_writer->writeCell(
            QFileInfo(p_msrun->getMsRunIdCstSPtr()->getFileName()).baseName());
          _p_writer->writeCell(p_quanti_item->getMz());
          _p_writer->writeCell(peak->getMaxXicElement().x);
          _p_writer->writeCell(peak->getMaxXicElement().y);
Langella Olivier's avatar
Langella Olivier committed
339

Langella Olivier's avatar
Langella Olivier committed
340
341
342
          _p_writer->writeCell(peak->getArea());
          _p_writer->writeCell(peak->getLeftBoundary().x);
          _p_writer->writeCell(peak->getRightBoundary().x);
Langella Olivier's avatar
Langella Olivier committed
343

Langella Olivier's avatar
Langella Olivier committed
344
          p_quanti_item->writeOdsPeptideLine(*_p_writer);
345
346

          _p_writer->writeLine();
Langella Olivier's avatar
Langella Olivier committed
347
348
349
350
351
352
353
354
        }
    }
  catch(OdsException &error)
    {
      throw mcqError(
        QObject::tr(
          "Error in MonitorOdsInterfaceOutput::privWriteMatchedPeak :\n%1")
          .arg(error.qwhat()));
Langella Olivier's avatar
Langella Olivier committed
355
    }
356
}