#REMOVE ME: mutDens_1reg.Rmd
#REMOVE ME: 8/3/2022
---
output:
  html_document:
    toc: true
    toc_depth: 1
    number_sections: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
library(knitr)
library(GenomicRanges)
library(funtimes) 
source('mutDens.R') 
# options must pre-exist.
options='optFiles/options_1reg.txt'
df <- read.delim(options,header=T,stringsAsFactors=FALSE) 
options <- split(df$Value,df$Key)
attach(options)
calcSrc.cmd<- paste0('calcSrc=c(',calcSrc,')') 
triCont.cmd<- paste0('triCont=c(',triCont,')') 
eval(parse(text=calcSrc.cmd)) 
eval(parse(text=triCont.cmd)) 
proj <- unlist(strsplit(gsub('.*/','',mutF),'.',fix=T))[1]
if (!dir.exists(proj)) {
  dir.create(proj)
}
```
---
title: "`r proj`: Mutational Density Spatial Trend"
date: "`r format(Sys.time())`"
---
<style type="text/css">
.main-container {
  max-width: 90%;
  margin-left: auto;
  margin-right: auto;
}
</style>
---

# Mutational class distribution (Whole-Genome & Focal Regions)
```{r classDistrib,echo=F, comment=F, warning=F, dependson=F, message=F,results='asis',fig.show='hold',out.width='25%'}
gnObj<- gnStr2Obj(gn) 
if (grepl('vcf$',mutF)) {
	wgRes <- classDistrib_wg(mutF,gnObj,triCont=triCont,chrCol=1,posCol=2,refCol=4,altCol=5) 
} else {
	wgRes <- classDistrib_wg(mutF,gnObj,triCont=triCont,chrCol=1,posCol=2,refCol=3,altCol=4) 
}
frRes <- classDistrib_fr(wgRes$mutS,pointsF,span=as.numeric(span))

if (triCont) {
	figProp='6%'
	cnt6.wg<- flatten6(wgRes$cnt2)
	cnt6.fr<- flatten6(frRes)
} else {
	figProp='30%'
	cnt6.wg<- wgRes$cnt2
	cnt6.fr<- frRes
}
plot_pieANDsym(cnt6.wg,title='Whole-genome')
plot_pieANDsym(cnt6.fr,title='Focal regions')
```

# Mutational density curves centered on input positions
```{r shape,echo=F, comment=F, warning=F, dependson=F, message=F,results='asis',fig.show='hold',out.width=figProp}
points0 <- read.delim(pointsF,as.is=T,head=T)
Gbg <- getBinsOut(points0,as.numeric(bsz),as.numeric(outerspan),as.numeric(inspan))
if (options$sbs=='SBS6') {
	SBSs <- c('C2A','C2G','C2T','T2A','T2C','T2G')
} else {
	SBSs <- gsub(' ','',unlist(strsplit(options$sbs,',')),fix=T)
}  
mutS<- wgRes$mutS #$mutS keeps all 6 sbs' subcategories, while mutS retains only those specified in input $sbs
mutS<- mutS[substr(names(mutS),1,3)%in%SBSs]
nsbs<- length( unique( substr(names(mutS),1,3) ) )
resShapeS <- resCmprS <- vector('list',length(mutS)) #length(SBSs)
names(resShapeS) <- names(resCmprS) <- names(mutS) 
for (trisbs in names(mutS) ) {
	sbs<- substr(trisbs,1,3) #C2T out of C2T_A*C
	mut <- mutS[[trisbs]]
	if (nrow(mut)>0) { #sbs%in%SBSs
		totMut <- as.vector(table(mut$strand)[c('+','-')])
		ll.res <- paramLocal(mut,Gbg,shapeModel)
		resX <- mutDensX(mut,points0,sbs,bsz=as.numeric(bsz),span=as.numeric(span),gFt=gFt,gn=gn,calcSrc=calcSrc)
		resShape <- mutDensShape(resX$mutCnt,ll.res$lamda2,ll.res$nbparam2,shapeModel=shapeModel,pTh=1e-5)
		if (all(is.na(resShape))) {
			bandIx <- NULL
		} else {
			bandIx <- resShape[,c(2:3,5:6)]
		}
		resCmpr <- mutDensCmpr(resX$mutDens,totMut)
		colnames(resShape) <- paste(trisbs,colnames(resShape),sep='.')
		colnames(resCmpr) <- paste(trisbs,colnames(resCmpr),sep='.')
		resShapeS[[trisbs]] <- resShape 
		resCmprS[[trisbs]] <- signif(resCmpr,3)
	  write.table(resX$mutCnt,paste0(proj,'/',trisbs,'.mutCnt.txt'))
  	write.table(resX$mutDens,paste0(proj,'/',trisbs,'.mutDens.txt'))
  	write.table(resShape,paste0(proj,'/',trisbs,'.resShape.txt'))
  	write.table(resCmpr,paste0(proj,'/',trisbs,'.resCmpr.txt'))
		if (gFt=='TSS') {
			lnames=c('coding','template') 
		} else {
			lnames=sbs2mut2(sbs)
		}
		plot2mutDens(resX$mutDens,totMut=totMut,lnames=lnames,bandIx=bandIx,title=gsub('2','>',trisbs),bsz=as.numeric(bsz),legend=TRUE)
	} else {
		plot.new()
	}
}
```

# Peaks/Dips detected from Mutational density curves
<font size="4">
*The super-table below may include one or multiple tables corresponding to the mutational class(es) designated in the option file. The column headers have a prefix indicating the specific mutational class (e.g., C2A).*

Each table has four rows and six columns. The first three rows tell if a central peak (Peak), left peak (PeakL), or a right peak (PeakR) is detected. The fourth row tells if a central dip (Dip) is detected. All six columns can be separated into one left block and one right block, each comprising three columns: p-values (out of Poisson or Negative Binomial distribution), left bin numbers, and right bin numbers. The three columns in the left block and the three columns in the right block correspond to the two density curves respectively ("L1" and "L2"), which correspond to the nominal mutation form and its reverse complementary form respectively.     
</font>
```{r showTable1,echo=F, comment=F, warning=F, dependson=F, message=F,results='asis'}
kable(resShapeS,format='markdown')
```

# Statistical tests of mutation densities (Wilcoxon and trend-test)
<font size="4">
*The super-table below may include one or multiple tables corresponding to the mutational class(es) designated in the option file. The column headers have a prefix indicating the specific mutational class (e.g., C2A).*

Each table has four rows and three columns. The first row shows  the mean difference of the density values in the two curves, the second row shows paired Wilcoxon rank test p-values between the density values in the two curves, and the third and fourth rows show the p-values out of the WAVK trend test from R package funtimes to detect any non-random spatial trend. The three columns refer to different ranges of the curves, namely whole (whole range), left (left half range), and right (right half range).   
</font>
```{r showTable2,echo=F, comment=F, warning=F, dependson=F, message=F,results='asis'}
kable(resCmprS,format='markdown')
detach(options)
``` 
