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

Q_INVOKABLE Trace ()
 
Q_INVOKABLE Trace (const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
 
Q_INVOKABLE Trace (const std::vector< std::pair< pappso_double, pappso_double >> &dataPoints)
 
Q_INVOKABLE Trace (const std::vector< DataPoint > &dataPoints)
 
Q_INVOKABLE Trace (const std::vector< DataPoint > &&dataPoints)
 
 Trace (const MapTrace &map_trace)
 
Q_INVOKABLE Trace (const Trace &other)
 
Q_INVOKABLE Trace (const Trace &&other)
 
virtual ~Trace ()
 
Q_INVOKABLE size_t initialize (const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
 
Q_INVOKABLE size_t initialize (const Trace &other)
 
Q_INVOKABLE size_t initialize (const std::map< pappso_double, pappso_double > &map)
 
Q_INVOKABLE size_t append (const DataPoint &data_point)
 
virtual Q_INVOKABLE Traceoperator= (const Trace &x)
 
virtual Traceoperator= (Trace &&x)
 
TraceSPtr makeTraceSPtr () const
 
TraceCstSPtr makeTraceCstSPtr () const
 
Q_INVOKABLE std::vector< pappso_doublexValues () const
 
Q_INVOKABLE std::vector< pappso_doubleyValues () const
 
std::map< pappso_double, pappso_doubletoMap () const
 
DataPoint containsX (pappso_double value, PrecisionPtr precision_p=nullptr) const
 
Q_INVOKABLE const DataPointminYDataPoint () const
 
Q_INVOKABLE const DataPointmaxYDataPoint () const
 
Q_INVOKABLE pappso_double minY () const
 
Q_INVOKABLE pappso_double maxY () const
 
Q_INVOKABLE pappso_double maxY (double mzStart, double mzEnd) const
 
Q_INVOKABLE pappso_double sumY () const
 
Q_INVOKABLE pappso_double sumY (double mzStart, double mzEnd) const
 
Q_INVOKABLE void sortX ()
 
Q_INVOKABLE void sortY ()
 
Q_INVOKABLE void unique ()
 
virtual Tracefilter (const FilterInterface &filter) final
 apply a filter on this trace More...
 
Q_INVOKABLE 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 37 of file trace.h.

Constructor & Destructor Documentation

◆ Trace() [1/8]

pappso::Trace::Trace ( )

Definition at line 471 of file trace.cpp.

472 {
473 }

◆ Trace() [2/8]

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

Definition at line 476 of file trace.cpp.

478 {
479  initialize(xVector, yVector);
480 }
Q_INVOKABLE size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
Definition: trace.cpp:550

References initialize().

◆ Trace() [3/8]

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

Definition at line 483 of file trace.cpp.

485 {
486  reserve(dataPoints.size());
487 
488  for(auto &dataPoint : dataPoints)
489  {
490  push_back(DataPoint(dataPoint));
491  }
492 
493  sortX();
494  // std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
495  // return (a.x < b.x);
496  //});
497 }
Q_INVOKABLE void sortX()
Definition: trace.cpp:983

References sortX().

◆ Trace() [4/8]

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

Definition at line 500 of file trace.cpp.

501  : std::vector<DataPoint>(dataPoints)
502 {
503  sortX();
504  // std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
505  // return (a.x < b.x);
506  //});
507 }

References sortX().

◆ Trace() [5/8]

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

Definition at line 510 of file trace.cpp.

511  : std::vector<DataPoint>(std::move(dataPoints))
512 {
513  // This constructor used by the MassSpectrum && constructor.
514 
515  sortX();
516  // std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
517  // return (a.x < b.x);
518  //});
519 }

References sortX().

◆ Trace() [6/8]

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

Definition at line 522 of file trace.cpp.

523 {
524  for(auto &&item : map_trace)
525  push_back(DataPoint(item.first, item.second));
526 
527  // No need to sort, maps are sorted by key (that is, x).
528 }

◆ Trace() [7/8]

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

Definition at line 530 of file trace.cpp.

530  : std::vector<DataPoint>(other)
531 {
532 }

◆ Trace() [8/8]

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

Definition at line 535 of file trace.cpp.

536  : std::vector<DataPoint>(std::move(other))
537 {
538  // This constructor used by the MassSpectrum && constructor.
539 }

◆ ~Trace()

pappso::Trace::~Trace ( )
virtual

Definition at line 542 of file trace.cpp.

543 {
544  // Calls the destructor for each DataPoint object in the vector.
545  clear();
546 }

Member Function Documentation

◆ append()

size_t pappso::Trace::append ( const DataPoint data_point)

Definition at line 610 of file trace.cpp.

611 {
612  push_back(data_point);
613 
614  return size();
615 }

◆ containsX()

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

Definition at line 762 of file trace.cpp.

763 {
764  // std::cout << std::setprecision(10) << "getting value: " << value
765  //<< " and precision: " << precision_p->getNominal() << std::endl;
766 
767  pappso_double delta = precision_p->delta(value);
768 
769  double left_most = value - delta;
770  double right_most = value + delta;
771 
772  // std::cout << std::setprecision(10) << "delta: " << delta
773  //<< " left_most: " << left_most << " right_most: " << right_most
774  //<< std::endl;
775 
776  auto iterator =
777  std::find_if(begin(),
778  end(),
779  [value, precision_p, delta, left_most, right_most](
780  const DataPoint &data_point) {
781  if(precision_p)
782  {
783 
784  // FIXME: unbelievable behaviour: when building in
785  // release mode this code, under i386 (but not x86_64),
786  // this code fails if the following cout statement is
787  // missing.
788 
789  // std::cout << std::setprecision(10)
790  //<< "Testing data_point.x: " << data_point.x
791  //<< std::endl;
792 
793  // For this reason I had to deactivate the related tests
794  // for i386 in tests/test_trace.cpp
795 
796  double diff_to_left_most = data_point.x - left_most;
797  double diff_to_right_most = data_point.x - right_most;
798 
799  // std::cout << std::setprecision(10)
800  //<< "diff_to_left_most: " << diff_to_left_most
801  //<< " diff_to_right_most: " << diff_to_right_most <<
802  // std::endl;
803 
804  // if(diff_to_left_most > 0)
805  //{
806  // std::cout << std::setprecision(10)
807  //<< " point is right of left_most: " <<
808  // diff_to_left_most
809  //<< std::endl;
810  //}
811  // if(diff_to_left_most < 0)
812  //{
813  // std::cout << std::setprecision(10)
814  //<< "point is left of left_most: " << diff_to_left_most
815  //<< std::endl;
816  //}
817  // if(!diff_to_left_most)
818  //{
819  // std::cout << std::setprecision(10)
820  //<< "point is spot on left_most: " << diff_to_left_most
821  //<< std::endl;
822  //}
823 
824  // if(diff_to_right_most > 0)
825  //{
826  // std::cout << std::setprecision(10)
827  //<< "point is right of right_most: " <<
828  // diff_to_right_most
829  //<< std::endl;
830  //}
831  // if(diff_to_right_most < 0)
832  //{
833  // std::cout << std::setprecision(10)
834  //<< "point is left or of right_most: "
835  //<< diff_to_right_most << std::endl;
836  //}
837  // if(!diff_to_right_most)
838  //{
839  // std::cout << std::setprecision(10)
840  //<< "point is spot on right_most: " <<
841  // diff_to_right_most
842  //<< std::endl;
843  //}
844 
845  if(diff_to_left_most >= 0 && diff_to_right_most <= 0)
846  {
847  // std::cout << "The point is inside the range,
848  // should return true."
849  //<< std::endl;
850  return true;
851  }
852  else
853  {
854  // std::cout
855  //<< "The point is outside the range, should return
856  // false."
857  //<< std::endl;
858  return false;
859  }
860  }
861  else
862  {
863  return (data_point.x == value);
864  }
865  });
866 
867  if(iterator != end())
868  {
869  // The returned data point is valid.
870  return *iterator;
871  }
872  else
873  {
874  // The returned data point is invalid because it is not initialized.
875  return DataPoint();
876  }
877 }
double pappso_double
A type definition for doubles.
Definition: types.h:49

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 737 of file trace.cpp.

738 {
739  auto iterator =
740  std::find_if(begin(), end(), [value](const DataPoint &dataPoint) {
741  return (dataPoint.x == value);
742  });
743 
744  return iterator;
745 }

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 749 of file trace.cpp.

750 {
751  std::vector<DataPoint>::const_iterator iterator =
753 
754  if(iterator != end())
755  return std::distance(begin(), iterator);
756 
757  return std::numeric_limits<std::size_t>::max();
758 }
std::vector< DataPoint >::const_iterator dataPointCstIteratorWithX(pappso_double value) const
find datapoint with exactly x value
Definition: trace.cpp:737

References dataPointCstIteratorWithX().

◆ dataPointIteratorWithX()

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

Definition at line 725 of file trace.cpp.

726 {
727  auto iterator =
728  std::find_if(begin(), end(), [value](const DataPoint &dataPoint) {
729  return (dataPoint.x == value);
730  });
731 
732  return iterator;
733 }

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 1028 of file trace.cpp.

1029 {
1030  return filter.filter(*this);
1031 }
virtual Trace & filter(const FilterInterface &filter) final
apply a filter on this trace
Definition: trace.cpp:1028

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 583 of file trace.cpp.

584 {
585 
586  // We are initializing, not appending.
587  erase(begin(), end());
588 
589  for(auto &&item : map)
590  {
591  push_back(DataPoint(item.first, item.second));
592  }
593 
594  // No need to sort, maps are sorted by key (that is, x).
595 
596  return size();
597 }

◆ initialize() [2/3]

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

Definition at line 550 of file trace.cpp.

552 {
553  // Sanity check
554  if(xVector.size() != yVector.size())
555  throw ExceptionNotPossible(
556  "trace.cpp -- ERROR xVector and yVector must have the same size.");
557 
558  // We are initializing, not appending.
559  erase(begin(), end());
560 
561  for(std::size_t iter = 0; iter < xVector.size(); ++iter)
562  {
563  push_back(DataPoint(xVector.at(iter), yVector.at(iter)));
564  }
565 
566  sortX();
567  // std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
568  // return (a.x < b.x);
569  //});
570 
571 #if 0
572  for(auto &item : *this)
573  {
574  std::cout << item.x << "-" << item.y;
575  }
576 #endif
577 
578  return size();
579 }

References sortX().

Referenced by Trace().

◆ initialize() [3/3]

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

Definition at line 601 of file trace.cpp.

602 {
603  *this = other;
604 
605  return size();
606 }

◆ makeTraceCstSPtr()

TraceCstSPtr pappso::Trace::makeTraceCstSPtr ( ) const

Definition at line 643 of file trace.cpp.

644 {
645  return std::make_shared<const Trace>(*this);
646 }

◆ makeTraceSPtr()

TraceSPtr pappso::Trace::makeTraceSPtr ( ) const

Definition at line 636 of file trace.cpp.

637 {
638  return std::make_shared<Trace>(*this);
639 }

◆ maxY() [1/2]

pappso_double pappso::Trace::maxY ( ) const

Definition at line 926 of file trace.cpp.

927 {
928  return maxYDataPoint().y;
929 }
Q_INVOKABLE const DataPoint & maxYDataPoint() const
Definition: trace.cpp:900
pappso_double y
Definition: datapoint.h:24

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

◆ maxY() [2/2]

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

Definition at line 965 of file trace.cpp.

966 {
967  std::vector<DataPoint>::const_iterator begin_it =
968  findFirstEqualOrGreaterX(this->begin(), this->end(), mzStart);
969 
970  double max_y = 0;
971 
972  while(begin_it != findFirstGreaterX(begin_it, this->end(), mzEnd))
973  {
974  if(begin_it->y > max_y)
975  max_y = begin_it->y;
976  begin_it++;
977  }
978  return max_y;
979 }
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:69
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:97

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

◆ maxYDataPoint()

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

Definition at line 900 of file trace.cpp.

901 {
902  auto dataPoint = std::max_element(
903  begin(), end(), [](const DataPoint &a, const DataPoint &b) {
904  return (a.y < b.y);
905  });
906 
907  if(dataPoint == end())
908  {
909  throw ExceptionOutOfRange(
910  QObject::tr("unable to get max peak intensity on spectrum size %1")
911  .arg(size()));
912  }
913 
914  return (*dataPoint);
915 }

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 919 of file trace.cpp.

920 {
921  return minYDataPoint().y;
922 }
Q_INVOKABLE const DataPoint & minYDataPoint() const
Definition: trace.cpp:881

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

◆ minYDataPoint()

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

Definition at line 881 of file trace.cpp.

882 {
883  auto dataPoint = std::min_element(
884  begin(), end(), [](const DataPoint &a, const DataPoint &b) {
885  return (a.y < b.y);
886  });
887 
888  if(dataPoint == end())
889  {
890  throw ExceptionOutOfRange(
891  QObject::tr("unable to get min peak intensity on spectrum size %1")
892  .arg(size()));
893  }
894 
895  return (*dataPoint);
896 }

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 619 of file trace.cpp.

620 {
621  assign(other.begin(), other.end());
622 
623  return *this;
624 }

◆ operator=() [2/2]

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

Definition at line 628 of file trace.cpp.

629 {
630  vector<DataPoint>::operator=(std::move(other));
631  return *this;
632 }

◆ sortX()

◆ sortY()

void pappso::Trace::sortY ( )

Definition at line 991 of file trace.cpp.

992 {
993  std::sort(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
994  return (a.y > b.y);
995  });
996 }

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 933 of file trace.cpp.

934 {
935  // double sum = 0;
936 
937  // for(auto &&dp : m_dataPoints)
938  // sum += dp.y;
939 
940  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()"
941  //<< "Returning sum/tic:" << sum;
942 
943  // return sum;
944 
945  return std::accumulate(begin(),
946  end(),
947  (double)0,
948  [](pappso_double sum, const DataPoint &dataPoint) {
949  return (sum + dataPoint.y);
950  });
951 }

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 955 of file trace.cpp.

956 {
957  auto begin_it = findFirstEqualOrGreaterX(this->begin(), this->end(), mzStart);
958  auto end_it = findFirstGreaterX(begin_it, this->end(), mzEnd);
959 
960  return sumYTrace(begin_it, end_it, 0);
961 }
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:242

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

◆ toMap()

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

Definition at line 678 of file trace.cpp.

679 {
680  std::map<pappso_double, pappso_double> map;
681 
682  std::pair<std::map<pappso_double, pappso_double>::iterator, bool> ret;
683 
684  for(auto &&dataPoint : *this)
685  {
686  ret = map.insert(
687  std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
688 
689  if(ret.second == false)
690  {
691  qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
692  << "It is odd that the Trace contains multiple same keys.";
693 
694  // No insertion, then increment the y value.
695  ret.first->second += dataPoint.y;
696  }
697  }
698 
699  return map;
700 }

◆ toString()

QString pappso::Trace::toString ( ) const

Definition at line 1011 of file trace.cpp.

1012 {
1013  // Even if the spectrum is empty, we should return an empty string.
1014  QString text;
1015 
1016  for(auto &&dataPoint : *this)
1017  {
1018  text.append(QString("%1 %2\n")
1019  .arg(dataPoint.x, 0, 'f', 10)
1020  .arg(dataPoint.y, 0, 'f', 10));
1021  }
1022 
1023  return text;
1024 }

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

◆ unique()

void pappso::Trace::unique ( )

Definition at line 999 of file trace.cpp.

1000 {
1001  auto last =
1002  std::unique(begin(), end(), [](const DataPoint &a, const DataPoint &b) {
1003  return (a.x == b.x);
1004  });
1005 
1006  erase(last, end());
1007 }

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 650 of file trace.cpp.

651 {
652  std::vector<pappso_double> values;
653 
654  for(auto &&dataPoint : *this)
655  {
656  values.push_back(dataPoint.x);
657  }
658 
659  return values;
660 }

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

◆ yValues()

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

Definition at line 664 of file trace.cpp.

665 {
666  std::vector<pappso_double> values;
667 
668  for(auto &&dataPoint : *this)
669  {
670  values.push_back(dataPoint.y);
671  }
672 
673  return values;
674 }

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

Friends And Related Function Documentation

◆ MassSpectrumCombinerInterface

friend class MassSpectrumCombinerInterface
friend

Definition at line 43 of file trace.h.

◆ TraceCombiner

friend class TraceCombiner
friend

Definition at line 39 of file trace.h.

◆ TraceMinusCombiner

friend class TraceMinusCombiner
friend

Definition at line 40 of file trace.h.

◆ TracePlusCombiner

friend class TracePlusCombiner
friend

Definition at line 41 of file trace.h.


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