libpappsomspp
Library for mass spectrometry
pappso::Trace Class Reference

A simple container of DataPoint instances. More...

#include <trace.h>

Inheritance diagram for pappso::Trace:
pappso::MassSpectrum pappso::Xic

Public Member Functions

 Trace ()
 
 Trace (const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
 
 Trace (const std::vector< std::pair< pappso_double, pappso_double >> &dataPoints)
 
 Trace (const std::vector< DataPoint > &dataPoints)
 
 Trace (const std::vector< DataPoint > &&dataPoints)
 
 Trace (const MapTrace &map_trace)
 
 Trace (const Trace &other)
 
 Trace (const Trace &&other)
 
virtual ~Trace ()
 
size_t initialize (const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
 
size_t initialize (const Trace &other)
 
size_t initialize (const std::map< pappso_double, pappso_double > &map)
 
virtual Traceoperator= (const Trace &x)
 
virtual Traceoperator= (Trace &&x)
 
TraceSPtr makeTraceSPtr () const
 
TraceCstSPtr makeTraceCstSPtr () const
 
std::vector< pappso_doublexValues () const
 
std::vector< pappso_doubleyValues () const
 
std::map< pappso_double, pappso_doubletoMap () const
 
DataPoint containsX (pappso_double value, PrecisionPtr precision_p=nullptr) const
 
const DataPointminYDataPoint () const
 
const DataPointmaxYDataPoint () const
 
pappso_double minY () const
 
pappso_double maxY () const
 
pappso_double maxY (double mzStart, double mzEnd) const
 
pappso_double sumY () const
 
pappso_double sumY (double mzStart, double mzEnd) const
 
void sortX ()
 
void sortY ()
 
void unique ()
 
virtual Tracefilter (const FilterInterface &filter) final
 apply a filter on this trace More...
 
QString toString () const
 
std::vector< DataPoint >::const_iterator dataPointCstIteratorWithX (pappso_double value) const
 find datapoint with exactly x value More...
 

Protected Member Functions

std::size_t dataPointIndexWithX (pappso_double value) const
 
std::vector< DataPoint >::iterator dataPointIteratorWithX (pappso_double value)
 

Friends

class TraceCombiner
 
class TraceMinusCombiner
 
class TracePlusCombiner
 
class MassSpectrumCombinerInterface
 

Detailed Description

A simple container of DataPoint instances.

Definition at line 146 of file trace.h.

Constructor & Destructor Documentation

◆ Trace() [1/8]

pappso::Trace::Trace ( )

Definition at line 434 of file trace.cpp.

435 {
436 }

◆ Trace() [2/8]

pappso::Trace::Trace ( const std::vector< pappso_double > &  xVector,
const std::vector< pappso_double > &  yVector 
)

Definition at line 439 of file trace.cpp.

441 {
442  initialize(xVector, yVector);
443 }
size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
Definition: trace.cpp:512

References initialize().

◆ Trace() [3/8]

pappso::Trace::Trace ( const std::vector< std::pair< pappso_double, pappso_double >> &  dataPoints)

Definition at line 446 of file trace.cpp.

448 {
449  reserve(dataPoints.size());
450 
451  for(auto &dataPoint : dataPoints)
452  {
453  push_back(DataPoint(dataPoint));
454  }
455 
456  sortX();
457  // std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
458  // return (a.x < b.x);
459  //});
460 }
void sortX()
Definition: trace.cpp:936

References sortX().

◆ Trace() [4/8]

pappso::Trace::Trace ( const std::vector< DataPoint > &  dataPoints)

Definition at line 463 of file trace.cpp.

464  : std::vector<DataPoint>(dataPoints)
465 {
466  sortX();
467  // std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
468  // return (a.x < b.x);
469  //});
470 }

References sortX().

◆ Trace() [5/8]

pappso::Trace::Trace ( const std::vector< DataPoint > &&  dataPoints)

Definition at line 473 of file trace.cpp.

474  : std::vector<DataPoint>(std::move(dataPoints))
475 {
476  // This constructor used by the MassSpectrum && constructor.
477 
478  sortX();
479  // std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
480  // return (a.x < b.x);
481  //});
482 }

References sortX().

◆ Trace() [6/8]

pappso::Trace::Trace ( const MapTrace map_trace)
explicit

Definition at line 485 of file trace.cpp.

486 {
487  for(auto &&item : map_trace)
488  push_back(DataPoint(item.first, item.second));
489 
490  // No need to sort, maps are sorted by key (that is, x).
491 }

◆ Trace() [7/8]

pappso::Trace::Trace ( const Trace other)

Definition at line 493 of file trace.cpp.

493  : std::vector<DataPoint>(other)
494 {
495 }

◆ Trace() [8/8]

pappso::Trace::Trace ( const Trace &&  other)

Definition at line 498 of file trace.cpp.

498  : std::vector<DataPoint>(std::move(other))
499 {
500  // This constructor used by the MassSpectrum && constructor.
501 }

◆ ~Trace()

pappso::Trace::~Trace ( )
virtual

Definition at line 504 of file trace.cpp.

505 {
506  // Calls the destructor for each DataPoint object in the vector.
507  clear();
508 }

Member Function Documentation

◆ containsX()

DataPoint pappso::Trace::containsX ( pappso_double  value,
PrecisionPtr  precision_p = nullptr 
) const

Definition at line 715 of file trace.cpp.

716 {
717  // std::cout << std::setprecision(10) << "getting value: " << value
718  //<< " and precision: " << precision_p->getNominal() << std::endl;
719 
720  pappso_double delta = precision_p->delta(value);
721 
722  double left_most = value - delta;
723  double right_most = value + delta;
724 
725  // std::cout << std::setprecision(10) << "delta: " << delta
726  //<< " left_most: " << left_most << " right_most: " << right_most
727  //<< std::endl;
728 
729  auto iterator =
730  std::find_if(begin(),
731  end(),
732  [value, precision_p, delta, left_most, right_most](
733  const DataPoint &data_point) {
734  if(precision_p)
735  {
736 
737  // FIXME: unbelievable behaviour: when building in
738  // release mode this code, under i386 (but not x86_64),
739  // this code fails if the following cout statement is
740  // missing.
741 
742  // std::cout << std::setprecision(10)
743  //<< "Testing data_point.x: " << data_point.x
744  //<< std::endl;
745 
746  // For this reason I had to deactivate the related tests
747  // for i386 in tests/test_trace.cpp
748 
749  double diff_to_left_most = data_point.x - left_most;
750  double diff_to_right_most = data_point.x - right_most;
751 
752  // std::cout << std::setprecision(10)
753  //<< "diff_to_left_most: " << diff_to_left_most
754  //<< " diff_to_right_most: " << diff_to_right_most <<
755  // std::endl;
756 
757  // if(diff_to_left_most > 0)
758  //{
759  // std::cout << std::setprecision(10)
760  //<< " point is right of left_most: " <<
761  // diff_to_left_most
762  //<< std::endl;
763  //}
764  // if(diff_to_left_most < 0)
765  //{
766  // std::cout << std::setprecision(10)
767  //<< "point is left of left_most: " << diff_to_left_most
768  //<< std::endl;
769  //}
770  // if(!diff_to_left_most)
771  //{
772  // std::cout << std::setprecision(10)
773  //<< "point is spot on left_most: " << diff_to_left_most
774  //<< std::endl;
775  //}
776 
777  // if(diff_to_right_most > 0)
778  //{
779  // std::cout << std::setprecision(10)
780  //<< "point is right of right_most: " <<
781  // diff_to_right_most
782  //<< std::endl;
783  //}
784  // if(diff_to_right_most < 0)
785  //{
786  // std::cout << std::setprecision(10)
787  //<< "point is left or of right_most: "
788  //<< diff_to_right_most << std::endl;
789  //}
790  // if(!diff_to_right_most)
791  //{
792  // std::cout << std::setprecision(10)
793  //<< "point is spot on right_most: " <<
794  // diff_to_right_most
795  //<< std::endl;
796  //}
797 
798  if(diff_to_left_most >= 0 && diff_to_right_most <= 0)
799  {
800  // std::cout << "The point is inside the range,
801  // should return true."
802  //<< std::endl;
803  return true;
804  }
805  else
806  {
807  // std::cout
808  //<< "The point is outside the range, should return
809  // false."
810  //<< std::endl;
811  return false;
812  }
813  }
814  else
815  {
816  return (data_point.x == value);
817  }
818  });
819 
820  if(iterator != end())
821  {
822  // The returned data point is valid.
823  return *iterator;
824  }
825  else
826  {
827  // The returned data point is invalid because it is not initialized.
828  return DataPoint();
829  }
830 }
double pappso_double
A type definition for doubles.
Definition: types.h:48

References pappso::PrecisionBase::delta(), and pappso::DataPoint::x.

◆ dataPointCstIteratorWithX()

std::vector< DataPoint >::const_iterator pappso::Trace::dataPointCstIteratorWithX ( pappso_double  value) const

find datapoint with exactly x value

Definition at line 690 of file trace.cpp.

691 {
692  auto iterator =
693  std::find_if(begin(), end(), [value](const DataPoint &dataPoint) {
694  return (dataPoint.x == value);
695  });
696 
697  return iterator;
698 }

References pappso::DataPoint::x.

Referenced by dataPointIndexWithX().

◆ dataPointIndexWithX()

std::size_t pappso::Trace::dataPointIndexWithX ( pappso_double  value) const
protected

Return a reference to the DataPoint instance that has its y member equal to value.

Definition at line 702 of file trace.cpp.

703 {
704  std::vector<DataPoint>::const_iterator iterator =
706 
707  if(iterator != end())
708  return std::distance(begin(), iterator);
709 
710  return std::numeric_limits<std::size_t>::max();
711 }
std::vector< DataPoint >::const_iterator dataPointCstIteratorWithX(pappso_double value) const
find datapoint with exactly x value
Definition: trace.cpp:690

References dataPointCstIteratorWithX().

◆ dataPointIteratorWithX()

std::vector< DataPoint >::iterator pappso::Trace::dataPointIteratorWithX ( pappso_double  value)
protected

Definition at line 678 of file trace.cpp.

679 {
680  auto iterator =
681  std::find_if(begin(), end(), [value](const DataPoint &dataPoint) {
682  return (dataPoint.x == value);
683  });
684 
685  return iterator;
686 }

References pappso::DataPoint::x.

◆ filter()

Trace & pappso::Trace::filter ( const FilterInterface filter)
finalvirtual

apply a filter on this trace

Parameters
filterto process the signal
Returns
reference on the modified Trace

Definition at line 981 of file trace.cpp.

982 {
983  return filter.filter(*this);
984 }
virtual Trace & filter(const FilterInterface &filter) final
apply a filter on this trace
Definition: trace.cpp:981

References filter().

Referenced by pappso::MsRunRetentionTime< T >::align(), filter(), pappso::FilterSuiteString::filter(), pappso::FilterSuite::filter(), and pappso::MassSpectrum::massSpectrumFilter().

◆ initialize() [1/3]

size_t pappso::Trace::initialize ( const std::map< pappso_double, pappso_double > &  map)

Definition at line 545 of file trace.cpp.

546 {
547 
548  // We are initializing, not appending.
549  erase(begin(), end());
550 
551  for(auto &&item : map)
552  {
553  push_back(DataPoint(item.first, item.second));
554  }
555 
556  // No need to sort, maps are sorted by key (that is, x).
557 
558  return size();
559 }

◆ initialize() [2/3]

size_t pappso::Trace::initialize ( const std::vector< pappso_double > &  xVector,
const std::vector< pappso_double > &  yVector 
)

Definition at line 512 of file trace.cpp.

514 {
515  // Sanity check
516  if(xVector.size() != yVector.size())
517  throw ExceptionNotPossible(
518  "trace.cpp -- ERROR xVector and yVector must have the same size.");
519 
520  // We are initializing, not appending.
521  erase(begin(), end());
522 
523  for(std::size_t iter = 0; iter < xVector.size(); ++iter)
524  {
525  push_back(DataPoint(xVector.at(iter), yVector.at(iter)));
526  }
527 
528  sortX();
529  // std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
530  // return (a.x < b.x);
531  //});
532 
533 #if 0
534  for(auto &item : *this)
535  {
536  std::cout << item.x << "-" << item.y;
537  }
538 #endif
539 
540  return size();
541 }

References sortX().

Referenced by Trace().

◆ initialize() [3/3]

size_t pappso::Trace::initialize ( const Trace other)

Definition at line 563 of file trace.cpp.

564 {
565  *this = other;
566 
567  return size();
568 }

◆ makeTraceCstSPtr()

TraceCstSPtr pappso::Trace::makeTraceCstSPtr ( ) const

Definition at line 596 of file trace.cpp.

597 {
598  return std::make_shared<const Trace>(*this);
599 }

◆ makeTraceSPtr()

TraceSPtr pappso::Trace::makeTraceSPtr ( ) const

Definition at line 589 of file trace.cpp.

590 {
591  return std::make_shared<Trace>(*this);
592 }

◆ maxY() [1/2]

pappso_double pappso::Trace::maxY ( ) const

Definition at line 879 of file trace.cpp.

880 {
881  return maxYDataPoint().y;
882 }
const DataPoint & maxYDataPoint() const
Definition: trace.cpp:853
pappso_double y
Definition: datapoint.h:23

References maxYDataPoint(), and pappso::DataPoint::y.

◆ maxY() [2/2]

pappso_double pappso::Trace::maxY ( double  mzStart,
double  mzEnd 
) const

Definition at line 918 of file trace.cpp.

919 {
920  std::vector<DataPoint>::const_iterator begin_it =
921  findFirstEqualOrGreaterX(this->begin(), this->end(), mzStart);
922 
923  double max_y = 0;
924 
925  while(begin_it != findFirstGreaterX(begin_it, this->end(), mzEnd))
926  {
927  if(begin_it->y > max_y)
928  max_y = begin_it->y;
929  begin_it++;
930  }
931  return max_y;
932 }
std::vector< DataPoint >::iterator findFirstEqualOrGreaterX(std::vector< DataPoint >::iterator begin, std::vector< DataPoint >::iterator end, const double &value)
find the first element in which X is equal or greater than the value searched important : it implies ...
Definition: trace.cpp:32
std::vector< DataPoint >::iterator findFirstGreaterX(std::vector< DataPoint >::iterator begin, std::vector< DataPoint >::iterator end, const double &value)
find the first element in which X is greater than the value searched important : it implies that Trac...
Definition: trace.cpp:60

References pappso::findFirstEqualOrGreaterX(), and pappso::findFirstGreaterX().

◆ maxYDataPoint()

const DataPoint & pappso::Trace::maxYDataPoint ( ) const

Definition at line 853 of file trace.cpp.

854 {
855  auto dataPoint = std::max_element(
856  begin(), end(), [](const DataPoint &a, const DataPoint &b) {
857  return (a.y < b.y);
858  });
859 
860  if(dataPoint == end())
861  {
862  throw ExceptionOutOfRange(
863  QObject::tr("unable to get max peak intensity on spectrum size %1")
864  .arg(size()));
865  }
866 
867  return (*dataPoint);
868 }

References pappso::a, and pappso::b.

Referenced by pappso::flooredLocalMaxima(), pappso::MassSpectrum::maxIntensityDataPoint(), and maxY().

◆ minY()

pappso_double pappso::Trace::minY ( ) const

Definition at line 872 of file trace.cpp.

873 {
874  return minYDataPoint().y;
875 }
const DataPoint & minYDataPoint() const
Definition: trace.cpp:834

References minYDataPoint(), and pappso::DataPoint::y.

◆ minYDataPoint()

const DataPoint & pappso::Trace::minYDataPoint ( ) const

Definition at line 834 of file trace.cpp.

835 {
836  auto dataPoint = std::min_element(
837  begin(), end(), [](const DataPoint &a, const DataPoint &b) {
838  return (a.y < b.y);
839  });
840 
841  if(dataPoint == end())
842  {
843  throw ExceptionOutOfRange(
844  QObject::tr("unable to get min peak intensity on spectrum size %1")
845  .arg(size()));
846  }
847 
848  return (*dataPoint);
849 }

References pappso::a, and pappso::b.

Referenced by pappso::MassSpectrum::minIntensityDataPoint(), and minY().

◆ operator=() [1/2]

Trace & pappso::Trace::operator= ( const Trace x)
virtual

Definition at line 572 of file trace.cpp.

573 {
574  assign(other.begin(), other.end());
575 
576  return *this;
577 }

◆ operator=() [2/2]

Trace & pappso::Trace::operator= ( Trace &&  x)
virtual

Definition at line 581 of file trace.cpp.

582 {
583  vector<DataPoint>::operator=(std::move(other));
584  return *this;
585 }

◆ sortX()

◆ sortY()

void pappso::Trace::sortY ( )

Definition at line 944 of file trace.cpp.

945 {
946  std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
947  return (a.y > b.y);
948  });
949 }

