Residual Disease Paper
========================================================
Quantifying Public PCR Data
--------------------------------------------------------

by Shelley Herbrich


```{r options, echo=FALSE}

opts_chunk$set(tidy=TRUE, message=FALSE, warning=FALSE)

```


## 1 Executive Summary

### 1.1 Introduction

In this report, we present the script used to generate the final PCR quantification summaries for ADH1B and FABP4.

### 1.2 Data & Methods

We use the "rawPCRData.RData" file  which contains the well-specific source, plate, randomized sample identifier, target gene, baseline-corrected fluorescence measurements for 40 cycles, and baseline coordiantes. For each sample, we have 2 to 3 technical replicates for both ADH1B and FABP4 as well as the control, 18S. Some wells were manually filtered for poor quality.  

We chose to use a "window-of-linearity" method, as introduced by Ramakers et al (2003), to quantify the PCR results from the baseline-corrected fluorescence measurements obtained from the vendor software. This method fits a linear model to the cycle number versus log fluorescence within a sliding window of defined size within a fixed border. The optimal model is that with the maximum log-linear range. Based on this model, the intercept corresponds to the initial template fluorescence and the slope is an estimate of the PCR efficiency. This algorithm is implemented in **sliwin** (part of the [qpcr](http://www.dr-spiess.de/) package). 

We require that the model not be fit within the baseline by forcing the lower bound of the border to be greater than the "take-off point" of exponential growth. Because the 18S fluorescence takes off almost immediately, we allow for a smaller window size to fit the optimal linear model.  

### 1.3 Results

We generate the ADH1B and FABP4 PCR values that correspond to the "PCRResults.RData" object.

## 2 Options and Libraries

We load the libraries we will use in this report.

```{r libraries, message=FALSE}
library(qpcR)
```

## 3 Loading Data

Next, we load the deidentified raw PCR data.

```{r loadRawPCRData}
load(file.path("RDataObjects","rawPCRData.RData"))
```

## 4 Quantifying PCR Data

We quantify the PCR measurements for FABP4 and ADH1B using the method described above and display the results.

```{r quantifyPCR,eval=TRUE,message=FALSE,results='hide'}
sampleID <- unique(rawPCRData$Sample.Name)
PCRquantifications <- data.frame(Source=rep("",length(sampleID)),
                         Plate=rep("",length(sampleID)),
                         Sample.Name=sampleID,
                         FABP4=rep(NA,length(sampleID)),
                         ADH1B=rep(NA,length(sampleID)),
                         stringsAsFactors=FALSE)

for(i1 in 1:length(sampleID)){
  tmp <- rawPCRData[which(rawPCRData$Sample.Name==sampleID[i1]),]
  tempNo <- array(NA,length(tmp$Well))
  for(i2 in 1:length(tmp$Well)){
    subDRn <- cbind(1:40, t(tmp[i2,grep("Cycle",colnames(tmp))]))
    m <- pcrfit(subDRn, cyc=1, fluo=2, model=l3)
    to <- tryCatch(takeoff(m)$top, error=function(e) NA)
    border <- c(0,0)
    if(to < tmp[i2,"Baseline.End"] & !is.na(to)) border <-c(tmp[i2,"Baseline.End"]-to,0)
    if(tmp$Target.Name[i2]=="18S"){
      sw <- tryCatch(sliwin(m,border=border,wsize=4:6,plot=FALSE), error=function(e) NULL)
    }else{
      sw <- tryCatch(sliwin(m,border=border,plot=FALSE), error=function(e) NULL) 
    }
    if(!is.null(sw)) tempNo[i2] <- unlist(sw["init"])
  }
  targetMeans <- sapply(split(tempNo,tmp$Target.Name),mean,na.rm=TRUE)
  PCRquantifications[i1,"Source"] <- as.character(tmp$Source[1])
  PCRquantifications[i1,"Plate"] <- tmp$Plate[1]
  PCRquantifications[i1,"FABP4"] <- log2(targetMeans["FABP4"])-log2(targetMeans["18S"])
  PCRquantifications[i1,"ADH1B"] <- log2(targetMeans["ADH1B"])-log2(targetMeans["18S"])
}
PCRquantifications <- PCRquantifications[order(PCRquantifications$FABP4,decreasing=TRUE),]
```

```{r printPCR}
head(PCRquantifications)
```

```{r plotPCR}
plot(PCRquantifications$FABP4,PCRquantifications$ADH1B,xlab="FABP4",ylab="ADH1B")
points(PCRquantifications$FABP4[which(PCRquantifications$Source=="MDACC")],PCRquantifications$ADH1B[which(PCRquantifications$Source=="MDACC")],pch="*",col="red")
legend("topleft",c("MDACC","Washington"),pch=c(8,1),col=c("red","black"),bty="n",cex=0.8)
abline(v=-20.05)
abline(a=-39.5,b=-1,lty=2)
```

## 5 Appendix

```{r getLocation}
getwd()
```

```{r sessionInfo}
sessionInfo()
```