26 #include "../../exception/exceptionnotpossible.h"
59 : m_rtPoints(rt_points){};
77 std::vector<RtPoint> &m_rtPoints;
83 : m_ms2MedianFilter(10), m_ms2MeanFilter(15), m_ms1MeanFilter(1)
88 msrun_reader_sp.get()->readSpectrumCollection(handler);
94 return (a.retentionTime < b.retentionTime);
100 : m_ms2MedianFilter(other.m_ms2MedianFilter),
101 m_ms2MeanFilter(other.m_ms2MeanFilter),
102 m_ms1MeanFilter(other.m_ms1MeanFilter)
127 m_ms2MedianFilter = ms2MedianFilter;
134 m_ms2MeanFilter = ms2MeanFilter;
141 m_ms1MeanFilter = ms1MeanFilter;
145 const std::vector<MsRunRetentionTimeSeamarkPoint<T>> &
148 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
153 const std::vector<double> &
156 return m_alignedRetentionTimeVector;
163 return m_valuesCorrected;
166 const std::vector<RtPoint> &
169 return m_ms1RetentionTimeVector;
178 getCommonDeltaRt(common_points, other_seamarks);
179 return common_points;
185 std::size_t ms2_spectrum_index)
188 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
192 msp_msrunReader.get()->qualifiedMassSpectrum(ms2_spectrum_index,
false);
204 m_allMs2Points.push_back(ms2point);
206 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
214 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
215 if(msp_msrunReader ==
nullptr)
218 QObject::tr(
"ERROR : msp_msrunReader == nullptr"));
221 if(m_retentionTimeReferenceMethod ==
222 ComputeRetentionTimeReference::maximum_intensity)
226 std::sort(m_allMs2Points.begin(),
227 m_allMs2Points.end(),
229 if(a.entityHash == b.entityHash)
231 return (a.precursorIntensity > b.precursorIntensity);
233 return (
a.entityHash <
b.entityHash);
237 std::unique(m_allMs2Points.begin(),
238 m_allMs2Points.end(),
240 return (a.entityHash == b.entityHash);
243 auto it = m_allMs2Points.begin();
246 m_seamarks.push_back(
247 {it->entityHash, it->retentionTime, it->precursorIntensity});
251 msp_msrunReader =
nullptr;
252 m_allMs2Points.clear();
254 std::sort(m_seamarks.begin(),
258 return (a.entityHash < b.entityHash);
260 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
270 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
271 auto it = other_seamarks.begin();
275 while((it != other_seamarks.end()) &&
276 (it->entityHash < seamark.entityHash))
280 if(it == other_seamarks.end())
282 if(it->entityHash == seamark.entityHash)
285 seamark.retentionTime, seamark.retentionTime - it->retentionTime));
289 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
290 if((m_ms2MedianFilter.getHalfWindowSize() * 2 + 1) >= delta_rt.size())
293 QObject::tr(
"ERROR : MS2 alignment of MS run '%1' (%2)' not possible : "
294 "\ntoo few MS2 points (%3) in common")
295 .arg(msp_msrunReader.get()->getMsRunId().get()->getXmlId())
296 .arg(msp_msrunReader.get()->getMsRunId().get()->getFileName())
297 .arg(delta_rt.size()));
300 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
301 if((m_ms2MeanFilter.getHalfWindowSize() * 2 + 1) >= delta_rt.size())
304 QObject::tr(
"ERROR : MS2 alignment of MS run '%1' (%2)' not possible : "
305 "\ntoo few MS2 points (%3) in common")
306 .arg(msp_msrunReader.get()->getMsRunId().get()->getXmlId())
307 .arg(msp_msrunReader.get()->getMsRunId().get()->getFileName())
308 .arg(delta_rt.size()));
316 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
326 return m_alignedRetentionTimeVector.front();
328 return m_ms1RetentionTimeVector.front().retentionTime;
336 return m_alignedRetentionTimeVector.back();
338 return m_ms1RetentionTimeVector.back().retentionTime;
345 double original_retention_time)
const
347 if(m_alignedRetentionTimeVector.size() < 3)
350 QObject::tr(
"ERROR : too few aligned points to compute aligned "
351 "retention time (%1)")
352 .arg(m_ms1RetentionTimeVector.size()));
354 if(m_alignedRetentionTimeVector.size() != m_ms1RetentionTimeVector.size())
357 QObject::tr(
"ERROR : m_alignedRetentionTimeVector.size() %1 != %2 "
358 "m_ms1RetentionTimeVector.size()")
359 .arg(m_alignedRetentionTimeVector.size())
360 .arg(m_ms1RetentionTimeVector.size()));
363 std::find_if(m_ms1RetentionTimeVector.begin(),
364 m_ms1RetentionTimeVector.end(),
365 [original_retention_time](
const RtPoint &rt_point) {
366 return original_retention_time < rt_point.retentionTime;
368 double rt1_a, rt2_a, rt1_b, rt2_b;
369 if(it_plus == m_ms1RetentionTimeVector.end())
373 if(it_plus == m_ms1RetentionTimeVector.begin())
377 auto it_minus = it_plus - 1;
379 rt1_a = it_minus->retentionTime;
380 rt2_a = it_plus->retentionTime;
382 double ratio = (original_retention_time - rt1_a) / (rt2_a - rt1_a);
384 auto itref = m_alignedRetentionTimeVector.begin() +
385 std::distance(m_ms1RetentionTimeVector.begin(), it_minus);
391 return (((rt2_b - rt1_b) * ratio) + rt1_b);
395 const std::vector<MsRunRetentionTimeSeamarkPoint<T>>
398 std::vector<MsRunRetentionTimeSeamarkPoint<T>> other_seamarks = m_seamarks;
399 for(
auto &seamark : other_seamarks)
401 seamark.retentionTime =
402 translateOriginal2AlignedRetentionTime(seamark.retentionTime);
404 return other_seamarks;
411 return (m_alignedRetentionTimeVector.size() > 0);
419 std::vector<MsRunRetentionTimeSeamarkPoint<T>> other_seamarks;
420 if(msrun_retention_time_reference.
isAligned())
426 other_seamarks = msrun_retention_time_reference.
getSeamarks();
428 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
429 if((m_ms1MeanFilter.getHalfWindowSize() * 2 + 1) >=
430 m_ms1RetentionTimeVector.size())
433 QObject::tr(
"ERROR : MS1 alignment of MS run '%1' (%2)' not possible : "
434 "\ntoo few MS1 points (%3)")
435 .arg(msp_msrunReader.get()->getMsRunId().get()->getXmlId())
436 .arg(msp_msrunReader.get()->getMsRunId().get()->getFileName())
437 .arg(m_ms1RetentionTimeVector.size()));
440 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"() "
441 << m_seamarks[0].entityHash <<
" " << m_seamarks[0].retentionTime
442 <<
" " << other_seamarks[0].entityHash
443 << other_seamarks[0].retentionTime <<
" ";
446 getCommonDeltaRt(common_points, other_seamarks);
450 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"() "
451 << common_points.front().x <<
" " << common_points.front().y;
452 m_ms2MedianFilter.
filter(common_points);
454 m_ms2MeanFilter.
filter(common_points);
457 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"() "
458 << common_points.front().x <<
" " << common_points.front().y;
463 first_point.
x = m_ms1RetentionTimeVector.front().retentionTime - (double)1;
464 if(first_point.
x < 0)
469 m_ms1RetentionTimeVector.front().retentionTime -
472 common_points.push_back(first_point);
475 last_point.
x = m_ms1RetentionTimeVector.back().retentionTime + 1;
476 last_point.
y = m_ms1RetentionTimeVector.back().retentionTime -
478 common_points.push_back(last_point);
479 common_points.
sortX();
483 m_alignedRetentionTimeVector.clear();
485 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"() "
486 << common_points.front().x <<
" " << common_points.front().y;
488 Trace ms1_aligned_points;
490 linearRegressionMs2toMs1(ms1_aligned_points, common_points);
493 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
495 m_ms1MeanFilter.filter(ms1_aligned_points);
500 for(
DataPoint &data_point : ms1_aligned_points)
502 data_point.y = (data_point.x - data_point.y);
505 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
508 double correction_parameter =
509 (m_ms1RetentionTimeVector.back().retentionTime -
510 m_ms1RetentionTimeVector.front().retentionTime) /
511 (ms1_aligned_points.size());
513 correction_parameter = correction_parameter / (double)4;
514 correctNewTimeValues(ms1_aligned_points, correction_parameter);
516 m_alignedRetentionTimeVector = ms1_aligned_points.yValues();
518 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__ <<
"()";
519 return ms1_aligned_points;
526 const Trace &common_points)
530 std::vector<DataPoint>::const_iterator itms2 = common_points.begin();
531 std::vector<DataPoint>::const_iterator itms2next = itms2 + 1;
532 if(itms2next == common_points.end())
536 QObject::tr(
"ERROR : MS1 alignment of MS run '%1' (%2)' not possible : "
537 "\ntoo few common points (%3)")
538 .arg(msp_msrunReader.get()->getMsRunId().get()->getXmlId())
539 .arg(msp_msrunReader.get()->getMsRunId().get()->getFileName())
540 .arg(common_points.size()));
542 qDebug() << __FILE__ <<
"@" << __LINE__ << __FUNCTION__
543 <<
"() itms2->x=" << itms2->x <<
" itms2->y=" << itms2->y;
545 for(
RtPoint &original_rt_point : m_ms1RetentionTimeVector)
548 ms1_point.
x = original_rt_point.retentionTime;
550 while(ms1_point.
x > itms2next->x)
556 double ratio = (itms2next->x - itms2->x);
559 ratio = (ms1_point.
x - itms2->x) / ratio;
569 ms1_point.
y = itms2->y + ((itms2next->y - itms2->y) * ratio);
573 ms1_aligned_points.push_back(ms1_point);
580 double correction_parameter)
583 m_valuesCorrected = 0;
584 auto new_it(ms1_aligned_points.begin());
585 auto new_nextit(ms1_aligned_points.begin());
587 for(; new_nextit != ms1_aligned_points.end(); ++new_nextit, ++new_it)
589 if(new_nextit->y < new_it->y)
592 new_nextit->y = new_it->y + correction_parameter;