xicbox.cpp 27.7 KB
Newer Older
Langella Olivier's avatar
Langella Olivier committed
1
2
3
4
5
6
7
/**
 * \file src/gui/xic_view/xic_box/xicbox.cpp
 * \date 12/1/2018
 * \author Olivier Langella
 * \brief XIC box widget
 */
/*******************************************************************************
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * Copyright (c) 2018 Olivier Langella <olivier.langella@u-psud.fr>.
 *
 * This file is part of XTPcpp.
 *
 *     XTPcpp 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.
 *
 *     XTPcpp 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 XTPcpp.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     Olivier Langella <olivier.langella@u-psud.fr> - initial API and
 *implementation
 ******************************************************************************/
Langella Olivier's avatar
Langella Olivier committed
29
30
31
32

#include "xicbox.h"
#include "ui_xic_box.h"
#include <QMessageBox>
Langella Olivier's avatar
Langella Olivier committed
33
#include <pappsomspp/exception/exceptionnotfound.h>
Langella Olivier's avatar
Langella Olivier committed
34
#include "../xicworkerthread.h"
35
#include "../../project_view/projectwindow.h"
36
#include <pappsomspp/processing/detection/tracedetectionzivy.h>
Langella Olivier's avatar
Langella Olivier committed
37
38
39
#include <pappsomspp/exception/exceptionnotpossible.h>


40
bool
41
XicBoxNaturalIsotope::contains(const pappso::TracePeakCstSPtr &peak) const
42
{
43
  for(const pappso::TracePeakCstSPtr &peak_i : detected_peak_list)
44
45
46
47
    {
      if(peak_i.get() == peak.get())
        {
          return true;
48
49
        }
    }
50
  return false;
51
}
Langella Olivier's avatar
Langella Olivier committed
52

Langella Olivier's avatar
Langella Olivier committed
53
void
54
55
XicBoxNaturalIsotope::computeMeanIntensityIn(
  const std::vector<std::size_t> &rt_list)
Langella Olivier's avatar
Langella Olivier committed
56
57
58
{
  meanIntensityInCurrentPeak = 0;

59
  for(auto indice : rt_list)
Langella Olivier's avatar
Langella Olivier committed
60
    {
61
62
      qDebug() << xic_sp.get()->at(indice).y;
      meanIntensityInCurrentPeak += xic_sp.get()->at(indice).y;
Langella Olivier's avatar
Langella Olivier committed
63
64
65
    }

  meanIntensityInCurrentPeak /= rt_list.size();
66
  qDebug() << meanIntensityInCurrentPeak;
Langella Olivier's avatar
Langella Olivier committed
67
68
69
}


Langella Olivier's avatar
Langella Olivier committed
70
class XicDetectionList : public pappso::TraceDetectionSinkInterface
71
72
73
74
75
76
77
78
79
80
81
82
{
  public:
  XicDetectionList()
  {
    _max_peak = nullptr;
  }
  void
  setPeptideEvidenceList(
    const std::vector<const PeptideEvidence *> &peptide_evidence_list)
  {
    for(const PeptideEvidence *p_evidence : peptide_evidence_list)
      {
83
        _rt_list.push_back(p_evidence->getHardenedRetentionTime());
84
85
86
      }
  }
  void
87
  setTracePeak(pappso::TracePeak &xic_peak) override
88
  {
89
    pappso::TracePeakCstSPtr peak_sp = xic_peak.makeTracePeakCstSPtr();
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    _peak_list.push_back(peak_sp);

    for(pappso::pappso_double rt : _rt_list)
      {
        if(xic_peak.containsRt(rt))
          {
            if((_max_peak == nullptr) ||
               (xic_peak.getArea() > _max_peak.get()->getArea()))
              {
                _max_peak = peak_sp;
              }
          }
      }
  };
104
  const pappso::TracePeakCstSPtr &
105
106
107
108
  getMatchedPeak() const
  {
    return _max_peak;
  }
109
  const std::vector<pappso::TracePeakCstSPtr> &
110
111
112
113
114
115
116
117
118
119
120
121
122
  getXicPeakList() const
  {
    return _peak_list;
  };
  void
  clear()
  {
    _peak_list.clear();
    _max_peak = nullptr;
  };

  private:
  unsigned int _count = 0;
123
  std::vector<pappso::TracePeakCstSPtr> _peak_list;
124
  std::vector<pappso::pappso_double> _rt_list;
125
  pappso::TracePeakCstSPtr _max_peak;
Langella Olivier's avatar
Langella Olivier committed
126
127
128
};


