Working from the COVID.rmd model, fill in the blank chunks with code to analize the dataset of your choice. You will need to think about what you are copying and pasting and what needs to be updated to make the code relevant to your chosen dataset. Double check all syntax and be precise!

###First, let’s 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_data<-read_csv(file="breast_cancer_cells.csv")

## make some new columns that store the log ratios 
##MCF10A is a control and is non-tumorigenic, it can be used to compare against the different genes and their respective replicates

breast_cancer_data<-breast_cancer_data %>% mutate(
  log_MCF7=log2((MCF7_2+MCF7_1)/(MCF10A_2+MCF10A_1)),  
  log_MDA231=log2((MDA231_2+MDA231_1)/(MCF10A_2+MCF10A_1)),
  log_MDA468=log2((MDA468_2+MDA468_1)/(MCF10A_2+MCF10A_1)), 
  log_SKBR3=log2((SKBR3_2+SKBR3_1)/(MCF10A_2+MCF10A_1)))
## make a matrix of just the data
##selected for the columns that I made above 

breast_cancer_mat1<-breast_cancer_data %>% select(log_MCF7:log_SKBR3) %>% as.matrix() %>% round(.,2)
## make a heatmap from the data

pheatmap(breast_cancer_mat1, 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

## The heat map above of cell line SKBR3 looked different near the top than the others so I am choosing this to pursue further.
##I am taking my data and adding a column so I mutate.
##I named my column neglog_SKBR3 (because that is the cell line of interest)
##I am taking the negative log of pvalue which is called pvalue_SKBR3_vs_MCF10A in the data column

breast_cancer_data<-breast_cancer_data %>% mutate(neglog_SKBR3=-log10(pvalue_SKBR3_vs_MCF10A))


## Use ggplot to plot the log ratio a column (cell line or treatment) of interest against the -log p-value of that same column of interest

##To make the volcano plot I take the cell line of interest (SKBR3) on the x, against the -log p-value column which I called neglog_SKBR
##geom point is how bright dot is

volcano_plot<-breast_cancer_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 so they can be colored
##Contains complete argument for determining significance (didn't change anything besides adding the name of the cell line I am interested in--SKBR3)

breast_cancer_data<-breast_cancer_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("pink","gray","green")


## volcano plot as before with some added graph features 
##colour is based on what I coded above
##Changed title to be more representative

better_volcano_plot<-breast_cancer_data %>% 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=" Regulatory Genes in log of SKBR3 compared to control",y="-log(p-value)")

better_volcano_plot


##12 rows were removed because of parameters set
##Noticed: 4 significant down (pink) and 5 significant up (green)
## use ggplotly to see hover over the points to see the gene names
##plotly allows to hover over and see gene symbol

ggplotly(better_volcano_plot)
##(in a pdf plooty doesn't plot wedget correctly, so used a url)

Barplots of significant points of interest

## EXAMPLES OF A COUPLE PROTEINS or GENES

##Down regulated (pink): APOA1, HLA-A, MYO1B, HMGN5
##Up regulated (green): GCLC, FNDP1C, KRT23, DENND4C, TDP2

# Make a pivot longer table of the data for initial columns. Exclude the p-values and log columns
##I named this new table "breast_cancer_long", then condensed all of the columns of interest and stacked them (MCF10A_1 all the way to SKBR3_2) by using c(MCF10A_1:SKBR3_2). I named this column variable.
##I deleted all the columns after SKBR3_2 (pvalue_MCF7_vs_MCF10A to significance) by using "select"
##I also deleted the colum description and peptides by using select

breast_cancer_long<-pivot_longer(breast_cancer_data, cols = c(MCF10A_1:SKBR3_2), names_to = 'variable')%>% select(-c(pvalue_MCF7_vs_MCF10A:significance))%>%select(-Description) %>% select(-Peptides)

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


## based on the gene symbols from plotly, select a few proteins from the table. Select just the data and gene symbols from the pivot longer table you just made 

##Used column title Gene_Symbol to find the significantly down regulated genes detected from the plot above.

