libpappsomspp
Library for mass spectrometry
pappso::Utils Class Reference

#include <utils.h>

Static Public Member Functions

static const QString getLexicalOrderedString (unsigned int num)
 
static void writeLexicalOrderedString (QTextStream *p_out, unsigned int num)
 
static int zeroDecimalsInValue (pappso_double value)
 0.11 would return 0 (no empty decimal) 2.001 would return 2 1000.0001254 would return 3 More...
 
static pappso_double roundToDecimals (pappso_double value, int decimal_places)
 
static std::string toUtf8StandardString (const QString &text)
 
static bool writeToFile (const QString &text, const QString &file_name)
 
static bool appendToFile (const QString &text, const QString &file_name)
 
static std::size_t extractScanNumberFromMzmlNativeId (const QString &spectrum_native_id)
 
static QString pointerToString (const void *const pointer)
 
static bool almostEqual (double value1, double value2, int decimalPlaces=10)
 
static double nearestGreater (double value)
 
static QString chronoTimePointDebugString (const QString &msg, std::chrono::system_clock::time_point chrono_time=std::chrono::system_clock::now())
 
static QString chronoIntervalDebugString (const QString &msg, std::chrono::system_clock::time_point chrono_start, std::chrono::system_clock::time_point chrono_finish=std::chrono::system_clock::now())
 
static std::vector< double > splitMzStringToDoubleVectorWithSpaces (const QString &text, std::size_t &error_count)
 
static std::vector< std::size_t > splitSizetStringToSizetVectorWithSpaces (const QString &text, std::size_t &error_count)
 

Static Public Attributes

static QRegularExpression xyMassDataFormatRegExp
 
static QRegularExpression mzListDataFormatRegExp
 Regular expression matching <m/z value><non-numerical*> More...
 
static QRegularExpression sizetListDataFormatRegExp
 Regular expression matching <size_t><non-numerical*> More...
 
static QRegularExpression endOfLineRegExp = QRegularExpression("^\\s+$")
 Regular expression that tracks the end of line in text files. More...
 

Detailed Description

Definition at line 48 of file utils.h.

Member Function Documentation

◆ almostEqual()

bool pappso::Utils::almostEqual ( double  value1,
double  value2,
int  decimalPlaces = 10 
)
static

Tell if both double values, are equal within the double representation capabilities of the platform.

Definition at line 233 of file utils.cpp.

234 {
235  // QString value1String = QString("%1").arg(value1,
236  // 0, 'f', 60);
237  // QString value2String = QString("%1").arg(value2,
238  // 0, 'f', 60);
239 
240  // qWarning() << __FILE__ << __LINE__ << __FUNCTION__
241  //<< "value1:" << value1String << "value2:" << value2String;
242 
243  // The machine epsilon has to be scaled to the magnitude of the values used
244  // and multiplied by the desired precision in ULPs (units in the last place)
245  // (decimal places).
246 
247  double valueSum = std::abs(value1 + value2);
248  // QString valueSumString = QString("%1").arg(valueSum,
249  // 0, 'f', 60);
250 
251  double valueDiff = std::abs(value1 - value2);
252  // QString valueDiffString = QString("%1").arg(valueDiff,
253  // 0, 'f', 60);
254 
255  double epsilon = std::numeric_limits<double>::epsilon();
256  // QString epsilonString = QString("%1").arg(epsilon,
257  // 0, 'f', 60);
258 
259  double scaleFactor = epsilon * valueSum * decimalPlaces;
260  // QString scaleFactorString = QString("%1").arg(scaleFactor,
261  // 0, 'f', 60);
262 
263  // qWarning() << "valueDiff:" << valueDiffString << "valueSum:" <<
264  // valueSumString <<
265  //"epsilon:" << epsilonString << "scaleFactor:" << scaleFactorString;
266 
267  bool res = valueDiff < scaleFactor
268  // unless the result is subnormal:
269  || valueDiff < std::numeric_limits<double>::min();
270 
271  // qWarning() << __FILE__ << __LINE__ << __FUNCTION__
272  //<< "returning res:" << res;
273 
274  return res;
275 }