129
XicBox::XicBox(XicWindow *parent) : QWidget(parent), ui(new Ui::XicBox)
Langella Olivier's avatar
Langella Olivier committed
130
{
131
132
  _p_xic_window = parent;
  ui->setupUi(this);
Langella Olivier's avatar
Langella Olivier committed
133

134
  XicWorkerThread *p_worker = new XicWorkerThread(this);
135
136
  p_worker->moveToThread(&_xic_thread);
  _xic_thread.start();
Langella Olivier's avatar
Langella Olivier committed
137

138

139
140
141
  // ui->histo_widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom );
  // ui->xic_list_widget->setLayout(new QVBoxLayout(ui->xic_list_widget));
  ui->histo_widget->legend->setVisible(true);
Langella Olivier's avatar
Langella Olivier committed
142

143
144
145
146
147
148
149
150
  // Set the Loading icon
  mp_loadingMovie = new QMovie(":icons/resources/icons/icon_wait.gif");
  //   mp_loadingMovie->setScaledSize(QSize(100, 100));
  ui->loading_label->setMovie(mp_loadingMovie);
  mp_loadingMovie->start();

  showLoadingIcon(true);

151
152
153
  if(_p_xic_window->isRetentionTimeSeconds())
    {
      ui->xic_widget->setRetentionTimeInSeconds();
154
    }
155
156
157
  else
    {
      ui->xic_widget->setRetentionTimeInMinutes();
158
159
    }

Langella Olivier's avatar
Langella Olivier committed
160
#if QT_VERSION >= 0x050000
161
162
163
164
165
166
  // Qt5 code
  connect(this, &XicBox::loadXic, p_worker, &XicWorkerThread::doXicLoad);
  connect(p_worker, &XicWorkerThread::xicLoaded, this, &XicBox::setXic);
  connect(p_worker, &XicWorkerThread::operationFailed, this, &XicBox::error);


167
168
169
  connect(this,
          &XicBox::computeIsotopeMassList,
          p_worker,
170
          &XicWorkerThread::doComputeIsotopeMassList);
171
172
173
  connect(p_worker,
          &XicWorkerThread::isotopeMassListComputed,
          this,
174
175
          &XicBox::setIsotopeMassList);

176
177
  connect(
    _p_xic_window, &XicWindow::reExtractXicNeeded, this, &XicBox::reExtractXic);
178
179
180
  /*  connect(ui->xic_widget,
          qOverload<std::vector<std::pair<pappso::XicCstSPtr,
  pappso::XicPeakSp>>>( &pappso::XicWidget::xicPeakListChanged), this,
181
          &XicBox::setXicPeakList);
182

183
184
185
  connect(ui->xic_widget,
          &pappso::XicWidget::clicked,
          this,
186
          &XicBox::onXicWidgetClick);
187
*/
188
189
190
  connect(_p_xic_window,
          &XicWindow::rtUnitChangeNeeded,
          this,
191
          &XicBox::onRtUnitChanged);
192
193
194
195
196
197
198
199
200
201
202
203
204
205

  /** @brief Handle axis size to move simultaneously in boxs
   */
  connect(ui->xic_widget->getRtAxisP(),
          qOverload<const QCPRange &>(&QCPAxis::rangeChanged),
          this,
          &XicBox::doGetNewRtRange);
  connect(ui->xic_widget->getIntensityAxisP(),
          qOverload<const QCPRange &>(&QCPAxis::rangeChanged),
          this,
          &XicBox::doGetNewIntensityRange);
  connect(
    this, &XicBox::fitAxisRangeInXics, _p_xic_window, &XicWindow::doFitAllAxis);

Langella Olivier's avatar
Langella Olivier committed
206
207
208
209
210
#else
// Qt4 code
#endif


211
212
213
214
215
216
  QStringList msrun_list;
  for(MsRunSp msrun_sp : _p_xic_window->getProjectWindow()
                           ->getProjectP()
                           ->getMsRunStore()
                           .getMsRunList())
    {
217
      msrun_list << msrun_sp.get()->getFileName();
218
      qDebug() << "ProteinListWindow::setIdentificationGroup "
219
               << msrun_sp.get()->getFileName();
Langella Olivier's avatar
Langella Olivier committed
220
    }
Langella Olivier's avatar
Langella Olivier committed
221
222
223
224
}

XicBox::~XicBox()
{
225
226
227
  qDebug() << "XicBox::~XicBox";
  _xic_thread.quit();
  _xic_thread.wait();
Langella Olivier's avatar
Langella Olivier committed
228
229
}

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
void
XicBox::rescaleXicWidget(QString axis_name, QCPRange new_range)
{
  ui->xic_widget->rescaleOneRange(axis_name, new_range);
}

void
XicBox::doGetNewRtRange(QCPRange new_range)
{
  emit fitAxisRangeInXics("xAxis", new_range);
}

void
XicBox::doGetNewIntensityRange(QCPRange new_range)
{
  emit fitAxisRangeInXics("yAxis", new_range);
}

Renne Thomas's avatar
Renne Thomas committed
248
249
250
251
252
253
QCPRange
XicBox::getRtRange()
{
  return ui->xic_widget->getRtAxisP()->range();
}

Renne Thomas's avatar
Renne Thomas committed
254
255
256
257
258
ZivyParams
XicBox::getZivyParamsUsed()
{
  return _p_xic_window->getZivyParams();
}
259

260
void
Langella Olivier's avatar
Langella Olivier committed
261
262
XicBox::onXicWidgetClick(double rt [[maybe_unused]],
                         double intensity [[maybe_unused]])
