Skip to content

Commit 8af46fb

Browse files
authored
add limits for constraining search space for spar (#32)
1 parent 824f1d3 commit 8af46fb

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

R/sbc_main.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ NULL
2828
#' (QC) samples required for signal correction within feature per batch. For
2929
#' features where signal was measured in less QC samples than threshold signal
3030
#' correction won't be applied.
31-
#'
31+
#' @param spar_lim A 2 element numeric vector containing the min and max
32+
#'values of spar when searching for an optimum. Default \code{spar_lim = c(-1.5,1.5)}
3233
#' @return Object of class \code{SummarizedExperiment}. If input data are a
3334
#' matrix-like (e.g. an ordinary matrix, a data frame) object, function returns
3435
#' the same R data structure as input with all value of data type
@@ -45,7 +46,7 @@ NULL
4546
#' @export
4647

4748
QCRSC <- function(df, order, batch, classes, spar = 0, log = TRUE,
48-
minQC = 5, qc_label="QC") {
49+
minQC = 5, qc_label="QC", spar_lim = c(-1.5,1.5)) {
4950

5051
df <- check_input_data(df=df, classes=classes)
5152

@@ -63,14 +64,15 @@ QCRSC <- function(df, order, batch, classes, spar = 0, log = TRUE,
6364
qc_order <- order[classes == qc_label]
6465
QC_fit <- lapply(seq_len(nrow(df)), sbcWrapper, qcData = assay(qcData),
6566
order = order, qcBatch = qc_batch, qcOrder = qc_order,
66-
log = log, spar = spar, batch = batch, minQC = minQC)
67+
log = log, spar = spar, batch = batch, minQC = minQC,
68+
spar_lim = spar_lim)
6769
QC_fit <- do.call(rbind, QC_fit)
6870

6971
meta_data <- metadata(df)
7072
meta_data$processing_history$QCRSC <- return_function_args()
7173
meta_data$processing_history$QCRSC$QC_fit=QC_fit
7274

73-
# Median value for each fature, and divide it by predicted value
75+
# Median value for each feature, and divide it by predicted value
7476
mpa <- matrixStats::rowMedians(assay(df), na.rm=TRUE)
7577
QC_fit <- QC_fit/mpa
7678

R/sbc_methods.R

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,27 @@ logFit <- function (x, a=1, inverse=FALSE) {
3131
##' @param spar Smoothing parameter of the fitted curve. Should be in the range
3232
##'0 to 1. If set to 0 it will be estimated using leave-one-out
3333
##'cross-validation.
34+
##' @param spar_lim A 2 element numeric vector containing the min and max
35+
##'values of spar when searching for an optimum. Default \code{spar_lim = c(-1.5,1.5)}
3436
##' @return vector of cubic smoothing spline fitted values
3537
##' @noRd
3638

37-
splineSmoother <- function(x, y, newX, log=log, a=1, spar) {
39+
splineSmoother <- function(x, y, newX, log=log, a=1, spar,
40+
spar_lim=c(-1.5,1.5)) {
3841
if(log == TRUE) {
3942
y <- logFit(y, a=a)
4043
}
44+
45+
control.spar=list(
46+
low = spar_lim[1],
47+
high = spar_lim[2]
48+
)
49+
4150
if (spar == 0) {
4251
# Supress spline fitting CV messages cluttering terminal output
4352
smoothSplineMessages <- capture.output(sp.obj
44-
<- smooth.spline(x, y, cv=TRUE), file=NULL, type="message")
53+
<- smooth.spline(x, y, cv=TRUE,control.spar=control.spar),
54+
file=NULL, type="message")
4555
} else {
4656
smoothSplineMessages <- character()
4757
sp.obj <- smooth.spline(x, y, spar=spar)
@@ -72,11 +82,13 @@ splineSmoother <- function(x, y, newX, log=log, a=1, spar) {
7282
##' @param minQC Minimum number of QC samples required for signal correction.
7383
##' @param order A numeric vector indicating the order in which samples
7484
##'were measured.
85+
##' @param spar_lim A 2 element numeric vector containing the min and max
86+
##'values of spar when searching for an optimum. Default \code{spar_lim = c(-1.5,1.5)}
7587
##' @return vector of corrected values of selected feature for QC data
7688
##' @noRd
7789

7890
sbcWrapper <- function(id, qcData, order, qcBatch, qcOrder, log=log, spar=spar,
79-
batch=batch, minQC) {
91+
batch=batch, minQC, spar_lim = c(-1.5,1.5)) {
8092

8193
out <- tryCatch ({
8294

@@ -108,7 +120,7 @@ sbcWrapper <- function(id, qcData, order, qcBatch, qcOrder, log=log, spar=spar,
108120
if (length(y) >= minQC) {
109121
# fit spline to QCs and get predictions for all samples in batch
110122
outl[batch==nbatch[nb]] <- splineSmoother(x=x, y=y, newX=order[batch==nbatch[nb]], log=log,
111-
a=1, spar=spar)
123+
a=1, spar=spar, spar_lim=spar_lim)
112124
} else {
113125
# otherwise replace with NA
114126
outl[batch==nbatch[nb]] = NA

man/QCRSC.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)