70 std::map<MzIntegrationParams::MzIntegrationParams::BinningType,
71 QString>::const_iterator the_iterator_const =
75 [text](
const std::pair<MzIntegrationParams::BinningType, QString> &pair) {
76 return pair.second == text;
80 return the_iterator_const->first;
100 int bin_size_divisor,
102 bool remove_zero_val_data_points,
136 return mz_integration_params_p;
147 return initialization_result;
158 bool binning_type_set =
false;
159 bool decimal_places_set =
false;
160 bool bin_size_model_set =
false;
161 bool bin_size_divisor_set =
false;
162 bool remove_zero_data_points_set =
false;
164 QStringList string_list = text.split(
"\n");
166 for(
int iter = 0; iter < string_list.size(); ++iter)
168 QString iter_string = string_list.at(iter);
172 if(iter_string.contains(
"Binning type:"))
176 binning_type_set =
true;
178 else if(iter_string.contains(
"Bin size model:"))
182 bin_size_model_set =
true;
184 else if(iter_string.contains(
"Bin size divisor:"))
187 bin_size_divisor_set =
true;
189 else if(iter_string.contains(
"Decimal places:"))
192 decimal_places_set =
true;
194 else if(iter_string.contains(
"Remove 0-val data points:"))
197 remove_zero_data_points_set =
true;
207 if(bin_size_model_set)
209 if(bin_size_divisor_set)
211 if(decimal_places_set)
214 if(remove_zero_data_points_set)
217 return initialization_result;
225 int bin_size_divisor,
227 bool remove_zero_val_data_points,
261 Q_ASSERT(other_p !=
nullptr);
272 if(
static_cast<bool>(
273 initialization_results &
277 if(
static_cast<bool>(
278 initialization_results &
282 if(
static_cast<bool>(
283 initialization_results &
287 if(
static_cast<bool>(
288 initialization_results &
519 errors += (
m_smallestMz == std::numeric_limits<double>::max() ? 1 : 0);
524 errors += (
m_greatestMz == std::numeric_limits<double>::min() ? 1 : 0);
529 qCritical() <<
"The m/z integration parameters are invalid.";
538 return (
m_smallestMz < std::numeric_limits<double>::max()) &&
547 std::vector<double> bins;
561 qFatal() <<
"Programming error. "
562 "Please use the createBins(pappso::MassSpectrumCstSPtr "
563 "mass_spectrum_csp) overload.";
574 std::vector<double> bins;
605 qFatal() <<
"Programming error. The MzIntegrationParams::BinningLogic is "
649 double first_mz = ceil((min_mz * std::pow(10,
m_decimalPlaces)) - 0.49) /
661 std::vector<double> bins;
662 double previous_mz_bin;
672 double lower_anchor_mz =
681 if(lower_anchor_mz <= first_mz)
692 first_mz = lower_anchor_mz;
707 QList<double> prepended_bins;
709 previous_mz_bin = lower_anchor_mz;
711 while(previous_mz_bin > first_mz)
720 double current_rounded_mz =
735 if(current_rounded_mz == previous_mz_bin)
752 prepended_bins.prepend(current_rounded_mz);
755 previous_mz_bin = current_rounded_mz;
761 bins.assign(prepended_bins.begin(), prepended_bins.end());
768 "/tmp/prependedMassSpecBins.txt-at-" +
769 QDateTime::currentDateTime().toString(
"yyyyMMdd-HH-mm-ss");
794 double upper_anchor_mz =
801 if(upper_anchor_mz > last_mz)
807 last_mz = upper_anchor_mz;
820 previous_mz_bin = first_mz;
822 bins.push_back(previous_mz_bin);
831 while(previous_mz_bin <= last_mz)
848 current_mz = previous_mz_bin +
859 double current_rounded_mz =
873 if(current_rounded_mz == previous_mz_bin)
886 bins.push_back(current_rounded_mz);
889 previous_mz_bin = current_rounded_mz;
894 QString bins_with_delta = binsToStringWithDelta(bins);
896 QString file_name =
"/tmp/massSpecArbitraryBins.txt-at-" +
897 QDateTime::currentDateTime().toString(
"yyyyMMdd-HH-mm-ss");
899 qDebug() <<
"Writing the list of bins setup in the mass spectrum in file " << file_name;
925 QList<double> deltas;
926 QList<double> resolutions;
928 double left_mz_value = 0;
929 double right_mz_value = 0;
931 double resolution = 0;
934 if(mass_spectrum_csp->size() < 3)
936 std::vector<double> bins_vector(bins.constBegin(), bins.constEnd());
944 mass_spectrum_copy.
sortMz();
946 std::vector<pappso::DataPoint>::const_iterator iterator_const =
947 mass_spectrum_copy.cbegin();
950 left_mz_value = iterator_const->x;
952 left_mz_value = ceil((left_mz_value * pow(10,
m_decimalPlaces)) - 0.49) /
955 qDebug() << qSetRealNumberPrecision(6)
956 <<
"left_mz_value in the template mass spectrum:" << left_mz_value;
958 bins.append(left_mz_value);
963 resolutions.append(0.0);
967 while(iterator_const != mass_spectrum_copy.cend())
969 right_mz_value = iterator_const->x;
971 qDebug() << qSetRealNumberPrecision(6)
972 <<
"right_mz_value:" << right_mz_value;
979 bins.append(right_mz_value);
981 mz_delta = right_mz_value - left_mz_value;
982 deltas.append(mz_delta);
983 Q_ASSERT(mz_delta != 0.0);
985 resolution = right_mz_value / mz_delta;
986 resolutions.append(resolution);
988 left_mz_value = right_mz_value;
998 std::vector<double> bins_vector(bins.constBegin(), bins.constEnd());
1003 "/tmp/massSpecDataBasedTemplateBinsWithDeltas.txt-at-" +
1004 QDateTime::currentDateTime().toString(
"yyyyMMdd-HH-mm-ss");
1035 bool uniform_deltas =
false;
1036 std::size_t deltas_count = deltas.size();
1038 double first_delta = deltas.at(1);
1039 double middle_delta = deltas.at(deltas_count / 2);
1040 double last_delta = deltas.at(deltas_count - 1);
1042 if(first_delta == middle_delta && middle_delta == last_delta)
1043 uniform_deltas =
true;
1051 bool uniform_resolutions =
false;
1052 std::size_t resolutions_count = resolutions.size();
1054 double first_resolution = resolutions.at(01);
1055 double middle_resolution = resolutions.at(resolutions_count / 2);
1056 double last_resolution = resolutions.at(resolutions_count - 1);
1058 if(first_resolution == middle_resolution &&
1059 middle_resolution == last_resolution)
1060 uniform_resolutions =
true;
1087 double first_bin_mz = bins.first();
1088 double last_bin_mz = bins.last();
1108 if(smallest_mz < first_bin_mz)
1119 double bin_mz = first_bin_mz;
1125 while(bin_mz > smallest_mz)
1127 double new_bin_mz = bin_mz - first_delta;
1128 bins.prepend(new_bin_mz);
1129 bin_mz = new_bin_mz;
1132 else if(uniform_resolutions)
1136 while(bin_mz > smallest_mz)
1138 double new_bin_mz = bin_mz - (bin_mz / first_resolution);
1139 bins.prepend(new_bin_mz);
1140 bin_mz = new_bin_mz;
1149 if(greatest_mz > last_bin_mz)
1157 double bin_mz = last_bin_mz;
1163 while(bin_mz < greatest_mz)
1165 double new_bin_mz = bin_mz + first_delta;
1166 bins.append(new_bin_mz);
1167 bin_mz = new_bin_mz;
1170 else if(uniform_resolutions)
1174 while(bin_mz > smallest_mz)
1176 double new_bin_mz = bin_mz + (bin_mz / first_resolution);
1177 bins.append(new_bin_mz);
1178 bin_mz = new_bin_mz;
1188 std::vector<double> full_bins_vector(bins.constBegin(), bins.constEnd());
1194 file_name =
"/tmp/massSpecDataBasedFullBinsWithDeltas.txt-at-" +
1195 QDateTime::currentDateTime().toString(
"yyyyMMdd-HH-mm-ss");
1197 qDebug() <<
"Writing the list of bins setup in the mass spectrum in file "
1208 return full_bins_vector;
1219 std::vector<double> bins;
1221 if(mass_spectrum_csp->size() < 2)
1228 sorted_mass_spectrum.
sortMz();
1242 double start_mz_in = min_mz;
1248 std::vector<pappso::DataPoint>::const_iterator it =
1249 sorted_mass_spectrum.begin();
1251 double prev_mz = it->x;
1259 while(it != sorted_mass_spectrum.end())
1261 double next_mz = it->x;
1267 double step = next_mz - prev_mz;
1268 end_mz_out = start_mz_in + step;
1279 bins.push_back(start_mz_in);
1282 start_mz_in = end_mz_out;
1293 QString fileName =
"/tmp/massSpecDataBasedBins.txt";
1295 qDebug() <<
"Writing the list of bins setup in the "
1296 "mass spectrum in file "
1299 QFile file(fileName);
1300 file.open(QIODevice::WriteOnly);
1302 QTextStream fileStream(&file);
1304 for(
auto &&bin : m_bins)
1305 fileStream << QString(
"[%1-%2]\n")
1306 .arg(bin.startMzIn, 0,
'f', 10)
1307 .arg(bin.endMzOut, 0,
'f', 10);
1312 qDebug() <<
"elements."
1313 <<
"starting with mz" << m_bins.front().startMzIn <<
"ending with mz"
1314 << m_bins.back().endMzOut;
1328 QString offset_lead;
1330 for(
int iter = 0; iter < offset; ++iter)
1331 offset_lead += spacer;
1333 QString text = offset_lead;
1334 text +=
"m/z integration parameters:\n";
1336 QString new_lead = QString(
"%1%2").arg(offset_lead, spacer);
1341 QString::asprintf(
"Smallest (first) m/z: %.6f\n",
m_smallestMz));
1345 text.append(QString::asprintf(
"Greatest (last) m/z: %.6f\n",
m_greatestMz));
1349 text.append(QString::asprintf(
"Lower anchor m/z: %.6f\n",
m_lowerAnchorMz));
1353 text.append(QString::asprintf(
"Upper anchor m/z: %.6f\n",
m_upperAnchorMz));
1356 text += QString(
"Remove 0-val data points: %1\n")
1360 text.append(
"Binning logic:\n");
1369 text.append(QString(
"Bin size model: %1\n").arg(
m_binSizeModel->toString()));
1389 text.append(QString(
"Bin size model: %1\n").arg(
m_binSizeModel->toString()));
1402 const std::vector<double> bins)
const
1404 QString bins_with_delta;
1405 double previous_bin_value = 0;
1407 for(
auto &&value : bins)
1409 double delta = value - previous_bin_value;
1410 bins_with_delta += QString(
"%1 - %2\n")
1413 previous_bin_value = value;
1416 return bins_with_delta;
1424 if(engine ==
nullptr)
1426 qFatal() <<
"Cannot register class: engine is null";
1429 QJSValue pappso_root_property;
1430 QJSValue pappso_enums_property;
1432 if(engine->globalObject().hasProperty(
"pappso"))
1434 qDebug() <<
"Global object property 'pappso' already exists.";
1435 pappso_root_property = engine->globalObject().property(
"pappso");
1437 if(pappso_root_property.hasProperty(
"Enums"))
1439 pappso_enums_property = pappso_root_property.property(
"Enums");
1443 pappso_enums_property = engine->newObject();
1444 pappso_root_property.setProperty(
"Enums", pappso_enums_property);
1449 qDebug() <<
"Global object property 'pappso' not found.";
1450 pappso_root_property = engine->newObject();
1451 pappso_enums_property = engine->newObject();
1452 pappso_root_property.setProperty(
"Enums", pappso_enums_property);
1453 engine->globalObject().setProperty(
"pappso", pappso_root_property);
1456 QJSValue enumObject;
1459 enumObject = engine->newObject();
1461 enumObject.setProperty(
1463 enumObject.setProperty(
1466 enumObject.setProperty(
1470 pappso_enums_property.setProperty(
"BinningType", enumObject);
1473 enumObject = engine->newObject();
1474 enumObject.setProperty(
1478 enumObject.setProperty(
1479 "fromSettingsBinSizeModelPartial",
1482 enumObject.setProperty(
1483 "fromSettingsBinSizeModelFull",
1486 enumObject.setProperty(
1490 pappso_enums_property.setProperty(
"InitializationResult", enumObject);
1493 QJSValue jsMetaObject =
1494 engine->newQMetaObject(&MzIntegrationParams::staticMetaObject);
1495 engine->globalObject().setProperty(
"MzIntegrationParams", jsMetaObject);
Class to represent a mass spectrum.
void sortMz()
Sort the DataPoint instances of this spectrum.
The MzIntegrationParams class provides the parameters definining how m/z integrations must be perform...
void binSizeDivisorChanged()
Q_INVOKABLE int getDecimalPlaces() const
@ REMOVE_ZERO_DATA_POINTS
Q_INVOKABLE MzIntegrationParams(QObject *parent=nullptr)
Q_INVOKABLE void updateGreatestMz(double value)
void binSizeModelChanged()
Q_INVOKABLE int getBinSizeDivisor() const
BinningType m_binningType
void upperAnchorMzChanged()
Q_INVOKABLE InitializationResult initialize(const QString &text)
std::vector< double > createDataBasedBinsOld(pappso::MassSpectrumCstSPtr massSpectrum)
Q_INVOKABLE double getLowerAnchorMz() const
Q_INVOKABLE void setBinSizeModel(pappso::PrecisionPtr bin_size_model_p)
Q_INVOKABLE void setBinSizeDivisor(int divisor)
std::vector< double > createArbitraryBins()
Q_INVOKABLE double getUpperAnchorMz() const
Q_INVOKABLE void setGreatestMz(double value)
void lowerAnchorMzChanged()
Q_INVOKABLE void setSmallestMz(double value)
Q_INVOKABLE QString binsToStringWithDeltas(const std::vector< double > bins) const
Q_INVOKABLE void updateLowerAnchorMz(double value)
Q_INVOKABLE bool isRemoveZeroValDataPoints() const
Q_INVOKABLE double getSmallestMz() const
pappso::PrecisionPtr m_binSizeModel
Q_INVOKABLE bool isValid() const
Q_INVOKABLE BinningType getBinningType() const
Q_INVOKABLE pappso::PrecisionPtr getBinSizeModel() const
Q_INVOKABLE void setUpperAnchorMz(double value)
Q_INVOKABLE bool hasValidMzRange() const
@ DATA_BASED
binning based on mass spectral data
@ ARBITRARY
binning based on arbitrary bin size value
void decimalPlacesChanged()
static void registerJsConstructor(QJSEngine *engine)
void removeZeroValDataPointsChanged()
Q_INVOKABLE void updateSmallestMz(double value)
void binningTypeChanged()
Q_INVOKABLE MzIntegrationParams * clone(QObject *parent=nullptr) const
Q_INVOKABLE void setMzValues(double smallest, double greatest)
bool m_removeZeroValDataPoints
Q_INVOKABLE void setBinningType(BinningType binningType)
Q_INVOKABLE void reset()
Reset the instance to default values.
std::vector< double > createDataBasedBins(pappso::MassSpectrumCstSPtr massSpectrum)
Q_INVOKABLE QString toString() const
Q_INVOKABLE void setDecimalPlaces(int decimal_places)
Q_INVOKABLE std::vector< double > createBins()
Q_INVOKABLE void setRemoveZeroValDataPoints(bool removeOrNot=true)
Q_INVOKABLE void updateUpperAnchorMz(double value)
virtual ~MzIntegrationParams()
Q_INVOKABLE double getGreatestMz() const
Q_INVOKABLE void setLowerAnchorMz(double value)
static PrecisionPtr getResInstance(pappso_double value)
get a resolution precision pointer
static PrecisionPtr fromString(const QString &str)
get a precision pointer from a string
static PrecisionPtr getPpmInstance(pappso_double value)
get a ppm precision pointer
static bool writeToFile(const QString &text, const QString &file_name)
static int zeroDecimalsInValue(pappso_double value)
Determine the number of zero decimals between the decimal point and the first non-zero decimal.
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
MzIntegrationParams::BinningType getBinningTypeFromString(const QString &text)
std::map< MzIntegrationParams::BinningType, QString > binningTypeMap
Map relating the BinningType to a textual representation.
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
const PrecisionBase * PrecisionPtr