Skip to content

DescendPeaks in C++#104

Open
Pascallio wants to merge 3 commits into
rformassspectrometry:mainfrom
Pascallio:DescendPeaks
Open

DescendPeaks in C++#104
Pascallio wants to merge 3 commits into
rformassspectrometry:mainfrom
Pascallio:DescendPeaks

Conversation

@Pascallio
Copy link
Copy Markdown

This is the C++ implementation of the descendPeaks function of MSnbase. It takes a slightly different approach to the original by first subsetting the maximum region to consider. Next for each left/right descend, it checks if the next signal is A) above the signal percentage threshold and B) not higher than the previous signal (rising). If the signal passes both tests, the left/right index is updated. After both descends, these indexes are used to define the region of which the weighted mean of the mass is taken.

It is recommended to sanatize all inputs first in R before calling this function to ensure the right values are used, e.g. C-indexes, not R-indexes.

This is the C++ implementation of the descendPeaks function of MSnbase. It takes a slightly different approach to the original by first subsetting the maximum region to consider. Next for each left/right descend, it checks if the next signal is A) above the signal percentage threshold and B) not higher than the previous signal (rising). If the signal passes both tests, the left/right index is updated. After both descends, these indexes are used to define the region of which the weighted mean of the mass is taken.
@lgatto
Copy link
Copy Markdown
Member

lgatto commented Nov 23, 2022

Just a quick comment - it would also be important to have unit tests to check that the results give expected results on small data or/and same as an R implementation on a more complex data.

Comment thread src/descendPeak.cpp
@sgibb
Copy link
Copy Markdown
Member

sgibb commented Nov 25, 2022

@Pascallio Thanks for your contribution. It is very welcome! Maybe a stupid question/suggestion. We try to keep the dependencies as low as possible (Suggests contains already a large list of dependencies). Currently we successfully avoid Rcpp as direct dependency (some packages in Suggests of course already depend on Rcpp). It would be great if you ensure that Rcpp would end in Suggests and not in Depends or Imports or (implement this whole stuff in pure C).

Comment thread src/descendPeak.cpp Outdated
Comment thread src/descendPeak.cpp
bool goodSignal = current / centroidValue > signalRatio;

// Compare current to previous value to check for a rising signal
bool risingSignal = current >= previous;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be a good test? Already a small plateau (2 indices of the same intensity value) would break here, even if it goes down after the overnext index.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do believe that is the default behaviour in the original MSnbase version here: https://github.com/lgatto/MSnbase/blob/1338323345d2a6cf856f1819dfb365300ab6d9d5/R/functions-Spectrum.R#L781-L788

I'm not sure about the current version in MsCoreUtils, but I could implement the stopAtTwo behaviour or perhaps stop at any given k if desired?

Pascallio and others added 2 commits November 26, 2022 16:00
Avoids looping over the intensity vector twice

Co-authored-by: Sebastian Gibb <mail@sebastiangibb.de>
Allows for setting a different maxK, which determines the maximum region the descendPeak algorithm is applied to
@Pascallio
Copy link
Copy Markdown
Author

@Pascallio Thanks for your contribution. It is very welcome! Maybe a stupid question/suggestion. We try to keep the dependencies as low as possible (Suggests contains already a large list of dependencies). Currently we successfully avoid Rcpp as direct dependency (some packages in Suggests of course already depend on Rcpp). It would be great if you ensure that Rcpp would end in Suggests and not in Depends or Imports or (implement this whole stuff in pure C).

Thank you @sgibb! I fully understand the concern about dependencies. I don't have much experience with Rcpp, so how do I ensure it would end up in Suggests? Does that mean I should replace the Rcpp Range and change NumericVectors with for instance std::vector?

As for implementing it in pure C, I have no experience with C so it might take me a while to submit a pure C implementation but I'm willing to give it a shot when I have some spare time.

@lgatto
Copy link
Copy Markdown
Member

lgatto commented Apr 20, 2026

Not sure if this PR is still relevant, but I would like to see a unit test, that compares the R- and C-level results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants