---
title: "R graph libraries"
author: "Roland Barriot"
date: "December 8, 2017"
output: html_document
editor_options: 
  chunk_output_type: console
---

knitr options
```{r setup}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(fig.width=6)
knitr::opts_chunk$set(fig.height=2)
```

## ggtree

```{r}
# source("https://bioconductor.org/biocLite.R")
# biocLite("ggtree")
library(ggtree)
#library(ape)
```


Celui du cours
```{r}
t = read.newick('Tree_toy_example.nw')
t
```

Premier plot avec les paramètres par défaut
```{r}
ggtree(t)
```

N'affiche que l'arbre, sans les étiquettes ni autre.

**Ladderize** : affiche les noeuds en fonction du nombre de descendants dans leurs sous-arbres
```{r eval=FALSE}
ggtree(t, ladderize=FALSE)
```
Fait planter rstudio sur inagua...

**branch length** : sans
```{r}
ggtree(t, branch.length="none")
```


### tree layout

Par défaut `rectangular`, autres possibilités : `slanted`, `circular`, `fan`

```{r}
par(mfrow=c(2,2))
ggtree(t) + ggtitle("default: rectangular")
ggtree(t, layout="slanted") + ggtitle("slanted")
ggtree(t, layout="circular") + ggtitle("circular")
#ggtree(t, layout="fan") + ggtitle("fan")
```

**rooted vs. unrooted**
```{r}
ggtree(t, layout="unrooted") + ggtitle("unrooted layout")
```

**add scale**
```{r}
ggtree(t) + geom_treescale() + ggtitle("rectangular tree with branch lengths and scale")
```

```{r}
p =ggtree(t) + theme_tree2() + ggtitle("rectangular tree with branch lengths and scale")
p
```

**add labels**
```{r}
p + geom_tiplab(size=3, color="orange")
```

### iTOL

A télécharger sur https://itol.embl.de/itol.cgi

```{r fig.width=12, fig.height=12}
t = read.newick('iTOL.export.newick.txt')
ggtree(t, layout="circular") + ggtitle("iTOL") + geom_tiplab(aes(angle=angle))+ geom_treescale()
```

**add annotations**
```{r}
t$tip.label[1:10]
t$node.label[1:10]
```

Recherche des n° de sommets correspondants aux ancêtres des bactéries, eukaryotes, et archées :
```{r}
all=c(t$tip.label, t$node.label)
which(all=='Bacteria')
bacteria = which(all=='Bacteria')[1]
which(all=='Eukaryota')
eukaryota = which(all=='Eukaryota')[1]
which(all=='Archaea')
archaea = which(all=='Archaea')[1]
```

plot avec l'annotation
```{r fig.width=12, fig.height=12}
ggtree(t, layout="circular") + 
  ggtitle("iTOL") + 
  geom_tiplab(aes(angle=angle))  + 
  geom_hilight(node=bacteria, fill="steelblue", alpha=.1) + 
  geom_hilight(node=eukaryota, fill="pink", alpha=.1) + 
  geom_hilight(node=archaea, fill="green", alpha=.1) 
```


## igraph

