---
title: "REVIGO MDS of semantic similarity between nonredundant GO terms enriched in significantly differentially expressed genes"
author: "Maggi Brisbin"
date: "8/9/2018"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width=8, fig.height=5)
```

```{r loadLibraries,echo=FALSE, message=FALSE}

library("ggplot2") 
library("stringi")
library(tidyr)
library( scales )
library(ggrepel)
library("shiny")
library(plotly)

```

```{r revigo, echo=FALSE, message=FALSE}
revigo.names <- c("term_ID","description","frequency_%","plot_X","plot_Y","plot_size","value","uniqueness","dispensability");
revigo.data <- rbind(c("GO:0009611","response to wounding", 0.127,-2.088,-4.004, 4.212, 0.0000,0.897,0.000),
                     c("GO:0019646","aerobic electron transport chain", 0.042, 1.185, 6.346, 3.726, 1.0000,0.835,0.000),
                     c("GO:0023052","signaling", 6.765, 3.744, 5.649, 5.939, 1.0000,0.960,0.000),
                     c("GO:0051641","cellular localization", 2.041, 6.184, 0.408, 5.418, 0.0000,0.810,0.000),
                     c("GO:0065007","biological regulation",20.498, 4.473,-4.496, 6.420, 1.0000,0.966,0.000),
                     c("GO:0071824","protein-DNA complex subunit organization", 0.238, 1.769,-6.584, 4.485, 0.0000,0.804,0.000),
                     c("GO:0006487","protein N-linked glycosylation", 0.076,-5.511,-0.765, 3.992, 0.0000,0.848,0.025),
                     c("GO:0010109","regulation of photosynthesis", 0.008,-3.113, 6.366, 2.992, 1.0000,0.821,0.034),
                     c("GO:0006468","protein phosphorylation", 4.137,-6.655,-0.222, 5.725, 1.0000,0.826,0.054),
                     c("GO:0051716","cellular response to stimulus", 9.561,-3.483,-4.855, 6.089, 1.0000,0.831,0.059),
                     c("GO:0007154","cell communication", 7.219, 4.498, 6.936, 5.967, 1.0000,0.916,0.065),
                     c("GO:0019321","pentose metabolic process", 0.131,-0.768, 1.188, 4.227, 1.0000,0.857,0.109),
                     c("GO:0006793","phosphorus metabolic process",13.507,-5.763, 3.275, 6.239, 1.0000,0.893,0.115),
                     c("GO:0007034","vacuolar transport", 0.133, 5.746,-0.821, 4.231, 0.0000,0.837,0.262),
                     c("GO:0006413","translational initiation", 0.518,-6.262,-1.365, 4.823, 0.0000,0.860,0.268),
                     c("GO:0015833","peptide transport", 0.298, 6.461,-1.084, 4.582, 0.0000,0.818,0.283),
                     c("GO:0072511","divalent inorganic cation transport", 0.393, 6.739,-0.117, 4.702, 0.0000,0.814,0.292),
                     c("GO:0046903","secretion", 0.810, 5.668, 1.006, 5.017, 0.0000,0.779,0.316),
                     c("GO:0031323","regulation of cellular metabolic process",11.662,-3.033, 5.629, 6.175, 1.0000,0.757,0.322),
                     c("GO:0010033","response to organic substance", 0.900,-3.199,-4.592, 5.062, 0.0000,0.873,0.324),
                     c("GO:0016192","vesicle-mediated transport", 1.085, 6.849, 0.498, 5.144, 0.0000,0.814,0.327),
                     c("GO:1901605","alpha-amino acid metabolic process", 3.625,-2.270, 0.920, 5.668, 0.0000,0.823,0.329),
                     c("GO:0006508","proteolysis", 5.223,-6.657,-1.994, 5.826, 0.0000,0.897,0.335),
                     c("GO:0045184","establishment of protein localization", 2.407, 6.385, 1.099, 5.490, 0.0000,0.764,0.361),
                     c("GO:0043467","regulation of generation of precursor metabolites and energy", 0.030,-0.531, 6.701, 3.578, 1.0000,0.812,0.486),
                     c("GO:0044085","cellular component biogenesis", 4.355, 0.620,-6.879, 5.747, 0.0000,0.833,0.490),
                     c("GO:0006887","exocytosis", 0.210, 4.720, 1.113, 4.430, 0.0000,0.754,0.492),
                     c("GO:0015693","magnesium ion transport", 0.086, 6.515, 1.992, 4.044, 0.0000,0.829,0.527),
                     c("GO:0007010","cytoskeleton organization", 0.786, 1.251,-6.780, 5.004, 0.0000,0.805,0.563),
                     c("GO:0006333","chromatin assembly or disassembly", 0.120, 2.094,-6.525, 4.188, 0.0000,0.804,0.572),
                     c("GO:0006323","DNA packaging", 0.227, 0.977,-6.802, 4.465, 0.0000,0.811,0.587),
                     c("GO:0019222","regulation of metabolic process",11.942,-2.367, 6.182, 6.185, 1.0000,0.811,0.619),
                     c("GO:0042549","photosystem II stabilization", 0.005,-0.918, 6.559, 2.814, 1.0000,0.787,0.650),
                     c("GO:0006796","phosphate-containing compound metabolic process",13.110,-6.717, 2.076, 6.226, 1.0000,0.870,0.664),
                     c("GO:0046373","L-arabinose metabolic process", 0.044,-0.569, 0.759, 3.752, 1.0000,0.853,0.664),
                     c("GO:1901700","response to oxygen-containing compound", 0.503,-2.856,-4.504, 4.810, 0.0000,0.877,0.675),
                     c("GO:0050794","regulation of cellular process",18.840,-2.302, 6.469, 6.383, 1.0000,0.777,0.698));