263
{
Langella Olivier's avatar
Langella Olivier committed
264
  qDebug();
265
  ui->xic_widget->clearXicPeakBorders();
266
  std::vector<pappso::TracePeakCstSPtr> draw_peak_borders;
267
268
  for(XicBoxNaturalIsotope peak : _natural_isotope_list)
    {
Langella Olivier's avatar
Langella Olivier committed
269
      qDebug();
270
271
272
273
      draw_peak_borders.push_back(peak.one_peak_sp);
      if(peak.one_peak_sp.get() != nullptr)
        {
          ui->xic_widget->drawXicPeakBorders(peak.one_peak_sp);
274
275
276
        }
    }

277
278
279
  if(_peptide_evidence_list.size() == 0)
    {
      drawObservedAreaBars(draw_peak_borders);
Langella Olivier's avatar
Langella Olivier committed
280
    }
Langella Olivier's avatar
Langella Olivier committed
281
282
}

283
284
285
286
void
XicBox::remove()
{
  _p_xic_window->removeXicBox(this);
Langella Olivier's avatar
Langella Olivier committed
287
288
}

289
290
291
292
void
XicBox::error(QString error_message)
{
  QMessageBox::warning(this, tr("Error extracting XIC :"), error_message);
Langella Olivier's avatar
Langella Olivier committed
293
}
Langella Olivier's avatar
Langella Olivier committed
294

295
296
297
void
XicBox::extractXicInOtherMsRun()
{
298
  qDebug();
299
300
  try
    {
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
      QString location =
        QFileInfo(_p_peptide_evidence->getMsRunP()->getFileName())
          .absoluteDir()
          .absolutePath();
      QStringList filenames = QFileDialog::getOpenFileNames(
        this, tr("Open new mzXML"), location, tr("mzXML Files (*.mzXML)"));

      foreach(QString new_filename, filenames)
        {
          MsRunSp msrun_sp = _p_xic_window->getProjectWindow()
                               ->getProjectP()
                               ->getMsRunStore()
                               .getInstance(new_filename);
          _p_xic_window->addXicInMsRun(_p_peptide_evidence, msrun_sp);
        }
Langella Olivier's avatar
Langella Olivier committed
316
    }
317
318
  catch(pappso::ExceptionNotFound &error)
    {
319
      qDebug() << " not found ";
Langella Olivier's avatar
Langella Olivier committed
320
321
322
    }
}

323
324
325
void
XicBox::setPeptideEvidence(const PeptideEvidence *p_peptide_evidence)
{
326

327
  showLoadingIcon(true);
328
329
330
331
332
333
334
335
336
  _p_peptide_evidence = p_peptide_evidence;
  _msrun_sp = p_peptide_evidence->getIdentificationDataSource()->getMsRunSp();

  ui->peptide_label->setText(
    _p_peptide_evidence->getPeptideXtpSp().get()->toString());
  ui->charge_label->setText(
    QString("%1").arg(_p_peptide_evidence->getCharge()));
  ui->mz_label->setText(
    QString::number(_p_peptide_evidence->getTheoreticalMz(), 'f', 4));
337
338
339
  ui->msrun_label->setText(
    QFileInfo(_p_peptide_evidence->getMsRunP()->getFileName()).fileName());
  ui->msrun_label->setToolTip("Test2");
340
341
342
343
344
345
  // get same xic peptide evidence (msrun, peptide, charge)
  // p_projet
  _peptide_evidence_list.clear();
  _p_xic_window->getProjectWindow()
    ->getProjectP()
    ->getSameXicPeptideEvidenceList(
346
347
      _peptide_evidence_list,
      _msrun_sp.get(),
348
349
350
351
352
      _p_peptide_evidence->getPeptideXtpSp().get(),
      _p_peptide_evidence->getCharge());

  emit computeIsotopeMassList(_p_peptide_evidence->getPeptideXtpSp(),
                              _p_peptide_evidence->getCharge(),
353
                              _p_xic_window->getXicExtractPrecision(),
354
                              ui->isotopic_distribution_spinbox->value());
Langella Olivier's avatar
Langella Olivier committed
355
356
}

357
358
359
360
void
XicBox::setPeptideEvidenceInMsRun(const PeptideEvidence *p_peptide_evidence,
                                  MsRunSp msrun_sp)
{
361
  showLoadingIcon(true);
362
363
364
365
366
367
368
369
370
  _p_peptide_evidence = p_peptide_evidence;
  _msrun_sp           = msrun_sp;

  ui->peptide_label->setText(
    _p_peptide_evidence->getPeptideXtpSp().get()->toString());
  ui->charge_label->setText(
    QString("%1").arg(_p_peptide_evidence->getCharge()));
  ui->mz_label->setText(
    QString::number(_p_peptide_evidence->getTheoreticalMz(), 'f', 4));
371
  ui->msrun_label->setText(QFileInfo(msrun_sp->getFileName()).fileName());
372
  ui->msrun_label->setToolTip(msrun_sp->getFileName());
373
374
375
376
377
378
379
  // get same xic peptide evidence (msrun, peptide, charge)
  // p_projet
  _peptide_evidence_list.clear();

  _p_xic_window->getProjectWindow()
    ->getProjectP()
    ->getSameXicPeptideEvidenceList(
380
381
      _peptide_evidence_list,
      _msrun_sp.get(),
382
383
384
385
386
387
388
      _p_peptide_evidence->getPeptideXtpSp().get(),
      _p_peptide_evidence->getCharge());
  //_p_xic_window->getProjectWindow()->getProjectP()->getSameXicPeptideEvidenceList(p_peptide_evidence,
  //_peptide_evidence_list);

  emit computeIsotopeMassList(_p_peptide_evidence->getPeptideXtpSp(),
                              _p_peptide_evidence->getCharge(),
389
                              _p_xic_window->getXicExtractPrecision(),
390
                              ui->isotopic_distribution_spinbox->value());
Langella Olivier's avatar
Langella Olivier committed
391
}
Langella Olivier's avatar
Langella Olivier committed
392