References pappso::res.

◆ appendToFile()

bool pappso::Utils::appendToFile ( const QString &  text,
const QString &  file_name 
)
static

Definition at line 167 of file utils.cpp.

168 {
169 
170  QFile file(file_name);
171 
172  if(file.open(QFile::WriteOnly | QFile::Append))
173  {
174 
175  QTextStream out(&file);
176 
177  out << text;
178 
179  out.flush();
180  file.close();
181 
182  return true;
183  }
184 
185  return false;
186 }

◆ chronoIntervalDebugString()

QString pappso::Utils::chronoIntervalDebugString ( const QString &  msg,
std::chrono::system_clock::time_point  chrono_start,
std::chrono::system_clock::time_point  chrono_finish = std::chrono::system_clock::now() 
)
static

Definition at line 302 of file utils.cpp.

306 {
307  QString debug_text =
308  QString(
309  "%1 %2 min = %3 s = %4 ms = %5 "
310  "µs\n")
311  .arg(msg)
312  .arg(std::chrono::duration_cast<std::chrono::minutes>(chrono_finish -
313  chrono_start)
314  .count())
315  .arg(std::chrono::duration_cast<std::chrono::seconds>(chrono_finish -
316  chrono_start)
317  .count())
318  .arg(std::chrono::duration_cast<std::chrono::milliseconds>(chrono_finish -
319  chrono_start)
320  .count())
321  .arg(std::chrono::duration_cast<std::chrono::microseconds>(chrono_finish -
322  chrono_start)
323  .count());
324 
325  return debug_text;
326 }

◆ chronoTimePointDebugString()

QString pappso::Utils::chronoTimePointDebugString ( const QString &  msg,
std::chrono::system_clock::time_point  chrono_time = std::chrono::system_clock::now() 
)
static

Definition at line 286 of file utils.cpp.

288 {
289 
290  time_t tt;
291 
292  tt = std::chrono::system_clock::to_time_t(chrono_time);
293 
294  QString debug_text =
295  QString("%1 - %2\n").arg(msg).arg(QString::fromLatin1(ctime(&tt)));
296 
297  return debug_text;
298 }

◆ extractScanNumberFromMzmlNativeId()

std::size_t pappso::Utils::extractScanNumberFromMzmlNativeId ( const QString &  spectrum_native_id)
static

TODO activate this in a future release to ensure scan number for(auto i = 0; i < native_id_list.size(); i += 2) { if(native_id_list[i] == "scan") { return native_id_list[i + 1].toULong(); } }

throw ExceptionNotFound( QObject::tr("scan number not found in mzML native id %1") .arg(spectrum_native_id));

Definition at line 190 of file utils.cpp.

191 {
192  qDebug() << " " << spectrum_native_id;
193  QStringList native_id_list = spectrum_native_id.split("=");
194  if(native_id_list.size() < 2)
195  {
196  throw ExceptionNotFound(
197  QObject::tr("scan number not found in mzML native id %1")
198  .arg(spectrum_native_id));
199  }
200  else
201  {
202  /** TODO activate this in a future release to ensure scan number
203  for(auto i = 0; i < native_id_list.size(); i += 2)
204  {
205  if(native_id_list[i] == "scan")
206  {
207  return native_id_list[i + 1].toULong();
208  }
209  }
210 
211  throw ExceptionNotFound(
212  QObject::tr("scan number not found in mzML native id %1")
213  .arg(spectrum_native_id));
214 
215 */
216  return native_id_list.back().toULong();
217  }
218  return 0;
219 }

◆ getLexicalOrderedString()

const QString pappso::Utils::getLexicalOrderedString ( unsigned int  num)
static

Definition at line 52 of file utils.cpp.

