-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.RKHS_SSI_FeBio_YBC.R
169 lines (118 loc) · 3.73 KB
/
3.RKHS_SSI_FeBio_YBC.R
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
#!/usr/bin/env Rscript
#SBATCH --time=00:15:00
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=16G
#SBATCH --constraint=intel16|intel18
#SBATCH --array=1-200
rm(list=ls())
library(SFSI)
library(BGLR)
load('YBC_GWASAssistedGP.RData')
job <- as.integer(Sys.getenv("SLURM_ARRAY_TASK_ID", "1"))
JOBS <- expand.grid(rep=1:100, trait=1:2)
trait <- as.vector(JOBS[job,"trait"])
rep <- as.vector(JOBS[job,"rep"])
rownames(YBC_GWASAssistedGP$pheno) <- YBC_GWASAssistedGP$pheno$taxa
FeBio <- c("FeBio_MI18", "FeBio_MI19")
index <- which(colnames(YBC_GWASAssistedGP$pheno) %in% FeBio)
FeBio_traits <- YBC_GWASAssistedGP$pheno[,index]
Y <- FeBio_traits
M <- YBC_GWASAssistedGP$geno
M <- scale(M)/sqrt(ncol(M))
G <- tcrossprod(M)
D <- as.matrix(dist(M,method="euclidean"))^2 #euclidian distance
D <- D/mean(D)
index <- grep('YBC', rownames(Y))
G <- G[index,index]
D <- D[index,index]
Y <- Y[index,]
M <- M[index,]
trait_name <- colnames(Y)[trait]
y <- Y[,trait]
index <- which(!is.na(y))
G <- G[index,index]
D <- D[index,index]
y <- y[index]
M <- M[index,]
Y <- Y[index,]
n <- length(y)
set.seed(rep)
tst <- sample(1:n, 0.3*n)
trn <- seq_along(1:n)[-tst]
# Calculate variance components ratio using training data
yNA = y
yNA[tst] = NA
fm0 = fitBLUP(yNA,K=G)
theta = fm0$varE/fm0$varU
h2 = fm0$varU/(fm0$varU + fm0$varE)
b = fm0$b # intercept
# Obtain an 'optimal' lambda by repeating the CV several times
fm1 = SSI.CV(y,K=G,trn=trn,
nCV=10,name="5 5CV", nfolds = 10)
lambda = summary(fm1)$optCOR["lambda"]
# Fit the index with the obtained lambda
fm2 = SSI(y,K=G,trn=trn, tst=tst,lambda=lambda)
SSI = summary(fm2)$optCOR
## KA
h <- c(.02,1,5) # bandwidth kernels
KList<-list()
for(i in 1:length(h)){ # Avering kernel
KList[[i]]<-list(K=exp(-h[i]*D),model='RKHS')
}
fmKA<-BGLR(y=yNA,ETA=KList,
nIter=12000,burnIn=2000,
saveAt=paste0("KA_",trait_name,"_rep_",rep))
KA = cor(y[tst],fmKA$yHat[tst],
use = 'pairwise.complete.obs')
############ Models with QTLs
# Obtain an 'optimal' lambda by repeating the CV several times
index <- which(colnames(YBC_GWASAssistedGP$pheno) %in% FeBio)
FeBio_traits <- YBC_GWASAssistedGP$pheno[,index]
Y = FeBio_traits
M = YBC_GWASAssistedGP$geno
indexQTL <- which(colnames(M) %in%
c('S07_29169848')) ## QTl for yield
M_new <- M[,-indexQTL]
QTL <- as.matrix(M[,indexQTL])
M_new=scale(M_new)/sqrt(ncol(M_new))
G_qtl = tcrossprod(M_new)
D_qtl<-as.matrix(dist(M_new,method="euclidean"))^2 #euclidian distance
D_qtl<-D_qtl/mean(D_qtl)
index <- grep('YBC', rownames(Y))
G_qtl <- G_qtl[index,index]
D_qtl <- D_qtl[index,index]
M_new <- M_new[index,]
Y <- Y[index,]
QTL <- QTL[index,]
y <- Y[,trait]
index <- which(!is.na(y)) # remove missing values
G_qtl <- G_qtl[index,index]
D_qtl <- D_qtl[index,index]
QTL <- QTL[index]
y <- y[index]
#### SSI + QTL
fm4 = SSI.CV(y, b=QTL, K=G_qtl,trn=trn,
nCV=10,name="5 5CV", nfolds = 10)
lambda = summary(fm4)$optCOR["lambda"]
# Fit the index with the obtained lambda
fm5 = SSI(y,b=QTL,K=G_qtl,trn=trn, tst=tst,lambda=lambda)
SSI_QTL = summary(fm5)$optCOR
### KA - QTL
## KA
h <- c(.02,1,5) # bandwidth kernels
KList <-list()
KList <- list(list(X=QTL,model="FIXED"))
for(i in 1:length(h)){
KList[[i+1]]<- list(K=exp(-h[i]*D_qtl),model='RKHS')
}
fmKA_qtl<-BGLR(y=yNA,ETA=KList,
nIter=12000,burnIn=2000,
saveAt=paste0("KA_qtl_",trait_name,"_rep_",rep))
KA_qtl = cor(y[tst],fmKA_qtl$yHat[tst],
use = 'pairwise.complete.obs')
results = c(SSI,SSI_QTL,KA,KA_qtl)
names(results) = c('SSI','SSI_MSE', 'SSI_df','SSI_lambda',
'SSI_qtl','SSI_MSE_qtl', 'SSI_df_qtl','SSI_lambda_qtl',
"KA", "KA_qtl")
save(results,
file=paste0("results_",trait_name,"_rep_",rep,".RData"))