####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 (breast cancer cell type comparisons)

breast_cancer_cells<-read_csv(file="breast_cancer_cells.csv")

## make some new columns that store the log ratios  

bcc_data<-breast_cancer_cells %>% 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))) %>% 
  arrange(-log_mcf7)

view(bcc_data)

## shows the log of each cell type/divided by the "normal" control cell MCF10A (fold change)
## make a matrix of just log values the data

bcc_mat_1<-bcc_data %>% select(log_mcf7:log_skbr3) %>% as.matrix() %>% round(.,2)

head(bcc_mat_1)
##      log_mcf7 log_mda231 log_mda468 log_skbr3
## [1,]    14.36      11.96      13.72     13.49
## [2,]    13.67      11.81      13.56     14.38
## [3,]    13.64      11.53      13.48     14.48
## [4,]    13.62      12.36      13.28     14.44
## [5,]    13.55      12.75      14.01     13.84
## [6,]    13.43       8.65      14.88     13.03
## make a heatmap from the data

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


# shows the heat map of the bcc_mat_1. Demonstrates that their are two proteins with a darker color in all cancer cell types (they are highly expressed in cancer cells compared to normal cells)

PLOTTING A VOLCANO

##BASIC VOLCANO PLOT

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

bcc_volcano_data<-bcc_data %>% mutate(neglog_skbr3=-log10(pvalue_SKBR3_vs_MCF10A))

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

