---
title: "Figure 3A, 3B, 3C, 3D R Code"
author: "Seufert et al."
date: "1/13/2022"
output:
  pdf_document: default
  html_document: default
  word_document: default
---
Packages for figures 3A, 3B, and 3C
```{r}
# Load packages
require(RColorBrewer)
require(ggplot2)
require(ggfortify)
require(pheatmap)
require(reshape2)
require(gplots)
require(iheatmapr)
require(plyr)
require(ggdendro)
require(glmulti)
require(factoextra)
require(readr)
require(ggplot)
require(rJava)
require(dplyr)
require(tibble)
require(stringr)
require(FactoMineR)
```

Figure 3A
```{r}
setwd("~/Desktop/Amy Fatty Acid Data/FA Data And Analysis")
NIH_FA_SCKD <- read.csv("FAsSCKD.csv", header= T, blank.lines.skip = TRUE, as.is=)
rownames(NIH_FA_SCKD) <- as.character(NIH_FA_SCKD$X)
NIH_FA_SCKD$X <- NULL
NIH_FA_SCKD$names <- rownames(NIH_FA_SCKD)
head(NIH_FA_SCKD)
```

```{r}
pca_NIH_FA_SCKD <- scale(NIH_FA_SCKD[1:21])
pca_NIH_FA_SCKD[is.na(pca_NIH_FA_SCKD)] <- 0
pca_NIH_FA_SCKD <- as.data.frame(pca_NIH_FA_SCKD)
pca_NIH_FA_SCKD$diet <- NIH_FA_SCKD$Diet
pca_NIH_FA_SCKD$names <- NIH_FA_SCKD$names
pca_NIH_FA_SCKD$time <- NIH_FA_SCKD$Time
pca_NIH_FA_SCKD$time <- as.factor(pca_NIH_FA_SCKD$time)
head(pca_NIH_FA_SCKD)
```

#Add in confidence interval elipses
```{r}
groups <- as.factor(pca_NIH_FA_SCKD$diet)
groups <- droplevels(groups)
fviz_pca_ind(prcomp(pca_NIH_FA_SCKD[1:21]),
             col.ind = groups, # color by groups
             palette = c('#FC8008', '#807F7F'),
             addEllipses = TRUE, # Concentration ellipses
             ellipse.type = "confidence",
             ellipse.level=0.95,
             legend.title = "Diet",
             repel = TRUE,
             labels=FALSE,
             labelsize=8,
             title="",
             show.legend=FALSE,
              mean.point=FALSE,
             legend="none",
              label="none",
             pointsize=5,
             pointshape=19
) +
  theme(text = element_text(family="Arial", size = 24),
        axis.title = element_text(family="Arial", size = 24),
        axis.text = element_text(family="Arial", size = 24), 
        legend.text= element_text(family="Arial", size=24), 
        panel.grid.major=element_blank(), 
        panel.grid.minor=element_blank(), 
        axis.line=element_line()
)

```

Figure 3B
```{r}
setwd("~/Desktop/Amy Fatty Acid Data/TAG Data And Analysis")
NIH_TAG_SCKD <- read.csv("TAGsSCKD.csv", header= T, blank.lines.skip = TRUE, as.is=)
rownames(NIH_TAG_SCKD) <- as.character(NIH_TAG_SCKD$X)
NIH_TAG_SCKD$X <- NULL
NIH_TAG_SCKD$names <- rownames(NIH_TAG_SCKD)
head(NIH_TAG_SCKD)
```