Examples_Down<-breast_cancer_long %>% filter(Gene_Symbol=="APOA1" | Gene_Symbol=="HLA-A" | Gene_Symbol=="MYO1B" | Gene_Symbol=="HMGN5") 

head(Examples_Down)
## # A tibble: 6 × 3
##   Gene_Symbol variable value
##   <chr>       <chr>    <dbl>
## 1 HLA-A       MCF10A_1  5.42
## 2 HLA-A       MCF10A_2  6.62
## 3 HLA-A       MCF7_1    2.22
## 4 HLA-A       MCF7_2    2.69
## 5 HLA-A       MDA231_1  4.56
## 6 HLA-A       MDA231_2  4.23

  
## make barplots facetted by Gene Symbol (if copying COVID example - change x=order to x = variable)

##x=variable since that is the column that contains what cell line it belongs to and y=value which is the number cooresponding to the cell line.
##changed colour to pink since that is the colour I choose for my down regulated genes.

Example_plot_down<-Examples_Down %>% 
  ggplot(aes(x=variable,y=value))+ 
  geom_bar(stat="identity",fill="pink")+
  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_down


##Shows the intensity of these genes throughout the different cell line.
## Same process for the upregulated ones

Examples_Up<-breast_cancer_long %>% filter(Gene_Symbol=="GCLC" | Gene_Symbol=="FNBP1L" | Gene_Symbol=="KRT23" | Gene_Symbol=="DENND4C" | Gene_Symbol=="TDP2")

head(Examples_Down)
## # A tibble: 6 × 3
##   Gene_Symbol variable value
##   <chr>       <chr>    <dbl>
## 1 HLA-A       MCF10A_1  5.42
## 2 HLA-A       MCF10A_2  6.62
## 3 HLA-A       MCF7_1    2.22
## 4 HLA-A       MCF7_2    2.69
## 5 HLA-A       MDA231_1  4.56
## 6 HLA-A       MDA231_2  4.23

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

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

 
downregulated_table<-breast_cancer_data %>% filter(Gene_Symbol=="APOA1" | Gene_Symbol=="HLA-A" | Gene_Symbol=="MYO1B" | Gene_Symbol=="HMGN5") %>% 
  select(Gene_Symbol:Peptides) 


head(downregulated_table)
## # A tibble: 6 × 3
##   Gene_Symbol Description                                                Pepti…¹
##   <chr>       <chr>                                                        <dbl>
## 1 HLA-A       HLA class I histocompatibility antigen, A-23 alpha chain …       3
## 2 HLA-A       HLA class I histocompatibility antigen, A-30 alpha chain …       1
## 3 HLA-A       HLA class I histocompatibility antigen, A-2 alpha chain O…      21
## 4 HMGN5       High mobility group nucleosome-binding domain-containing …       4
## 5 MYO1B       Isoform 2 of Myosin-Ib OS=Homo sapiens GN=MYO1B                 22
## 6 HLA-A       HLA class I histocompatibility antigen, A-1 alpha chain O…       1
## # … with abbreviated variable name ¹​Peptides


#Upregulated

upregulated_table<-breast_cancer_data %>% filter(Gene_Symbol=="GCLC" | Gene_Symbol=="FNBP1L" | Gene_Symbol=="KRT23" | Gene_Symbol=="DENND4C" | Gene_Symbol=="TDP2") %>% 
  select(Gene_Symbol:Peptides) 


head(upregulated_table)
## # A tibble: 5 × 3
##   Gene_Symbol Description                                                Pepti…¹
##   <chr>       <chr>                                                        <dbl>
## 1 TDP2        Isoform 2 of Tyrosyl-DNA phosphodiesterase 2 OS=Homo sapi…       3
## 2 DENND4C     Isoform 2 of DENN domain-containing protein 4C OS=Homo sa…       1
## 3 KRT23       Keratin, type I cytoskeletal 23 OS=Homo sapiens GN=KRT23 …       3
## 4 FNBP1L      Isoform 2 of Formin-binding protein 1-like OS=Homo sapien…       6
## 5 GCLC        Glutamate--cysteine ligase catalytic subunit OS=Homo sapi…       6
## # … with abbreviated variable name ¹​Peptides