---
# System prepended metadata

title: Projet du module « Graphe »

---

# Projet du module « Graphe »
## Master 2 Bio-Informatique, 2019/2020
*EVAN SEFFAR*

*Installation des packages & lancement des libraires*
```{r}
#install.packages("knitr")
#install.packages("rmarkdown")
#install.packages("markdown")
#install.packages("stringr")
#install.packages("tidyverse")
#install.packages("htmlwidgets")
#install.packages("reticulate")
#install.packages("cowplot")
#install.packages("igraph")
#install.packages("poweRlaw")
```
---
Chargement des libraires
```{r}
library(knitr)
library(rmarkdown)
library(markdown)
library(tidyverse)
library(stringr)
library(dplyr)
library(htmlwidgets)
library(ggplot2)
library(dplyr)
library(ggplot2)
```
---
Chemin d'accès des fichiers
```{r}
setwd("C:/Users/Evan Seffar/Desktop/Projet_Graphe_V2")
```
---
# PARTIE 1 

## Question 1
Chargement du fichier
```{r}
refPhageTSV <- read.delim("refPhages_and_VirSorter.tsv",header = TRUE)
```
Création d'une tibble ne contenant que les virus segmentés 
```{r}
virus_segment <- refPhageTSV %>% filter(str_detect(Definition,"segment"))
virus_segment_filtered <- virus_segment %>% group_by(Name) %>% 
  summarise_all(funs(if(. == GC) mean(.) else if(is.numeric(.)) sum(.) else str_c(unique(.),collapse="_")))
as.tibble(virus_segment_filtered)
```
Création d'une table filtrée ne contenant pas les virus segmentés 
```{r}
virus_no_segment_filtered <- filter(refPhageTSV, !Definition %in% c('segment'))
as.tibble(virus_no_segment_filtered)
```
Concaténation des deux tibbles
```{r}
Virus_Filtre1 <- rbind(virus_segment_filtered,virus_no_segment_filtered)
Virus_Filtre1
```
Dataframe contenant les virus filtrés 
```{r}
virus_filtered_prblm <- filter(Virus_Filtre1, nb_prot*10000 > Length)
virus_filtered_prblm
```

## Question 1.3 
Création d'une dataframe unique pour chaque genre de virus.
```{r}
Synechococcus_DF <- virus_filtered_prblm %>% filter(str_detect(genus,"Synechococcus"))
Clostridium_DF <- virus_filtered_prblm %>% filter(str_detect(genus,"Clostridium"))
Streptomyces_DF <- virus_filtered_prblm %>% filter(str_detect(genus,"Streptomyces"))
Lactococcus_DF <- virus_filtered_prblm %>% filter(str_detect(genus,"Lactococcus"))
Neisseria_DF <- virus_filtered_prblm %>% filter(str_detect(genus,"Neisseria"))
Shigella_DF <- virus_filtered_prblm %>% filter(str_detect(genus,"Shigella"))
Streptococcus_pyogenes_DF <- virus_filtered_prblm %>% filter(str_detect(species,"Streptococcus pyogenes"))
```
Commenter ICI
```{r}
Synechococcus_DF %>% select(1) %>% summarise_all(funs(n_distinct(.)))
Clostridium_DF %>% select(1) %>% summarise_all(funs(n_distinct(.)))
Streptomyces_DF %>% select(1) %>% summarise_all(funs(n_distinct(.)))
Lactococcus_DF %>% select(1) %>% summarise_all(funs(n_distinct(.)))
Neisseria_DF %>% select(1) %>% summarise_all(funs(n_distinct(.)))
Shigella_DF %>% select(1) %>% summarise_all(funs(n_distinct(.)))
```
---
### Creation des BoxPlots
GC Pourcent
```{r}
boxplot(Synechococcus_DF$GC,Clostridium_DF$GC,Streptomyces_DF$GC,Lactococcus_DF$GC,Neisseria_DF$GC,Shigella_DF$GC, col = c("blue","red","purple","orange","green","pink"),names = c("Synechococcus","Clostridium","Streptomyces","Lactococcus","Neisseria","Shigella"))
```
---
Taille des génomes
```{r}
boxplot(Synechococcus_DF$Length,Clostridium_DF$Length,Streptomyces_DF$Length,Streptomyces_DF$Length,Lactococcus_DF$Length,Neisseria_DF$Length,Shigella_DF$Length)
```
---
## Question 1.4
Export des dataframes en fichiers tsv pour exploiter les données en **Python**

