R/icoshift_scal.R

Defines functions checkIntervals getIntervalsScal dscal2dpts scal2pts chkScal

# Porte literal de private/chkScal.m, private/scal2pts.m e do ramo numerico
# com Scal de private/getIntervals.m + private/checkIntervals.m (icoshift v3.0 beta).
# Cada linha segue o MATLAB o mais proximo possivel; divergencias sao fatais
# para a reproducao exata dos deslocamentos.

chkScal <- function(Scal, len, flag = FALSE, name = "Scal") {
  if (!(is.numeric(Scal) && length(Scal) == len))
    stop(sprintf("%s must be a vector having the same length as the corresponding dimension", name))
  dScal   <- diff(Scal)
  incScal <- sign(dScal[1])
  if (incScal == 0 || !all(sign(dScal) == incScal))
    stop(sprintf("%s must be strictly monotonic", name))
  flagDirect <- incScal > 0
  # NOTA: mediana, nao minimo (chkScal.m linha 13)
  prec <- abs(stats::median(unique(dScal)))
  if (flag && any(abs(dScal) > 2 * min(abs(dScal))))
    warning(sprintf("%s vector is not continuous, the defined intervals might not reflect actual ranges", name))
  list(scal = as.vector(Scal), precision = prec, scalDir = flagDirect)
}

scal2pts <- function(ppmi, ppm, prec = NULL) {
  if (is.null(prec)) prec <- min(abs(unique(diff(ppm))))
  dimppmi <- dim(ppmi)
  ppmi <- as.vector(ppmi)
  ppm  <- as.vector(ppm)
  rev_ <- ppm[1] > ppm[2]
  if (rev_) ppm <- rev(ppm)
  nppm  <- length(ppm)
  nppmi <- length(ppmi)
  if (nppmi > nppm) warning("ppm vector is shorter than the value's")

  ubound <- (ppmi - ppm[nppm]) < prec & (ppmi - ppm[nppm]) > 0
  lbound <- (ppm[1] - ppmi)    < prec & (ppm[1] - ppmi)    > 0
  ppmi[ubound] <- ppm[nppm]
  ppmi[lbound] <- ppm[1]

  # rank de cada ppmi dentro de ppm, via ordenacao conjunta (sort estavel)
  k   <- order(ppmi)
  xxi <- ppmi[k]
  j   <- order(c(ppm, xxi))
  r   <- numeric(nppm + nppmi)
  r[j] <- seq_len(nppm + nppmi)
  r    <- r[(nppm + 1):(nppm + nppmi)] - seq_len(nppmi)
  r[k] <- r
  r[ppmi == ppm[nppm]] <- nppm

  ind <- which(r > 0 & r <= nppm)
  pts <- rep(Inf, nppmi)
  pts[ind] <- r[ind]
  ptsp1 <- pmin(nppm, abs(pts + 1))
  ptsm1 <- pmax(1,    abs(pts - 1))

  ind  <- which(is.finite(pts))
  dp0  <- abs(ppm[pts[ind]]   - ppmi[ind])
  dpp1 <- abs(ppm[ptsp1[ind]] - ppmi[ind])
  dpm1 <- abs(ppm[ptsm1[ind]] - ppmi[ind])
  pts[ind[dpp1 < dp0]] <- pts[ind[dpp1 < dp0]] + 1
  pts[ind[dpm1 < dp0]] <- pts[ind[dpm1 < dp0]] - 1

  pts[!is.finite(pts)] <- NA_real_
  if (rev_) pts <- nppm - pts + 1
  if (!is.null(dimppmi)) dim(pts) <- dimppmi
  pts
}

dscal2dpts <- function(d, ppm, prec = NULL) {
  if (length(d) == 0) return(numeric(0))
  if (d <= 0) stop("Delta must be positive")
  if (ppm[1] < ppm[2]) scal2pts(ppm[1] + d, ppm, prec) - 1
  else length(ppm) - scal2pts(ppm[length(ppm)] + d, ppm, prec) + 1
}

# getIntervals.m, ramo numeric com vetor de pares + useScal
getIntervalsScal <- function(inter, mP, scalStr) {
  prec <- scalStr$precision
  Scal <- scalStr$scal
  inter <- scal2pts(inter, Scal, prec)
  odd <- seq(1, length(inter), by = 2)
  if (any(inter[odd] > inter[odd + 1]) && Scal[2] < Scal[1]) {
    m <- matrix(inter, nrow = 2)      # reshape column-wise
    m <- m[2:1, , drop = FALSE]       # flipud
    inter <- as.vector(m)
  }
  inter
}

# checkIntervals.m
checkIntervals <- function(inter, mP) {
  if (length(inter) <= 1) stop("Debug: this error should never be reached!!!")
  allint <- t(matrix(inter, nrow = 2))
  if (max(allint[, 2]) > mP)
    stop('Intervals ("inter") exceed samples matrix dimension')
  sallint <- t(allint[order(allint[, 1]), , drop = FALSE])
  intdif  <- diff(as.vector(sallint)[-1])
  if (any(intdif[mseq(1, 2, length(intdif))] < 0))
    warning("The user-defined intervals are overlapping: is that intentional?")
  cbind(seq_len(nrow(allint)), allint, allint[, 2] - allint[, 1] + 1)
}

Try the iSTATS package in your browser

Any scripts or data that you put into this service are public.

iSTATS documentation built on Nov. 6, 2026, 1:07 a.m.