---
title: treestructure analysis 
output: html_document
params:
  date: !r Sys.Date()
  treefn: sim.nwk
  minCladeSize: 10 
  minOverlap: -Inf
  nsim: 1000
  level: 0.01
  ncpu: 1
  verbosity: 1 
  debugLevel: 0 
  output: treestructure_output
...


Available options: 

```
     tre: A tree of type ape::phylo. Must be rooted and binary.

minCladeSize: All clusters within partition must have at least this
          many tips.

minOverlap: Threshold time overlap required to find splits in a clade

    nsim: Number of simulations for computing null distribution of test
          statistics

   level: Significance level for finding new split within a set of tips

    ncpu: If >1 will compute statistics in parallel using multiple CPUs

verbosity: If > 0 will print information about progress of the
          algorithm

debugLevel: If > 0 will produce additional data in return value

```

```{r, echo=FALSE}
print( t( as.data.frame( params, row.names='Parameter value' )) )
```



```{r, echo = FALSE}
suppressPackageStartupMessages ( library( treestructure )  )
tr <- read.tree( params$treefn )
ts <- trestruct( tr
 , minCladeSize = as.numeric( params$minCladeSize )
 , minOverlap = as.numeric( params$minOverlap )
 , nsim = as.numeric( params$nsim )
 , level = as.numeric( params$level )
 , ncpu = as.numeric( params$ncpu )
 , verbosity = as.numeric( params$verbosity )
 , debugLevel = as.numeric( params$debugLevel )
 )
```


```{r, echo = FALSE}

print(ts)

```


```{r, echo = FALSE}

dir.create( params$output ) 

for ( k in 1:length( ts$clusterSets ) ){
  x <- which( as.numeric(ts$clustering)==k)
  tr1 <- keep.tip( tr, x )
  write.tree( tr1, file = paste( sep='/' , params$output, paste0('cluster' , k , '.nwk'))  )
}

for ( k in 1:length( ts$partitionSets ) ){
  x <- which( as.numeric(ts$partition)==k)
  tr1 <- keep.tip( tr, x )
  write.tree( tr1, file = paste( sep='/' , params$output, paste0('partition' , k , '.nwk'))  )
}

saveRDS(ts,  paste(sep='/', params$output, 'treestructure.rds' ))

```


```{r, dpi = 300, fig.width=7, fig.height = 11, echo=FALSE }
suppressPackageStartupMessages( plot(ts ) )
```

Cluster & partition assigment: 
```{r, echo=FALSE}
print( as.data.frame( ts ))
```