References pappso::a, and pappso::b.

Referenced by pappso::FilterChargeDeconvolution::filter(), and pappso::FilterMzExclusion::filter().

◆ sumY() [1/2]

pappso_double pappso::Trace::sumY ( ) const

Definition at line 886 of file trace.cpp.

887 {
888  // double sum = 0;
889 
890  // for(auto &&dp : m_dataPoints)
891  // sum += dp.y;
892 
893  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()"
894  //<< "Returning sum/tic:" << sum;
895 
896  // return sum;
897 
898  return std::accumulate(begin(),
899  end(),
900  (double)0,
901  [](pappso_double sum, const DataPoint &dataPoint) {
902  return (sum + dataPoint.y);
903  });
904 }

References pappso::sum, and pappso::DataPoint::y.

Referenced by pappso::PsmFeatures::setPeptideSpectrumCharge(), pappso::MassSpectrum::tic(), and pappso::MassSpectrum::totalIonCurrent().

◆ sumY() [2/2]

pappso_double pappso::Trace::sumY ( double  mzStart,
double  mzEnd 
) const

Definition at line 908 of file trace.cpp.

909 {
910  auto begin_it = findFirstEqualOrGreaterX(this->begin(), this->end(), mzStart);
911  auto end_it = findFirstGreaterX(begin_it, this->end(), mzEnd);
912 
913  return sumYTrace(begin_it, end_it, 0);
914 }
double sumYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end, double init)
calculate the sum of y value of a trace
Definition: trace.cpp:205

