---
title: "synapter report"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  html_document:
    mode: selfcontained
    toc: true
    toc_float:
      collapsed: false
      smooth_scroll: false
    fig_width: 10
    fig_height: 8
    fig_caption: true
    out_width: 2048px
    mathjax: null
---

```{r setup, echo=FALSE}
knitr::read_chunk(system.file("reports", "scripts", "synergise.R",
                              package="synapter"))
```

# Input {.tabset}

## Files

```{r input.files, echo=FALSE, results="asis"}
inputFiles <-
    rbind(c("Identification peptide:", basename(obj$IdentPeptideFile), nrow(obj$IdentPeptideData)),
          c("Quantitation peptide:",   basename(obj$QuantPeptideFile), nrow(obj$QuantPeptideData)),
          c("Quantitation Pep3D:",     basename(obj$QuantPep3DFile),   nrow(obj$QuantPep3DData)),
          c("Fasta file:",    basename(obj$DbFastaFile), ""))
colnames(inputFiles) <- c("", "File name", "Number of features")
knitr::kable(inputFiles, align="llr")
```

## General Parameters

```{r input.parameters, echo=FALSE, results="asis"}
parameters <-
    rbind(c("Master", master),
          c("Peptide FDR", fdr),
          c("Protein FPR", fpr),
          c("Peptide Length", peplen),
          c("Missed cleavages", missedCleavages),
          c("Isoleucin equals Leucin", IisL),
          c("Identification mass tolerance (ppm)", identppm),
          c("Quantitation mass tolerance (ppm)", quantppm),
          c("Loess span (RT modelling)", span.rt),
          c("Filtering unique peptides", as.character(uniquepep)))
colnames(parameters) <- c("Parameter name", "Value")
knitr::kable(parameters, align="lr")
```

## Grid Search Parameters

```{r input.gridparameters, echo=FALSE, results="asis"}
gridParameters <-
    rbind(c("Mass tolerance (ppm) start", grid.ppm.from),
          c("Mass tolerance (ppm) end", grid.ppm.to),
          c("Mass tolerance (ppm) by", grid.ppm.by),
          c("Retention time stdev start", grid.nsd.from),
          c("Retention time stdev end", grid.nsd.to),
          c("Retention time stdev by", grid.nsd.by),
          c("Feature proportion ", grid.subset),
          c("Number of features", grid.n),
          c("parameters selection", grid.param.sel))
colnames(gridParameters) <- c("Grid parameter", "Value")
knitr::kable(gridParameters, align="lr")
```

# Filtering

## Peptide False Discovery Rate

### Peptide Scores

```{r synergise.filtering.pepScores, echo=FALSE}
```

### Peptide Numbers

```{r synergise.filtering.pepNumbers, echo=FALSE}
```

### Peptide FDR

```{r synergise.filtering.fdr, echo=FALSE}
```

```{r synergise.filtering.data, echo=FALSE}
```

## Mass tolerance

```{r synergise.filtering.ppmErrorPre, echo=FALSE}
```

```{r synergise.filtering.ppmErrorQsPre, echo=FALSE, results="asis"}
```

```{r synergise.filtering.ppm, echo=FALSE}
```

```{r synergise.filtering.ppmErrorPost, echo=FALSE}
```

```{r synergise.filtering.ppmErrorQsPost, echo=FALSE, results="asis"}
```

```{r synergise.filtering.fpr, echo=FALSE}
```

```{r synergise.merge, echo=FALSE}
```

# Retention Time Modelling

## Retention Time Model

```{r synergise.rtmodel.plotRt, echo=FALSE}
```

```{r synergise.rtmodel.modelling, echo=FALSE}
```

```{r synergise.rtmodel.rtdiffs, echo=FALSE}
```

```{r synergise.rtmodel.rtdiffsTable, echo=FALSE, results="asis"}
```

## Feature Space

```{r rtmodel.plotAllFeatures, echo=FALSE}
plotFeatures(obj, what="all")
```