volcano_plot<-bcc_volcano_data %>% ggplot(aes(x=log_skbr3,y=neglog_skbr3,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

bcc_better_volcano<-bcc_volcano_data %>% mutate(significance=ifelse((log_skbr3>2 & neglog_skbr3>2.99),"UP", ifelse((log_skbr3<c(-2) & neglog_skbr3>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<-bcc_better_volcano %>% ggplot(aes(x=log_skbr3,y=neglog_skbr3,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 SKBR3 proteins compared to control MCF10A",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: GCLC,FNBP1L,DENND4C, KRT23, TDP2 ## Significant down regulated are: APOA1, HLA-A, MYO1B, HMGN5

Barplots of significant points of interest

EXAMPLES OF A COUPLE PROTEINS or GENES

# Make a pivot longer table of the data 

bcc_long<-pivot_longer(bcc_better_volcano, cols = c(MCF10A_1:SKBR3_2), names_to = 'variable')%>% select(-c(pvalue_MCF7_vs_MCF10A:significance))

head(bcc_long)
## # A tibble: 6 × 5
##   Gene_Symbol Description                                 Pepti…¹ varia…²  value
##   <chr>       <chr>                                         <dbl> <chr>    <dbl>
## 1 CDC37L1     Hsp90 co-chaperone Cdc37-like 1 OS=Homo sa…       1 MCF10A…  0.001
## 2 CDC37L1     Hsp90 co-chaperone Cdc37-like 1 OS=Homo sa…       1 MCF10A…  0.001
## 3 CDC37L1     Hsp90 co-chaperone Cdc37-like 1 OS=Homo sa…       1 MCF7_1  23.2  
## 4 CDC37L1     Hsp90 co-chaperone Cdc37-like 1 OS=Homo sa…       1 MCF7_2  18.9  
## 5 CDC37L1     Hsp90 co-chaperone Cdc37-like 1 OS=Homo sa…       1 MDA231…  3.85 
## 6 CDC37L1     Hsp90 co-chaperone Cdc37-like 1 OS=Homo sa…       1 MDA231…  4.11 
## # … with abbreviated variable names ¹​Peptides, ²​variable

## 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

bcc_long$order <- as.character(bcc_long$variable)
bcc_long$order <- factor(bcc_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

  
bcc_down<-bcc_long %>% filter(Gene_Symbol=="GCLC" | Gene_Symbol=="TDP2" | Gene_Symbol=="KRT23") 

head(bcc_down)
## # A tibble: 6 × 6
##   Gene_Symbol Description                            Pepti…¹ varia…² value order
##   <chr>       <chr>                                    <dbl> <chr>   <dbl> <fct>
## 1 GCLC        Glutamate--cysteine ligase catalytic …       6 MCF10A…  3.01 MCF1…
## 2 GCLC        Glutamate--cysteine ligase catalytic …       6 MCF10A…  2.82 MCF1…
## 3 GCLC        Glutamate--cysteine ligase catalytic …       6 MCF7_1   9.28 MCF7…
## 4 GCLC        Glutamate--cysteine ligase catalytic …       6 MCF7_2   9.85 MCF7…
## 5 GCLC        Glutamate--cysteine ligase catalytic …       6 MDA231…  5.66 MDA2…
## 6 GCLC        Glutamate--cysteine ligase catalytic …       6 MDA231…  4.96 MDA2…
## # … with abbreviated variable names ¹​Peptides, ²​variable

## make barplots facetted by Gene Symbol (when working with other data sets - change x=order to x = variable)

bcc_plot_down<-bcc_down %>% 
  ggplot(aes(x=variable,y=value))+ 
  geom_bar(stat="identity",fill="red")+
  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")
  
bcc_plot_down

## Same process for the up-regulated ones

bcc_up<-bcc_long %>% filter(Gene_Symbol=="APOA1" | Gene_Symbol=="HLA-A" | Gene_Symbol=="MYO1B")

head(bcc_up)
## # A tibble: 6 × 6
##   Gene_Symbol Description                            Pepti…¹ varia…² value order
##   <chr>       <chr>                                    <dbl> <chr>   <dbl> <fct>
## 1 HLA-A       HLA class I histocompatibility antige…       1 MCF10A… 0.001 MCF1…
## 2 HLA-A       HLA class I histocompatibility antige…       1 MCF10A… 1.69  MCF1…
## 3 HLA-A       HLA class I histocompatibility antige…       1 MCF7_1  1.22  MCF7…
## 4 HLA-A       HLA class I histocompatibility antige…       1 MCF7_2  3.07  MCF7…
## 5 HLA-A       HLA class I histocompatibility antige…       1 MDA231… 1.43  MDA2…
## 6 HLA-A       HLA class I histocompatibility antige…       1 MDA231… 1.16  MDA2…
## # … with abbreviated variable names ¹​Peptides, ²​variable


bcc_plot_up<-bcc_up %>% 
  ggplot(aes(x=variable,y=value))+ 
  geom_bar(stat="identity",fill="blue")+
  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")
  
bcc_plot_up

BONUS CODE- for figuring out what the gene symbols are

#your original table contains descriptions of each gene. Write some code to create yourself a table to filter for your gene symbols of interest and select for the gene symbol, description, and peptides. Do this for both the significantly upregulated and downregulated points of interest.


#Downregulated

down_regulated<-bcc_data %>% 
  filter(Gene_Symbol=="GCLC" | Gene_Symbol=="TDP2" | Gene_Symbol=="KRT23") %>% 
  select(Gene_Symbol,Description,Peptides)

down_regulated
## # A tibble: 3 × 3
##   Gene_Symbol Description                                                Pepti…¹
##   <chr>       <chr>                                                        <dbl>
## 1 GCLC        Glutamate--cysteine ligase catalytic subunit OS=Homo sapi…       6
## 2 TDP2        Isoform 2 of Tyrosyl-DNA phosphodiesterase 2 OS=Homo sapi…       3
## 3 KRT23       Keratin, type I cytoskeletal 23 OS=Homo sapiens GN=KRT23 …       3
## # … with abbreviated variable name ¹​Peptides

#Upregulated

up_regulated<-bcc_data %>% 
  filter(Gene_Symbol=="APOA1" | Gene_Symbol=="HLA-A" | Gene_Symbol=="MYO1B") %>% 
  select(Gene_Symbol,Description,Peptides)

up_regulated
## # A tibble: 6 × 3
##   Gene_Symbol Description                                                Pepti…¹
##   <chr>       <chr>                                                        <dbl>
## 1 HLA-A       HLA class I histocompatibility antigen, A-30 alpha chain …       1
## 2 HLA-A       HLA class I histocompatibility antigen, A-23 alpha chain …       3
## 3 HLA-A       HLA class I histocompatibility antigen, A-2 alpha chain O…      21
## 4 MYO1B       Isoform 2 of Myosin-Ib OS=Homo sapiens GN=MYO1B                 22
## 5 APOA1       Apolipoprotein A-I OS=Homo sapiens GN=APOA1 PE=1 SV=1            2
## 6 HLA-A       HLA class I histocompatibility antigen, A-1 alpha chain O…       1
## # … with abbreviated variable name ¹​Peptides

##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!