library(microarray) library(stats) library(nlme) options(width=140, digits=3) cens=.7 # # This function defines the polynomial-hyperbolic spot shape model # # Transformed for stability: Spotintensity # gamma spotshape.fct.polyhyp <- function(xcord, ycord, background, spotintensity, sigma, gamma, mux, muy, alpha) { # Transformations for stability gamma <- exp(gamma)+1 alpha <- exp(alpha) # Calculate euclidian diatance from spot centre distance <- sqrt((xcord-mux)**2+(ycord-muy)**2) distance <- distance/sigma xg <- (distance>=gamma) h <- exp(spotintensity + distance*alpha/gamma**2 + .5*alpha*(1/(gamma-1)**2 - 1/gamma**2 )*distance^2 - alpha/(gamma-distance)) h <- h/(sigma**2) h[xg] <- 0 return(background + h) } polyhypInit <- function(mCall, LHS, data) { # xy <- sortedXyData(mCall[["x"]], LHS, data) y <- eval(LHS, data, parent.frame()) xcord <- data$xcord ycord <- data$ycord # if(max(y)>1) { # stop("Spot intensities should be standardized before using polyhyp") # } value <- rep(0,7) # Set background value[1] <- quantile(y, probs=.15) # Set spotintensity to value[2] <- quantile(y, probs=.85)/value[1] # Set sigma to 6 value[3] <- 6 # Set gamma to 1.7 value[4] <- log(.7) # Set spot centres value[5] <- mean(eval(mCall[["xcord"]], data, parent.frame())) #+ rnorm(1, 0, .2) value[6] <- mean(eval(mCall[["ycord"]], data, parent.frame())) #+ rnorm(1, 0, .2) # Try to find the spot centre by the top from a 2nd degree polynomial xval <- 9:17 xxx <- y[ycord==13] xxx <- xxx[xval] res <- lm(xxx ~ xval + I(xval^2)) top <- -coef(res)[2]/(2*coef(res)[3]) value[5] <- (value[5] + top)/2 value[5] <- top xxx <- y[xcord==13] xxx <- xxx[xval] res <- lm(xxx ~ xval + I(xval^2)) top <- -coef(res)[2]/(2*coef(res)[3]) value[6] <- (value[5] + top)/2 value[6] <- top value[5] <- 14.6 value[6] <- 12.5 value[7] <- .7 # value[7] <- .2 value[7] <- log(value[7]) # # let optim run a bit to get optimized (and possible a bit more robust) starting values # wrap <- function(theta, y, xcord, ycord) { res <- -sum(-.5*( log(2*pi*exp(theta[8])) + 1/exp(theta[8])* (y-spotshape.fct.polyhyp(xcord, ycord, theta[1], theta[2], theta[3], theta[4], theta[5], theta[6], theta[7]))**2)) res } # # Now run optimize to get a good starting point # # 28/1-2004 Run optim a few times to select the best starting point by jiggling the starting values a little beststart <- value startvalue <- value bestres <- 1000000 for (i in 1:3) { opt <- optim(c(value,log(.2)), wrap, control=c(maxit=150), method="BFGS", # control=c(maxit=250), method="L-BFGS-B", lower=c(0, -Inf, 0, -Inf, 0, 0, 0, -Inf), upper=c(1, Inf, Inf, Inf, 25, 25, 2, Inf ), y=y, xcord=xcord, ycord=ycord) # Set the values to the optimized values value <- opt$par[-length(opt$par)] if (opt$value=cens] <- cens data } read.spot.file <- function(file, transformation=c("boxcox", "log", "none"), lambda1=10, lambda2=.2) { load(file) transformation <- match.arg(transformation) origy <- spot xx <- seq(1,nrow(origy),1)-((nrow(origy)+1)/2) distance <- sqrt(outer(xx**2,xx**2, "+")) M <- 2**16-1 y <- as.vector(origy) x <- as.vector(distance) x1 <- as.vector(distance) - 5 x1[x1<0] <- 0 x2 <- as.vector(distance) - 7 x2[x2<0] <- 0 x3 <- as.vector(distance) -9 x3[x3<0] <- 0 x4 <- as.vector(distance) -11 x4[x4<0] <- 0 xcord <- rep(1:nrow(origy), nrow(origy)) ycord <- rep(1:nrow(origy), rep(nrow(origy), nrow(origy))) y <- switch(transformation, boxcox = ((y+lambda1)**lambda2 -1)/((M+lambda1)**lambda2 -1), log = log(y+lambda1)/log(M+lambda1), none=y) data.frame(y, x, xcord, ycord) } plot.spot.model <- function(object, data) { # data <- (object$call)$data oldpar <- par() size <- sqrt(nrow(data)) layout(matrix(c(1, 2, 1, 3, 1, 4), ncol=3)) model.distance <- sqrt((data$xcord-(object$coefficients)["mux"])**2 + (data$ycord-(object$coefficients)["muy"])**2 ) plot(model.distance, data$y, xlab="Distance from spot center", ylab="Spot intensity", col="black", ylim=c(0, 1.1)) newx.2 <- order(model.distance) points(model.distance, data$newy, col="green") lines(model.distance[newx.2], predict(object)[newx.2], lwd=2, col="red", lty=1) abline(h=(object$coefficients)["background"], col="blue") abline(v=(object$coefficients)["sigma"], col="blue") abline(h=(object$coefficients)["background"] + exp((object$coefficients)["spotintensity"]), col="blue") m <- diag(size) m[cbind(data$xcord, data$ycord)] <- data$y my.estimate <<- spotshape.fct.polyhyp(data$xcord,data$ycord, object$coefficients["background"], object$coefficients["spotintensity"], object$coefficients["sigma"], object$coefficients["gamma"], object$coefficients["mux"], object$coefficients["muy"], object$coefficients["alpha"] ) attributes(my.estimate) <- NULL m2 <- diag(size) m2[cbind(data$xcord, data$ycord)] <- my.estimate contour(1:size, 1:size, m) par(mar=c(0,0,0,0) + .1) persp(1:size, 1:size, m, phi=35, theta=25, zlim=c(0, max(m, m2)), zlab="Intensity") persp(1:size, 1:size, m2, phi=35, theta=25, zlim=c(0, max(m, m2)), zlab="Intensity") par(oldpar) } originalParameters <- function(x) { if (length(x) != 7) stop("Wrong length for parameters") # The parameters are # # background, spotintensity, sigma, gamma, mux, muy, alpha # Spotintensity x[2] <- exp(x[2]) # Gamma x[4] <- exp(x[4])+1 # Gamma x[7] <- exp(x[7]) x } iterate.cond.exp <- function(obj, y, data, censoring.level=1) { cens <- (y>=censoring.level) obs <- !cens # imputy.cond.exp <- predict + sigma12 solve(sigma22) (x2-ksi2) } compareModels <- function(data, spotnum="Unknown ", gain="Unknown ") { # First we estimate the parameters without worrying about the censoring claus.2 <<- gnls(y ~ polyhyp(xcord, ycord, background, spotintensity, sigma, gamma, mux, muy, alpha), data=data, control=gnlsControl(maxIter=100), verbose=FALSE) # # Identify censored observations # cens.obs <- (data$y>=cens) # Vector of indicators for the censored nocens.obs <- (data$y