####Run through this example and try to understand what is going on with the data

####So, lets load RMarkdown

library(rmarkdown)

set messages to FALSE on everything (prevents certain boring things from being shown in the results)

knitr::opts_chunk$set(echo = TRUE, message=FALSE,warning=FALSE,collapse = TRUE)

PACKAGES

library(reshape2)
library(ggplot2)
library(dplyr)
library(plotly)
library(viridis)
library(data.table)
library(pheatmap)
library(tidyverse)
library(ggthemes)
library(clipr)
library(tidyr)
library(Rcpp)

COLORS

mycolors<-c(viridis(15))
felix_cols<-mycolors[c(5,2)]
felix_4cols<-mycolors[c(15,10,8,2)]
plain_cols1<-c("blue","gray")
plain_cols2<-c("red","gray")

pats_cols<-colorRampPalette(c("#FDE725FF", "white","#440154FF"))(21)
leos_cols<-colorRampPalette(c("white","blue"))(10)

LOAD DATA AND MAKE AN OVERALL HEATMAP

## load the dataset 

cancer_data <- read.csv(file = "breast_cancer_cells.csv")

## make some new columns that store the log ratios  
cancer_data_logs <- cancer_data %>% 
  mutate(log_mcf7 = log2((MCF7_1 + MCF7_2) / (MCF10A_1 + MCF10A_2)),
         log_mda231 = log2((MDA231_1 + MDA231_2) / (MCF10A_1 + MCF10A_2)),
         log_mda468 = log2((MDA468_1 + MDA468_2) / (MCF10A_1 + MCF10A_2)),
         log_skbr3 = log2((SKBR3_1 + SKBR3_2) / (MCF10A_1 + MCF10A_2)))
view(cancer_data_logs)
## make a matrix of just log values the data

cancer_mat <- cancer_data_logs %>% 
  select(log_mcf7 : log_skbr3) %>% 
  as.matrix() %>% 
  round(. ,2)
head(cancer_mat)
##      log_mcf7 log_mda231 log_mda468 log_skbr3
## [1,]    -0.43       1.90      -0.74         0
## [2,]    -0.96      -0.35      -0.71         0
## [3,]     0.27       0.29      -0.04         0
## [4,]     0.88       0.78      -0.05         0
## [5,]     0.79       1.19       0.92         0
## [6,]    -0.79      -0.90      -1.28         0
## make a heatmap from the data

pheatmap(cancer_mat, color=pats_cols,cellwidth=30,cellheight=.03,cluster_cols=FALSE,cluster_rows=FALSE,legend=TRUE,fontsize = 7,scale="column")


# 

PLOTTING A VOLCANO

##BASIC VOLCANO PLOT

## Add a column that stores the negative log of the pvalue of interest

cancer_data_logs<-cancer_data_logs %>% mutate(neglog_mcf7=-log10(pvalue_MCF7_vs_MCF10A))

## Use ggplot to plot the log ratio at 24 hours against the -log p-value at 24 hours

volcano_plot<-cancer_data_logs %>% ggplot(aes(x=log_mcf7,y=neglog_mcf7,description=Gene_Symbol))+
  geom_point(alpha=0.7,color="royalblue")
volcano_plot


  
## BETTER VOLCANO PLOT

## Define the significant ones (by using ifelse and setting # parameters) so they can be colored
cancer_data_logs<-cancer_data_logs %>% mutate(significance=ifelse((log_mcf7>2 & neglog_mcf7>2.99),"UP", ifelse((log_mcf7<c(-2) & neglog_mcf7>2.99),"DOWN","NOT SIG")))

## Some standard colors
plain_cols3<-c("red","gray","blue")

## volcano plot as before with some added things

better_volcano_plot<-cancer_data_logs %>% ggplot(aes(x=log_mcf7,y=neglog_mcf7,description=Gene_Symbol,color=significance))+
  geom_point(alpha=0.7)+
  scale_color_manual(values=plain_cols3)+
  xlim(-6,6)+
  theme_bw()+
  theme(axis.text = element_text(colour = "black",size=14))+
  theme(text = element_text(size=14))+
  labs(x="log ratio of MCF7 compared to control",y="-log(p-value)")
better_volcano_plot

## use ggplotly to see hover over the points to see the gene names. Record these for the next step 

ggplotly(better_volcano_plot)
## Significant up regulated are: C17orf28, VAV2 and EPPK1 ## Significant down regulated are: ADCK3. APOA1 and CTSZ

Barplots of significant points of interest

EXAMPLES OF A COUPLE PROTEINS or GENES

# Make a pivot longer table of the data 
cancer_long<-pivot_longer(cancer_data_logs, cols = c(MCF10A_1:SKBR3_2), names_to = 'variable')%>% select(-c(pvalue_MCF7_vs_MCF10A:significance))%>%select(-Description)

head(cancer_long)
## # A tibble: 6 × 4
##   Gene_Symbol Peptides variable value
##   <chr>          <int> <chr>    <dbl>
## 1 NES                7 MCF10A_1  9.54
## 2 NES                7 MCF10A_2  4.58
## 3 NES                7 MCF7_1    5.07
## 4 NES                7 MCF7_2    5.42
## 5 NES                7 MDA231_1 25.4 
## 6 NES                7 MDA231_2 27.4

