libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
massspectraceplotwidget.cpp
Go to the documentation of this file.
1/* This code comes right from the msXpertSuite software project.
2 *
3 * msXpertSuite - mass spectrometry software suite
4 * -----------------------------------------------
5 * Copyright(C) 2009,...,2018 Filippo Rusconi
6 *
7 * http://www.msxpertsuite.org
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * END software license
23 */
24
25
26/////////////////////// StdLib includes
27#include <vector>
28
29
30/////////////////////// Qt includes
31#include <QVector>
32
33
34/////////////////////// Local includes
35
36// For the proton mass
37#include "../../core/types.h"
38
41
42
44 qRegisterMetaType<pappso::MassSpecTracePlotContext>("pappso::MassSpecTracePlotContext");
45
47 qRegisterMetaType<pappso::MassSpecTracePlotContext *>("pappso::MassSpecTracePlotContext *");
48
49
50namespace pappso
51{
52
54{
56 m_context.m_dataKind = Enums::DataKind::mz;
57
58 // qDebug() << "Data kind:" << static_cast<int>(m_context.m_dataKind);
59}
60
62 const QString &x_axis_label,
63 const QString &y_axis_label)
64 : BaseTracePlotWidget(parent, x_axis_label, y_axis_label)
65{
66 // Set the base context to be of kind Enums::DataKind::mz;
67
69 m_context.m_dataKind = Enums::DataKind::mz;
70
71 // qDebug() << "Data kind:" << static_cast<int>(m_context.m_dataKind);
72}
73
74
78
79
80//! Set the \c m_pressedKeyCode to the key code in \p event.
81void
83{
84 // qDebug() << "ENTER";
86
87 // Before working on the various data belonging to the base context, we need
88 // to get it from the base class and refresh our local context with it.
90
91 // qDebug() << "Going to emit keyPressEventSignal(m_context);";
92
94}
95
96
97//! Handle specific key codes and trigger respective actions.
98void
100{
101 // Before working on the various data belonging to the base context, we need
102 // to get it from the base class and refresh our local context with it.
104
106}
107
108
109//! Handle mouse movements, in particular record all the last visited points.
110/*!
111
112 This function is reponsible for storing at each time the last visited point
113 in the graph. Here, point is intended as any x/y coordinate in the plot
114 widget viewport, not a graph point.
115
116 The stored values are then the basis for a large set of calculations
117 throughout all the plot widget.
118
119 \param pointer to QMouseEvent from which to retrieve the coordinates of the
120 visited viewport points.
121 */
122void
124{
125 // Before working on the various data belonging to the base context, we need
126 // to get it from the base class and refresh our local context with it.
128
130}
131
132
133void
135{
136 // Before working on the various data belonging to the base context, we need
137 // to get it from the base class and refresh our local context with it.
139
141
142 // qDebug() << "The current m_lastZ value:" << m_context.m_lastZ;
143}
144
145
146void
148{
150
151 // Before working on the various data belonging to the base context, we need
152 // to get it from the base class and refresh our local context with it.
154
155 if(m_context.m_mouseButtonsAtMousePress & Qt::LeftButton)
156 {
157 if(!m_context.m_isMeasuringDistance)
158 return;
159
160 // qDebug() << "Mouse Buttons at Mouse press:" << m_context.m_mouseButtonsAtMousePress;
161
162 // qDebug() << "The current m_lastZ value:" << m_context.m_lastZ;
163
164 deconvolute();
166 }
167}
168
169
170//! Record the clicks of the mouse.
171void
172MassSpecTracePlotWidget::mousePressHandler([[maybe_unused]] QMouseEvent *event)
173{
174 // qDebug() << "Entering";
175
176 // Before working on the various data belonging to the base context, we need
177 // to get it from the base class and refresh our local context with it.
179
181
182 // qDebug() << "The current m_lastZ value:" << m_context.m_lastZ;
183
185
186 // qDebug() << "Exiting after having emitted mousePressEventSignal with context:"
187 // << m_context.toString();
188}
189
190
191//! React to the release of the mouse buttons.
192void
194{
195 // qDebug() << "Entering";
196
197 // Before working on the various data belonging to the base context, we need
198 // to get it from the base class and refresh our local context with it.
200
202
203 // qDebug() << "The current m_lastZ value:" << m_context.m_lastZ;
204
205 if(!m_context.m_isMouseDragging)
206 {
207 // Since we were not dragging, by essence we were not measuring
208 // distances and _a fortiori_ we were not deconvoluting. So
209 // reset deconvolution data in the context.
210
211 m_context.resetDeconvolutionData();
212 }
213
215
216 // qDebug() << "Exiting after having emitted mouseReleaseEventSignal with context:"
217 // << m_context.toString();
218}
219
220
223{
224 // BasePlotWidget has a member m_context of type BasePlotContext.
225 // Here we also have a m_context *distinct* member that is of type
226 // MassSpecTracePlotContext.
227
228 // While MassSpecTracePlotContext is derived from BasePlotContext, the two
229 // m_context members are distinct and there are lots of housekeeping data that
230 // are managed by the parent BasePlotWidget class in its m_context member
231 // *independently* of what we have in the ::BasePlotContext part of our
232 // m_context member that is of type MassSpecTracePlotContext. We thus need to
233 // resynchronize the data from BasePlotWidget::m_context to our m_context.
234
235 // qDebug().noquote() << "The base plot context:" <<
236 // BasePlotWidget::m_context.toString();
237
238
239 // qDebug() << "Right before refreshing the context, the current m_lastZ value:"
240 // << m_context.m_lastZ;
241
243
244
245 // qDebug() << "Right after refreshing the context, the current m_lastZ value:"
246 // << m_context.m_lastZ;
247
248 // qDebug().noquote() << "After refreshing the base context, base context:"
249 // << m_context.toString();
250
251 return m_context;
252}
253
254
255void
257{
258 m_chargeMinimalFractionalPart = charge_fractional_part;
259}
260
261
262double
267
268
269void
274
275
276int
281
282
283//! Deconvolute the mass peaks into charge and molecular mass.
284bool
286{
287
288 // qDebug() << "The current m_lastZ value:" << m_context.m_lastZ;
289
290 // There are two situations: when the user is deconvoluting on the
291 // basis of the distance between two consecutive peaks of a same
292 // isotopic cluster or when the user deconvolutes on the basis of two
293 // different charged-stated peaks that belong to the same envelope.
294
295 // We can tell the difference because in the first case the xDelta
296 // should be less than 1. In the other case, of course the difference
297 // is much greater than 1.
298
299 // In order to do the deconvolutions, we need to know what is the tolerance
300 // on the fractional part of the deconvoluted charge value. This value is set
301 // in the parent window's double spin box.
302
303 if(fabs(m_context.m_xDelta) >= 0 && fabs(m_context.m_xDelta) <= 1.1)
304 {
305 // qDebug() << "m_xDelta:" << m_context.m_xDelta
306 //<< "trying isotope-based deconvolution.";
307
309 }
310
311 // If not deconvoluting on the basis of the isotopic cluster, then:
312
314}
315
316
317//! Deconvolute the mass peaks into charge and molecular mass.
318/*!
319
320 This is one of two methods to deconvolute mass data into a charge value and
321 a Mr value. The method implemented in this function is based on the charge
322 state envelope offered by the mass spectrum (most often for polymers of a
323 reasonable size).
324
325 \param span value representing the number of peaks of the charge state
326 envelope that are spanned by the user selection. Defaults to 1, that is, the
327 span encompasses two \e consecutive mass peaks of a given charge state
328 envelope.
329
330 The z charge that is computed is for the first peak that was clicked
331 in the trace plot, and thus the Mr value that is returned is calculated
332 from the m/z value under the peak that was clicked first.
333
334 Set m_lastMz, m_lastZ and m_lastMass.
335
336 \return true if the deconvolution could be performed, false otherwise.
337 */
338bool
340{
341 // We assume that we are dealing with two successive (if span is 1) mass
342 // peaks belonging to a given charge state family.
343
344 // We call span the number of intervals in a given charge state envelope
345 // that separate the initial peak (lowerMz) from the last peak (upperMz).
346 // That parameter defaults to 1, that is the two peaks are immediately
347 // consecutive, that is, there is only one interval.
348
349 // qDebug() << "The span is:" << span;
350
351 // The m_context ranges are inherently sorted. This is not something that
352 // is favorable to us because we need to know what is the direction of the
353 // drag. That should be configurable, but we can establish that by
354 // default, the z value will be calculated for the mass peak at which
355 // the mouse drag movement started.
356
357 // These two values reflect a m/z range:
358 // startMz and curMz are always sorted (startMz < curMz)
359 double startMz = m_context.m_xRegionRangeStart;
360 double curMz = m_context.m_xRegionRangeEnd;
361
362 // qDebug() << "startMz:" << startMz << "curMz:" << curMz;
363
364 if(startMz == curMz)
365 {
366 m_context.m_lastZ = std::numeric_limits<quint16>::max();
367 m_context.m_lastMz = qQNaN();
368 m_context.m_lastTicIntensity = qQNaN();
369 m_context.m_lastMr = qQNaN();
370
371 // qDebug() << "Start and Cur are the same, no deconvolution possible.";
372 return false;
373 }
374
375 // After much discussing, the agreement we reached with users is that the
376 // m/z value that is reported has to be that of the clicked peak, that is
377 // the start point of the mouse cursor drag operation. The z charge value
378 // has to be related to that m/z value.
379
380 double chargeTemp = ((startMz * span) - span) / (curMz - startMz);
381 // This is the charge of the least charged peak, that is, by necessity
382 // the charge of the peak on the right (curMz, because the values
383 // are sorted).
384
385 // Make a judicious roundup.
386
387 double chargeIntPart;
388 double chargeFracPart = modf(chargeTemp, &chargeIntPart);
389
390 // When calculating the charge of the ion, very rarely does it provide a
391 // perfect integer value. Most often (if deconvolution is for bona fide
392 // peaks belonging to the same charge state envelope) that value is with
393 // either a large fractional part or a very small fractional part. What we
394 // test here, it that fractional part. If it is greater than
395 // m_chargeMinimalFractionalPart, then we simply round up to the next integer
396 // value (that is, chargeIntPart = 27 and chargeFracPart 0.995, then we
397 // set charge to 28). If it is lesser or equal to (1 -
398 // m_chargeMinimalFractionalPart /* that is >= 0.01 */, then we let
399 // chargeIntPart unmodified (that is, chargeIntPart = 29 and
400 // chargeFracPart 0.01, then we set charge to 29). If chargeFracPart is in
401 // between (1 - m_chargeMinimalFractionalPart) and
402 // m_chargeMinimalFractionalPart, then we consider that the peaks do not
403 // belong to the same charge state envelope.
404
405 // qDebug() << "Charge:" << chargeIntPart << "Charge fractional part: " << chargeFracPart;
406
407
408 if(chargeFracPart >= (1 - m_chargeMinimalFractionalPart /* that is >= 0.01 */) &&
409 chargeFracPart <= m_chargeMinimalFractionalPart /* that is <= 0.99 */)
410 {
411 m_context.m_lastZ = std::numeric_limits<quint16>::max();
412 m_context.m_lastMz = qQNaN();
413 m_context.m_lastTicIntensity = qQNaN();
414 m_context.m_lastMr = qQNaN();
415
416 // qDebug() << "Not a charge state family peak,"
417 // << "returning from deconvoluteChargeState";
418
419 return false;
420 }
421
422 if(chargeFracPart > m_chargeMinimalFractionalPart)
423 m_context.m_lastZ = chargeIntPart + 1;
424 else
425 m_context.m_lastZ = chargeIntPart;
426
427 // qDebug() << "Initially computed m_lastZ as " << m_context.m_lastZ;
428
429 // We know the that formula above computed the charge of the least charged
430 // ion, that corresponds to the greater m/z value of the range: curMz.
431
432 // Now, if the user did click on that least-charged peak, nothing is
433 // to be modified. But, if the user did click on the most-charged
434 // peak, that is on startMz, the we need to fix the values:
435
436 if(static_cast<int>(m_context.m_dragDirections) & static_cast<int>(DragDirections::RIGHT_TO_LEFT))
437 {
438 // qDebug() << "Dragging from right to left, that is from higher m/z value.";
439 m_context.m_lastMz = curMz;
440 }
441 else if(static_cast<int>(m_context.m_dragDirections) &
442 static_cast<int>(DragDirections::LEFT_TO_RIGHT))
443 {
444 // qDebug() << "Dragging from left to right, we need to report"
445 // << "the mass for the left peak, that is for z+span and lower m/z value.";
446 m_context.m_lastZ += span;
447 m_context.m_lastMz = startMz;
448 }
449
450 // qDebug() << "Final m_lastZ is " << m_context.m_lastZ << "m/z: m_context.m_lastMz";
451
452 m_context.m_lastMr = (m_context.m_lastMz * m_context.m_lastZ) - (m_context.m_lastZ * MPROTON);
453
454 // qDebug() << "startMz:" << QString("%1").arg(startMz, 0, 'f', 6)
455 // << "curMz:" << QString("%1").arg(curMz, 0, 'f', 6)
456 // << "m_lastMz (make computation for this startMz):"
457 // << QString("%1").arg(m_context.m_lastMz, 0, 'f', 6)
458 // << "m_lastZ:" << QString("%1").arg(m_context.m_lastZ)
459 // << "m_lastMass:" << QString("%1").arg(m_context.m_lastMr, 0, 'f', 6);
460
461 // The m_context was refreshed with the base class context in the calling
462 // chain.
464
465 return true;
466}
467
468
469//! Deconvolute the mass peaks into charge and molecular mass.
470/*!
471
472 This is one of two methods to deconvolute mass data into a charge value and
473 a Mr value. The method implemented in this function is based on the distance
474 that separates two immediately consecutive peaks of an isotopic cluster.
475 This method can be used as long as the instrument produced data with a
476 resolution sufficient to separate reasonably well the different peaks of an
477 isotopic cluster.
478
479 Set m_lastMz, m_lastZ and m_lastMass.
480
481 \return true if the deconvolution could be performed, false otherwise.
482 */
483bool
485{
486 // The m_context ranges are inherently sorted. This is not something that
487 // is favorable to us because we need to know what is the direction of the
488 // drag. That should be configurable, but we can establish that by
489 // default, the z value will be calculated for the mass peak at which
490 // the mouse drag movement started.
491
492 double startMz = m_context.m_xRegionRangeStart;
493 double curMz = m_context.m_xRegionRangeEnd;
494
495 // qDebug() << "startMz:" << startMz << "curMz:" << curMz;
496
497 if(startMz == curMz)
498 {
499 m_context.m_lastZ = std::numeric_limits<quint16>::max();
500 m_context.m_lastMz = qQNaN();
501 m_context.m_lastTicIntensity = qQNaN();
502 m_context.m_lastMr = qQNaN();
503
504 return false;
505 }
506
507 // After much discussion with people who perform mass data analysis for
508 // heavy isotope fully labelled analytes, it was decided that the Mr
509 // value thas is returned is for the peak from which the drag operation
510 // was performed. This is an analogous way of computing Mr as that used
511 // in the charge state envelope method.
512
513
514 double chargeTemp = 1 / fabs(m_context.m_xDelta);
515
516 // qDebug() << qSetRealNumberPrecision(6) << "curMz - startMz = " << fabs(curMz - startMz)
517 // << "while m_context.m_xDelta = " << m_context.m_xDelta;
518
519 // Sanity check
520 if(fabs(m_context.m_xDelta) != fabs(curMz - startMz))
521 qFatal() << "Programming error.";
522
523 // Make a judicious roundup.
524 double chargeIntPart;
525 double chargeFracPart = modf(chargeTemp, &chargeIntPart);
526
527 // qDebug() << "m_xDelta:" << m_context.m_xDelta
528 // << "chargeTemp:" << QString("%1").arg(chargeTemp, 0, 'f', 6)
529 // << "chargeIntPart:" << chargeIntPart
530 // << "chargeFracPart:" << QString("%1").arg(chargeFracPart, 0, 'f', 6)
531 // << "m_chargeMinimalFractionalPart:" << m_chargeMinimalFractionalPart;
532
533 if(chargeFracPart >= (1 - m_chargeMinimalFractionalPart /* that is >= 0.01 */) &&
534 chargeFracPart <= m_chargeMinimalFractionalPart /* that is <= 0.99 */)
535 {
536 m_context.m_lastZ = std::numeric_limits<quint16>::max();
537 m_context.m_lastMz = qQNaN();
538 m_context.m_lastTicIntensity = qQNaN();
539 m_context.m_lastMr = qQNaN();
540
541 // qDebug() << "Not in a isotopic cluster peak:"
542 // << "returning from deconvoluteIsotopicCluster";
543
544 return false;
545 }
546
547 if(chargeFracPart > m_chargeMinimalFractionalPart)
548 {
549 m_context.m_lastZ = chargeIntPart + 1;
550
551 // qDebug() << "chargeFracPart > m_chargeMinimalFractionalPart -> m_lastZ
552 // = "
553 //<< m_context.m_lastZ;
554 }
555 else
556 {
557 m_context.m_lastZ = chargeIntPart;
558
559 // qDebug()
560 //<< "chargeFracPart <= m_chargeMinimalFractionalPart -> m_lastZ = "
561 //<< m_context.m_lastZ;
562 }
563
564 // qDebug() << "Settled on m_lastZ: " << m_context.m_lastZ;
565
566 // Now that we have the charge in the form of an int, we can compute the
567 // Mr of the isotopic cluster peak that was under the cursor when the
568 // mouse drag started.
569
570 // If we know the drag direction, we would automatically know what is the
571 // peak that was under the cursor when the mouse drag mouvement was initiated.
572 // This is because the curMz is inherently larger than startMz because
573 // m_context.m_xRegionRangeStart and m_context.m_xRegionRangeEnd are inherently
574 // sorted (start < end).
575
576 if(static_cast<int>(m_context.m_dragDirections) & static_cast<int>(DragDirections::RIGHT_TO_LEFT))
577 {
578 // qDebug() << "Dragging from right to left, that is from higher m/z value.";
579 m_context.m_lastMz = curMz;
580 }
581 else if(static_cast<int>(m_context.m_dragDirections) &
582 static_cast<int>(DragDirections::LEFT_TO_RIGHT))
583 {
584 // qDebug() << "Dragging from left to right, we need to report"
585 // << "the mass for the left peak.";
586 m_context.m_lastMz = startMz;
587 }
588
589 m_context.m_lastMr = (m_context.m_lastMz * m_context.m_lastZ) - (m_context.m_lastZ * MPROTON);
590
591 // qDebug() << "m_lastMz :" << QString("%1").arg(m_context.m_lastMz, 0, 'f', 6)
592 // << "m_lastZ:" << QString("%1").arg(m_context.m_lastZ)
593 // << "m_lastMr:" << QString("%1").arg(m_context.m_lastMr, 0, 'f', 6);
594
595 // The m_context was refreshed with the base class context in the calling
596 // chain.
598
599 return true;
600}
601
602
603bool
605{
606
607 // m_xRangeLower and m_xRangeUpper and m_xDelta (in fabs() form) have been set
608 // during mouve movement handling. Note that the range values *are
609 // sorted*.
610
611 if(!m_context.m_xDelta || m_context.m_xDelta > 1)
612 {
613 m_context.m_lastResolvingPower = std::numeric_limits<double>::min();
614
615 return false;
616 }
617
618 // Resolving power is m/z / Delta(m/z), for singly-charged species.
619
620 // qDebug() << "Calculating the resolving power with these data:"
621 //<< "m_context.m_xRegionRangeStart: " << m_context.m_xRegionRangeStart
622 //<< "m_context.m_xRegionRangeEnd: " << m_context.m_xRegionRangeEnd
623 //<< "m_context.m_xDelta / 2: " << m_context.m_xDelta / 2;
624
625 m_context.m_lastResolvingPower =
626 (std::min<double>(m_context.m_xRegionRangeStart, m_context.m_xRegionRangeEnd) +
627 (m_context.m_xDelta / 2)) /
628 m_context.m_xDelta;
629
630 // The m_context was refreshed with the base class context in the calling
631 // chain.
633
634 return true;
635}
636
637
638} // namespace pappso
virtual void mouseMoveHandlerDraggingCursor()
virtual void keyPressEvent(QKeyEvent *event)
KEYBOARD-related EVENTS.
virtual void mouseMoveHandlerNotDraggingCursor()
virtual void mousePressHandler(QMouseEvent *event)
KEYBOARD-related EVENTS.
virtual void mouseReleaseHandler(QMouseEvent *event)
virtual void mouseMoveHandler(QMouseEvent *event)
KEYBOARD-related EVENTS.
virtual void keyReleaseEvent(QKeyEvent *event)
Handle specific key codes and trigger respective actions.
BasePlotContext m_context
virtual void mouseMoveHandler(QMouseEvent *event) override
Handle mouse movements, in particular record all the last visited points.
void resolvingPowerComputationSignal(const MassSpecTracePlotContext &context)
virtual void keyReleaseEvent(QKeyEvent *event) override
Handle specific key codes and trigger respective actions.
const MassSpecTracePlotContext & refreshBaseContext() const
bool deconvoluteChargedState(int span=1)
Deconvolute the mass peaks into charge and molecular mass.
void mousePressEventSignal(const MassSpecTracePlotContext &context)
bool deconvoluteIsotopicCluster()
Deconvolute the mass peaks into charge and molecular mass.
bool deconvolute()
Deconvolute the mass peaks into charge and molecular mass.
virtual void mouseMoveHandlerDraggingCursor() override
void keyPressEventSignal(const MassSpecTracePlotContext &context)
void massDeconvolutionSignal(const MassSpecTracePlotContext &context)
virtual void mousePressHandler(QMouseEvent *event) override
Record the clicks of the mouse.
void setChargeMinimalFractionalPart(double charge_fractional_part)
virtual void keyPressEvent(QKeyEvent *event) override
Set the m_pressedKeyCode to the key code in event.
void mouseReleaseEventSignal(const MassSpecTracePlotContext &context)
virtual void mouseMoveHandlerNotDraggingCursor() override
virtual void mouseReleaseHandler(QMouseEvent *event) override
React to the release of the mouse buttons.
int massSpecTracePlotContextMetaTypeId
int massSpecTracePlotContextPtrMetaTypeId
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
const pappso_double MPROTON(1.007276466879)