Renne Thomas's avatar
Renne Thomas committed
393
394
395
396
397
398
const PeptideEvidence *
XicBox::getPeptideEvidenceP() const
{
  return _p_peptide_evidence;
}

Renne Thomas's avatar
Renne Thomas committed
399
400
401
402
403
404
MsRunSp
XicBox::getMsRunSp()
{
  return _msrun_sp;
}

405
void
406
XicBox::setXic(std::vector<pappso::XicCoordSPtr> xic_sp_list)
407
{
408
  qDebug() << xic_sp_list.size() << _natural_isotope_list.size();
409
  ui->xic_widget->clear();
Langella Olivier's avatar
Langella Olivier committed
410
  for(std::size_t i = 0; i < xic_sp_list.size(); i++)
411
    {
412
      //_natural_isotope_list[i].xic_sp = xic_sp_list[i];
413
    }
414
415
416
417
418
419
420
421
422
  // pappso::XicWidget * xic_widget = new pappso::XicWidget(this);
  // ui->xic_list_widget->layout()->addWidget(xic_widget);
  for(XicBoxNaturalIsotope &xic_isotope : _natural_isotope_list)
    {
      // qDebug() << "XicBox::setXic xic " << xic_isotope.xic_sp.get();
      if(xic_isotope.xic_sp.get() == nullptr)
        {
          throw new pappso::PappsoException(
            "Error in XicBox::setXic:\n xic_isotope.xic_sp.get() == nullptr");
423
        }
424
      qDebug() << xic_isotope.xic_sp.get()->size();
425
426
427
428
429
430
431
432
433
434
435
      ui->xic_widget->addXicSp(xic_isotope.xic_sp);
      QString isotope_name = QString("+%1").arg(
        xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeNumber());
      if(xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeRank() > 1)
        {
          isotope_name =
            QString("+%1 [%2]")
              .arg(xic_isotope.peptide_natural_isotope_sp.get()
                     ->getIsotopeNumber())
              .arg(
                xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeRank());
Langella Olivier's avatar
Langella Olivier committed
436
        }
437
438
439
440
441
442
443
444
445
      isotope_name.append(QString(" (%1%)").arg((
        int)(xic_isotope.peptide_natural_isotope_sp.get()->getIntensityRatio() *
             100)));
      ui->xic_widget->setName(xic_isotope.xic_sp.get(), isotope_name);

      if(xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeNumber() == 0)
        {
          for(const PeptideEvidence *peptide_evidence : _peptide_evidence_list)
            {
446
              qDebug() << peptide_evidence->getHardenedRetentionTime();
447
              ui->xic_widget->addMsMsEvent(
448
449
                xic_isotope.xic_sp.get(),
                peptide_evidence->getHardenedRetentionTime());
450
451
            }
        }
Langella Olivier's avatar
Langella Olivier committed
452
    }
453
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
454

455
456
457
458
  if(!_scaled)
    {
      ui->xic_widget->rescale();
      _scaled = true;
Langella Olivier's avatar
Langella Olivier committed
459
    }
460

461
462
463
464
465
466
467
468
469
470
  /*
  if (_isotope_mass_list.size() > _xic_widget_list.size()) {
      emit loadXic(_p_peptide_evidence->getMsRunP(),
  _isotope_mass_list[_xic_widget_list.size()].get()->getMz(),
  _p_xic_window->getXicExtractPrecision(), XicExtractMethod::max);
  }
  */
  XicDetectionList xic_list;
  xic_list.setPeptideEvidenceList(_peptide_evidence_list);

471
  std::vector<pappso::TracePeakCstSPtr> draw_peak_borders;
472
473
474
475
476
477
478

  for(XicBoxNaturalIsotope &xic_isotope : _natural_isotope_list)
    {
      try
        {
          xic_list.clear();
          _p_xic_window->xicDetect(*(xic_isotope.xic_sp.get()), &xic_list);
479
480
          xic_isotope.matched_peak_sp = xic_list.getMatchedPeak();
          qDebug() << "size : " << xic_list.getXicPeakList().size();
481
482
483
484
          xic_isotope.detected_peak_list = xic_list.getXicPeakList();
          ui->xic_widget->addXicPeakList(xic_isotope.xic_sp.get(),
                                         xic_list.getXicPeakList());
          draw_peak_borders.push_back(xic_isotope.matched_peak_sp);
Langella Olivier's avatar
Langella Olivier committed
485
        }
486
487
488
      catch(const pappso::ExceptionNotPossible &e)
        {
          qDebug() << e.qwhat();
Langella Olivier's avatar
Langella Olivier committed
489
490
        }
    }
491
492
493
  qDebug() << "XicBox::setXic plot";
  ui->xic_widget->plot();
  drawObservedAreaBars(draw_peak_borders);
494
  showLoadingIcon(false);
495
496
}