53 {
54  int size = log10(num);
55  size += 97;
56  QString base(size);
57  base.append(QString().setNum(num));
58  return (base);
59 }

References pappso::log10.

Referenced by pappso::GrpGroup::getGroupingId(), pappso::GrpPeptide::getGroupingId(), pappso::GrpProtein::getGroupingId(), pappso::GrpSubGroup::getGroupingId(), pappso::PwizMsFileReader::getMsRunIds(), and pappso::XyMsFileReader::getMsRunIds().

◆ nearestGreater()

double pappso::Utils::nearestGreater ( double  value)
static

Definition at line 279 of file utils.cpp.

280 {
281  return std::nextafter(value, value + 1);
282 }

◆ pointerToString()

QString pappso::Utils::pointerToString ( const void *const  pointer)
static

Definition at line 223 of file utils.cpp.

224 {
225  return QString("%1").arg(
226  (quintptr)pointer, QT_POINTER_SIZE * 2, 16, QChar('0'));
227 }

Referenced by pappso::QualifiedMassSpectrum::toString(), and pappso::MsRunDataSetTreeNode::toString().

◆ roundToDecimals()

pappso_double pappso::Utils::roundToDecimals ( pappso_double  value,
int  decimal_places 
)
static

Definition at line 104 of file utils.cpp.

105 {
106  if(decimal_places < 0)
107  return value;
108 
109  return ceil((value * pow(10, decimal_places)) - 0.49) /
110  pow(10, decimal_places);
111 }

Referenced by pappso::MassSpectrumMinusCombiner::combine(), pappso::MassSpectrumPlusCombiner::combine(), pappso::TraceMinusCombiner::combine(), and pappso::TracePlusCombiner::combine().

◆ splitMzStringToDoubleVectorWithSpaces()

std::vector< double > pappso::Utils::splitMzStringToDoubleVectorWithSpaces ( const QString &  text,
std::size_t &  error_count 
)
static

Definition at line 330 of file utils.cpp.

332 {
333 
334  QStringList string_list =
335  text.split(QRegularExpression("[\\s]+"), QString::SkipEmptyParts);
336 
337  // qDebug() << "string list:" << string_list;
338 
339  std::vector<double> double_vector;
340 
341  for(int iter = 0; iter < string_list.size(); ++iter)
342  {
343  QString current_string = string_list.at(iter);
344 
345  bool ok = false;
346 
347  double current_double = current_string.toDouble(&ok);
348 
349  if(!current_double && !ok)
350  {
351  ++error_count;
352  continue;
353  }
354 
355  double_vector.push_back(current_double);
356  }
357 
358  return double_vector;
359 }

◆ splitSizetStringToSizetVectorWithSpaces()

std::vector< std::size_t > pappso::Utils::splitSizetStringToSizetVectorWithSpaces ( const QString &  text,
std::size_t &  error_count 
)
static

Definition at line 363 of file utils.cpp.

365 {
366  // qDebug() << "Parsing text:" << text;
367 
368  QStringList string_list =
369  text.split(QRegularExpression("[\\s]+"), QString::SkipEmptyParts);
370 
371  // qDebug() << "string list size:" << string_list.size()
372  //<< "values:" << string_list;
373 
374  std::vector<std::size_t> sizet_vector;
375 
376  for(int iter = 0; iter < string_list.size(); ++iter)
377  {
378  QString current_string = string_list.at(iter);
379 
380  bool ok = false;
381 
382  std::size_t current_sizet = current_string.toUInt(&ok);
383 
384  if(!current_sizet && !ok)
385  {
386  ++error_count;
387  continue;
388  }
389 
390  sizet_vector.push_back(current_sizet);
391  }
392 
393  return sizet_vector;
394 }

◆ toUtf8StandardString()

std::string pappso::Utils::toUtf8StandardString ( const QString &  text)
static

Definition at line 115 of file utils.cpp.