```{r}
write.table(Synechococcus_DF, file='dataframe_Synechococcus_filtered.tsv', quote=FALSE, sep='\t',row.names = FALSE,col.names = FALSE)

write.table(Clostridium_DF, file='dataframe_Clostridium_filtered.tsv', quote=FALSE, sep='\t',row.names = FALSE,col.names = FALSE)

write.table(Streptomyces_DF, file='dataframe_Streptomyces_filtered.tsv', quote=FALSE, sep='\t',row.names = FALSE,col.names = FALSE)

write.table(Lactococcus_DF, file='dataframe_Lactococcus_filtered.tsv', quote=FALSE, sep='\t',row.names = FALSE,col.names = FALSE)

write.table(Neisseria_DF, file='dataframe_Neisseria_filtered.tsv', quote=FALSE, sep='\t',row.names = FALSE,col.names = FALSE)

write.table(Shigella_DF, file='dataframe_Shigella_filtered.tsv', quote=FALSE, sep='\t',row.names = FALSE,col.names = FALSE)
```
---
### Boucle Python générant les données des questions 1.4 / 2.1 / 3.1

```{python}
Code ython
```
---
Importation des fichiers de conservation
```{r}
conserv_Clostridium <- read.delim("Conserv_Clostridium.tsv",header = TRUE)
conserv_Synechococcus <- read.delim("Conserv_Synechococcus.tsv",header = TRUE)
conserv_Streptomyces <- read.delim("Conserv_Streptomyces.tsv",header = TRUE)
conserv_Lactococcus <- read.delim("Conserv_Lactococcus.tsv",header = TRUE)
conserv_Neisseria <- read.delim("Conserv_Neisseria.tsv",header = TRUE)
conserv_Shigella <- read.delim("Conserv_Shigella.tsv",header = TRUE)
```
---
#### Creations des histogrammes de conservation avec GGPLOT2

Genre Clostridium
```{r}
Clostri = ggplot(data=conserv_Clostridium, aes(conserv_Clostridium$Nb_Virus, y= conserv_Clostridium$Nb_Orthologue)) + 
          geom_bar(stat = "identity",color="black",fill="deeppink") + 
          ggtitle("Conservation du groupe de Virus infectant les bactéries du genre Clostridium") + 
          xlab("Occurence de Virus") + 
          ylab("Nombre de groupes orthologues") + 
          xlim(c(0,100))
          ylim(c(0,2200))
Clostri
```
---
Genre Synechococcus
```{r}
Syneco = ggplot(data=conserv_Synechococcus, aes(conserv_Synechococcus$Nb_Virus, y= conserv_Synechococcus$Nb_Orthologue)) + 
  geom_bar(stat = "identity",color="black",fill="cyan2") + 
  ggtitle("Conservation du groupe de Virus infectant les bact?ries du genre Synechococcus") + 
  xlab("Occurence de Virus") + 
  ylab("Nombre de groupes orthologues") + 
  xlim(c(0,100))
ylim(c(0,2200))
Syneco
```
---
Genre Streptomyces
```
Strepto = ggplot(data=conserv_Streptomyces, aes(conserv_Streptomyces$Nb_Virus, y= conserv_Streptomyces$Nb_Orthologue)) + 
  geom_bar(stat = "identity",color="black",fill="chartreuse2") + 
  ggtitle("Conservation du groupe de Virus infectant les bact?ries du genre Steptomyces") + 
  xlab("Occurence de Virus") + 
  ylab("Nombre de groupes orthologues") + 
  xlim(c(0,100))
ylim(c(0,2200))
Strepto
```
---
Genre Lactococcus
```{r}
Lacto = ggplot(data=conserv_Lactococcus, aes(conserv_Lactococcus$Nb_Virus, y= conserv_Lactococcus$Nb_Orthologue)) + 
  geom_bar(stat = "identity",color="black",fill="purple") + 
  ggtitle("Conservation du groupe de Virus infectant les bact?ries du genre Lactococcus") + 
  xlab("Occurence de Virus") + 
  ylab("Nombre de groupes orthologues") + 
  xlim(c(0,100))
ylim(c(0,2200))
Lacto
```
---
Genre Neisseria
```{r}
Neiss = ggplot(data=conserv_Neisseria, aes(conserv_Neisseria$Nb_Virus, y= conserv_Neisseria$Nb_Orthologue)) + 
  geom_bar(stat = "identity",color="black",fill="orange") + 
  ggtitle("Conservation du groupe de Virus infectant les bact?ries du genre Neisseria") + 
  xlab("Occurence de Virus") + 
  ylab("Nombre de groupes orthologues") + 
  xlim(c(0,100))
ylim(c(0,2200))
Neiss
```
---
Genre Shigella
```{r}
Shig = ggplot(data=conserv_Shigella, aes(conserv_Shigella$Nb_Virus, y= conserv_Shigella$Nb_Orthologue)) + 
  geom_bar(stat = "identity",color="black",fill="firebrick3") + 
  ggtitle("Conservation du groupe de Virus infectant les bact?ries du genre Shigella") + 
  xlab("Occurence de Virus") + 
  ylab("Nombre de groupes orthologues") + 
  xlim(c(0,100))
ylim(c(0,2200))
Shig
```
---
### Creation du tableau contenant les 6 histogrammes
Chargement d'une librairie dédiée