Langella Olivier's avatar
Langella Olivier committed
497

498
499
void
XicBox::drawObservedAreaBars(
500
  const std::vector<pappso::TracePeakCstSPtr> &observed_peak_to_draw_list)
501
{
502
  if(m_observedAreaBars == nullptr)
503
    {
504
505
      qDebug() << " new QCPBars";
      m_observedAreaBars =
506
        new QCPBars(ui->histo_widget->xAxis, ui->histo_widget->yAxis2);
507
508
      //       m_observedRatioBars = new QCPBars(ui->histo_widget->xAxis,
      //       ui->histo_widget->yAxis);
509
    }
510
511
  else
    {
512
513
      qDebug() << "plot clearData";
      m_observedAreaBars->data().clear();
514
      //       m_observedRatioBars->data().clear();
515
    }
516
  m_observedAreaBars->setName("peak area");
517
  //   m_observedRatioBars->setName("obs. ratio");
518
519
520
521
  // ui->histo_widget->yAxis2->setLabel("intensity");


  QVector<double> observed_intensity_data;
522
  QVector<double> observed_ratio_data;
523
524
525
526
527
528
  QVector<QString> labels;

  QVector<double> ticks;
  double sum = 0;
  int i      = 0;

529
  std::vector<std::size_t> common_peak_rt_measure_list;
Langella Olivier's avatar
Langella Olivier committed
530

531
  qDebug() << " plot _isotope_mass_list";
532
533
  for(const XicBoxNaturalIsotope &xic_isotope : _natural_isotope_list)
    {
534
535
      pappso::TracePeakCstSPtr peak_to_draw;
      for(pappso::TracePeakCstSPtr observed_peak : observed_peak_to_draw_list)
536
537
538
        {
          if(xic_isotope.contains(observed_peak))
            {
539
              qDebug() << "OK";
540
              peak_to_draw = observed_peak;
541
542
            }
        }
543
544
      if(peak_to_draw.get() == nullptr)
        {
545
        }
546
547
      else
        {
Langella Olivier's avatar
Langella Olivier committed
548
549
550
551
552
          addCommonRt(common_peak_rt_measure_list,
                      peak_to_draw.get(),
                      xic_isotope.xic_sp.get());


553
          sum += peak_to_draw.get()->getArea();
554
          observed_intensity_data << peak_to_draw.get()->getArea();
555
556
557
558
559
560
561
562
563
564
          QString isotope_name = QString("+%1").arg(
            xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeNumber());
          if(xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeRank() > 1)
            {
              isotope_name =
                QString("+%1 [%2]")
                  .arg(xic_isotope.peptide_natural_isotope_sp.get()
                         ->getIsotopeNumber())
                  .arg(xic_isotope.peptide_natural_isotope_sp.get()
                         ->getIsotopeRank());
565
            }
566
567
568
569
570
571
          isotope_name.append(QString(" (%1%)").arg(
            (int)(xic_isotope.peptide_natural_isotope_sp.get()
                    ->getIntensityRatio() *
                  100)));
          labels << isotope_name;
          ticks << i;
572
        }
573
      i++;
574
575
    }

576
  //   m_observedRatioBars->setPen(QPen(QColor("red")));
Renne Thomas's avatar
Renne Thomas committed
577
578
  m_observedAreaBars->setPen(QColor(180, 15, 32, 200));
  m_observedAreaBars->setBrush(QColor(180, 15, 32, 200));
579
580
  //_graph_peak_surface_list.back()->setScatterStyle(QCPScatterStyle::ssDot);
  // observed_intensity->setBrush(QBrush(QColor(170, 255, 0, 0)));
581

582
583
  double sum_observed_ratio = 0;
  double total_ratio        = 0;
Langella Olivier's avatar
Langella Olivier committed
584
585
586
587
  for(XicBoxNaturalIsotope &xic_isotope : _natural_isotope_list)
    {
      xic_isotope.computeMeanIntensityIn(common_peak_rt_measure_list);

588
589
590
591
592
593
594
595
596
      sum_observed_ratio += xic_isotope.meanIntensityInCurrentPeak;
      total_ratio +=
        xic_isotope.peptide_natural_isotope_sp.get()->getIntensityRatio();
    }
  for(XicBoxNaturalIsotope &xic_isotope : _natural_isotope_list)
    {
      observed_ratio_data << ((xic_isotope.meanIntensityInCurrentPeak /
                               sum_observed_ratio) *
                              total_ratio);
Langella Olivier's avatar
Langella Olivier committed
597
598
    }

599
600
  if(ticks.size() > 0)
    {
Renne Thomas's avatar
Renne Thomas committed
601
602
603
604
      for(double &tick : ticks)
        {
          tick += 0.1;
        }
605
      m_observedAreaBars->setData(ticks, observed_intensity_data);
Renne Thomas's avatar
Renne Thomas committed
606
607
608
609
610
611
612
613
614
615
      m_observedAreaBars->setWidth(0.6);

      //       QVector<QString> labels;
      //       for(double tick : ticks)
      //         {
      //           labels.push_back(QString::number(tick));
      //         }
      //       QSharedPointer<QCPAxisTickerText> textTicker(new
      //       QCPAxisTickerText); textTicker->addTicks(ticks, labels);
      //       ui->histo_widget->yAxis->setTicker(textTicker);
616
      //       m_observedRatioBars->setData(ticks, observed_ratio_data);
617

618
      ui->histo_widget->yAxis2->setVisible(true);
619
      ui->histo_widget->yAxis2->setRange(0, sum);
Renne Thomas's avatar
Renne Thomas committed
620
621
      ui->histo_widget->yAxis2->setLabel("observed intensity");
      ui->histo_widget->yAxis2->setLabelPadding(0);
622
      ui->histo_widget->replot();
623
    }
624
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
625
}
Langella Olivier's avatar
Langella Olivier committed
626
627