one.data <- data.frame(revigo.data);
names(one.data) <- revigo.names;
one.data <- one.data [(one.data$plot_X != "null" & one.data$plot_Y != "null"), ];
one.data$plot_X <- as.numeric( as.character(one.data$plot_X) );
one.data$plot_Y <- as.numeric( as.character(one.data$plot_Y) );
one.data$plot_size <- as.numeric( as.character(one.data$plot_size) );
one.data$frequency <- as.numeric( as.character(one.data$frequency) );
one.data$uniqueness <- as.numeric( as.character(one.data$uniqueness) );
one.data$dispensability <- as.numeric( as.character(one.data$dispensability) )

one.data$value <- gsub('0', 'Down', one.data$value)
one.data$value <- gsub('1', 'Up', one.data$value)

ex <- one.data [ c(2:11,17,18, 22,30,34:36), ] 

p1 <- ggplot( data = one.data ) +
  geom_point( aes( plot_X, plot_Y, colour = value), alpha = I(0.6), size =7) +
  scale_colour_manual(values =c("#3B9AB2", "red"), labels= c("Down", "Up")) +
  geom_point( aes(plot_X, plot_Y), shape = 21, fill = "transparent", colour = I (alpha ("black", 0.6) ), size = 7) +   scale_size_area() + scale_size( range=c(5, 30)) + theme_bw() +
  geom_label_repel(data = ex, aes(plot_X, plot_Y, label = description), colour = I(alpha("black", 0.85)), size = 4, nudge_x = 0 , point.padding = 0.2, label.padding = 0.1)+ labs (y = "semantic space y", x = "semantic space x") +
  theme(legend.key = element_blank()) + theme(text = element_text(size=16)) + theme(legend.title=element_blank())
```
```{r, message= FALSE, echo=FALSE}
key = one.data$description

p1 <- ggplot( data = one.data, aes(key= key, x= plot_X, y= plot_Y, color = value) ) +
  geom_point(alpha = I(0.6), size =7) +
  scale_colour_manual(values =c("#3B9AB2", "red"), labels= c("Down", "Up")) +
  geom_point( aes(plot_X, plot_Y), shape = 21, fill = "transparent", colour = I (alpha ("black", 0.6) ), size = 7) +   scale_size_area() + scale_size( range=c(5, 30)) + theme_bw() + labs (y = "Axis 2", x = "Axis 1") +
  theme(legend.key = element_blank()) + theme(text = element_text(size=16)) + theme(legend.title=element_blank())
pp=plotly_build(p1)  
```

```{r, echo=FALSE}
ui <- fluidPage(
  plotlyOutput("plot"))
server <- function(input, output) {
  output$plot <-renderPlotly({
  style( pp, text= key, hoverinfo = "text")
})
}

shinyApp(ui, server)
```