```{r}
library(cowplot)
```
Afficher les 6 histogrammes
```{r}
plot_grid(Clostri,Syneco,Strepto,Lacto,Neiss,Shig, labels = c("Clostridium","Synechococcus","Streptomyces","Lactococcus","Neisseria","Shigella"),ncol = 3, nrow = 2)
```

Courbes de conservations des groupes orthologues pour 6 groupes de virus
```{r}
ggplot()+
  geom_line(data=conserv_Clostridium, aes(conserv_Clostridium$Nb_Virus, y= conserv_Clostridium$Nb_Orthologue), color = 'deeppink') +
  geom_line(data=conserv_Synechococcus, aes(conserv_Synechococcus$Nb_Virus, y= conserv_Synechococcus$Nb_Orthologue), color = 'cyan2') +
  geom_line(data=conserv_Streptomyces, aes(conserv_Streptomyces$Nb_Virus, y= conserv_Streptomyces$Nb_Orthologue), color = 'chartreuse2') +
  geom_line(data=conserv_Lactococcus, aes(conserv_Lactococcus$Nb_Virus, y= conserv_Lactococcus$Nb_Orthologue), color = 'purple') +
  geom_line(data=conserv_Neisseria, aes(conserv_Neisseria$Nb_Virus, y= conserv_Neisseria$Nb_Orthologue), color = 'orange') +
  geom_line(data=conserv_Shigella, aes(conserv_Shigella$Nb_Virus, y= conserv_Shigella$Nb_Orthologue), color = 'firebrick3') +
  ggtitle("Courbes de conservation des groupes orthologues pour plusieurs groupe de virus") +
  xlab("Occurence de virus") +
  ylab("Nombre de groupes orthologues") + 
  xlim(c(0,300)) +
  ylim(c(0,50)) +
  scale_color_discrete(name = "Genres", labels = c('Clostridium', "Synechococcus", "Streptomyces","Lactococcus", "Neisseria", "Shigella")) 
```
---
# PARTIE 2 

