@brief Returns the precursor spectrum of the scan pointed to by @p iterator If there is no precursor scan the past-the-end iterator is returned. This assumes that precursors occur somewhere before the current spectrum but not necessarily the first one from the last MS level (we double-check with the annotated precursorList. */
| 512 | the annotated precursorList. |
| 513 | */ |
| 514 | MSExperiment::ConstIterator MSExperiment::getPrecursorSpectrum(ConstIterator iterator) const |
| 515 | { |
| 516 | // if we are after the end or at the beginning where we can't go "up" |
| 517 | if (iterator == spectra_.end() || iterator == spectra_.begin()) |
| 518 | { |
| 519 | return spectra_.end(); |
| 520 | } |
| 521 | UInt ms_level = iterator->getMSLevel(); |
| 522 | |
| 523 | if (ms_level == 1) // assumes there is not level 0 |
| 524 | { |
| 525 | return spectra_.end(); |
| 526 | } |
| 527 | |
| 528 | if (!iterator->getPrecursors().empty()) |
| 529 | { |
| 530 | //TODO warn about taking first with the blocking LOG_WARN in such a central class? |
| 531 | //if (iterator->getPrecursors().size() > 1) ... |
| 532 | |
| 533 | const auto precursor = iterator->getPrecursors()[0]; |
| 534 | if (precursor.metaValueExists("spectrum_ref")) |
| 535 | { |
| 536 | String ref = precursor.getMetaValue("spectrum_ref"); |
| 537 | auto tmp_spec_iter = iterator; // such that we can reiterate later |
| 538 | do |
| 539 | { |
| 540 | --tmp_spec_iter; |
| 541 | if ((ms_level - tmp_spec_iter->getMSLevel() == 1) && (tmp_spec_iter->getNativeID() == ref)) |
| 542 | { |
| 543 | return tmp_spec_iter; |
| 544 | } |
| 545 | } while (tmp_spec_iter != spectra_.begin()); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // if no precursor annotation was found or it did not have a spectrum reference, |
| 550 | // just |
| 551 | do |
| 552 | { |
| 553 | --iterator; |
| 554 | if (ms_level - iterator->getMSLevel() == 1) |
| 555 | { |
| 556 | return iterator; |
| 557 | } |
| 558 | } while (iterator != spectra_.begin()); |
| 559 | |
| 560 | return spectra_.end(); |
| 561 | } |
| 562 | |
| 563 | // same as above but easier to wrap in python |
| 564 | int MSExperiment::getPrecursorSpectrum(int zero_based_index) const |