References pappso::findFirstEqualOrGreaterX(), pappso::findFirstGreaterX(), and pappso::sumYTrace().

◆ toMap()

std::map< pappso_double, pappso_double > pappso::Trace::toMap ( ) const

Definition at line 631 of file trace.cpp.

632 {
633  std::map<pappso_double, pappso_double> map;
634 
635  std::pair<std::map<pappso_double, pappso_double>::iterator, bool> ret;
636 
637  for(auto &&dataPoint : *this)
638  {
639  ret = map.insert(
640  std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
641 
642  if(ret.second == false)
643  {
644  qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
645  << "It is odd that the Trace contains multiple same keys.";
646 
647  // No insertion, then increment the y value.
648  ret.first->second += dataPoint.y;
649  }
650  }
651 
652  return map;
653 }

◆ toString()

QString pappso::Trace::toString ( ) const

Definition at line 964 of file trace.cpp.

965 {
966  // Even if the spectrum is empty, we should return an empty string.
967  QString text;
968 
969  for(auto &&dataPoint : *this)
970  {
971  text.append(QString("%1 %2\n")
972  .arg(dataPoint.x, 0, 'f', 10)
973  .arg(dataPoint.y, 0, 'f', 10));
974  }
975 
976  return text;
977 }

Referenced by pappso::FilterSuiteString::filter(), and pappso::FilterSuiteString::toString().

◆ unique()

void pappso::Trace::unique ( )

Definition at line 952 of file trace.cpp.

953 {
954  auto last =
955  std::unique(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
956  return (a.x == b.x);
957  });
958 
959  erase(last, end());
960 }

