forked from timbeissinger/Maize-Teo-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingletonsVsPi.R
200 lines (150 loc) · 9.44 KB
/
SingletonsVsPi.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
#####################################################################
### Use this script to plot Singletons vs. Pi in Maize and Teo ###
#####################################################################
### Timothy M. Beissinger
### 5-15-2015
### updated 8/17/2015
####################################################################
####################################################################
####################################################################
### Load Pi data and re-fit to neutral >= 0.02 cM
####################################################################
####################################################################
####################################################################
load("GENE_GERP.RData")
### Standardize by neutral diversity
tP_maize_gene_N <- mean(div0Maize$tP[which(div0Maize$distanceToGene>=0.02)],na.rm=T) # NOTICE THE 0.02!!! Different from regular script!!!
tP_teo_gene_N <- mean(div0Teo$tP[which(div0Teo$distanceToGene>=0.02)],na.rm=T)# NOTICE THE 0.02!!! Different from regular script!!!
div0Maize$tP.standardized.by.gene.N <- div0Maize$tP/tP_maize_gene_N
div0Teo$tP.standardized.by.gene.N <- div0Teo$tP/tP_teo_gene_N
### Neutral-Standardized Spline
TeoSpline.neutral.gene.standardized <- smooth.spline(div0Teo$distanceToGene,div0Teo$tP.standardized.by.gene.N)
MaizeSpline.neutral.gene.standardized <- smooth.spline(div0Maize$distanceToGene,div0Maize$tP.standardized.by.gene.N)
### Plot
plot(NULL,xlim=c(0,0.1),ylim=c(0.4,1.2),xlab="Distance to Gene or Element (cM)",ylab="Singleton Diversity / Neutral Singleton Diversity (1kb windows)")
lines(TeoSpline.neutral.gene.standardized,col="blue",lwd=3)
lines(MaizeSpline.neutral.gene.standardized,col="red",lwd=3)
legend("topright","(x,y)",col=c("red","blue"),c("Maize","Teosinte"),lwd=3)
par(new = TRUE)
par(fig = c(0.47, 0.97, 0.1, 0.6))
plot(NULL,xlim=c(0,0.002),ylim=c(0.4,1),xlab="",ylab="",main="",cex.axis=0.75)
lines(TeoSpline.neutral.gene.standardized,col="blue",lwd=2)
lines(MaizeSpline.neutral.gene.standardized,col="red",lwd=2)
#text(0.004,-0.55,"Zoom",cex=1.5)
#############################################################################
### Assess significance! ##########################################
#############################################################################
TeoGeneMat <- matrix(NA,nrow=100000,ncol=100)
MaizeGeneMat <- matrix(NA,nrow=100000,ncol=100)
for(i in 1:100){
print(i)
maizeBoot <- sample(nrow(div0Maize),replace=T)
teoBoot <- sample(nrow(div0Teo),replace=T)
div0Maize.boot <- div0Maize[maizeBoot,]
div0Teo.boot <- div0Teo[teoBoot,]
### Standardize by neutral diversity
tP_maize_gene_N.boot <- mean(div0Maize.boot$tP[which(div0Maize.boot$distanceToGene>=0.02)])
tP_teo_gene_N.boot <- mean(div0Teo.boot$tP[which(div0Teo.boot$distanceToGene>=0.02)])
div0Maize.boot$tP.standardized.by.gene.N <- div0Maize.boot$tP/tP_maize_gene_N.boot
div0Teo.boot$tP.standardized.by.gene.N <- div0Teo.boot$tP/tP_teo_gene_N.boot
### Neutral-Standardized Spline
TeoSpline.neutral.gene.standardized.boot <- smooth.spline(div0Teo.boot$distanceToGene,div0Teo.boot$tP.standardized.by.gene.N)
MaizeSpline.neutral.gene.standardized.boot <- smooth.spline(div0Maize.boot$distanceToGene,div0Maize.boot$tP.standardized.by.gene.N)
TeoGeneMat[,i] <- predict(TeoSpline.neutral.gene.standardized.boot,seq(0,0.1,length.out=100000))$y
MaizeGeneMat[,i] <- predict(MaizeSpline.neutral.gene.standardized.boot,seq(0,0.1,length.out=100000))$y
}
###Checkpoint###
save.image("SingletonsVsPi.RData")
### Determine 95% intervals
TeoGeneLow <- apply(TeoGeneMat,1,quantile,0.025)
TeoGeneHigh <- apply(TeoGeneMat,1,quantile,0.975)
MaizeGeneLow <- apply(MaizeGeneMat,1,quantile,0.025)
MaizeGeneHigh <- apply(MaizeGeneMat,1,quantile,0.975)
options(scipen=100)
### Append splines so it is clear that these are pi
TeoGeneLow_Pi <- TeoGeneLow
TeoGeneHigh_Pi <- TeoGeneHigh
MaizeGeneLow_Pi <- MaizeGeneLow
MaizeGeneHigh_Pi <- MaizeGeneHigh
TeoSpline.neutral.gene.standardized_Pi <- TeoSpline.neutral.gene.standardized
MaizeSpline.neutral.gene.standardized_Pi <- MaizeSpline.neutral.gene.standardized
### Load Singleton curves
load("GENE_GERP_Singletons.RData")
### Append splines so it is clear that these are pi
TeoGeneLow_Single <- TeoGeneLow
TeoGeneHigh_Single <- TeoGeneHigh
MaizeGeneLow_Single <- MaizeGeneLow
MaizeGeneHigh_Single <- MaizeGeneHigh
TeoSpline.neutral.gene.standardized_Single <- TeoSpline.neutral.gene.standardized
MaizeSpline.neutral.gene.standardized_Single <- MaizeSpline.neutral.gene.standardized
################ Maize Figure #################
png("distanceToGene_WithSignificance_Folded2_maizeSingleVsPi.png",height=10,width=10,units="in",res=300,pointsize=18)
x <- seq(0,0.1,length.out=100000)
plot(x,MaizeGeneLow_Pi,col="blue",type="n",xlim=c(0,0.02),ylim=c(0.4,1.1),ylab="Diversity / Neutral Diversity",xlab="Distance to Gene (cM)",main="Maize Diversity Away from Genes")
polygon(c(x,rev(x)), c(MaizeGeneLow_Pi,rev(MaizeGeneHigh_Pi)),col=rgb(1, 0, 0,0.8),border=NA)
x <- seq(0,0.1,length.out=1000000)
polygon(c(x,rev(x)), c(MaizeGeneLow_Single,rev(MaizeGeneHigh_Single)),col=rgb(1, 0, 0,0.5),border=NA)
observedMaize_Pi <- predict(MaizeSpline.neutral.gene.standardized_Pi,x)$y
observedMaize_Single <- predict(MaizeSpline.neutral.gene.standardized_Single,x)$y
lines(x,observedMaize_Pi,col=rgb(1, 0, 0,1),lwd=2)
lines(x,observedMaize_Single,col=rgb(1, 0, 0,0.75),lwd=2)
legend("bottomright","(x,y)",col=c(rgb(1, 0, 0,0.9),rgb(1, 0, 0,0.5)),c("Pi","Singletons"),lwd=3)
#par(new = TRUE)
#par(fig = c(0.47, 0.97, 0.1, 0.6))
#plot(x,TeoLow,col="blue",type="n",xlim=c(0,0.0005),ylim=c(0.6,1),ylab="",xlab="",xaxt="n",yaxt="n")
#axis(1,c(0,0.0001,0.0002,0.0003,0.0004,0.0005),at=c(0,0.0001,0.0002,0.0003,0.0004,0.0005))
#axis(2,c(0.6,0.7,0.8,0.9,1),at=c(0.6,0.7,0.8,0.9,1))
#polygon(c(x,rev(x)), c(TeoGeneLow,rev(TeoGeneHigh)),col=rgb(0, 0, 1,0.5),border=NA)
#polygon(c(x,rev(x)), c(MaizeGeneLow,rev(MaizeGeneHigh)),col=rgb(1, 0, 0,0.5),border=NA)
#lines(observedTeo,col=rgb(0, 0, 1,0.75),lwd=2)
#lines(x,observedMaize,col=rgb(1, 0, 0,0.75),lwd=2)
dev.off()
################ Teosinte Figure #################
png("distanceToGene_WithSignificance_Folded2_teoSingleVsPi.png",height=10,width=10,units="in",res=300,pointsize=18)
x <- seq(0,0.1,length.out=100000)
plot(x,TeoGeneLow_Pi,col="blue",type="n",xlim=c(0,0.02),ylim=c(0.4,1.1),ylab="Diversity / Neutral Diversity",xlab="Distance to Gene (cM)",main="Teosinte Diversity Away from Genes")
polygon(c(x,rev(x)), c(TeoGeneLow_Pi,rev(TeoGeneHigh_Pi)),col=rgb(0, 0, 1,0.8),border=NA)
x <- seq(0,0.1,length.out=1000000)
polygon(c(x,rev(x)), c(TeoGeneLow_Single,rev(TeoGeneHigh_Single)),col=rgb(0, 0, 1,0.5),border=NA)
observedTeo_Pi <- predict(TeoSpline.neutral.gene.standardized_Pi,x)$y
observedTeo_Single <- predict(TeoSpline.neutral.gene.standardized_Single,x)$y
lines(x,observedTeo_Pi,col=rgb(0, 0, 1,1),lwd=2)
lines(x,observedTeo_Single,col=rgb(0, 0, 1,0.75),lwd=2)
legend("bottomright","(x,y)",col=c(rgb(0, 0, 1,0.9),rgb(0, 0, 1,0.5)),c("Pi","Singletons"),lwd=3)
#par(new = TRUE)
#par(fig = c(0.47, 0.97, 0.1, 0.6))
#plot(x,TeoLow,col="blue",type="n",xlim=c(0,0.0005),ylim=c(0.6,1),ylab="",xlab="",xaxt="n",yaxt="n")
#axis(1,c(0,0.0001,0.0002,0.0003,0.0004,0.0005),at=c(0,0.0001,0.0002,0.0003,0.0004,0.0005))
#axis(2,c(0.6,0.7,0.8,0.9,1),at=c(0.6,0.7,0.8,0.9,1))
#polygon(c(x,rev(x)), c(TeoGeneLow,rev(TeoGeneHigh)),col=rgb(0, 0, 1,0.5),border=NA)
#polygon(c(x,rev(x)), c(MaizeGeneLow,rev(MaizeGeneHigh)),col=rgb(1, 0, 0,0.5),border=NA)
#lines(observedTeo,col=rgb(0, 0, 1,0.75),lwd=2)
#lines(x,observedMaize,col=rgb(1, 0, 0,0.75),lwd=2)
dev.off()
##########################################
##### Everything Figure #######
##########################################
png("distanceToGene_WithSignificance_Folded2_maizeAndTeoSingleVsPi.png",height=10,width=10,units="in",res=300,pointsize=18)
x <- seq(0,0.1,length.out=100000)
plot(x,MaizeGeneLow_Pi,col="blue",type="n",xlim=c(0,0.01),ylim=c(0.4,1.1),ylab="Diversity / Neutral Diversity",xlab="Distance to Gene (cM)",main="")
polygon(c(x,rev(x)), c(MaizeGeneLow_Pi,rev(MaizeGeneHigh_Pi)),col=rgb(1, 0, 0,0.8),border=NA)
x <- seq(0,0.1,length.out=1000000)
polygon(c(x,rev(x)), c(MaizeGeneLow_Single,rev(MaizeGeneHigh_Single)),col=rgb(1, 0, 0,0.4),border=NA)
observedMaize_Pi <- predict(MaizeSpline.neutral.gene.standardized_Pi,x)$y
observedMaize_Single <- predict(MaizeSpline.neutral.gene.standardized_Single,x)$y
lines(x,observedMaize_Pi,col=rgb(1, 0, 0,1),lwd=2)
lines(x,observedMaize_Single,col=rgb(1, 0, 0,0.75),lwd=2)
### Teo
x <- seq(0,0.1,length.out=100000)
#plot(x,TeoGeneLow_Pi,col="blue",type="n",xlim=c(0,0.02),ylim=c(0.4,1.1),ylab="Diversity / Neutral Diversity",xlab="Distance to Gene (cM)",main="Teosinte Diversity Away from Genes")
polygon(c(x,rev(x)), c(TeoGeneLow_Pi,rev(TeoGeneHigh_Pi)),col=rgb(0, 0, 1,0.8),border=NA)
x <- seq(0,0.1,length.out=1000000)
polygon(c(x,rev(x)), c(TeoGeneLow_Single,rev(TeoGeneHigh_Single)),col=rgb(0, 0, 1,0.4),border=NA)
observedTeo_Pi <- predict(TeoSpline.neutral.gene.standardized_Pi,x)$y
observedTeo_Single <- predict(TeoSpline.neutral.gene.standardized_Single,x)$y
lines(x,observedTeo_Pi,col=rgb(0, 0, 1,1),lwd=2)
lines(x,observedTeo_Single,col=rgb(0, 0, 1,0.75),lwd=2)
source("legend.v2.R")
legend.v2("bottomright","(x,y)",col=c("red","blue","red","blue"),fill=c(rgb(1, 0, 0,0.8),rgb(0, 0, 1,0.8),rgb(1, 0, 0,0.4),rgb(0, 0, 1,0.4)),c("Pi maize","Pi teosinte","Singletons maize","Singletons teosinto"),lwd=3,bty="n",border=NA)
dev.off()
save.image("SingletonsVsPi.RData")