---
title: "TimeScape vignette"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{TimeScape vignette}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

TimeScape is a visualization tool for temporal clonal evolution.

# Installation 

To install TimeScape, type the following commands in R:

```{r, eval=FALSE}
# try http:// if https:// URLs are not supported
source("https://bioconductor.org/biocLite.R")
biocLite("timescape")
```

# Examples 

Run the examples by: 

```{r, eval=FALSE}
example("timescape")
```

The following visualizations will appear in your browser (optimized for Chrome):

The first visualization is of the acute myeloid leukemia patient from Ding et al., 2012.
The second visualization is of the metastatic ovarian cancer patient 7 from McPherson and Roth et al., 2016.

```{r, echo=FALSE}
library(timescape)  
```
```{r, echo=FALSE}
# EXAMPLE 1 - Acute myeloid leukemia patient, Ding et al., 2012
# genotype tree edges
tree_edges <- read.csv(system.file("extdata", "AML_tree_edges.csv", package = "timescape"))
# clonal prevalences
clonal_prev <- read.csv(system.file("extdata", "AML_clonal_prev.csv", package = "timescape"))
# targeted mutations
mutations <- read.csv(system.file("extdata", "AML_mutations.csv", package = "timescape"))
# perturbations
perturbations <- data.frame( pert_name = c("Chemotherapy"), 
                             prev_tp = c("Diagnosis"))
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, perturbations = perturbations, height=260)
```


```{r, echo=FALSE}
# EXAMPLE 2 - Patient 7, McPherson and Roth et al., 2016
# genotype tree edges
tree_edges <- read.csv(system.file("extdata", "px7_tree_edges.csv", package = "timescape"))
# clonal prevalences
clonal_prev <- read.csv(system.file("extdata", "px7_clonal_prev.csv", package = "timescape"))
# clone colours
clone_colours <- data.frame(clone_id = c("A","B","C","D","E"), 
                            colour = c("d0ced0", "2CD0AB", "FFD94B", "FD8EE5", "F8766D"))
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, clone_colours = clone_colours, height=260, alpha=15)
```

# Required parameters 

The required parameters for TimeScape are as follows:

$clonal\_prev$ is a data frame consisting of clonal prevalences for each clone at each time point. The columns in this data frame are:

1. character() $timepoint$ - time point
2. character() $clone\_id$ - clone id
3. numeric() $clonal\_prev$ - clonal prevalence.

$tree\_edges$ is a data frame describing the edges of a rooted clonal phylogeny. The columns in this data frame are:

1. character() $source$ - source node id
2. character() $target$ - target node id.

# Optional parameters 

## Mutations 

$mutations$ is a data frame consisting of the mutations originating in each clone. The columns in this data frame are:

1. character() $chrom$ - chromosome number
2. numeric() $coord$ - coordinate of mutation on chromosome
3. character() $clone\_id$ - clone id
4. character() $timepoint$ - time point
5. numeric() $VAF$ - variant allele frequency of the mutation in the corresponding timepoint. 

If this parameter is provided, a mutation table will appear at the bottom of the view.

## Clone colours 

Clone colours may be changed using the $clone\_colours$ parameter, for instance, compare the default colours :

```{r, echo=FALSE}

# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, height=260, alpha=15)
```

with specified custom colours:

```{r, echo=FALSE}
# clone colours
clone_colours <- data.frame(clone_id = c("A","B","C","D","E"), 
                            colour = c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00"))
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, clone_colours = clone_colours, height=260, alpha=15)
```

## Alpha value

The alpha value of each colour may be tweaked in the $alpha$ parameter (a numeric value between [0, 100]). Compare alpha of 10: 
```{r, echo=FALSE}
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, height=260, alpha=10)
```

with the alpha value of 90:
```{r, echo=FALSE}
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, height=260, alpha=90)
```

## Titles 

The x-axis, y-axis and phylogeny titles may be changed using the $xaxis\_title$, $yaxis\_title$ and $phylogeny\_title$ parameters, which take in a character string.

Here are some custom titles:
```{r, echo=FALSE}
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, height=260, alpha=15, xaxis_title="My X Axis", yaxis_title="My Y Axis", phylogeny_title="My Phylogeny")
```

## Genotype position

The position of each genotype with respect to its ancestor can be altered. The “stack” layout is the default layout. It stacks genotypes one on top of another to clearly display genotype prevalences at each time point. The “space” layout uses the same stacking method while maintaining (where possible) a minimum amount of space between each genotype. The “centre” layout centers genotypes with respect to their ancestors. Here we'll see an example of each:

### Stacked
```{r, echo=FALSE}
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, height=260, alpha=15, genotype_position="stack")
```

### Spaced
```{r, echo=FALSE}
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, height=260, alpha=15, genotype_position="space")
```

### Centered
```{r, echo=FALSE}
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, height=260, alpha=15, genotype_position="centre")
```

## Perturbation events

Perturbation events may be added to the TimeScape using the $perturbations$ parameter. Adding perturbations will simply add a label along the x-axis where the perturbation occurs. The $perturbations$ parameter is a data frame consisting of the following columns:

