wesleyburr / subMaldi

subMaldi: a package for R
GNU General Public License v3.0
3 stars 3 forks source link

JOSS 12 #17

Closed wesleyburr closed 3 years ago

wesleyburr commented 3 years ago

There are common patterns in the code base that can be generalized easily.

For example:

if(is.null(spec3)){

  if(is.null(spec2)){

  # Single spectrum
  mass <- dat[[mass_dat]]
  intense <- dat[[spec1]]

  max_Intensity <- max(intense)

  dat <- select(dat, all_of(mass_dat))
  dat <- transform(dat, "Normalized" = 0)
  dat["Normalized"] <- .normalize(intense)
  return(dat)
  }

  # Two spectra
  else{
    mass <- dat[[mass_dat]]
    intense1 <- dat[[spec1]]
    intense2 <- dat[[spec2]]

    max_Intensity1 <- max(intense1)
    max_Intensity2 <- max(intense2)

    dat <- select(dat, all_of(mass_dat))
    dat[spec1] <- .normalize(intense1)
    dat[spec2] <- .normalize(intense2)
    return(dat)
  }}

  # Three spectra
  else{
    mass <- dat[[mass_dat]]
    intense1 <- dat[[spec1]]
    intense2 <- dat[[spec2]]
    intense3 <- dat[[spec3]]

    max_Intensity1 <- max(intense1)
    max_Intensity2 <- max(intense2)
    max_Intensity3 <- max(intense3)

    dat <- select(dat, all_of(mass_dat))
    dat[spec1] <- .normalize(intense1)
    dat[spec2] <- .normalize(intense2)
    dat[spec3] <- .normalize(intense3)
    return(dat)
  }}

can be replaced with something like so:

spectra_cols <- c(spec1, spec2, spec3)

xxx <- function(..., mass_dat, spectra_cols) {
    mass <- dat[[mass_dat]]
    intensities <- lapply(spectra_cols, function(x){ dat[[x]] })
    intensities <- lapply(intensities, .normalize)

    dat <- do.call(what = data.frame, args = c(list(mass), intensities))
    colnames(dat) <- c(mass_dat, spectra_cols)
    return(dat)
}