## Because ggplot will automatically plot alphabetically on the x-axis, we need to make sure that our timepoints will be in the proper order by adding a new column using the factor() function
#this should be excluded in other datasets where order is not needed

cancer_long$order <- as.character(cancer_long$variable)
cancer_long$order <- factor(cancer_long$order,levels=c('MCF10A_1','MCF10A_2','MCF7_1','MCF7_2','MDA231_1','MDA231_2','MDA468_1','MDA468_2','SKBR3_1','SKBR3_2'))


## based on the gene symbols from plotly, select a few proteins from the table. Select just the data and gene symbols and pivot longer
Examples_Down<-cancer_long %>% filter(Gene_Symbol=="CTSZ" | Gene_Symbol=="APOA1" | Gene_Symbol=="ADCK3") 

head(Examples_Down)
## # A tibble: 6 × 5
##   Gene_Symbol Peptides variable value order   
##   <chr>          <int> <chr>    <dbl> <fct>   
## 1 CTSZ               8 MCF10A_1 19.8  MCF10A_1
## 2 CTSZ               8 MCF10A_2 19.8  MCF10A_2
## 3 CTSZ               8 MCF7_1    0.83 MCF7_1  
## 4 CTSZ               8 MCF7_2    0.91 MCF7_2  
## 5 CTSZ               8 MDA231_1 11.2  MDA231_1
## 6 CTSZ               8 MDA231_2 15.4  MDA231_2
  
## make barplots facetted by Gene Symbol (when working with other data sets - change x=order to x = variable)
Example_plot_down<-Examples_Down %>% 
  ggplot(aes(x=variable,y=value))+ 
  geom_bar(stat="identity",fill="red")+
  facet_wrap(~Gene_Symbol)+
  theme_bw()+
  theme(axis.text = element_text(color = "black",size=10))+
  theme(text = element_text(size=14))+
  theme(axis.text.x = element_text(angle=45, hjust=1))+
  labs(x="sample",y="relative intensity")
Example_plot_down

## Same process for the up-regulated ones

Examples_Up<-cancer_long %>% filter(Gene_Symbol=="C17orf28" | Gene_Symbol=="VAV2" | Gene_Symbol == "EPPK1")

head(Examples_Up)
## # A tibble: 6 × 5
##   Gene_Symbol Peptides variable value order   
##   <chr>          <int> <chr>    <dbl> <fct>   
## 1 EPPK1              4 MCF10A_1  2.76 MCF10A_1
## 2 EPPK1              4 MCF10A_2  2.89 MCF10A_2
## 3 EPPK1              4 MCF7_1   22.4  MCF7_1  
## 4 EPPK1              4 MCF7_2   22.3  MCF7_2  
## 5 EPPK1              4 MDA231_1  1.9  MDA231_1
## 6 EPPK1              4 MDA231_2  1.76 MDA231_2

Example_plot_up<-Examples_Up %>% ggplot(aes(x=variable,y=value))+ 
  geom_bar(stat="identity",fill="royalblue")+
  facet_wrap(~Gene_Symbol)+
  theme_bw()+
  theme(axis.text = element_text(colour = "black",size=10))+
  theme(text = element_text(size=14))+
  theme(axis.text.x = element_text(angle=45, hjust=1))+
  labs(x="sample",y="relative intensity")
  
Example_plot_up

##now you can knit this and publish to save and share your code in your ELN. Make sure each person in your group has completed this.

##Annotate when you have trouble and reference which line of code you need help on ## good luck and have fun!

##Bonus- create table of significant up/down regulated (select gene symbol, description, and peptides)

sig_upregulated <- cancer_data_logs %>% 
  select(Gene_Symbol, Description, Peptides) %>% 
  filter(Gene_Symbol == "C17orf28" |
           Gene_Symbol == "EPPK1" |
           Gene_Symbol == "VAV2")
sig_upregulated
##   Gene_Symbol
## 1       EPPK1
## 2       EPPK1
## 3        VAV2
## 4    C17orf28
##                                                                        Description
## 1                       Uncharacterized protein OS=Homo sapiens GN=EPPK1 PE=4 SV=1
## 2                                     Epiplakin OS=Homo sapiens GN=EPPK1 PE=1 SV=2
## 3     Isoform 2 of Guanine nucleotide exchange factor VAV2 OS=Homo sapiens GN=VAV2
## 4  Isoform 2 of UPF0663 transmembrane protein C17orf28 OS=Homo sapiens GN=C17orf28
##   Peptides
## 1        4
## 2       71
## 3        4
## 4        3
sig_downregulated <- cancer_data_logs %>% 
  select(Gene_Symbol, Description, Peptides) %>% 
  filter(Gene_Symbol == "CTSZ" |
           Gene_Symbol == "APOA1" |
           Gene_Symbol == "ADCK3")
sig_downregulated
##   Gene_Symbol
## 1        CTSZ
## 2       ADCK3
## 3       APOA1
##                                                                                    Description
## 1                                                Cathepsin Z OS=Homo sapiens GN=CTSZ PE=1 SV=1
## 2  Isoform 3 of Chaperone activity of bc1 complex-like, mitochondrial OS=Homo sapiens GN=ADCK3
## 3                                        Apolipoprotein A-I OS=Homo sapiens GN=APOA1 PE=1 SV=1
##   Peptides
## 1        8
## 2        2
## 3        2