628
629
630
631
632
void
XicBox::setIsotopeMassList(
  std::vector<pappso::PeptideNaturalIsotopeAverageSp> isotope_mass_list)
{
  _natural_isotope_list.clear();
633
634
635
636
637
638

  pappso::XicCoordSPtr xic_coord_ref =
    _msrun_sp.get()
      ->getMsRunReaderSPtr()
      .get()
      ->newXicCoordSPtrFromSpectrumIndex(
639
        _p_peptide_evidence->getSpectrumIndexByScanNumber(),
640
641
642
        _p_xic_window->getXicExtractPrecision());

  std::vector<pappso::XicCoordSPtr> mass_list;
643
644
645
  for(pappso::PeptideNaturalIsotopeAverageSp &natural_isotope_average :
      isotope_mass_list)
    {
646
      qDebug() << natural_isotope_average.get()->getMz();
647
648
649
650
651
652
653
654
655
656
657
658
      pappso::XicCoordSPtr xic_coord =
        xic_coord_ref.get()->initializeAndClone();
      xic_coord.get()->mzRange =
        pappso::MzRange(natural_isotope_average.get()->getMz(),
                        _p_xic_window->getXicExtractPrecision());
      mass_list.push_back(xic_coord);

      _natural_isotope_list.push_back({xic_coord.get()->xicSptr,
                                       natural_isotope_average,
                                       nullptr,
                                       nullptr,
                                       {}});
Langella Olivier's avatar
Langella Olivier committed
659
    }
660

661
  emit loadXic(_msrun_sp, mass_list, pappso::XicExtractMethod::max);
662
663

  // histogram
664
  if(m_theoreticalRatioBars == nullptr)
665
    {
666
      m_theoreticalRatioBars =
667
668
669
670
        new QCPBars(ui->histo_widget->xAxis, ui->histo_widget->yAxis);
    }
  else
    {
671
      m_theoreticalRatioBars->data()->clear();
672
    }
673
  m_theoreticalRatioBars->setName("th. ratio");
674
  ui->histo_widget->xAxis->setLabel("isotopes");
Renne Thomas's avatar
Renne Thomas committed
675
676
  m_theoreticalRatioBars->setPen(QColor(59, 154, 178));
  m_theoreticalRatioBars->setBrush(QColor(59, 154, 178));
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
  // ui->histo_widget->yAxis->setLabel("th. ratio");

  QVector<double> theoretical_ratio_data;

  QVector<double> ticks;
  QVector<QString> labels;
  int i      = 0;
  double sum = 0;
  for(const XicBoxNaturalIsotope &xic_isotope : _natural_isotope_list)
    {
      sum += xic_isotope.peptide_natural_isotope_sp.get()->getIntensityRatio();
      theoretical_ratio_data
        << xic_isotope.peptide_natural_isotope_sp.get()->getIntensityRatio();
      QString isotope_name = QString("+%1").arg(
        xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeNumber());
      if(xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeRank() > 1)
        {
          isotope_name =
            QString("+%1 [%2]")
              .arg(xic_isotope.peptide_natural_isotope_sp.get()
                     ->getIsotopeNumber())
              .arg(
                xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeRank());
700
        }
701
702
703
      isotope_name.append(QString(" (%1%)").arg((
        int)(xic_isotope.peptide_natural_isotope_sp.get()->getIntensityRatio() *
             100)));
Renne Thomas's avatar
Renne Thomas committed
704
705
      labels << QString::number(
        xic_isotope.peptide_natural_isotope_sp.get()->getIsotopeNumber());
706
707
      ticks << i;
      i++;
708
    }
Renne Thomas's avatar
Renne Thomas committed
709
710
711
712
  QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
  textTicker->addTicks(ticks, labels);
  ui->histo_widget->xAxis->setTicker(textTicker);
  ui->histo_widget->xAxis->setTickLabelRotation(0);
713
714
715
  // ui->histo_widget->xAxis->setSubTicks(false);
  // ui->histo_widget->xAxis->setTickLength(0, 4);
  // ui->histo_widget->xAxis->setRange(0, 8);
716
717
718
  /*
    ui->histo_widget->xAxis->setAutoTickStep(
      false); // <-- disable to use your own value
719

720
721
    ui->histo_widget->xAxis->setTickStep(1);
  */
Renne Thomas's avatar
Renne Thomas committed
722
723
724
725
  for(double &tick : ticks)
    {
      tick -= 0.1;
    }
726
  m_theoreticalRatioBars->setData(ticks, theoretical_ratio_data);
Renne Thomas's avatar
Renne Thomas committed
727
  m_theoreticalRatioBars->setWidth(0.6);
728
729


730
  ui->histo_widget->yAxis->setRange(0, sum);
Renne Thomas's avatar
Renne Thomas committed
731
732
733
  ui->histo_widget->yAxis->setLabel("intensity ratio");
  ui->histo_widget->yAxis->setLabelPadding(0);
  ui->histo_widget->xAxis->setRange(-0.5, _natural_isotope_list.size() - 0.5);
734
  ui->histo_widget->replot();
Langella Olivier's avatar
Langella Olivier committed
735
}
736
737


