-
Notifications
You must be signed in to change notification settings - Fork 0
/
Section1_DataPreprocessing.Rmd
332 lines (277 loc) · 13.2 KB
/
Section1_DataPreprocessing.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
---
title: "Data preprocessing"
author: "Ye Bi"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# loading packages
```{r, message=F}
library(tidyverse)
library(lme4)
```
# 0. Grain-size phenotype data BLUE calculation
```{r}
grain_size <- read.csv("./dataset/raw_grain_size.csv")
traits.L = grain_size %>% group_split(treatment)
traits.con = traits.L[[1]]
traits.trt = traits.L[[2]]
colnames(traits.con)
BLUE_func <- function(traits, trait, treatment){
traits$NSFTV_ID <- as.factor(traits$NSFTV_ID)
traits$replication <- as.factor(traits$replication)
form = as.formula(paste0(trait,"~-1 + replication + NSFTV_ID"))
fit_fix = lm(form, data = traits)
# summary(fit_fix)
blue = fit_fix$coefficients
if(treatment == "control"){
blue1 = blue[-c(1:8)] #remove replications
}else{
blue1 = blue[-c(1:7)]
}
blue1 = c(NSFTV_ID1 = 0, blue1)
names(blue1) = gsub("NSFTV_ID", "", names(blue1))
blue.df = data.frame(treatment = treatment,
NSFTV_ID = names(blue1),
blue = blue1)
return(blue.df)
}
majoraxis0 = rbind.data.frame(BLUE_func(traits = traits.con,
trait = "C_MajorAxisLength",
treatment = "control"),
BLUE_func(traits = traits.trt,
trait = "C_MajorAxisLength",
treatment = "stress"))
majoraxis0 = majoraxis0[!majoraxis0$NSFTV_ID == "98", ]
minoraxis0 = rbind.data.frame(BLUE_func(traits = traits.con,
trait = "C_MinorAxisLength",
treatment = "control"),
BLUE_func(traits = traits.trt,
trait = "C_MinorAxisLength",
treatment = "stress"))
perimeter0 = rbind.data.frame(BLUE_func(traits = traits.con,
trait = "C_Perimeter",
treatment = "control"),
BLUE_func(traits = traits.trt,
trait = "C_Perimeter",
treatment = "stress"))
write.csv(majoraxis0, file = "grainlength.csv", quote = F, row.names = F)
write.csv(minoraxis0, file = "grainwidth.csv", quote = F, row.names = F)
write.csv(perimeter0, file = "grainperimeter.csv", quote = F, row.names = F)
```
# 1. Metabolite data cleaning.
- Keep "NSFTV.Id" "Treatment" "Ext.batch" "Run" and 73 Metabolites
- Remove NA, duplicated, no matched genotypes (NSFTV_ID).
```{r, eval=F}
met22 <- read_excel("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Pheno/raw_data/221019_metabolite raw data.xlsx")
met22_info <- read_excel("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Pheno/raw_data/221019_metabolite raw data.xlsx", sheet = "Sample information")
colnames(met22_info)
all(met22_info$Sample == met22$Sample)
met = cbind.data.frame(NSFTV.Id = met22_info$NSFTV.Id, Treatment = met22_info$Treatment, Ext.batch = met22_info$Ext.batch, Run = met22_info$Run, met22[,-1]) # "NSFTV.Id" "Treatment" "Ext.batch" "Run" and 73 Metabolites
colnames(met)[1] = "NSFTV_ID"
colnames(met)[3] = "Batch"
met = met[!is.na(met$NSFTV_ID),] # one NA in NSFTV_ID
met$NSFTV_Id= paste0("NSFTV_",met$NSFTV_ID)
met <- met %>% arrange(NSFTV_ID)
## Change met names into a1-h3.
name = c(paste0("a",1:10),paste0("b",1:10),paste0("c",1:10),paste0("d",1:10),paste0("e",1:10),paste0("f",1:10),paste0("g",1:10),paste0("h",1:3))
colnames(met)[5:77] = name
metL <- met %>% group_split(Treatment);names(metL) <- c("Control", "Stress")
#remove duplicated genotypes in met.stress (no dupicated genotypes in control group)
dd = duplicated(metL[[2]]$NSFTV_ID)
metL[[2]] = metL[[2]][!dd, ]
#remove the unused NSFTV_18. Since phenotype data in control group doesn't have it.
unuse = which(metL[[2]]$NSFTV_ID == "NSFTV_18")
metL[[2]] = metL[[2]][-unuse, ]
unuse1 = which(metL[[1]]$NSFTV_ID == "NSFTV_18")
metL[[1]] = metL[[1]][-unuse1, ]
save(metL, file="../metL_clean.RData")
```
# 2. Match genotype, phenotype, trait datasets.
```{r, eval = F}
setwd("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Trait/codes")
library(tidyverse)
###########################
# put three traits together
###########################
###Remove one genotype
majoraxis <- read.csv("../raw_data/grainlength.csv")
minoraxis <- read.csv("../raw_data/grainwidth.csv")
#NSFTV_98 just not show in minor axis and perimeter data set, so I decided to delete it.
minoraxis <- minoraxis[-which(minoraxis$NSFTV_ID == 98),]
perimeter <- read.csv("../raw_data/grainperimeter.csv")
perimeter <- perimeter[-which(perimeter$NSFTV_ID == 98),]
###Remove NA in NSFTV_ID
majoraxis=majoraxis[!is.na(majoraxis$NSFTV_ID),]
minoraxis=minoraxis[!is.na(minoraxis$NSFTV_ID),]
perimeter=perimeter[!is.na(perimeter$NSFTV_ID),]
table(is.na(majoraxis$NSFTV_ID))
table(is.na(minoraxis$NSFTV_ID))
table(is.na(perimeter$NSFTV_ID))
#Change names into more readable names.
majoraxis <- majoraxis[!is.na(majoraxis$NSFTV_ID),];colnames(majoraxis)[3]="MajorAxis"
minoraxis <- minoraxis[!is.na(minoraxis$NSFTV_ID),];colnames(minoraxis)[3]="MinorAxis"
perimeter <- perimeter[!is.na(perimeter$NSFTV_ID),];colnames(perimeter)[3]="Perimeter"
#Change NSFTV_ID from "number" into "NSFTV_ + number".
majoraxis$NSFTV_ID <- paste0("NSFTV_", majoraxis$NSFTV_ID)
minoraxis$NSFTV_ID <- paste0("NSFTV_", minoraxis$NSFTV_ID)
perimeter$NSFTV_ID <- paste0("NSFTV_", perimeter$NSFTV_ID)
#Split control and stress and save in one list.
majoraxisL <- majoraxis %>% group_split(treatment)
minoraxisL <- minoraxis %>% group_split(treatment)
perimeterL <- minoraxis %>% group_split(treatment)
#Merge three traits and save as csv file.
three_traits <- merge(merge(majoraxis,minoraxis),perimeter)
write.csv(three_traits, "../three_traits.csv", row.names = F, quote = F)
#Split traits into control and stress groups, then save.
three_traitsL <- three_traits %>% group_split(treatment)
three_traits_control <- three_traitsL[[1]];colnames(three_traits_control)[1]="Treatment"
three_traits_stress <- three_traitsL[[2]];colnames(three_traits_stress)[1]="Treatment"
write.csv(three_traits_control,"../three_traits_control.csv", row.names = F, quote = F)
write.csv(three_traits_stress, "../three_traits_stress.csv", row.names = F, quote = F)
######################################################
##Match phenotyped, genotyped, metabolited data set.
######################################################
# 1. Loading data
## 1.1 Read geno data.
geno_700K <- readRDS("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Geno/geno_final_700K.rds")
map <- readRDS("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Geno/mapinfo_final_700k.rds")
SNP_names <- map$V2
colnames(geno_700K) = SNP_names
geno_id <- rownames(geno_700K)
path.met = "../../Pheno"
path.trait = "../../Trait"
## 1.2 Read metabolite data
###this is list after cleaning duplicated and unuseful metabolites.
load(file=file.path(path.met, "metL_clean.RData")) #metL
## 1.3 Phenotype (traits) data
trait.con <- read.csv(file=file.path(path.trait, "three_traits_control.csv"))
trait.trt <- read.csv(file=file.path(path.trait, "three_traits_stress.csv"))
three_traitsL <- list(trait.con, trait.trt)
#function to find out overlap in control and stress
id_func <- function(dfL){
length(unique(dfL[[1]]$NSFTV_ID)) #234 #so group1 is control group.
length(unique(dfL[[2]]$NSFTV_ID)) #229
ix <- dfL[[1]]$NSFTV_ID %in% dfL[[2]]$NSFTV_ID #192 42
ids_c_t = dfL[[1]]$NSFTV_ID[ix]
ids_c_others = dfL[[1]]$NSFTV_ID[!ix]
table(dfL[[2]]$NSFTV_ID %in% dfL[[1]]$NSFTV_ID) #192 38
ix1 <- dfL[[2]]$NSFTV_ID %in% dfL[[1]]$NSFTV_ID
ids_t_others <- dfL[[2]]$NSFTV_ID[!ix1]
ids_c_t1 <- dfL[[2]]$NSFTV_ID[ix1]
ll <- list(ids_c_t=ids_c_t,ids_t_c=ids_c_t1, ids_c_others=ids_c_others, ids_t_others=ids_t_others)
return(ll)
}
three_traits_ll <- id_func(three_traitsL)
met_ll <- id_func(metL)
#function to find out overlap with genotype data.
id_match <- function(dfL){
ix2 <- dfL[[1]]$NSFTV_ID %in% geno_id
ids_c_g <- dfL[[1]]$NSFTV_ID[ix2]
ix3 <- dfL[[2]]$NSFTV_ID %in% geno_id
ids_t_g <- dfL[[2]]$NSFTV_ID [ix3]
ll <- list(ids_c_g = ids_c_g, ids_t_g = ids_t_g)
return(ll)
}
geno_trait<- id_match(three_traitsL) #overlap between metabolite and geno data.
met_geno_trait <- id_match(metL) #since all metabolites are phenotyped, so this is also overlap among met&geno&trait.
## 1.3 clean met and trait dataset.
met.con <- as.data.frame(metL[[1]])
met.trt <- as.data.frame(metL[[2]])
met.con <- met.con[met.con$NSFTV_ID %in% met_geno_trait$ids_c_g, ] #192*77 run batch
met.trt <- met.trt[met.trt$NSFTV_ID %in% met_geno_trait$ids_t_g, ] #188*77 run batch
trait.con <- trait.con[trait.con$NSFTV_ID %in% met_geno_trait$ids_c_g, ] #192*5
trait.trt <- trait.trt[trait.trt$NSFTV_ID %in% met_geno_trait$ids_t_g, ] #188*5
##change all "control" to "Control", "stress" to "Stress"
trait.con$Treatment = "Control"
trait.trt$Treatment = "Stress"
#save all the matched met and trait files.
save(met.con, file=file.path(path.met, "met.con.match.Rdata"))
save(met.trt, file=file.path(path.met, "met.trt.match.Rdata"))
save(trait.con, file = file.path(path.trait, "trait.con.Rdata"))
save(trait.trt, file = file.path(path.trait, "trait.trt.Rdata"))
```
# 3. Adjust metabolites
- Relative metabolite abundance was corrected for run and experimental batch effects by treating them as random, separately for the control and HNT conditions.
```{r, eval = F}
# control group
met.con = read.csv("../met.control.noNA.csv")
met.con = met.con %>% arrange(NSFTV_ID)
met.con <- met.con %>% mutate(across(c(Run, Batch, NSFTV_ID), as.factor))
# treated group
met.trt = read.csv("../met.stress.noNA.csv")
met.trt = met.trt %>% arrange(NSFTV_ID)
met.trt <- met.trt %>% mutate(across(c(Run, Batch, NSFTV_ID), as.factor))
correct_met_func <- function(x, n_met = 73, treatment){
# x = met.con
# n_met = 73
# treatment="control"
rr = matrix(NA, nrow = nrow(x), ncol = n_met); colnames(rr) = colnames(x)[-c(1:4)]
for (i in colnames(rr)){
# i="a1"
formu = as.formula(paste0(i, " ~ 1+(1|Run) + (1|Batch)"))
print(formu)
# fit <- lm(formula=formu, data=x)
# summary(fit)
fit1 = lmer(formula = formu, data = x)
summary(fit1)
rrr = residuals(fit1)
rr[,i] = rrr
}
met.rr = cbind.data.frame(x[,1:2], rr)
# treatment = "con"
write.csv(met.rr, paste0("../met.rr.",treatment, ".csv"), row.names = F, quote = F)
}
# correct_met_func(x=met.comb, n_met=73, treatment="comb")
#
correct_met_func(met.con, n_met = 73, treatment="con")
correct_met_func(met.trt, n_met = 73, treatment="trt")
```
# 4. Genotype data cleaning.
```{r, eval = F}
## Read into met.rr dataset to get NSFTV_ID in treatments.
met_rr.con <- read_csv("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Pheno/met.rr.con.csv")
met_rr.trt <- read_csv("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Pheno/met.rr.trt.csv")
id_con <- met_rr.con$NSFTV_ID
id_trt <- met_rr.trt$NSFTV_ID
## Read into raw genotype data.
geno_700k <- readRDS("geno_final_700K.rds") #SNPs
map <- readRDS("mapinfo_final_700k.rds") #map
SNP_names <- map$V2
colnames(geno_700k) = SNP_names
####################################
#### Control group #################
####################################
geno_con <- geno_700k[id_con,]
#MAF check
p <- colMeans(geno_con)/2
maf <- ifelse(p> 0.5, 1-p, p)
maf.index <- which(maf < 0.05)
geno_con <- geno_con[,-maf.index] # 192 385118
save(geno_con, file="geno.rr.con.RData")
####################################
#### Stress group #################
####################################
geno_trt <- geno_700k[id_trt,]
#MAF check
p <- colMeans(geno_trt)/2
maf <- ifelse(p> 0.5, 1-p, p)
maf.index <- which(maf < 0.05)
geno_trt <- geno_trt[,-maf.index] # 188 389854
save(geno_trt, file="geno.rr.trt.RData")
```
# 5. Subpopulation summary
```{r, eval = F}
panel <- read.delim("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Geno/raw_data/panel_info_clean.txt")
panel <- panel %>% filter(Sample.set == "RDP1")
panel$NSFTV_ID <- gsub("NSFTV","NSFTV_", panel$Other.accession.ID)
load("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Trait/trait.trt.Rdata")
load("~/Library/CloudStorage/OneDrive-VirginiaTech/Research/Codes/research/RiceUNLMetabolites/Trait/trait.con.Rdata")
trait <- rbind(trait.con, trait.trt)
lines <- unique(trait$NSFTV_ID)
our <- panel[panel$NSFTV_ID %in% lines, ]
#percentage of subpopulation
round(table(our$fastStructure.subpopulation.call)/219*100, digit=2)
```