17#include "../processing/combiners/tracepluscombiner.h"
18#include "../processing/combiners/traceminuscombiner.h"
20#include "../pappsoexception.h"
21#include "../exception/exceptionoutofrange.h"
22#include "../exception/exceptionnotpossible.h"
23#include "../processing/filters/filterresample.h"
24#include "../processing/filters/filterpass.h"
37 for(
auto &dataPoint : trace)
55 QString(
"error in QDataStream unserialize operator>> of trace:\n"
56 "read datastream failed status=%1")
60 for(
auto &dataPoint : trace)
70std::vector<DataPoint>::iterator
72 std::vector<DataPoint>::iterator end,
75 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
76 if(to_compare.
x < value)
84std::vector<DataPoint>::const_iterator
86 std::vector<DataPoint>::const_iterator end,
89 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
90 if(to_compare.
x < value)
98std::vector<DataPoint>::iterator
100 std::vector<DataPoint>::iterator end,
103 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
104 if(to_compare.
x > value)
112std::vector<DataPoint>::const_iterator
114 std::vector<DataPoint>::const_iterator end,
117 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
118 if(to_compare.
x > value)
126std::vector<DataPoint>::iterator
128 std::vector<DataPoint>::iterator end,
129 const double &y_value)
131 return std::find_if(begin, end, [y_value](
const DataPoint &to_compare) {
132 if(to_compare.
y != y_value)
141std::vector<DataPoint>::const_iterator
143 std::vector<DataPoint>::const_iterator end,
144 const double &y_value)
146 return std::find_if(begin, end, [y_value](
const DataPoint &to_compare) {
147 if(to_compare.
y != y_value)
157std::vector<DataPoint>::const_iterator
159 std::vector<DataPoint>::const_iterator end)
161 return std::min_element(
168std::vector<DataPoint>::iterator
170 std::vector<DataPoint>::iterator end)
172 return std::min_element(
179std::vector<DataPoint>::const_iterator
181 std::vector<DataPoint>::const_iterator end)
183 return std::max_element(
190std::vector<DataPoint>::iterator
192 std::vector<DataPoint>::iterator end)
194 return std::max_element(
204std::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))
222std::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;
456 const std::vector<pappso_double> &yVector)
465 QStringList string_list = space_sep_text.split(
"\n", Qt::SkipEmptyParts);
470 for(
int iter = 0; iter < string_list.size(); ++iter)
472 QString
line = string_list.at(iter);
497 QStringList x_string_list = x_text.split(
"\n", Qt::SkipEmptyParts);
498 QStringList y_string_list = y_text.split(
"\n", Qt::SkipEmptyParts);
500 if(x_string_list.size() != y_string_list.size())
502 "trace.cpp -- ERROR x_text and y_text must have the same number of "
505 for(
int iter = 0; iter < x_string_list.size(); ++iter)
507 QString x_line = x_string_list.at(iter);
508 QString y_line = y_string_list.at(iter);
513 x_line = x_line.simplified();
514 y_line = y_line.simplified();
523 const std::vector<std::pair<pappso_double, pappso_double>> &dataPoints)
525 reserve(dataPoints.size());
527 for(
auto &dataPoint : dataPoints)
550 : std::vector<
DataPoint>(std::move(dataPoints))
563 for(
auto &&item : map_trace)
564 push_back(
DataPoint(item.first, item.second));
589 const std::vector<pappso_double> &yVector)
592 if(xVector.size() != yVector.size())
594 "trace.cpp -- ERROR xVector and yVector must have the same size.");
597 erase(begin(), end());
599 for(std::size_t iter = 0; iter < xVector.size(); ++iter)
601 push_back(
DataPoint(xVector.at(iter), yVector.at(iter)));
610 for(
auto &item : *
this)
612 std::cout << item.x <<
"-" << item.y;
625 erase(begin(), end());
627 for(
auto &&item : map)
629 push_back(
DataPoint(item.first, item.second));
650 push_back(data_point);
659 assign(other.begin(), other.end());
668 vector<DataPoint>::operator=(std::move(other));
676 return std::make_shared<Trace>(*
this);
683 return std::make_shared<const Trace>(*
this);
687std::vector<pappso_double>
690 std::vector<pappso_double> values;
692 for(
auto &&dataPoint : *
this)
694 values.push_back(dataPoint.x);
701std::vector<pappso_double>
704 std::vector<pappso_double> values;
706 for(
auto &&dataPoint : *
this)
708 values.push_back(dataPoint.y);
715std::map<pappso_double, pappso_double>
718 std::map<pappso_double, pappso_double> map;
720 std::pair<std::map<pappso_double, pappso_double>::iterator,
bool> ret;
722 for(
auto &&dataPoint : *
this)
725 std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
727 if(ret.second ==
false)
729 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()"
730 <<
"It is odd that the Trace contains multiple same keys.";
733 ret.first->second += dataPoint.y;
762std::vector<DataPoint>::iterator
766 std::find_if(begin(), end(), [value](
const DataPoint &dataPoint) {
767 return (dataPoint.
x == value);
774std::vector<DataPoint>::const_iterator
778 std::find_if(begin(), end(), [value](
const DataPoint &dataPoint) {
779 return (dataPoint.
x == value);
789 std::vector<DataPoint>::const_iterator iterator =
792 if(iterator != end())
793 return std::distance(begin(), iterator);
795 return std::numeric_limits<std::size_t>::max();
807 double left_most = value - delta;
808 double right_most = value + delta;
815 std::find_if(begin(),
817 [value, precision_p, delta, left_most, right_most](
834 double diff_to_left_most = data_point.
x - left_most;
835 double diff_to_right_most = data_point.
x - right_most;
883 if(diff_to_left_most >= 0 && diff_to_right_most <= 0)
901 return (data_point.
x == value);
905 if(iterator != end())
921 auto dataPoint = std::min_element(
926 if(dataPoint == end())
929 QObject::tr(
"unable to get min peak x on spectrum size %1")
940 auto dataPoint = std::max_element(
945 if(dataPoint == end())
948 QObject::tr(
"unable to get max peak x on spectrum size %1")
959 auto dataPoint = std::min_element(
964 if(dataPoint == end())
967 QObject::tr(
"unable to get min peak intensity on spectrum size %1")
978 auto dataPoint = std::max_element(
983 if(dataPoint == end())
986 QObject::tr(
"unable to get max peak intensity on spectrum size %1")
1035 return std::accumulate(begin(),
1039 return (
sum + dataPoint.
y);
1057 std::vector<DataPoint>::const_iterator begin_it =
1064 if(begin_it->y > max_y)
1065 max_y = begin_it->y;
1079 return sortX(sort_order);
1081 return sortY(sort_order);
1118 return (
a.x ==
b.x);
1131 auto end_it = end();
1133 std::size_t count = 0;
1158 for(
auto &&dataPoint : *
this)
1160 text.append(QString(
"%1\n").arg(dataPoint.toString()));
1170 QByteArray unencoded_array;
1172 for(
auto &&data_point : *
this)
1174 QByteArray local_array;
1175 local_array.setNum(data_point.x,
'f', 12);
1176 local_array.append(
"\n");
1178 unencoded_array.append(local_array);
1181 QByteArray base64_encoded_array = unencoded_array.toBase64(
1182 QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals);
1184 return base64_encoded_array;
1191 QByteArray unencoded_array;
1193 for(
auto &&data_point : *
this)
1195 QByteArray local_array;
1196 local_array.setNum(data_point.y,
'f', 12);
1197 local_array.append(
"\n");
1199 unencoded_array.append(local_array);
1202 QByteArray base64_encoded_array = unencoded_array.toBase64(
1203 QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals);
1205 return base64_encoded_array;
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
void sortX(SortOrder sort_order=SortOrder::ascending)
pappso_double sumY() const
const DataPoint & maxYDataPoint() const
std::map< pappso_double, pappso_double > toMap() const
std::vector< pappso_double > xValues() const
const DataPoint & maxXDataPoint() const
pappso_double minX() const
QByteArray yAsBase64Encoded() const
pappso_double maxX() const
QByteArray xAsBase64Encoded() const
void sortY(SortOrder sort_order=SortOrder::ascending)
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
const DataPoint & minXDataPoint() 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
void sort(SortType sort_type, SortOrder sort_order=SortOrder::ascending)
std::size_t removeZeroYDataPoints()
static bool almostEqual(double value1, double value2, int decimalPlaces=10)
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)