738
739
740
void
XicBox::reExtractXic()
{
741
  qDebug();
742
743
  ui->xic_widget->clear();

744
745
746
747
748
749
750
  pappso::XicCoordSPtr xic_coord_ref =
    _msrun_sp.get()
      ->getMsRunReaderSPtr()
      .get()
      ->newXicCoordSPtrFromSpectrumIndex(
        _p_peptide_evidence->getSpectrumIndex(),
        _p_xic_window->getXicExtractPrecision());
751

752
753
  std::vector<pappso::XicCoordSPtr> mass_list;
  for(XicBoxNaturalIsotope &xic_isotope : _natural_isotope_list)
754
    {
755
756
757
758
759
760
761
      pappso::XicCoordSPtr xic_coord =
        xic_coord_ref.get()->initializeAndClone();
      xic_coord.get()->mzRange =
        pappso::MzRange(xic_isotope.peptide_natural_isotope_sp.get()->getMz(),
                        _p_xic_window->getXicExtractPrecision());
      mass_list.push_back(xic_coord);
      xic_isotope.xic_sp = xic_coord.get()->xicSptr;
762
    }
Langella Olivier's avatar
Langella Olivier committed
763

764
  emit loadXic(_msrun_sp, mass_list, pappso::XicExtractMethod::max);
765

766
767
  qDebug();
}
768

769
770
771
void
XicBox::setRetentionTime(double rt)
{
772
  qDebug() << " begin";
773
  ui->rt_label->setText(tr("rt=%1 (sec) rt=%2 (min)").arg(rt).arg(rt / 60));
Langella Olivier's avatar
Langella Olivier committed
774
}
775
776
void
XicBox::setXicPeakList(
777
  std::vector<std::pair<pappso::XicCstSPtr, pappso::TracePeakCstSPtr>>
Langella Olivier's avatar
Langella Olivier committed
778
    xic_peak_list)
779
{
780
  qDebug() << " begin";
781
782
783
784
  QString html;
  for(XicBoxNaturalIsotope &xic_isotope : _natural_isotope_list)
    {
      xic_isotope.one_peak_sp = nullptr;
785
    }
Langella Olivier's avatar
Langella Olivier committed
786

787
  for(std::pair<pappso::XicCstSPtr, pappso::TracePeakCstSPtr> &pair_xic_peak :
788
789
790
791
792
793
794
      xic_peak_list)
    {
      for(XicBoxNaturalIsotope &xic_isotope : _natural_isotope_list)
        {
          if(xic_isotope.xic_sp.get() == pair_xic_peak.first.get())
            {
              xic_isotope.one_peak_sp = pair_xic_peak.second;
795
796
            }
        }
797
798
799
800
801
802
803
804
805
      html.append(
        tr("<p>%1<br/>area: %2<br/>rt begin: %3<br/>rt max: "
           "%4<br/>rt end: %5<br/></p>")
          .arg(ui->xic_widget->getName(pair_xic_peak.first.get()))
          .arg(QString::number(
            std::trunc(pair_xic_peak.second.get()->getArea()), 'G', 16))
          .arg(pair_xic_peak.second.get()->getLeftBoundary().x)
          .arg(pair_xic_peak.second.get()->getMaxXicElement().x)
          .arg(pair_xic_peak.second.get()->getRightBoundary().x));
Langella Olivier's avatar
Langella Olivier committed
806
807
    }

808
809
810
811
812
813
814
815
  ui->xic_widget->setToolTip(html);
  /*
      if (_popup_peak_info == nullptr) delete _popup_peak_info;
      _popup_peak_info = new QFrame( this ,Qt::Popup);
      //_popup_peak_info->setFrameStyle(Qt::WinPanel|Qt::Raised );
      _popup_peak_info->resize(150,100);
      _popup_peak_info->show();
      */
Langella Olivier's avatar
Langella Olivier committed
816
}
817
818


