32 for(
auto &dataPoint : trace)
49 throw PappsoException(QString(
"error in QDataStream unserialize operator>> of trace:\n"
50 "read datastream failed status=%1")
54 for(
auto &dataPoint : trace)
64std::vector<DataPoint>::iterator
66 std::vector<DataPoint>::iterator end,
69 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
70 if(to_compare.
x < value)
78std::vector<DataPoint>::const_iterator
80 std::vector<DataPoint>::const_iterator end,
83 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
84 if(to_compare.
x < value)
92std::vector<DataPoint>::iterator
94 std::vector<DataPoint>::iterator end,
97 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
98 if(to_compare.
x > value)
106std::vector<DataPoint>::const_iterator
108 std::vector<DataPoint>::const_iterator end,
111 return std::find_if(begin, end, [value](
const DataPoint &to_compare) {
112 if(to_compare.
x > value)
120std::vector<DataPoint>::iterator
122 std::vector<DataPoint>::iterator end,
123 const double &y_value)
125 return std::find_if(begin, end, [y_value](
const DataPoint &to_compare) {
126 if(to_compare.
y != y_value)
135std::vector<DataPoint>::const_iterator
137 std::vector<DataPoint>::const_iterator end,
138 const double &y_value)
140 return std::find_if(begin, end, [y_value](
const DataPoint &to_compare) {
141 if(to_compare.
y != y_value)
151std::vector<DataPoint>::const_iterator
153 std::vector<DataPoint>::const_iterator end)
155 return std::min_element(
160std::vector<DataPoint>::iterator
161minYDataPoint(std::vector<DataPoint>::iterator begin, std::vector<DataPoint>::iterator end)
163 return std::min_element(
168std::vector<DataPoint>::const_iterator
170 std::vector<DataPoint>::const_iterator end)
172 return std::max_element(
177std::vector<DataPoint>::iterator
178maxYDataPoint(std::vector<DataPoint>::iterator begin, std::vector<DataPoint>::iterator end)
180 return std::max_element(
188std::vector<DataPoint>::const_iterator
191 if(begin == trace.end())
197 while((it != trace.end()) && (it->y <= result->y))
205std::vector<DataPoint>::const_iterator
208 if(begin == trace.begin())
216 while((it != trace.begin()) && (it->y <= result->y))
227 std::vector<DataPoint>::const_iterator end,
230 return std::accumulate(begin, end, init, [](
double a,
const DataPoint &
b) {
return a +
b.y; });
234meanYTrace(std::vector<DataPoint>::const_iterator begin, std::vector<DataPoint>::const_iterator end)
239 return (
sumYTrace(begin, end, 0) / nb_element);
245 std::vector<DataPoint>::const_iterator end,
248 std::size_t nb_element = distance(begin, end);
253 std::size_t ieth_element = std::round((
double)nb_element * quantile);
254 if(ieth_element > nb_element)
258 std::vector<DataPoint> data(begin, end);
259 std::nth_element(data.begin(),
260 data.begin() + ieth_element,
263 return data[ieth_element].y;
268 std::vector<DataPoint>::const_iterator end)
270 std::size_t nb_element = distance(begin, end);
274 std::vector<DataPoint> data(begin, end);
275 std::nth_element(data.begin(),
276 data.begin() + data.size() / 2,
279 return data[data.size() / 2].y;
283areaTrace(std::vector<DataPoint>::const_iterator begin, std::vector<DataPoint>::const_iterator end)
288 auto previous = begin;
289 auto next = begin + 1;
293 area += ((next->x - previous->x) * (previous->y + next->y)) / (
double)2;
303 std::vector<DataPoint>::const_iterator end,
306 Trace local_maxima_trace;
308 Trace single_peak_trace;
312 for(
auto iter = begin; iter != end; ++iter)
314 DataPoint iterated_data_point(iter->x, iter->y);
319 if(iterated_data_point.
y < y_floor)
323 if(single_peak_trace.size())
327 local_maxima_trace.push_back(single_peak_trace.
maxYDataPoint());
333 single_peak_trace.clear();
335 previous_data_point = iterated_data_point;
343 previous_data_point = iterated_data_point;
355 if(iterated_data_point.
y == previous_data_point.
y)
361 else if(iterated_data_point.
y > previous_data_point.
y)
370 single_peak_trace.push_back(iterated_data_point);
375 previous_data_point = iterated_data_point;
386 single_peak_trace.push_back(iterated_data_point);
391 previous_data_point = iterated_data_point;
403 if(single_peak_trace.size())
406 local_maxima_trace.push_back(single_peak_trace.
maxYDataPoint());
413 return local_maxima_trace;
419 std::vector<DataPoint>::const_iterator end,
423 trace.reserve(std::distance(begin, end) + 1);
425 std::vector<DataPoint>::const_iterator the_iterator = begin;
427 while(the_iterator != end)
429 trace.
append({the_iterator->x + increment, the_iterator->y});
438 std::vector<DataPoint>::const_iterator end,
442 trace.reserve(std::distance(begin, end) + 1);
444 std::vector<DataPoint>::const_iterator the_iterator = begin;
446 while(the_iterator != end)
448 trace.
append({the_iterator->x, the_iterator->y + increment});
466Trace::Trace(
const std::vector<pappso_double> &xVector,
const std::vector<pappso_double> &yVector)
475 QStringList string_list = space_sep_text.split(
"\n", Qt::SkipEmptyParts);
480 for(
int iter = 0; iter < string_list.size(); ++iter)
482 QString
line = string_list.at(iter);
497 qDebug() <<
"Now appending DataPoint as this line:" <<
line;
508 QStringList x_string_list = x_text.split(
"\n", Qt::SkipEmptyParts);
509 QStringList y_string_list = y_text.split(
"\n", Qt::SkipEmptyParts);
511 if(x_string_list.size() != y_string_list.size())
513 "trace.cpp -- ERROR x_text and y_text must have the same number of "
516 for(
int iter = 0; iter < x_string_list.size(); ++iter)
518 QString x_line = x_string_list.at(iter);
519 QString y_line = y_string_list.at(iter);
524 x_line = x_line.simplified();
525 y_line = y_line.simplified();
533Trace::Trace(
const std::vector<std::pair<pappso_double, pappso_double>> &dataPoints)
535 reserve(dataPoints.size());
537 for(
auto &dataPoint : dataPoints)
559 : std::vector<
DataPoint>(std::move(dataPoints))
572 for(
auto &&item : map_trace)
573 push_back(
DataPoint(item.first, item.second));
598 const std::vector<pappso_double> &yVector)
601 if(xVector.size() != yVector.size())
605 erase(begin(), end());
607 for(std::size_t iter = 0; iter < xVector.size(); ++iter)
609 push_back(
DataPoint(xVector.at(iter), yVector.at(iter)));
618 for(
auto &item : *
this)
620 std::cout << item.x <<
"-" << item.y;
633 erase(begin(), end());
635 for(
auto &&item : map)
637 push_back(
DataPoint(item.first, item.second));
659 push_back(data_point);
668 assign(other.begin(), other.end());
677 vector<DataPoint>::operator=(std::move(other));
685 return std::make_shared<Trace>(*
this);
692 return std::make_shared<const Trace>(*
this);
696std::vector<pappso_double>
699 std::vector<pappso_double> values;
701 for(
auto &&dataPoint : *
this)
703 values.push_back(dataPoint.x);
710std::vector<pappso_double>
713 std::vector<pappso_double> values;
715 for(
auto &&dataPoint : *
this)
717 values.push_back(dataPoint.y);
724std::map<pappso_double, pappso_double>
727 std::map<pappso_double, pappso_double> map;
729 std::pair<std::map<pappso_double, pappso_double>::iterator,
bool> ret;
731 for(
auto &&dataPoint : *
this)
733 ret = map.insert(std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
735 if(ret.second ==
false)
737 qDebug() <<
"It is odd that the Trace contains multiple same keys.";
740 ret.first->second += dataPoint.y;
769std::vector<DataPoint>::iterator
772 auto iterator = std::find_if(
773 begin(), end(), [value](
const DataPoint &dataPoint) {
return (dataPoint.
x == value); });
779std::vector<DataPoint>::const_iterator
782 auto iterator = std::find_if(
783 begin(), end(), [value](
const DataPoint &dataPoint) {
return (dataPoint.
x == value); });
794 if(iterator != end())
795 return std::distance(begin(), iterator);
797 return std::numeric_limits<std::size_t>::max();
809 double left_most = value - delta;
810 double right_most = value + delta;
816 auto iterator = std::find_if(
817 begin(), end(), [value, precision_p, left_most, right_most](
const DataPoint &data_point) {
833 double diff_to_left_most = data_point.
x - left_most;
834 double diff_to_right_most = data_point.
x - right_most;
882 if(diff_to_left_most >= 0 && diff_to_right_most <= 0)
900 return (data_point.
x == value);
904 if(iterator != end())
920 auto dataPoint = std::min_element(
923 if(dataPoint == end())
926 QObject::tr(
"unable to get min peak x on spectrum size %1").arg(size()));
936 auto dataPoint = std::max_element(
939 if(dataPoint == end())
942 QObject::tr(
"unable to get max peak x on spectrum size %1").arg(size()));
952 auto dataPoint = std::min_element(
955 if(dataPoint == end())
958 QObject::tr(
"unable to get min peak intensity on spectrum size %1").arg(size()));
968 auto dataPoint = std::max_element(
971 if(dataPoint == end())
974 QObject::tr(
"unable to get max peak intensity on spectrum size %1").arg(size()));
1022 return std::accumulate(
1024 return (sum + dataPoint.
y);
1042 std::vector<DataPoint>::const_iterator begin_it =
1049 if(begin_it->y > max_y)
1050 max_y = begin_it->y;
1064 return sortX(sort_order);
1066 return sortY(sort_order);
1093 auto last = std::unique(
1106 auto end_it = end();
1108 std::size_t count = 0;
1133 for(
auto &&dataPoint : *
this)
1135 text.append(QString(
"%1\n").arg(dataPoint.toString()));
1145 QByteArray unencoded_array;
1147 for(
auto &&data_point : *
this)
1149 QByteArray local_array;
1150 local_array.setNum(data_point.x,
'f', 12);
1151 local_array.append(
"\n");
1153 unencoded_array.append(local_array);
1156 QByteArray base64_encoded_array =
1157 unencoded_array.toBase64(QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals);
1159 return base64_encoded_array;
1166 QByteArray unencoded_array;
1168 for(
auto &&data_point : *
this)
1170 QByteArray local_array;
1171 local_array.setNum(data_point.y,
'f', 12);
1172 local_array.append(
"\n");
1174 unencoded_array.append(local_array);
1177 QByteArray base64_encoded_array =
1178 unencoded_array.toBase64(QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals);
1180 return base64_encoded_array;
1187 return filter.filter(*
this);
1193 QJsonObject json_trace;
1197 for(
auto &&data_point : *
this)
1199 x_arr.push_back(data_point.x);
1200 y_arr.push_back(data_point.y);
1202 json_trace.insert(x_label, x_arr);
1203 json_trace.insert(y_label, y_arr);
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
const DataPoint & maxXDataPoint() const
pappso_double minX() const
QByteArray yAsBase64Encoded() const
pappso_double maxX() const
void sort(Enums::SortType sort_type, Enums::SortOrder sort_order=Enums::SortOrder::ascending)
QByteArray xAsBase64Encoded() const
void sortY(Enums::SortOrder sort_order=Enums::SortOrder::ascending)
TraceCstSPtr makeTraceCstSPtr() const
virtual Trace & filter(const FilterInterface &filter) final
apply a filter on this trace
QJsonObject toJsonObject(const QString &x_label="x", const QString &y_label="y") const
serialize a PAPPSO trace object to JSON
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)
void sortX(Enums::SortOrder sort_order=Enums::SortOrder::ascending)
size_t append(const DataPoint &data_point)
appends a datapoint and return new size
std::size_t dataPointIndexWithX(pappso_double value) const
Return a reference to the DataPoint instance that has its x member equal to value.
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
std::size_t removeZeroYDataPoints()
static bool almostEqual(double value1, double value2, int decimalPlaces=10)
Tell if both double values, are equal within the double representation capabilities of the platform.
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)
Trace incrementByValueX(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end, double increment)
double medianYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
calculate the median of y value of a trace
Trace incrementByValueY(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end, double increment)
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
const PrecisionBase * PrecisionPtr
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)