libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
qcpspectrum.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/widget/spectrumwidget/qcpspectrum.cpp
3 * \date 31/12/2017
4 * \author Olivier Langella
5 * \brief Custom plot derivative to plot a spectrum
6 */
7
8
9/*******************************************************************************
10 * Copyright (c) 2017 Olivier Langella <Olivier.Langella@u-psud.fr>.
11 *
12 * This file is part of the PAPPSOms++ library.
13 *
14 * PAPPSOms++ is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * PAPPSOms++ is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * Contributors:
28 * Olivier Langella <Olivier.Langella@u-psud.fr> - initial API and
29 *implementation
30 ******************************************************************************/
31
32#include "qcpspectrum.h"
33#include "massspectrumwidget.h"
34
35using namespace pappso;
36
37QCPSpectrum::QCPSpectrum(MassSpectrumWidget *parent, bool visible) : QCustomPlot(parent)
38{
39 qDebug() << "QCPSpectrum::QCPSpectrum begin";
40
41 setFocusPolicy(Qt::ClickFocus);
42 _parent = parent;
43 _mz_range.lower = 0;
44 _mz_range.upper = 0;
45 _intensity_range.lower = 0;
46 _intensity_range.upper = 0;
47 _mass_delta_range.upper = -100;
48 _mass_delta_range.lower = 100;
49
50
51 // make axis rects' left side line up:
52 QCPMarginGroup *group = new QCPMarginGroup(this);
53 this->axisRect()->setMarginGroup(QCP::msLeft | QCP::msRight, group);
54
55
56 _p_peak_bars = new QCPBars(xAxis, yAxis);
57 _p_peak_bars->setWidthType(QCPBars::WidthType::wtAbsolute);
58 _p_peak_bars->setWidth(_bar_width);
59 _p_peak_bars->setPen(QPen(Qt::black, 1));
60 _p_peak_bars->setVisible(true);
61
62 _p_peak_bars_isotope = new QCPBars(xAxis, yAxis);
63 _p_peak_bars_isotope->setWidthType(QCPBars::WidthType::wtAbsolute);
64 _p_peak_bars_isotope->setWidth(_bar_width * 10);
65 QColor red(Qt::red);
66 red.setAlpha(100);
67 _p_peak_bars_isotope->setPen(QPen(red, 1));
68 _p_peak_bars_isotope->setBrush(QBrush(red));
69
70 mp_peak_bars_precursor = new QCPBars(xAxis, yAxis);
71 mp_peak_bars_precursor->setWidthType(QCPBars::WidthType::wtAbsolute);
72 mp_peak_bars_precursor->setWidth(_bar_width * 1.5);
73 mp_peak_bars_precursor->setPen(QPen(Qt::cyan, 1));
74 mp_peak_bars_precursor->setVisible(true);
75
76 connect(this->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(setMzRangeChanged(QCPRange)));
77
78
79 std::vector<Enums::PeptideIon> all_ion_list = {
88 Enums::PeptideIon::y, ///< Cter amino ions
89 Enums::PeptideIon::ystar, ///< Cter amino ions + NH3 loss
90 Enums::PeptideIon::yo, ///< Cter amino ions + H2O loss
91 Enums::PeptideIon::z, ///< Cter carbocations
94 qDebug() << "SpectrumWidget::setVisibleMassDelta 5";
95
96 for(Enums::PeptideIon ion_type : all_ion_list)
97 {
98 QCPBars *p_peak_bars = new QCPBars(xAxis, yAxis);
99 p_peak_bars->setWidthType(QCPBars::WidthType::wtAbsolute);
100 p_peak_bars->setWidth(_bar_width);
101 p_peak_bars->setPen(QPen(PeptideFragmentIon::getPeptideIonColor(ion_type), 1));
102
103 p_peak_bars->setVisible(true);
104 _map_ion_type_bars.insert(std::pair<Enums::PeptideIon, QCPBars *>(ion_type, p_peak_bars));
105 }
106 qDebug() << "SpectrumWidget::setVisibleMassDelta visible ?";
107 setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
108 if(visible)
109 {
110
111 _p_delta_axis_rect = new QCPAxisRect(this);
112 this->plotLayout()->addElement(1, 0, _p_delta_axis_rect);
113 _p_delta_axis_rect->axis(QCPAxis::atBottom)->setLayer("axes");
114 _p_delta_axis_rect->axis(QCPAxis::atBottom)->grid()->setLayer("grid");
115 _p_delta_axis_rect->axis(QCPAxis::atLeft)->setLabel("mass delta");
116 // bring bottom and main axis rect closer together:
117 this->plotLayout()->setRowSpacing(0);
118 _p_delta_axis_rect->setAutoMargins(QCP::msLeft | QCP::msRight | QCP::msBottom);
119 _p_delta_axis_rect->setMargins(QMargins(0, 0, 0, 0));
120 this->setAutoAddPlottableToLegend(false);
121 _p_delta_graph = new QCPGraph(_p_delta_axis_rect->axis(QCPAxis::atBottom),
122 _p_delta_axis_rect->axis(QCPAxis::atLeft));
123 _p_delta_graph->setLineStyle(QCPGraph::LineStyle::lsNone);
124 _p_delta_graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 4.0));
125
126 _p_delta_axis_rect->setMarginGroup(QCP::msLeft | QCP::msRight, group);
127 _p_delta_axis_rect->setMaximumSize(QSize(QWIDGETSIZE_MAX, 100));
128
129
130 //_p_delta_axis_rect->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom );
131 _p_delta_axis_rect->setRangeDrag(Qt::Horizontal | Qt::Vertical);
132 _p_delta_axis_rect->setRangeZoom(Qt::Vertical);
133
134
135 connect(_p_delta_axis_rect->axis(QCPAxis::atBottom),
136 SIGNAL(rangeChanged(QCPRange)),
137 this,
138 SLOT(setMzRangeChanged(QCPRange)));
139 }
140 else
141 {
142 _p_delta_axis_rect = nullptr;
143 }
144 qDebug() << "QCPSpectrum::QCPSpectrum end";
145}
149
150void
152{
153 _mass_delta_range.upper = -100;
154 _mass_delta_range.lower = 100;
155 // generate basic peaks:
156 _p_spectrum = spectrum;
157 if((spectrum != nullptr) && (spectrum->size() != 0))
158 {
159 // throw PappsoException(QObject::tr("Error in
160 // SpectrumWidget::setSpectrumSp :\n_spectrum_sp.get() == nullptr"));
161 //}
162 _p_peak_bars->setVisible(true);
163 for(const DataPoint &peak : *spectrum)
164 {
165 _p_peak_bars->addData(peak.x, peak.y);
166 }
167 _mz_range.lower = spectrum->front().x - 1;
168 _mz_range.upper = spectrum->back().x + 1;
169 _intensity_range.lower = 0;
170 _intensity_range.upper = spectrum->maxY();
171 }
172 else
173 {
174 _mz_range.lower = 0;
175 _mz_range.upper = 0;
176 _intensity_range.lower = 0;
177 _intensity_range.upper = 0;
178 }
179}
180
181void
183{
184 _p_peak_bars->setVisible(true);
185
186 //_p_peak_bars->rescaleAxes(false);
187 // this->rescaleAxes(false);
188
189
190 if(_mz_range.lower < 0)
191 {
192 _mz_range.lower = 0;
193 }
194 if(_mz_range.upper < 0)
195 {
196 _mz_range.upper = 1000;
197 }
198 else
199 {
200 xAxis->setRange(_mz_range);
201 yAxis->setRange(_intensity_range);
202 if(_p_delta_axis_rect != nullptr)
203 {
204 _p_delta_axis_rect->axis(QCPAxis::AxisType::atLeft)->setRange(_mass_delta_range);
205 }
206 }
207}
208
209void
211{
212 qDebug() << "QCPSpectrum::setMzRangeChanged _mz_range.lower" << _mz_range.lower;
213 if(_mz_range.lower > 0)
214 {
215 if(range.lower < _mz_range.lower)
216 {
217 range.lower = _mz_range.lower;
218 }
219 if(range.upper > _mz_range.upper)
220 {
221 range.upper = _mz_range.upper;
222 }
223 }
224
225 xAxis->setRange(range);
226
227 if(_p_delta_axis_rect != nullptr)
228 {
229 _p_delta_axis_rect->axis(QCPAxis::atBottom)->setRange(range);
230 }
231}
232
233void
235{
236 qDebug();
237 _p_peak_bars->setData(QVector<double>(), QVector<double>());
238 _p_peak_bars->data().clear();
239 qDebug();
240 _p_peak_bars_isotope->setData(QVector<double>(), QVector<double>());
241 _p_peak_bars_isotope->data().clear();
242 qDebug();
243 mp_peak_bars_precursor->setData(QVector<double>(), QVector<double>());
244 mp_peak_bars_precursor->data().clear();
245 for(std::pair<Enums::PeptideIon, QCPBars *> pair_ion_bar : _map_ion_type_bars)
246 {
247 pair_ion_bar.second->setData(QVector<double>(), QVector<double>());
248 pair_ion_bar.second->data().clear();
249 }
250 // this->clearPlottables();
251 // this->clearItems();
252 if(_p_delta_axis_rect != nullptr)
253 {
254 _p_delta_graph->setData(QVector<double>(), QVector<double>());
255 _p_delta_graph->data().clear();
256 }
257
258 _mz_range.lower = 0;
259 _mz_range.upper = 0;
260
261 _mass_delta_range.upper = -100;
262 _mass_delta_range.lower = 100;
263 qDebug();
264}
265
266void
268{
269 if(event->key() == Qt::Key_Control)
270 {
271 _control_key = true;
272 }
273 qDebug() << "QCPSpectrum::keyPressEvent end";
274}
275
276void
278{
279 if(event->key() == Qt::Key_Control)
280 {
281 _control_key = false;
282 }
283 qDebug() << "QCPSpectrum::keyReleaseEvent end";
284}
285
286void
288{
289 /*qDebug() << "QCPSpectrum::mousePressEvent begin "
290 << xAxis->pixelToCoord(event->x()) << " "
291 << yAxis->pixelToCoord(event->y());*/
292 _click = true;
293
294#if QT_VERSION < 0x060000
295 _old_x = event->x();
296 _old_y = yAxis->pixelToCoord(event->y());
297#else
298 _old_x = event->position().x();
299 _old_y = yAxis->pixelToCoord(event->position().y());
300#endif
301 if(_old_y < 0)
302 _old_y = 0;
303 /* qDebug() << "QCPSpectrum::mousePressEvent end";*/
304}
305void
306QCPSpectrum::mouseReleaseEvent(QMouseEvent *event [[maybe_unused]])
307{
308 /*qDebug() << "QCPSpectrum::mouseReleaseEvent begin "
309 << xAxis->pixelToCoord(event->x()) << " "
310 << yAxis->pixelToCoord(event->y());*/
311 _click = false;
312 // qDebug() << "QCPSpectrum::mouseReleaseEvent end";
313}
314void
316{
317
318#if QT_VERSION < 0x060000
319 pappso::pappso_double x = xAxis->pixelToCoord(event->x());
320#else
321 pappso::pappso_double x = xAxis->pixelToCoord(event->position().x());
322#endif
323 if(_click)
324 {
325 /* qDebug() << "QCPSpectrum::mouseMoveEvent begin "
326 << xAxis->pixelToCoord(event->x()) << " "
327 << yAxis->pixelToCoord(event->y());*/
328
329#if QT_VERSION < 0x060000
330 pappso::pappso_double y = yAxis->pixelToCoord(event->y());
331#else
332 pappso::pappso_double y = yAxis->pixelToCoord(event->position().y());
333#endif
334 if(y < 0)
335 {
336 y = 0;
337 }
338 if(_control_key)
339 {
340 if(y > 0)
341 {
342 this->yAxis->scaleRange(_old_y / y, 0);
343 }
344 }
345 else
346 {
347
348#if QT_VERSION < 0x060000
349 this->xAxis->moveRange(xAxis->pixelToCoord(_old_x) - xAxis->pixelToCoord(event->x()));
350#else
351 this->xAxis->moveRange(xAxis->pixelToCoord(_old_x) -
352 xAxis->pixelToCoord(event->position().x()));
353#endif
354 }
355
356#if QT_VERSION < 0x060000
357 _old_x = event->x();
358#else
359 _old_x = event->position().x();
360#endif
361 _old_y = y;
362 replot();
363 // qDebug() << "QCPSpectrum::mouseMoveEvent end";
364 }
365 else
366 {
367 if(_p_spectrum != nullptr)
368 {
369 pappso::pappso_double mouse_mz_range = xAxis->pixelToCoord(10) - xAxis->pixelToCoord(8);
370 getMostIntensePeakBetween(x, mouse_mz_range);
371 _parent->mzChangeEvent(x);
372 }
373 }
374}
375
376void
378{
379 /*qDebug() << "QCPSpectrum::getNearestPeakBetween begin " << mz << " "
380 << mouse_mz_range;*/
381 const DataPoint *p_peak_match;
382 p_peak_match = nullptr;
383 pappso_double min = mz - mouse_mz_range;
384 pappso_double max = mz + mouse_mz_range;
385
386 for(const DataPoint &peak : *_p_spectrum)
387 {
388 if((peak.x > min) && (peak.x < max))
389 {
390 if(p_peak_match == nullptr)
391 {
392 p_peak_match = &peak;
393 }
394 else
395 {
396 if(fabs(mz - peak.x) < fabs(mz - p_peak_match->x))
397 {
398 p_peak_match = &peak;
399 }
400 }
401 }
402 }
403 _parent->peakChangeEvent(p_peak_match);
404 // qDebug() << "QCPSpectrum::getNearestPeakBetween end";
405}
406
407
408void
410{
411 /*qDebug() << "QCPSpectrum::getNearestPeakBetween begin " << mz << " "
412 << mouse_mz_range;*/
413 const DataPoint *p_peak_match;
414 p_peak_match = nullptr;
415 pappso_double min = mz - mouse_mz_range;
416 pappso_double max = mz + mouse_mz_range;
417
418 for(const DataPoint &peak : *_p_spectrum)
419 {
420 if((peak.x > min) && (peak.x < max))
421 {
422 if(p_peak_match == nullptr)
423 {
424 p_peak_match = &peak;
425 }
426 else
427 {
428 if(p_peak_match->y < peak.y)
429 {
430 p_peak_match = &peak;
431 }
432 }
433 }
434 }
435 _parent->peakChangeEvent(p_peak_match);
436 // qDebug() << "QCPSpectrum::getNearestPeakBetween end";
437}
438
439void
441{
442 if(_p_delta_axis_rect != nullptr)
443 {
444 // observed - theoretical
445 double diff = peak_ion_match.getPeak().x -
446 peak_ion_match.getPeptideNaturalIsotopeAverageSp().get()->getMz();
447 _p_delta_graph->addData(peak_ion_match.getPeak().x, diff);
448
449 if(diff > _mass_delta_range.upper)
450 _mass_delta_range.upper = diff;
451 if(diff < _mass_delta_range.lower)
452 _mass_delta_range.lower = diff;
453
454 _p_delta_axis_rect->axis(QCPAxis::AxisType::atLeft)->setRange(_mass_delta_range);
455 }
456}
457
458void
460{
461 _map_ion_type_bars[peak_ion_match.getPeptideIonType()]->addData(peak_ion_match.getPeak().x,
462 peak_ion_match.getPeak().y);
463}
464
465void
467 const std::vector<pappso::PeptideNaturalIsotopeAverageSp> &isotope_mass_list,
468 pappso_double intensity)
469{
470 pappso_double total_intensity =
471 ((pappso_double)1.0 / isotope_mass_list.at(0).get()->getIntensityRatio()) * intensity;
472 for(PeptideNaturalIsotopeAverageSp peptide : isotope_mass_list)
473 {
474 _p_peak_bars_isotope->addData(peptide.get()->getMz(),
475 peptide.get()->getIntensityRatio() * total_intensity);
476 }
477}
478
479void
481 int charge,
482 PrecisionPtr ms2_precision)
483{
484 MzRange range(precursor_mz, ms2_precision);
485 double precursor_mz_c13 = precursor_mz + (DIFFC12C13 / charge);
486 MzRange range_c13(precursor_mz_c13, ms2_precision);
487
488 for(const DataPoint &peak : *_p_spectrum)
489 {
490 if(((peak.x > range.lower()) && (peak.x < range.upper())) ||
491 ((peak.x > range_c13.lower()) && (peak.x < range_c13.upper())))
492 {
493 mp_peak_bars_precursor->addData(peak.x, peak.y);
494 QCPItemText *text_label = new QCPItemText(this);
495 text_label->setVisible(true);
496 text_label->setPositionAlignment(Qt::AlignBottom | Qt::AlignHCenter);
497 text_label->position->setType(QCPItemPosition::ptPlotCoords);
498 text_label->position->setCoords(peak.x, peak.y);
499 // place position at center/top of axis rect
500 text_label->setFont(QFont(font().family(), 8));
501 text_label->setText("precursor");
502 text_label->setColor(Qt::cyan);
503 }
504 }
505}
Class to represent a mass spectrum.
pappso_double lower() const
Definition mzrange.h:71
pappso_double upper() const
Definition mzrange.h:77
virtual const PeptideNaturalIsotopeAverageSp & getPeptideNaturalIsotopeAverageSp() const
const DataPoint & getPeak() const
Enums::PeptideIon getPeptideIonType() const
static const QColor getPeptideIonColor(Enums::PeptideIon ion_type)
QCPGraph * _p_delta_graph
Definition qcpspectrum.h:92
void highlightPrecursorPeaks(double precursor_mz, int charge, PrecisionPtr ms2_precision)
virtual void keyPressEvent(QKeyEvent *event) override
QCPAxisRect * _p_delta_axis_rect
Definition qcpspectrum.h:91
QCPBars * _p_peak_bars
Definition qcpspectrum.h:87
MassSpectrumWidget * _parent
Definition qcpspectrum.h:82
QCPRange _mass_delta_range
Definition qcpspectrum.h:86
void addPeakIonIsotopeMatch(const PeakIonIsotopeMatch &peak_ion_match)
QCPSpectrum(MassSpectrumWidget *parent, bool visible)
virtual void mouseReleaseEvent(QMouseEvent *event) override
friend class MassSpectrumWidget
Definition qcpspectrum.h:49
QCPRange _intensity_range
Definition qcpspectrum.h:85
void addMassDelta(const PeakIonIsotopeMatch &peak_ion_match)
void getNearestPeakBetween(pappso_double mz, pappso_double mouse_mz_range) const
virtual void mouseMoveEvent(QMouseEvent *event) override
pappso::pappso_double _old_x
Definition qcpspectrum.h:96
void getMostIntensePeakBetween(pappso_double mz, pappso_double mouse_mz_range) const
QCPBars * mp_peak_bars_precursor
Definition qcpspectrum.h:89
void setSpectrumP(const MassSpectrum *spectrum)
std::map< Enums::PeptideIon, QCPBars * > _map_ion_type_bars
Definition qcpspectrum.h:90
void addMs1IsotopePattern(const std::vector< pappso::PeptideNaturalIsotopeAverageSp > &isotope_mass_list, pappso_double intensity)
virtual void keyReleaseEvent(QKeyEvent *event) override
QCPBars * _p_peak_bars_isotope
Definition qcpspectrum.h:88
const MassSpectrum * _p_spectrum
Definition qcpspectrum.h:83
virtual void mousePressEvent(QMouseEvent *event) override
Q_SLOT void setMzRangeChanged(QCPRange range)
pappso::pappso_double _old_y
Definition qcpspectrum.h:97
PeptideIon
Enums::PeptideIon enum defines all types of ions (Nter or Cter).
Definition types.h:286
@ a
Nter aldimine ions.
Definition types.h:290
@ y
Cter amino ions.
Definition types.h:295
@ c
Nter amino ions.
Definition types.h:294
@ astar
Nter aldimine ions + NH3 loss.
Definition types.h:291
@ ystar
Cter amino ions + NH3 loss.
Definition types.h:296
@ yo
Cter amino ions + H2O loss.
Definition types.h:297
@ bstar
Nter acylium ions + NH3 loss.
Definition types.h:288
@ b
Nter acylium ions.
Definition types.h:287
@ x
Cter acylium ions.
Definition types.h:300
@ bo
Nter acylium ions + H2O loss.
Definition types.h:289
@ ao
Nter aldimine ions + H2O loss.
Definition types.h:292
@ z
Cter carbocations.
Definition types.h:298
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
double pappso_double
A type definition for doubles.
Definition types.h:60
std::shared_ptr< const PeptideNaturalIsotopeAverage > PeptideNaturalIsotopeAverageSp
const PrecisionBase * PrecisionPtr
Definition precision.h:122
const pappso_double DIFFC12C13(1.0033548378)
pappso_double x
Definition datapoint.h:24
pappso_double y
Definition datapoint.h:25