```{python}
```
## Question 2.1
Creation & Exportation d'une dataframe pour les virus infectant les bacteries contenant **Streptococcus pyogenes** dans espece
```{r}
write.table(Streptococcus_pyogenes_DF, file='dataframe_Streptococcus_pyogenes_filtered.tsv', quote=FALSE, sep='\t',row.names = FALSE,col.names = FALSE)
```
---
Code Python pour sortir le fichier contenant les couples de virus.
*L'essentiel du code se trouve dans la question 1.4 qui contient le code pour répondre aux questions 1.4 / 2.1 & 3.1* 
```{python}
Code Python
```
---
Importation du fichier virus_GpOrthoCommuns.tsv
```
my_dataframes <- read.delim("all_dataframes.tsv",header = TRUE)
couples_virus <- read.delim("virus_GpOrthoCommuns_v2.tsv",header = FALSE)
```
Chargement de la librairie iGraph
```{r}
library(igraph)
```
Afficher le graphe du réseau de virus infectant les bacteries contenant *Streptococcus pyogenes* dans espece
```{r}
graphe_virus=graph_from_data_frame(couples_virus, directed = FALSE, vertices = NULL)
plot(graphe_virus)
```
---
## Question 2.2.1
Densite du graphe
```{r}
edge_density(graphe_virus, loops = FALSE)
```
---
## Question 2.2.2
Diametre du graphe
```{r}
diameter(graphe_virus, directed = TRUE, unconnected = TRUE, weights = NULL)
```
---
## Question 2.2.3
Degre
```{r}
degree(graphe_virus, v = V(graphe_virus), mode = c("all", "out", "in", "total"),
       loops = TRUE, normalized = FALSE)
```
---
Degre d'intermediarite (betwenness)
```{r}
betweenness(graphe_virus, v = V(graphe_virus), directed = TRUE, weights = NULL,
            nobigint = TRUE, normalized = FALSE)
```
---
Degre de proximité (closeness)
```{r}
closeness(graphe_virus, vids = V(graphe_virus), mode = c("out", "in", "all",
                                           "total"), weights = NULL, normalized = FALSE)
```
---
## Question 2.2.4 Histogramme des degres
Chargement d'une librarie dédiée
```{r}
library("poweRlaw")
```
Création de l'histogramme
```{r}
graphe_virus.degree <- degree(graphe_virus)
graphe_virus.degree.histogram <- as.data.frame(table(graphe_virus.degree))

#On applique un log dessus : 
graphe_virus.degree.histogram[,1] <- as.numeric(graphe_virus.degree.histogram[,1])
```
Affichage de l'histogramme
```{r}
##Plot Degres Histogramme
hist(graphe_virus.degree,
     main="Histogramme du degre des noeuds",
     xlab="Degre des noeuds",
     ylab= "Frequence",
     xlim=c(9,10),
     col="antiquewhite1",
     freq=TRUE
)
```
---
## Question 2.2.5 
Graphe Directed Force & colorier en fonction de leur degré d'intermediarité
```{r}
force.directed <-layout_with_fr(graphe_virus)
#Graphe Directed Force
plot(graphe_virus,layout=force.directed)
betweenness(graphe_virus)
###PROBLEM HERE
plot(graphe_virus,vertex.color=(betweenness(graphe_virus)),vertex.label=NA,layout=force.directed)
```
---
## Question 2.2.6 
Colorier les noeuds en fonction de la centralite de degre
```{r}
plot(graphe_virus,vertex.color=degree(graphe_virus),vertex.label=NA,layout=force.directed)
```
---
## Question 2.2.7
```{r}
```
---
## Question 2.2.8
```{r}
count_components(graphe_virus)
```
---
## Question 2.2.9
```{r}
commu = cluster_label_prop(graphe_virus,weights = couples_virus$V3)
commu
plot(commu, graphe_virus, layout = force.directed, vertex.label = NA)
```
---
## Question 2.2.10
```{r}
clusterlouvain <- cluster_louvain(graphe_virus, weights = couples_virus$V3)
clusterlouvain
```
---
## Question 2.2.11
```{r}
commu2 = cluster_edge_betweenness(graphe_virus,weights=couples_virus$V3)
commu2
plot(commu2, graphe_virus, layout = force.directed, vertex.label = NA)
```
---
## Question 2.2.12
```{r}
plot(graphe_virus, vertex.label=NA,layout= force.directed, vertex.color=clusterlouvain$membership)
```
---
## Question 2.2.13