```{r synergise.rtmodel.plotSomeFeatures, echo=FALSE, dev="svg", fig.cap="Using nsd = 2 and ppm error = 10 as an example.<br />Optimal values are selected below."}
```

# Grid Optimisation

`r if(grid.n) { paste0("Using", grid.n, "features.") } else { paste0("Using ",
grid.subset * 100, "% of the data.") }`
```{r, grid.comment, echo=FALSE, eval=FALSE, results="hide"}
# abuse inline r here to show the progress bar of the gridsearch in the
# R console - regular r chunks capture stdout
```
`r if (grid.n) { searchGrid(obj, ppms=seq(grid.ppm.from, grid.ppm.to, grid.ppm.by), nsds=seq(grid.nsd.from, grid.nsd.to, grid.nsd.by), imdiffs=Inf, n=grid.n, verbose=verbose) } else { searchGrid(obj, ppms=seq(grid.ppm.from, grid.ppm.to, grid.ppm.by), nsds=seq(grid.nsd.from, grid.nsd.to, grid.nsd.by), imdiffs=Inf, subset=grid.subset, verbose=verbose) }`

plotGrid(obj, what="details")

## Grid Tables {.tabset}

```{r grid.table, echo=FALSE, results="asis"}
names <- c("total", "model", "details")
what <- c("total", "model", "details")
for (i in seq(along=names)){
  cat("\n####", names[i], "\n")
  cat("\n##### Image\n")
  plotGrid(obj, what=what[i])
  cat("\n\n##### Table\n")
  print(knitr::kable(as.data.frame(getGrid(obj)[[i]][,,1])))
  cat("\nBest Grid Value ", names[i], ": ", getBestGridValue(obj)[i], "\n", sep="")
  #cat("\n##### Best Grid Parameters\n")
  print(knitr::kable(as.data.frame(getBestGridParams(obj)[[i]])))
}
```

## Grid Details

```{r grid.details, echo=FALSE, results="asis"}
gridDetails <- do.call(rbind, getGridDetails(obj))
cond <- do.call(rbind, strsplit(rownames(gridDetails), ":"))[, 1:2] ## imdiff not supported by synergise1
colnames(cond) <- c("nsd", "ppm")
gridDetails <- cbind(cond, gridDetails)
gridDetails[] <- as.numeric(gridDetails)
rownames(gridDetails) <- NULL
knitr::kable(as.data.frame(gridDetails))
```

## Best Grid Parameters

```{r synergise.grid.bestParamaters, echo=FALSE, results="asis"}
```

Setting best grid parameters using '`r grid.param.sel`'

  - Mass tolerance (ppm): `r obj$PpmError`
  - Number of retention time stdev: `r obj$RtNsd`

# EMRT Matching

```{r synergise.emrt.find, echo=FALSE}
findEMRTs(obj)
rescueEMRTs(obj, mergedEMRTs)
plotEMRTtable(obj)
```

```{r synergise.emrt.table, echo=FALSE, results="asis"}
```

# Performance

## Enrichment
```{r synergise.performance.enrichment, echo=FALSE, results="asis"}
```

## Overlap
```{r synergise.performance.overlap, echo=FALSE, results="asis"}
```

## Source
```{r synergise.performance.source, echo=FALSE, results="asis"}
```

# Exported Files

```{r synergise.export, echo=FALSE}
```

- [Identification Peptides](IdentPeptides.csv)
- [Quantitation Peptides](QuantPeptides.csv)
- [Merged Peptides](MergedPeptides.csv)
- [Matched Peptides](MatchedPeptides.csv)
- [QuantPeptides.csv](QuantPeptides.csv)
- [Grid Details](GridDetails.txt)
- [Saved R Object (binary)](SynapterObject.rda)

# Full Analysis Log

```{r synergise.log, echo=FALSE, results="asis"}
```

# Session Info

```{r synergise.sessionInfo, echo=FALSE}
```