References pappso::a, pappso::b, and pappso::last.

Referenced by pappso::MsRunRetentionTime< T >::getCommonDeltaRt().

◆ xValues()

std::vector< pappso_double > pappso::Trace::xValues ( ) const

Definition at line 603 of file trace.cpp.

604 {
605  std::vector<pappso_double> values;
606 
607  for(auto &&dataPoint : *this)
608  {
609  values.push_back(dataPoint.x);
610  }
611 
612  return values;
613 }

Referenced by pappso::BaseTracePlotWidget::addTrace().

◆ yValues()

std::vector< pappso_double > pappso::Trace::yValues ( ) const

Definition at line 617 of file trace.cpp.

618 {
619  std::vector<pappso_double> values;
620 
621  for(auto &&dataPoint : *this)
622  {
623  values.push_back(dataPoint.y);
624  }
625 
626  return values;
627 }

Referenced by pappso::BaseTracePlotWidget::addTrace(), and pappso::MsRunRetentionTime< T >::align().

Friends And Related Function Documentation

◆ MassSpectrumCombinerInterface

friend class MassSpectrumCombinerInterface
friend

Definition at line 153 of file trace.h.

◆ TraceCombiner

friend class TraceCombiner
friend

Definition at line 149 of file trace.h.

◆ TraceMinusCombiner

friend class TraceMinusCombiner
friend

Definition at line 150 of file trace.h.

◆ TracePlusCombiner

friend class TracePlusCombiner
friend

Definition at line 151 of file trace.h.


The documentation for this class was generated from the following files: