-
Notifications
You must be signed in to change notification settings - Fork 0
/
RealTime_plot_FigsIndHeter.R
381 lines (348 loc) · 16 KB
/
RealTime_plot_FigsIndHeter.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
##############################################################################/
##############################################################################/
#Individual genetic diversity analyses
##############################################################################/
##############################################################################/
#loading the necessary packages and data sets
source("RealTime_load.R")
#loading and preparing the dataset
#you first need to run the 'RealTime_GENHET.R' script
HetVivMort<-read.table(file="data/Hetind_DoA.txt",sep="\t",
header=TRUE,colClasses="character")
HetVivMortExp<-HetVivMort[HetVivMort$moda=="exp",]
#removing global
HetVivMortExp<-HetVivMortExp[HetVivMortExp$fam!="Global",]
HetVivMortExp$fam[HetVivMortExp$fam=="1"]<-"01"
HetVivMortExp$fam[HetVivMortExp$fam=="9"]<-"09"
HetVivMortLim<-HetVivMort[HetVivMort$moda=="low",]
#removing global
HetVivMortLim<-HetVivMortLim[HetVivMortLim$fam!="Global",]
HetVivMortLim$fam[HetVivMortLim$fam=="1"]<-"01"
HetVivMortLim$fam[HetVivMortLim$fam=="9"]<-"09"
##############################################################################/
#Figure 8: comparing Dead or Alive heterozygosities indices by family####
##############################################################################/
#plot of the PHt index for both natural and protected treatment by family
pdf(file="output/Figure_8_PHt.pdf",width=10,height=10)
op<-par(mfrow=c(2,1),mar=c(2,4,4,1))
#semi violin plot for the natural treatment
HetAli<-HetVivMortExp[HetVivMortExp$vivmor==1,]
HetDea<-HetVivMortExp[HetVivMortExp$vivmor==0,]
colovec<-c(brewer.pal(12,"Set3")[6:7],
brewer.pal(9,"Set1")[1:2])
vioplot(as.numeric(HetVivMortExp$PHt)~HetVivMortExp$fam,xaxt="n",yaxt="n",
col = c("transparent"),sep=":",las=1,border="transparent",
ylab="PHt",xlab="",ylim=c(0.07,0.35),frame.plot=FALSE,cex.main=2,
lineCol="transparent",rectCol="transparent",main="Natural exposure")
axis(1,lwd=2,at=c(1:15),labels=levels(as.factor(HetVivMortExp$fam)),font=2)
axis(2,lwd=2,las=1,font=2)
vioplot(as.numeric(HetDea$PHt)~HetDea$fam,plotCentre="line",
col=colovec[1],sep=":",las=1,side="left",frame.plot=FALSE,
add=TRUE)
stripchart(as.numeric(HetDea$PHt)~HetDea$fam,vertical=TRUE,
method="jitter",pch=21,add=TRUE,col=colovec[3],
at=c(1:15)-0.2,cex=0.8)
vioplot(as.numeric(HetAli$PHt)~HetAli$fam,plotCentre="line",
col=colovec[2],sep=":",las=1,side="right",frame.plot=FALSE,
add=TRUE)
stripchart(as.numeric(HetAli$PHt)~HetAli$fam,vertical=TRUE,
method="jitter",pch=21,add=TRUE,col=colovec[4],
at=c(1:15)+0.2,cex=0.8)
segments(c(1:15)-0.2,aggregate(as.numeric(HetDea$PHt),
list(HetDea$fam),FUN=mean)[,2],
c(1:15)+0.2,aggregate(as.numeric(HetAli$PHt),
list(HetAli$fam),FUN=mean)[,2],
col=grey(0.95,0.9),lwd=6)
points(x=c(1:15)-0.2,y=aggregate(as.numeric(HetDea$PHt),
list(HetDea$fam),FUN=mean)[,2],
pch=21,bg=colovec[3],cex=1.2)
points(x=c(1:15)+0.2,y=aggregate(as.numeric(HetAli$PHt),
list(HetAli$fam),FUN=mean)[,2],
pch=21,bg=colovec[4],cex=1.2)
box(bty="l",lwd=2)
legend(0.1,0.15,c("dead","alive"),fill=colovec[1:2],cex=1.5,
bty="n",x.intersp=0.7,y.intersp=0.9,xpd=TRUE)
#semi violin plot for the protected exposure
HetAli<-HetVivMortLim[HetVivMortLim$vivmor==1,]
HetDea<-HetVivMortLim[HetVivMortLim$vivmor==0,]
colovec<-c(brewer.pal(12,"Set3")[6:7],
brewer.pal(9,"Set1")[1:2])
vioplot(as.numeric(HetVivMortLim$PHt)~HetVivMortLim$fam,xaxt="n",yaxt="n",
col = c("transparent"),sep=":",las=1,border="transparent",
ylab="PHt",xlab="",ylim=c(0.07,0.35),frame.plot=FALSE,
lineCol="transparent",rectCol="transparent",cex.main=2,
main="Protected exposure")
axis(1,lwd=2,at=c(1:15),labels=levels(as.factor(HetVivMortExp$fam)),font=2)
axis(2,lwd=2,las=1,font=2)
vioplot(as.numeric(HetDea$PHt)~HetDea$fam,plotCentre="line",
col=colovec[1],sep=":",las=1,side="left",frame.plot=FALSE,
add=TRUE)
stripchart(as.numeric(HetDea$PHt)~HetDea$fam,vertical=TRUE,
method="jitter",pch=21,add=TRUE,col=colovec[3],
at=c(1:15)-0.2,cex=0.8)
vioplot(as.numeric(HetAli$PHt)~HetAli$fam,plotCentre="line",
col=colovec[2],sep=":",las=1,side="right",frame.plot=FALSE,
add=TRUE)
stripchart(as.numeric(HetAli$PHt)~HetAli$fam,vertical=TRUE,
method="jitter",pch=21,add=TRUE,col=colovec[4],
at=c(1:15)+0.2,cex=0.8)
segments(c(1:15)-0.2,aggregate(as.numeric(HetDea$PHt),
list(HetDea$fam),FUN=mean)[,2],
c(1:15)+0.2,aggregate(as.numeric(HetAli$PHt),
list(HetAli$fam),FUN=mean)[,2],
col=grey(0.95,0.9),lwd=6)
points(x=c(1:15)-0.2,y=aggregate(as.numeric(HetDea$PHt),
list(HetDea$fam),FUN=mean)[,2],
pch=21,bg=colovec[3],cex=1.2)
points(x=c(1:15)+0.2,y=aggregate(as.numeric(HetAli$PHt),
list(HetAli$fam),FUN=mean)[,2],
pch=21,bg=colovec[4],cex=1.2)
box(bty="l",lwd=2)
par(op)
#export to .pdf 10 x 10 inches
dev.off()
##############################################################################/
#Figure S7/A17: Correlation between individual heterozygosity indices####
##############################################################################/
#a function to compute the absolute correlation between pairs of variables
panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- abs(cor(x, y))
txt <- format(c(r, 0.123456789), digits=digits)[1]
txt <- paste(prefix, txt, sep="")
if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * r)
}
#loading and preparing the dataset
#you first need to run the 'RealTime_GENHET.R' script
HetVivMort<-read.table(file="data/Hetind_DoA.txt",sep="\t",
header=TRUE)
HetVivMortGlob<-HetVivMort[HetVivMort$fam=="Global",]
#ploting the figure S7/A17
pdf(file="output/Figure_S7_corHeteroz.pdf",width=10,height=7)
pairs(HetVivMortGlob[,c(2:6)],las=1,cex.main=2,cex.cor=2.2,
main="Correlation between heterozygosity indices",
lower.panel=panel.smooth,upper.panel=panel.cor)
#export to pdf 10 x 7 inches
dev.off()
##############################################################################/
#Figure S14/A24: Plot of the distribution and probability curve of PHt####
##############################################################################/
#you first need to run the 'RealTime_GENHET.R' script
HetVivMort<-read.table(file="data/Hetind_DoA.txt",sep="\t",
header=TRUE)
HetVivMort$vivmor<-as.factor(HetVivMort$vivmor)
VivMortGlob<-HetVivMort[HetVivMort$fam=="Global",]
#plotting the Figure S14
pdf(file="output/Figure_S14_dist_PHt.pdf",width=10,height=6)
nf<-layout(matrix(c(1,2,2,2,2,2,3,3,
1,2,2,2,2,2,3,3),
2,8,byrow=TRUE))
op<-par(mar=c(5.1,0,4.1,0),lwd=1.5)
boxplot(VivMortGlob$PHt,horizontal=FALSE,boxwex=0.9,
las=1,notch=TRUE,frame=FALSE,axes=FALSE)
par(mar=c(5.1,5.1,4.1,1.1))
qqnorm(VivMortGlob$PHt,ylab="PHt",las=1,frame=TRUE,
cex.lab=2,cex.main=2.5)
qqline(VivMortGlob$PHt,col="red",lwd=2)
par(mar=c(5.1,0,4.1,1.1))
histPH<-hist(VivMortGlob$PHt,plot=FALSE)
barplot(histPH$counts,space=0,horiz=TRUE,
xlab="Counts",cex.lab=2)
par(op)
#export to .pdf 5 x 8 inches
dev.off()
##############################################################################/
#Figure S16/A26: Plot of the distribution of mortality by PHt classes####
##############################################################################/
#you first need to run the 'RealTime_GENHET.R' script
pdf(file="output/Figure_S16_mortaPHtclass.pdf",width=7,height=8)
colovec<-c(brewer.pal(12,"Set3")[6:7],
brewer.pal(9,"Set1")[1:2])
HetVivMort<-read.table(file="data/Hetind_DoA.txt",sep="\t",
header=TRUE)
HetVivMortFam<-HetVivMort[HetVivMort$fam!="Global",]
HetVivMortFam$catHet<-cut(HetVivMortFam$PHt,
breaks=c(0.07,0.22,0.24,0.26,0.28,0.30,0.32,0.36))
effectif<-colSums(table(HetVivMortFam$vivmor,HetVivMortFam$catHet))
freqMor<-proportions(table(HetVivMortFam$vivmor,HetVivMortFam$catHet),
margin=2)*100
temp<-barplot(freqMor,las=1,main="Mortality rate by PHt classes",
col=colovec[1:2],axes=FALSE,axisnames=FALSE,space=0.6)
axis(1,at=temp,labels=FALSE,lwd=3,font=2)
text(temp+0.3,par("usr")[1]-10,labels=names(effectif),srt=-60,
xpd=TRUE,cex=1,font=2)
axis(2,lwd=3,font=2,cex.axis=1.2,las=1)
box(bty="l",lwd=3)
text(temp,102,paste("n=",effectif,sep=""),font=3,cex=0.9,xpd=TRUE)
legend(-1,115,c("dead","alive"),fill=colovec[1:2],cex=1.3,
bty="n",x.intersp=0.5,y.intersp=0.7,xpd=TRUE)
#export to .pdf 5 x 8 inches
dev.off()
##############################################################################/
#Additional Figure: global data comparing Exposed and Limited treatments####
##############################################################################/
HetVivMort<-read.table(file="data/Hetind_DoA.txt",sep="\t",
header=TRUE)
#preparing the data set
HetVivMortGlob<-HetVivMort[HetVivMort$fam=="Global",]
#semi violin plot for the exposed treatment
HetAli<-HetVivMortGlob[HetVivMortGlob$vivmor==1,]
HetDea<-HetVivMortGlob[HetVivMortGlob$vivmor==0,]
colovec<-c(brewer.pal(12,"Set3")[6:7],
brewer.pal(9,"Set1")[1:2])
vioplot(as.numeric(HetVivMortGlob$PHt)~HetVivMortGlob$moda,
col = c("transparent"),sep=":",las=1,border="transparent",
ylab="PHt",xlab="",ylim=c(0.07,0.35),frame.plot=FALSE,
lineCol="transparent",rectCol="transparent",
main="Comparison between treatments")
vioplot(as.numeric(HetDea$PHt)~HetDea$moda,plotCentre="line",
col=colovec[1],sep=":",las=1,side="left",frame.plot=FALSE,
add=TRUE)
stripchart(as.numeric(HetDea$PHt)~HetDea$moda,vertical=TRUE,
method="jitter",pch=21,add=TRUE,col=colovec[3],
at=c(1:2)-0.2)
vioplot(as.numeric(HetAli$PHt)~HetAli$moda,plotCentre="line",
col=colovec[2],sep=":",las=1,side="right",frame.plot=FALSE,
add=TRUE)
stripchart(as.numeric(HetAli$PHt)~HetAli$moda,vertical=TRUE,
method="jitter",pch=21,add=TRUE,col=colovec[4],
at=c(1:2)+0.2)
segments(c(1:2)-0.2,aggregate(as.numeric(HetDea$PHt),
list(HetDea$moda),FUN=mean)[,2],
c(1:2)+0.2,aggregate(as.numeric(HetAli$PHt),
list(HetAli$moda),FUN=mean)[,2],
col=grey(0.95,0.9),lwd=6)
points(x=c(1:2)-0.2,y=aggregate(as.numeric(HetDea$PHt),
list(HetDea$moda),FUN=mean)[,2],
pch=21,bg=colovec[3],col="black")
points(x=c(1:2)+0.2,y=aggregate(as.numeric(HetAli$PHt),
list(HetAli$moda),FUN=mean)[,2],
pch=21,bg=colovec[4],col="black")
box(bty="l")
legend(0.2,0.4,c("dead","alive"),fill=colovec[1:2],cex=1.3,
bty="n",x.intersp=0.5,y.intersp=0.7,xpd=TRUE)
#export to .pdf 6 x 8 inches
##############################################################################/
#Additional Figure: Heterozygosity computation: total vs surviving####
##############################################################################/
#reshaping the data set for computation
n.temp<-seppop(snpGen2,treatOther=TRUE)
n.temp.other<-lapply(n.temp,as.data.frame(other))
temp<-repool(n.temp$exp1,n.temp$exp0)
pop(temp)<-rep("expinit",times=nInd(temp))
temp<-repool(temp,n.temp$exp1)
temp2<-repool(n.temp$low1,n.temp$low0)
pop(temp2)<-rep("lowinit",times=nInd(temp2))
snpGen3<-repool(temp,temp2,n.temp$low1)
snpGen3@other<-rbind(n.temp.other$exp1,n.temp.other$exp0,
n.temp.other$exp1,n.temp.other$low1,
n.temp.other$low0,n.temp.other$low1)
colnames(snpGen3@other)<-c("fam","treat","height","DoA","newPop")
snpGen3@other$newPop<-pop(snpGen3)
nomFam<-popNames(snpGen3)
temp<-genind2df(snpGen3,oneColPerAll=TRUE)
temp[temp=="A"]<-10
temp[temp=="T"]<-20
temp[temp=="C"]<-30
temp[temp=="G"]<-40
tempop<-temp[,1]
sampleid<-row.names(temp)
temp<-temp[,-1]
temp<-data.frame(lapply(temp,as.numeric))
temp$sampleid<-sampleid
temp<-temp[,c(1639,1:1638)]
temp2<-GENHET(dat=temp,estimfreq="T",locname=nomSNP$simpleNames)
temp2<-as.data.frame(temp2)
temp2$pop<-tempop
temp2$moda<-stringr::str_match(tempop, "(...)(.*)")[,2]
temp2$vivmor<-stringr::str_match(tempop, "(...)(.*)")[,3]
temp2$fam<-"Global"
HetStarSto<-temp2
#same thing by family
pop(snpGen3)<-snpGen3@other$fam
snpGen3fam<-seppop(snpGen3)
nomFam<-popNames(snpGen3)
for (i in 1:length(nomFam)) {
pop(snpGen3fam[[i]])<-snpGen3fam[[i]]@other$newPop
temp<-genind2df(snpGen3fam[[i]],oneColPerAll=TRUE)
temp[temp=="A"]<-10
temp[temp=="T"]<-20
temp[temp=="C"]<-30
temp[temp=="G"]<-40
tempop<-temp[,1]
sampleid<-row.names(temp)
temp<-temp[,-1]
temp<-data.frame(lapply(temp,as.numeric))
temp$sampleid<-sampleid
temp<-temp[,c(1639,1:1638)]
temp2<-GENHET(dat=temp,estimfreq="T",locname=nomSNP$simpleNames)
temp2<-as.data.frame(temp2)
temp2$pop<-tempop
temp2$moda<-stringr::str_match(tempop, "(...)(.*)")[,2]
temp2$vivmor<-stringr::str_match(tempop, "(...)(.*)")[,3]
temp2$fam<-nomFam[i]
HetStarSto<-rbind(HetStarSto,temp2)
}
#in order to have the good order of the categories, we change
#the vivmor catefories
HetStarSto[HetStarSto$vivmor=="init","vivmor"]<-"beg"
HetStarSto[HetStarSto$vivmor=="1","vivmor"]<-"end"
HetStarStoExp<-HetStarSto[HetStarSto$moda=="exp",]
HetStarStoLim<-HetStarSto[HetStarSto$moda=="low",]
#the plot
op<-par(mfrow=c(5,1))
vioplot(as.numeric(HetStarStoExp$PHt)~HetStarStoExp$vivmor:HetStarStoExp$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Exposed / PHt")
vioplot(as.numeric(HetStarStoExp$Hs_obs)~
HetStarStoExp$vivmor:HetStarStoExp$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Exposed / Hs_obs")
vioplot(as.numeric(HetStarStoExp$Hs_exp)~
HetStarStoExp$vivmor:HetStarStoExp$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Exposed / Hs_exp")
vioplot(as.numeric(HetStarStoExp$IR)~HetStarStoExp$vivmor:HetStarStoExp$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Exposed / IR")
vioplot(as.numeric(HetStarStoExp$HL)~HetStarStoExp$vivmor:HetStarStoExp$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Exposed / HL")
par(op)
#export to .pdf 20 x 20 inches
op<-par(mfrow=c(5,1))
vioplot(as.numeric(HetStarStoLim$PHt)~HetStarStoLim$vivmor:HetStarStoLim$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Limited / PHt")
vioplot(as.numeric(HetStarStoLim$Hs_obs)~
HetStarStoLim$vivmor:HetStarStoLim$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Limited / Hs_obs")
vioplot(as.numeric(HetStarStoLim$Hs_exp)~
HetStarStoLim$vivmor:HetStarStoLim$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Limited / Hs_exp")
vioplot(as.numeric(HetStarStoLim$IR)~HetStarStoLim$vivmor:HetStarStoLim$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Limited / IR")
vioplot(as.numeric(HetStarStoLim$HL)~HetStarStoLim$vivmor:HetStarStoLim$fam,
col = c("orange","yellow"),sep=":",las=1,
ylab="Value",xlab="Pheno:Family",
main="Begin vs end: Limited / HL")
par(op)
#export to .pdf 20 x 20 inches
##############################################################################/
#END
##############################################################################/