1. character() $pert\_name$ - the perturbation name
2. character() $prev\_tp$ - the time point (as labelled in clonal prevalence data) BEFORE perturbation.

### Without perturbation

```{r, echo=FALSE}
# EXAMPLE 1 - Acute myeloid leukemia patient, Ding et al., 2012
# genotype tree edges
tree_edges <- read.csv(system.file("extdata", "AML_tree_edges.csv", package = "timescape"))
# clonal prevalences
clonal_prev <- read.csv(system.file("extdata", "AML_clonal_prev.csv", package = "timescape"))
# targeted mutations
mutations <- read.csv(system.file("extdata", "AML_mutations.csv", package = "timescape"))

# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, height=260)
```

### With perturbation

```{r, echo=FALSE}
# perturbations
perturbations <- data.frame( pert_name = c("Chemotherapy"), 
                             prev_tp = c("Diagnosis"))
# run timescape
timescape(clonal_prev = clonal_prev, tree_edges = tree_edges, perturbations = perturbations, height=260)
```

# Obtaining the data 

E-scape takes as input a clonal phylogeny and clonal prevalences per clone per sample.  At the time of submission many methods have been proposed for obtaining these values, and accurate estimation of these quantities is the focus of ongoing research.  We describe a method for estimating clonal phylogenies and clonal prevalence using PyClone (Roth et al., 2014; source code available at https://bitbucket.org/aroth85/pyclone/wiki/Home) and citup (Malikic et al., 2016; source code available at https://github.com/sfu-compbio/citup).  In brief, PyClone inputs are prepared by processing fastq files resulting from a targeted deep sequencing experiment.  Using samtools mpileup (http://samtools.sourceforge.net/mpileup.shtml), the number of nucleotides matching the reference and non-reference are counted for each targeted SNV.  Copy number is also required for each SNV.  We recommend inferring copy number from whole genome or whole exome sequencing of samples taken from the same anatomic location / timepoint as the samples to which targeted deep sequencing was applied.  Copy number can be inferred using Titan (Ha et al., 2014; source code available at https://github.com/gavinha/TitanCNA).  Sample specific SNV information is compiled into a set of TSV files, one per sample.  The tables includes mutation id, reference and variant read counts, normal copy number, and major and minor tumour copy number (see PyClone readme).  PyClone is run on these files using the `PyClone run_analysis_pipeline` subcommand, and produces the `tables/cluster.tsv` in the working directory.  Citup can be used to infer a clonal phylogeny and clone prevalences from the cellular prevalences produced by PyClone.  The `tables/cluster.tsv` file contains per sample, per SNV cluster estimates of cellular prevalence.  The table is reshaped into a TSV file of cellular prevalences with rows as clusters and columns as samples, and the `mean` of each cluster taken from `tables/cluster.tsv` for the values of the table.  The iterative version of citup is run on the table of cellular frequencies, producing an hdf5 output results file.  Within the hdf5 results, the `/results/optimal` can be used to identify the id of the optimal tree solution.  The clonal phylogeny as an adjacency list is then the `/trees/{tree_solution}/adjacency_list` entry and the clone frequencies are the `/trees/{tree_solution}/clone_freq` entry in the hdf5 file.  The adjacency list can be written as a TSV with the column names `source`, `target` to be input into E-scape, and the clone frequencies should be reshaped such that each row represents a clonal frequency in a specific sample for a specific clone, with the columns representing the time or space ID, the clone ID, and the clonal prevalence.

# Interactivity

Interactive components: 

1. Mouseover any clone to view its (i) clone ID and (ii) clonal prevalence at each time point. 
2. Click the view switch button to switch from the traditional timescape view to the clonal trajectory view, where each clone changes prevalence on its own track. 
3. Click the download buttons to download a PNG or SVG of the view. 
4. If a mutation table is present, click a clone in the phylogeny to filter the mutation table by the selected clone.

# Documentation 

To view the documentation for TimeScape, type the following command in R:

```{r, eval=FALSE}
?timescape
```

or:

```{r, eval=FALSE}
browseVignettes("timescape") 
```

# References

TimeScape was developed at the Shah Lab for Computational Cancer Biology at the BC Cancer Research Centre.

References:

Ding, Li, et al. "Clonal evolution in relapsed acute myeloid leukaemia revealed by whole-genome sequencing." Nature 481.7382 (2012): 506-510.

Ha, Gavin, et al. "TITAN: inference of copy number architectures in clonal cell populations from tumor whole-genome sequence data." Genome research 24.11 (2014): 1881-1893.

Malikic, Salem, et al. "Clonality inference in multiple tumor samples using phylogeny." Bioinformatics 31.9 (2015): 1349-1356.

McPherson, Andrew, et al. "Divergent modes of clonal spread and intraperitoneal mixing in high-grade serous ovarian cancer." Nature genetics (2016).

Roth, Andrew, et al. "PyClone: statistical inference of clonal population structure in cancer." Nature methods 11.4 (2014): 396-398.