---
title: "ifCNV report"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Results Table

```{r echo=FALSE}
library(data.table)
library(knitr)
library(DT)

DT::datatable(resTable, rownames = FALSE)
```

## Plots

```{r , echo=FALSE}


data <- readsMatrix
amp <- data[,1]
data <- data.frame(lapply(data[,-1],as.numeric))
data.norm <- apply(data, 2, function(x) x/median(x))

nam <- paste(do.call(rbind,strsplit(amp,split = "_"))[,1],do.call(rbind,strsplit(amp,split = "_"))[,2],sep="-")
tmp <- do.call(rbind,strsplit(amp,split = "_"))[,1]

vlines=NULL
for (i in 1:(length(tmp)-1)){
  if (tmp[i]!=tmp[i+1]){
    vlines = c(vlines,i)
  }
}

m.norm = rowMeans(data.norm[,CNVpos$normSamples])

soi = resTable$samples

for (i in unique(soi)){
  goi = resTable$RoI[resTable$samples==i]
  for (j in goi){
    cr = rep("black",length(nam))
    cr[abs(log2(as.numeric(data.norm[,i])/m.norm))>1] = "gray35"
    cr[grepl(j,nam)] = "red"
    plot(log2(as.numeric(data.norm[,i])/m.norm),xaxt='n',xlab=NA,ylab="log ratio",pch=20,main=paste(i, "-", j))
    axis(1, at=1:nrow(data), labels=nam,las=2,cex.axis=0.5) 
    abline(v=vlines,col='gray')
    points(log2(as.numeric(data.norm[,i])/m.norm),col=cr,pch=20,cex=1.01)
    abline(h=1,lty=2)
    abline(h=-1,lty=2)
  }
}


```

