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_cells<-read_csv(file="breast_cancer_cells.csv")

## make some new columns that store the log ratios  
breast_cancer_cells_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)))

view(breast_cancer_cells_data)
## make a matrix of just the data
breast_cancer_cells_mat1<-breast_cancer_cells_data %>% select(MCF7_1:SKBR3_2) %>% as.matrix() %>% round(.,2)

head(breast_cancer_cells_mat1)
##      MCF7_1 MCF7_2 MDA231_1 MDA231_2 MDA468_1 MDA468_2 SKBR3_1 SKBR3_2
## [1,]   5.07   5.42    25.43    27.42     4.56     3.88    8.46    5.64
## [2,]   6.49   6.64     9.80    10.31     6.84     8.75   11.91   13.67
## [3,]  11.55  10.82    12.48    10.11     9.54     8.46    9.10    9.43
## [4,]  13.40  14.82    14.94    11.33     6.87     7.92    8.54    6.82
## [5,]  12.08   9.82    16.51    12.34    15.43     8.50    7.93    4.75
## [6,]   8.05   8.38     6.78     8.46     4.48     7.18   13.27   15.05
## make a heatmap from the data
pheatmap(breast_cancer_cells_mat1, color=pats_cols,cellwidth=30,cellheight=.03,cluster_cols=FALSE,cluster_rows=TRUE,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
breast_cancer_cells_data<-breast_cancer_cells_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
volcano_plot<-breast_cancer_cells_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
breast_cancer_cells_data<-breast_cancer_cells_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 graph features 
better_volcano_plot<-breast_cancer_cells_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="log ratio of cells in cancer patient(log_SKBR3)compared to control(neg_log_SKBR3)",y="-log(p-value)")
  

better_volcano_plot

## use ggplotly to see hover over the points to see the gene names
ggplotly(better_volcano_plot)

Barplots of significant points of interest

## EXAMPLES OF A COUPLE PROTEINS or GENES
## UP GCLC, FNBP1L
breast_cancer_cells_long <- breast_cancer_cells %>%
  pivot_longer(cols = c(MCF10A_1:SKBR3_2), names_to = 'variable') %>%
  select(-c(pvalue_MCF7_vs_MCF10A:pvalue_SKBR3_vs_MCF10A)) %>%
  mutate(order = factor(variable, levels = c("MCF10A_1", "MCF10A", "MCF7_1", "MCF7_2", "MDA231_1", "MDA231_2", "MDA468_1", "MDA468_2", "SKBR3_1", "SKBR3_2")))

breast_cancer_cells_long
## # A tibble: 51,440 × 6
##    Gene_Symbol Description                           Pepti…¹ varia…² value order
##    <chr>       <chr>                                   <dbl> <chr>   <dbl> <fct>
##  1 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 MCF10A…  9.54 MCF1…
##  2 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 MCF10A…  4.58 <NA> 
##  3 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 MCF7_1   5.07 MCF7…
##  4 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 MCF7_2   5.42 MCF7…
##  5 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 MDA231… 25.4  MDA2…
##  6 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 MDA231… 27.4  MDA2…
##  7 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 MDA468…  4.56 MDA4…
##  8 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 MDA468…  3.88 MDA4…
##  9 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 SKBR3_1  8.46 SKBR…
## 10 NES         Nestin OS=Homo sapiens GN=NES PE=1 S…       7 SKBR3_2  5.64 SKBR…
## # … with 51,430 more rows, and abbreviated variable names ¹​Peptides, ²​variable
         
examples_up<-breast_cancer_cells_long%>% filter(Gene_Symbol=="GCLC" | Gene_Symbol=="FNBP1L" | Gene_Symbol=="KRT23" | Gene_Symbol=="DENND4C" | Gene_Symbol=="TDP2")
  
  
examples_plot_up<-examples_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")
examples_plot_up



# Make a pivot longer table of the data for initial columns. Exclude the p-values and log columns
breast_cancer_cells_long<-pivot_longer(breast_cancer_cells_data, cols = c(MCF10A_1:SKBR3_2), names_to = 'variable')%>% select(-c(pvalue_SKBR3_vs_MCF10A:significance))

head(breast_cancer_cells_long)
## # A tibble: 6 × 8
##   Gene_Symbol Description          Pepti…¹ pvalu…² pvalu…³ pvalu…⁴ varia…⁵ value
##   <chr>       <chr>                  <dbl>   <dbl>   <dbl>   <dbl> <chr>   <dbl>
## 1 NES         Nestin OS=Homo sapi…       7   0.542  0.0185   0.374 MCF10A…  9.54
## 2 NES         Nestin OS=Homo sapi…       7   0.542  0.0185   0.374 MCF10A…  4.58
## 3 NES         Nestin OS=Homo sapi…       7   0.542  0.0185   0.374 MCF7_1   5.07
## 4 NES         Nestin OS=Homo sapi…       7   0.542  0.0185   0.374 MCF7_2   5.42
## 5 NES         Nestin OS=Homo sapi…       7   0.542  0.0185   0.374 MDA231… 25.4 
## 6 NES         Nestin OS=Homo sapi…       7   0.542  0.0185   0.374 MDA231… 27.4 
## # … with abbreviated variable names ¹​Peptides, ²​pvalue_MCF7_vs_MCF10A,
## #   ³​pvalue_MDA231_vs_MCF10A, ⁴​pvalue_MDA468_vs_MCF10A, ⁵​variable


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

  
## make barplots facetted by Gene Symbol (if copying COVID example - change x=order to x = variable)
## Same process for the upregulated ones
examples_down<-breast_cancer_cells_long%>% filter(Gene_Symbol=="APOA1" | Gene_Symbol=="HLA-A" | Gene_Symbol=="MYO1B" | Gene_Symbol=="HMGN5")
  
  
examples_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(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")
examples_plot_down

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

#Upregulated