15 #include "../processing/combiners/tracepluscombiner.h"
16 #include "../processing/combiners/traceminuscombiner.h"
18 #include "../pappsoexception.h"
19 #include "../exception/exceptionoutofrange.h"
20 #include "../exception/exceptionnotpossible.h"
21 #include "../processing/filters/filterresample.h"
22 #include "../processing/filters/filterpass.h"
35 for(
auto &dataPoint : trace)
53 QString(
"error in QDataStream unserialize operator>> of trace:\n"
54 "read datastream failed status=%1")
58 for(
auto &dataPoint : trace)
68 std::vector<DataPoint>::iterator
70 std::vector<DataPoint>::iterator end,
73 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
74 if(to_compare.
x < value)
82 std::vector<DataPoint>::const_iterator
84 std::vector<DataPoint>::const_iterator end,
87 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
88 if(to_compare.
x < value)
96 std::vector<DataPoint>::iterator
98 std::vector<DataPoint>::iterator end,
101 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
102 if(to_compare.
x > value)
110 std::vector<DataPoint>::const_iterator
112 std::vector<DataPoint>::const_iterator end,
115 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
116 if(to_compare.
x > value)
124 std::vector<DataPoint>::iterator
126 std::vector<DataPoint>::iterator end,
127 const double &y_value)
129 return std::find_if(begin, end, [y_value](
const DataPoint &to_compare) {
131 if(to_compare.
y != y_value)
140 std::vector<DataPoint>::const_iterator
142 std::vector<DataPoint>::const_iterator end,
143 const double &y_value)
145 return std::find_if(begin, end, [y_value](
const DataPoint &to_compare) {
147 if(to_compare.
y != y_value)
157 std::vector<DataPoint>::const_iterator
159 std::vector<DataPoint>::const_iterator end)
161 return std::min_element(
168 std::vector<DataPoint>::iterator
170 std::vector<DataPoint>::iterator end)
172 return std::min_element(
179 std::vector<DataPoint>::const_iterator
181 std::vector<DataPoint>::const_iterator end)
183 return std::max_element(
190 std::vector<DataPoint>::iterator
192 std::vector<DataPoint>::iterator end)
194 return std::max_element(
204 std::vector<DataPoint>::const_iterator
206 std::vector<DataPoint>::const_iterator begin)
208 if(begin == trace.end())
214 while((it != trace.end()) && (it->y <= result->y))
222 std::vector<DataPoint>::const_iterator
224 std::vector<DataPoint>::const_iterator begin)
226 if(begin == trace.begin())
234 while((it != trace.begin()) && (it->y <= result->y))
245 std::vector<DataPoint>::const_iterator end,
248 return std::accumulate(
249 begin, end, init, [](
double a,
const DataPoint &
b) {
return a +
b.y; });
254 std::vector<DataPoint>::const_iterator end)
259 QObject::tr(
"unable to compute mean on a trace of size 0"));
260 return (
sumYTrace(begin, end, 0) / nb_element);
266 std::vector<DataPoint>::const_iterator end,
269 std::size_t nb_element = distance(begin, end);
272 QObject::tr(
"unable to compute quantile on a trace of size 0"));
275 std::size_t ieth_element = std::round((
double)nb_element * quantile);
276 if(ieth_element > nb_element)
278 QObject::tr(
"quantile value must be lower than 1"));
281 std::vector<DataPoint> data(begin, end);
284 data.begin() + ieth_element,
287 return data[ieth_element].y;
292 std::vector<DataPoint>::const_iterator end)
294 std::size_t nb_element = distance(begin, end);
297 QObject::tr(
"unable to compute median on a trace of size 0"));
299 std::vector<DataPoint> data(begin, end);
302 data.begin() + data.size() / 2,
305 return data[data.size() / 2].y;
310 std::vector<DataPoint>::const_iterator end)
315 auto previous = begin;
316 auto next = begin + 1;
320 area += ((next->x - previous->x) * (previous->y + next->y)) / (
double)2;
330 std::vector<DataPoint>::const_iterator end,
333 Trace local_maxima_trace;
335 Trace single_peak_trace;
339 for(
auto iter = begin; iter != end; ++iter)
341 DataPoint iterated_data_point(iter->x, iter->y);
346 if(iterated_data_point.
y < y_floor)
350 if(single_peak_trace.size())
354 local_maxima_trace.push_back(single_peak_trace.
maxYDataPoint());
360 single_peak_trace.clear();
362 previous_data_point = iterated_data_point;
370 previous_data_point = iterated_data_point;
382 if(iterated_data_point.
y == previous_data_point.
y)
388 else if(iterated_data_point.
y > previous_data_point.
y)
397 single_peak_trace.push_back(iterated_data_point);
402 previous_data_point = iterated_data_point;
413 single_peak_trace.push_back(iterated_data_point);
418 previous_data_point = iterated_data_point;
430 if(single_peak_trace.size())
433 local_maxima_trace.push_back(single_peak_trace.
maxYDataPoint());
440 return local_maxima_trace;
450 const std::vector<pappso_double> &yVector)
457 const std::vector<std::pair<pappso_double, pappso_double>> &dataPoints)
459 reserve(dataPoints.size());
461 for(
auto &dataPoint : dataPoints)
484 : std::vector<
DataPoint>(std::move(dataPoints))
497 for(
auto &&item : map_trace)
498 push_back(
DataPoint(item.first, item.second));
509 : std::vector<
DataPoint>(std::move(other))
524 const std::vector<pappso_double> &yVector)
527 if(xVector.size() != yVector.size())
529 "trace.cpp -- ERROR xVector and yVector must have the same size.");
532 erase(begin(), end());
534 for(std::size_t iter = 0; iter < xVector.size(); ++iter)
536 push_back(
DataPoint(xVector.at(iter), yVector.at(iter)));
545 for(
auto &item : *
this)
547 std::cout << item.x <<
"-" << item.y;
560 erase(begin(), end());
562 for(
auto &&item : map)
564 push_back(
DataPoint(item.first, item.second));
585 push_back(data_point);
594 assign(other.begin(), other.end());
603 vector<DataPoint>::operator=(std::move(other));
611 return std::make_shared<Trace>(*
this);
618 return std::make_shared<const Trace>(*
this);
622 std::vector<pappso_double>
625 std::vector<pappso_double> values;
627 for(
auto &&dataPoint : *
this)
629 values.push_back(dataPoint.x);
636 std::vector<pappso_double>
639 std::vector<pappso_double> values;
641 for(
auto &&dataPoint : *
this)
643 values.push_back(dataPoint.y);
650 std::map<pappso_double, pappso_double>
653 std::map<pappso_double, pappso_double> map;
655 std::pair<std::map<pappso_double, pappso_double>::iterator,
bool> ret;
657 for(
auto &&dataPoint : *
this)
660 std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
662 if(ret.second ==
false)
664 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()"
665 <<
"It is odd that the Trace contains multiple same keys.";
668 ret.first->second += dataPoint.y;
697 std::vector<DataPoint>::iterator
701 std::find_if(begin(), end(), [value](
const DataPoint &dataPoint) {
702 return (dataPoint.
x == value);
709 std::vector<DataPoint>::const_iterator
713 std::find_if(begin(), end(), [value](
const DataPoint &dataPoint) {
714 return (dataPoint.
x == value);
724 std::vector<DataPoint>::const_iterator iterator =
727 if(iterator != end())
728 return std::distance(begin(), iterator);
730 return std::numeric_limits<std::size_t>::max();
742 double left_most = value - delta;
743 double right_most = value + delta;
750 std::find_if(begin(),
752 [value, precision_p, delta, left_most, right_most](
769 double diff_to_left_most = data_point.
x - left_most;
770 double diff_to_right_most = data_point.
x - right_most;
818 if(diff_to_left_most >= 0 && diff_to_right_most <= 0)
836 return (data_point.
x == value);
840 if(iterator != end())
856 auto dataPoint = std::min_element(
861 if(dataPoint == end())
864 QObject::tr(
"unable to get min peak intensity on spectrum size %1")
875 auto dataPoint = std::max_element(
880 if(dataPoint == end())
883 QObject::tr(
"unable to get max peak intensity on spectrum size %1")
918 return std::accumulate(begin(),
922 return (
sum + dataPoint.
y);
940 std::vector<DataPoint>::const_iterator begin_it =
947 if(begin_it->y > max_y)
989 for(
auto &&dataPoint : *
this)
991 text.append(QString(
"%1 %2\n")
992 .arg(dataPoint.x, 0,
'f', 10)
993 .arg(dataPoint.y, 0,
'f', 10));
generic interface to apply a filter on a trace
virtual pappso_double delta(pappso_double value) const =0
A simple container of DataPoint instances.
virtual Trace & operator=(const Trace &x)
pappso_double maxY() const
pappso_double sumY() const
const DataPoint & maxYDataPoint() const
std::map< pappso_double, pappso_double > toMap() const
std::vector< pappso_double > xValues() const
TraceCstSPtr makeTraceCstSPtr() const
virtual Trace & filter(const FilterInterface &filter) final
apply a filter on this trace
DataPoint containsX(pappso_double value, PrecisionPtr precision_p=nullptr) const
std::vector< pappso_double > yValues() const
pappso_double minY() const
size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
size_t append(const DataPoint &data_point)
appends a datapoint and return new size
std::size_t dataPointIndexWithX(pappso_double value) const
std::vector< DataPoint >::const_iterator dataPointCstIteratorWithX(pappso_double value) const
find datapoint with exactly x value
std::vector< DataPoint >::iterator dataPointIteratorWithX(pappso_double value)
const DataPoint & minYDataPoint() const
TraceSPtr makeTraceSPtr() const
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
std::shared_ptr< const Trace > TraceCstSPtr
std::vector< DataPoint >::iterator findDifferentYvalue(std::vector< DataPoint >::iterator begin, std::vector< DataPoint >::iterator end, const double &y_value)
find the first element in which Y is different of value
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 ...
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...
QDataStream & operator<<(QDataStream &outstream, const MassSpectrum &massSpectrum)
QDataStream & operator>>(QDataStream &instream, MassSpectrum &massSpectrum)
std::vector< DataPoint >::const_iterator moveLowerYLeftDataPoint(const Trace &trace, std::vector< DataPoint >::const_iterator begin)
Move left to the lower value.
std::vector< DataPoint >::const_iterator maxYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
double medianYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
calculate the median of y value of a trace
double areaTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
calculate the area of a trace
std::shared_ptr< Trace > TraceSPtr
double pappso_double
A type definition for doubles.
double meanYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
calculate the mean of y value of a trace
std::vector< DataPoint >::const_iterator moveLowerYRigthDataPoint(const Trace &trace, std::vector< DataPoint >::const_iterator begin)
Move right to the lower value.
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
std::vector< DataPoint >::const_iterator minYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
double quantileYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end, double quantile)
calculate the quantile of y value of a trace
Trace flooredLocalMaxima(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end, double y_floor)