Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce number of comparisons in fastquant_max #280

Merged
merged 2 commits into from
Nov 27, 2017
Merged

Reduce number of comparisons in fastquant_max #280

merged 2 commits into from
Nov 27, 2017

Conversation

sgibb
Copy link
Collaborator

@sgibb sgibb commented Nov 27, 2017

This PR is more or less just for documentation. It reduce the number of comparisons via <= and saves some time (if the peaks are already in memory speed gain is nearly two fold; for on disk objects it is just around 10%); see also #130.

library("msdata")
library("MSnbase")
library("microbenchmark")
f <- msdata::proteomics(full.names = TRUE,
                        pattern = "TMT_Erwinia_1uLSike_Top10HCD_isol2_45stepped_60min_01.mzML.gz")

fastquant_max2 <- function(f, pk, spi, wd = 0.5) {
    ramp <- MSnbase:::.openMSfile(f)
    on.exit(close(ramp))
    pks <- peaks(ramp, spi)
    if (length(spi) == 1)
        pks <- list(pks)
    mzs <- res <- matrix(NA_real_,
                         ncol = length(pk),
                         nrow = length(pks))
    for (i in seq_along(pks)) {
        for (ii in seq_along(pk)) {
            k <- abs(pks[[i]][, 1L] - pk[ii]) <= wd
            if (any(k)) {
                j <- which.max(pks[[i]][k, 2])
                res[i, ii] <- pks[[i]][k, , drop = FALSE][j, 2]
                mzs[i, ii] <- pks[[i]][k, , drop = FALSE][j, 1]
            }
        }
    }
    list(exprs = res,
         mzs = mzs)
}

all.equal(MSnbase:::fastquant_max(f, mz(TMT6), spi=1:509, wd=0.5),
          fastquant_max2(f, mz(TMT6), spi=1:509, wd=0.5))
# [1] TRUE

microbenchmark(MSnbase:::fastquant_max(f, mz(TMT6), spi=1:509, wd=0.5),
               fastquant_max2(f, mz(TMT6), spi=1:509, wd=0.5), times=10)

# MSnbase:::fastquant_max(f, mz(TMT6), spi = 1:509, wd = 0.5) 739.2620 746.6394
#          fastquant_max2(f, mz(TMT6), spi = 1:509, wd = 0.5) 682.8757 687.2723
#     mean   median       uq      max neval
# 765.8628 757.8052 760.7415 879.4974    10
# 718.0008 692.4549 700.9097 829.5581    10

@sgibb sgibb merged commit 46f4b6a into master Nov 27, 2017
@sgibb sgibb deleted the fastquant_max branch December 19, 2017 15:01
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.

None yet

1 participant