```{r}
pca_NIH_TAG_SCKD <- scale(NIH_TAG_SCKD[1:86])
pca_NIH_TAG_SCKD[is.na(pca_NIH_TAG_SCKD)] <- 0
pca_NIH_TAG_SCKD <- as.data.frame(pca_NIH_TAG_SCKD)
pca_NIH_TAG_SCKD$diet <- NIH_TAG_SCKD$Diet
pca_NIH_TAG_SCKD$names <- NIH_TAG_SCKD$names
pca_NIH_TAG_SCKD$time <- NIH_TAG_SCKD$Time
pca_NIH_TAG_SCKD$time <- as.factor(pca_NIH_TAG_SCKD$time)
head(pca_NIH_TAG_SCKD)
```
#Add in confidence interval elipses
```{r}
# Draw ellipses with factoextra package

groups <- as.factor(pca_NIH_TAG_SCKD$diet)

groups <- droplevels(groups)
fviz_pca_ind(prcomp(pca_NIH_TAG_SCKD[1:86]),
             col.ind = groups, # color by groups
             palette = c('#FC8008', '#807F7F'),
             addEllipses = TRUE, # Concentration ellipses
             ellipse.type = "confidence",
             ellipse.level=0.95,
             legend.title = "Diet",
             repel = TRUE,
           labels=TRUE,
             labelsize=8,
             title="",
             show.legend=FALSE,
              mean.point=FALSE,
          legend="none",
           label="none",
             pointsize=5,
             pointshape=19
) +
  theme(text = element_text(family="Arial", size = 24),
        
        axis.title = element_text(family="Arial", size = 24),
        axis.text = element_text(family="Arial", size = 24), 
        legend.text= element_text(family="Arial", size=24), 
        panel.grid.major=element_blank(), 
        panel.grid.minor=element_blank(), 
        axis.line=element_line()
)

```

Figure 3C
```{r}
setwd("~/Desktop/Amy Fatty Acid Data/PC Data And Analysis")
NIH_PC_SCKD <- read.csv("PCsSCKD.csv", header= T, blank.lines.skip = TRUE, as.is=)
rownames(NIH_PC_SCKD) <- as.character(NIH_PC_SCKD$X)
NIH_PC_SCKD$X <- NULL
NIH_PC_SCKD$names <- rownames(NIH_PC_SCKD)
head(NIH_PC_SCKD)
```

```{r}
pca_NIH_PC_SCKD <- scale(NIH_PC_SCKD[1:65])
pca_NIH_PC_SCKD[is.na(pca_NIH_PC_SCKD)] <- 0
pca_NIH_PC_SCKD <- as.data.frame(pca_NIH_PC_SCKD)
pca_NIH_PC_SCKD$diet <- NIH_PC_SCKD$Diet
pca_NIH_PC_SCKD$names <- NIH_PC_SCKD$names
pca_NIH_PC_SCKD$time <- NIH_PC_SCKD$Time
pca_NIH_PC_SCKD$time <- as.factor(pca_NIH_PC_SCKD$time)
head(pca_NIH_PC_SCKD)
```
#Add in confidence interval elipses
```{r}
groups <- as.factor(pca_NIH_PC_SCKD$diet)
groups <- droplevels(groups)
fviz_pca_ind(prcomp(pca_NIH_PC_SCKD[1:65]),
             col.ind = groups, # color by groups
             palette = c('#FC8008', '#807F7F'),
             addEllipses = TRUE, # Concentration ellipses
             ellipse.type = "confidence",
             ellipse.level=0.95,
             legend.title = "Diet",
             repel = TRUE,
             labels=FALSE,
             labelsize=8,
             title="",
             show.legend=FALSE,
              mean.point=FALSE,
             label="none",
            legend="none",
             pointsize=5,
             pointshape=19
) +
  theme(text = element_text(family="Arial", size = 24),
        axis.title = element_text(family="Arial", size = 24),
        axis.text = element_text(family="Arial", size = 24), 
        legend.text= element_text(family="Arial", size=24), 
        panel.grid.major=element_blank(), 
        panel.grid.minor=element_blank(), 
        axis.line=element_line()
)

```

Figure 3D
```{r pressure, echo=FALSE}
library(pheatmap)
```

```{r}
data <- read.csv("/Users/thenapierlab/Desktop/Amy Fatty Acid Data/FA Data And Analysis/pHeatmapFAsSCKD.csv", header= T, blank.lines.skip = TRUE, as.is=)
rownames(data) <- as.character(data$X)
data$X <- NULL
data <- as.data.frame(data)

my_sample_col <- data.frame(Diet = factor(rep(c("KD", "SC"),each=3)))
row.names(my_sample_col) <- colnames(data)
my_sample_col$Diet = factor(my_sample_col$Diet, levels = c("KD", "SC"))
my_color = list(Diet = c(KD="#FC8008", SC="#807F7F"))



pheatmap(data, scale="row", color=colorRampPalette(c("blue", "white", "red"))(50), cellwidth=12, cellheight=12, treeheight_row=0, treeheight_col=0, annotation_col = my_sample_col, annotation_colors =my_color)

```