116 {
117  std::string env_backup = setlocale(LC_ALL, "");
118 
119  // struct lconv *lc_backup = localeconv();
120  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
121  //<< "env_backup=" << env_backup.c_str() << "lc_backup->decimal_point"
122  //<< lc_backup->decimal_point;
123 
124  // Force locale to be "C".
125  setlocale(LC_ALL, "C");
126 
127  // Now perform the conversion.
128  QByteArray byte_array = text.toUtf8();
129  std::string stdText = "";
130 
131  for(char c : byte_array)
132  {
133  stdText += c;
134  }
135 
136  // Set back the locale to the backed-up one.
137  setlocale(LC_ALL, env_backup.c_str());
138 
139  return stdText;
140 }

References pappso::c.

Referenced by pappso::PwizMsFileReader::initialize(), and pappso::PwizMsRunReader::initialize().

◆ writeLexicalOrderedString()

void pappso::Utils::writeLexicalOrderedString ( QTextStream *  p_out,
unsigned int  num 
)
static

Definition at line 63 of file utils.cpp.

64 {
65  *p_out << (char)(log10(num) + 97) << num;
66 }

References pappso::log10.

◆ writeToFile()

bool pappso::Utils::writeToFile ( const QString &  text,
const QString &  file_name 
)
static

Definition at line 144 of file utils.cpp.

145 {
146 
147  QFile file(file_name);
148 
149  if(file.open(QFile::WriteOnly | QFile::Truncate))
150  {
151 
152  QTextStream out(&file);
153 
154  out << text;
155 
156  out.flush();
157  file.close();
158 
159  return true;
160  }
161 
162  return false;
163 }

◆ zeroDecimalsInValue()

int pappso::Utils::zeroDecimalsInValue ( pappso_double  value)
static

0.11 would return 0 (no empty decimal) 2.001 would return 2 1000.0001254 would return 3

Determine the number of zero decimals between the decimal point and the first non-zero decimal.

Parameters
valuethe value to be analyzed
Returns
the number of '0' decimals between the decimal separator '.' and the first non-0 decimal

Definition at line 81 of file utils.cpp.

82 {
83  int intPart = static_cast<int>(value);
84 
85  double decimalPart = value - intPart;
86 
87  int count = -1;
88 
89  while(1)
90  {
91  ++count;
92 
93  decimalPart *= 10;
94 
95  if(decimalPart > 1)
96  return count;
97  }
98 
99  return count;
100 }

Referenced by pappso::MzIntegrationParams::createArbitraryBins().

Member Data Documentation

◆ endOfLineRegExp

QRegularExpression pappso::Utils::endOfLineRegExp = QRegularExpression("^\\s+$")
static

Regular expression that tracks the end of line in text files.

Definition at line 62 of file utils.h.

Referenced by pappso::XyMsRunReader::accept(), pappso::XyMsFileReader::initialize(), and pappso::XyMsRunReader::qualifiedMassSpectrumFromXyMSDataFile().

◆ mzListDataFormatRegExp

QRegularExpression pappso::Utils::mzListDataFormatRegExp
static

Regular expression matching <m/z value><non-numerical*>

Definition at line 56 of file utils.h.

◆ sizetListDataFormatRegExp

QRegularExpression pappso::Utils::sizetListDataFormatRegExp
static

Regular expression matching <size_t><non-numerical*>

Definition at line 59 of file utils.h.

◆ xyMassDataFormatRegExp

QRegularExpression pappso::Utils::xyMassDataFormatRegExp
static
Initial value:
=
QRegularExpression("^(\\d*\\.?\\d+)([^\\d^\\.^-]+)(-?\\d*\\.?\\d*[e-]?\\d*)")

Regular expression matching <numerical value><non-numerical*><numerical value>

Definition at line 53 of file utils.h.

Referenced by pappso::XyMsRunReader::accept(), pappso::XyMsFileReader::initialize(), pappso::DataPoint::initialize(), and pappso::XyMsRunReader::qualifiedMassSpectrumFromXyMSDataFile().


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