-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSection2_Correlation_analysis.Rmd
176 lines (155 loc) · 5.95 KB
/
Section2_Correlation_analysis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
---
title: "Section2 Correlation analysis"
author: "Ye Bi"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = "~/OneDrive - Virginia Tech/Research/Codes/research/RiceUNLMetabolites/Prediction/all_mets/HeatmapPCA")
path.met = "../../../Pheno"
path_trait = "../../../Trait"
```
# Loading packages
```{r, message=F, eval = F}
library(devtools)
library(ggbiplot)
library(pheatmap)
library(tidyverse)
library(ggplot2)
library(ggpubr)
library(data.table)
```
# 1. Read metabolite and traits data.
```{r, eval = F}
#change metabolite names
met_names <- read.delim("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Pheno/raw_data/met_names.txt", header=FALSE)
names <- met_names$V1
met.con <- read.csv(file=file.path(path.met, "met.rr.con.csv")) #192*75
colnames(met.con)[-c(1:2)] = names
met.trt <- read.csv(file=file.path(path.met, "met.rr.trt.csv")) #188*75
colnames(met.trt)[-c(1:2)] = names
load(file=file.path(path_trait, "trait.con.Rdata"))
load(file=file.path(path_trait, "trait.trt.Rdata"))
```
# 2. Heatmap among metabolites
```{r, eval=F}
#Control grgoup
cor_con = cor(met.con[,-c(1:2)])
g1 = melt(cor_con); colnames(g1) = c("Metabolite1", "Metabolite2", "value")
p1 = ggplot(g1, aes(x = Metabolite1, y = Metabolite2, fill=value)) +
geom_tile() +
coord_fixed() +
scale_fill_distiller(palette = 'PiYG', limits = c(-0.5,1)) +
ggtitle("(A)") +
xlab("Metabolite")+
ylab("Metabolite")+
theme(plot.title = element_text(size=18, face = "bold", hjust = 0),
axis.title.x = element_text(size = 16),
axis.title.y = element_text(size = 16),
axis.text.x = element_text(size = 6, angle = 90,hjust=0.95,vjust=0.2),
axis.text.y = element_text(size = 6))
print(p1)
dev.print(pdf, file="./heatmap_control_met.pdf", height = 10, width = 10)
#HNT stress group
cor_trt = cor(met.trt[,-c(1:2)])
g2 = melt(cor_trt) ; colnames(g2) = c("Metabolite1", "Metabolite2", "value")
p2 = ggplot(g2, aes(x = Metabolite1, y = Metabolite2, fill=value)) +
geom_tile() +
coord_fixed() +
scale_fill_distiller(palette = 'PiYG', limits = c(-0.5,1)) +
ggtitle("(B)")+
xlab("Metabolite")+
ylab("Metabolite")+
theme(plot.title = element_text(size=18, face = "bold", hjust = 0),
axis.title.x = element_text(size = 16),
axis.title.y = element_text(size = 16),
axis.text.x = element_text(size = 6, angle = 90,hjust=0.95,vjust=0.2),
axis.text.y = element_text(size = 6))
print(p2)
dev.print(pdf, file="./heatmap_stress_met.pdf", height = 10, width = 10)
# Ratio of trt to con
met.con.over <- met.con[met.con$NSFTV_ID %in% met.trt$NSFTV_ID, ]
met.trt.over <- met.trt[met.trt$NSFTV_ID %in% met.con$NSFTV_ID, ]
# all(met.con.over$NSFTV_ID == met.trt.over$NSFTV_ID)
cor_ratio = cor(met.con.over[,-c(1:2)]/met.trt.over[,-c(1:2)])
g4 = melt(cor_ratio) ; colnames(g4) = c("Metabolite1", "Metabolite2", "value")
p4 = ggplot(g4, aes(x = Metabolite1, y = Metabolite2, fill=value)) +
geom_tile() +
coord_fixed() +
scale_fill_distiller(palette = 'PiYG', limits=c(-0.8, 1)) +
ggtitle("")+
xlab("Metabolite")+
ylab("Metabolite")+
theme(plot.title = element_text(size=18, face = "bold", hjust = 0),
axis.title.x = element_text(size = 16),
axis.title.y = element_text(size = 16),
axis.text.x = element_text(size = 6, angle = 90,hjust=0.95,vjust=0.2),
axis.text.y = element_text(size = 6))
print(p4)
dev.print(pdf, file="./heatmap_ratio_met.pdf", height = 10, width = 10)
```
## 2.1 Summarize correlation coefficients
### 2.1.1 Control group
```{r, eval=F}
#Control group
summary(g1$value)
met_con_heatmap <- g1[abs(g1$value)>0.9 & abs(g1$value)<1, ]
dim(met_con_heatmap)
write.csv(met_con_heatmap, "./met_con_heatmap.csv", row.names = F, quote = F)
```
### 2.1.2 Stress group
```{r, eval=F}
met_trt_heatmap <- g2[abs(g2$value)>0.9 & abs(g2$value) <1, ]
dim(met_trt_heatmap)
write.csv(met_trt_heatmap, "./met_trt_heatmap.csv", row.names = F, quote = F)
```
### 2.1.3 Ratio of stress to control
```{r, eval=F}
met_ratio_heatmap <- g4[abs(g4$value) > 0.9 & abs(g4$value) <1, ]
dim(met_ratio_heatmap)
write.csv(met_ratio_heatmap, "./met_ratio_hatmap.csv", row.names = F, quote = F)
```
# 3. Heatmap for traits v.s. metabolites
```{r,eval=F}
heatmap_met_trait_func <- function(met, traits, treatment,tt){
# met <- met.con
# traits <- trait.con[,-c(1:2)]
met <- met[,-c(1:2)]
aa = cor(traits$MajorAxis, met)
bb = cor(traits$MinorAxis, met)
cc = cor(traits$Perimeter, met)
cor.trait.met = rbind(aa,bb,cc)
rownames(cor.trait.met) = c("Grain length", "Grain width", "Grain perimeter")
g4 = reshape2::melt(cor.trait.met) ; colnames(g4) = c("Phenotype", "Metabolite", "value")
p4 = ggplot(g4, aes(x = Phenotype, y = Metabolite, fill=value)) +
geom_tile() +
ggtitle(tt) +
scale_fill_distiller(palette = 'PiYG', limits = c(-0.41,0.41)) +
theme(plot.title = element_text(size=18, face = "bold", hjust = 0),
axis.title.x = element_text(size = 18),
axis.title.y = element_text(size = 18),
axis.text.x = element_text(size = 9),
axis.text.y = element_text(size = 9))
print(p4)
dev.print(pdf, file=paste0("./met.trait.heatmap_", treatment, ".pdf"), height=10,width=10)
return(p4)
}
g4_con <- heatmap_met_trait_func(met.con,trait.con,treatment = "Control", tt="(A)")
g4_trt <- heatmap_met_trait_func(met.trt,trait.trt,treatment = "Stress", tt="(B)")
```
## 3.1 sumamry correlation coefficients
### 3.1.1 Control group
```{r,eval=F}
summary(g4_con$value)
g4_con_heatmap <- g4_con[abs(g4_con$value)>0.3, ]
dim(g4_con_heatmap) #10
write.csv(g4_con_heatmap, "./trait_met_con_heatmap.csv", row.names = F, quote = F)
```
### 3.1.2 Stress group
```{r,eval=F}
summary(g4_trt$value)
g4_trt_haetmap <- g4_trt[abs(g4_trt$value)>0.3, ]
dim(g4_trt_haetmap) #2
write.csv(g4_trt_haetmap, "./trait_met_trt_heatmap.csv", row.names = F, quote = F)
```