819
820
821
822
823
824
void
XicBox::onRtUnitChanged()
{
  if(_p_xic_window->isRetentionTimeSeconds())
    {
      ui->xic_widget->setRetentionTimeInSeconds();
825
    }
826
827
828
  else
    {
      ui->xic_widget->setRetentionTimeInMinutes();
829
830
    }
}
831
832
833
834
835
836
837

void
XicBox::onIsotopicDistributionClick()
{
  //   ui->histo_widget->legend->clear();
  setPeptideEvidence(_p_peptide_evidence);
}
Langella Olivier's avatar
Langella Olivier committed
838
839

void
840
XicBox::addCommonRt(std::vector<std::size_t> &common_peak_rt_measure_list,
Langella Olivier's avatar
Langella Olivier committed
841
842
843
844
845
846
847
848
                    const pappso::TracePeak *peak,
                    const pappso::Xic *xic) const
{

  if(common_peak_rt_measure_list.size() == 0)
    { // no common peaks, filling with whole peak
      auto it = pappso::findFirstEqualOrGreaterX(
        xic->begin(), xic->end(), peak->getLeftBoundary().x);
Langella Olivier's avatar
Langella Olivier committed
849
850
      auto itend = pappso::findFirstGreaterX(
        xic->begin(), xic->end(), peak->getRightBoundary().x);
Langella Olivier's avatar
Langella Olivier committed
851

852
853
      std::size_t i = it - xic->begin();

Langella Olivier's avatar
Langella Olivier committed
854
      while(it != itend)
Langella Olivier's avatar
Langella Olivier committed
855
        {
856
          if(it->y > (0.6 * peak->getMaxXicElement().y))
Langella Olivier's avatar
Langella Olivier committed
857
            {
858
              common_peak_rt_measure_list.push_back(i);
Langella Olivier's avatar
Langella Olivier committed
859
            }
Langella Olivier's avatar
Langella Olivier committed
860
          it++;
861
          i++;
Langella Olivier's avatar
Langella Olivier committed
862
863
864
865
        }
    }
  else
    {
866
867
      std::vector<std::size_t> intersect(common_peak_rt_measure_list);
      std::vector<std::size_t> new_rt;
Langella Olivier's avatar
Langella Olivier committed
868
869
      auto it = pappso::findFirstEqualOrGreaterX(
        xic->begin(), xic->end(), peak->getLeftBoundary().x);
870
871
      auto itend = pappso::findFirstGreaterX(
        xic->begin(), xic->end(), peak->getRightBoundary().x);
Langella Olivier's avatar
Langella Olivier committed
872

873
874
      std::size_t i = it - xic->begin();
      while(it != itend)
Langella Olivier's avatar
Langella Olivier committed
875
        {
Langella Olivier's avatar
Langella Olivier committed
876
877
          if(it->y > 0)
            {
878
              new_rt.push_back(i);
Langella Olivier's avatar
Langella Olivier committed
879
            }
Langella Olivier's avatar
Langella Olivier committed
880
          it++;
881
          i++;
Langella Olivier's avatar
Langella Olivier committed
882
        }
Langella Olivier's avatar
Langella Olivier committed
883
884
885
886
887
888
889
890

      auto itinter = std::set_intersection(intersect.begin(),
                                           intersect.end(),
                                           new_rt.begin(),
                                           new_rt.end(),
                                           common_peak_rt_measure_list.begin());
      common_peak_rt_measure_list.resize(itinter -
                                         common_peak_rt_measure_list.begin());
891
892
893
894
895
896
897
      /*
            for(auto indice : common_peak_rt_measure_list)
              {
                qDebug() << indice << " " << xic->at(indice).x << " "
                         << xic->at(indice).y;
              }
              */
Langella Olivier's avatar
Langella Olivier committed
898
899
    }
}
Renne Thomas's avatar
Renne Thomas committed
900
901
902
903
904
905

std::vector<XicBoxNaturalIsotope>
XicBox::getNaturalIsotopList() const
{
  return _natural_isotope_list;
}
Renne Thomas's avatar
Renne Thomas committed
906
907
908
909
910
911

double
XicBox::getIsotopicFractionDistribution() const
{
  return ui->isotopic_distribution_spinbox->value();
}
912
913
914
915
916
917
918
919
920
921
922
923
924
925

void
XicBox::showLoadingIcon(bool loading_status)
{
  ui->loading_label->setVisible(loading_status);
  if(loading_status)
    {
      int size = ui->xic_widget->size().rheight() * 0.5;
      mp_loadingMovie->setScaledSize(QSize(size, size));
    }

  ui->xic_widget->setVisible(!loading_status);
  ui->histo_widget->setVisible(!loading_status);
}