-
Notifications
You must be signed in to change notification settings - Fork 0
/
effect_frequency_trend_unlinked.Rmd
170 lines (140 loc) · 4.94 KB
/
effect_frequency_trend_unlinked.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
---
title: "Binned Effect Size"
---
```{r}
library(dplyr)
library(ggplot2)
```
```{r}
unlinked_sites <- read.csv("unlinked_sites.csv")
adult <- unlinked_sites[which(unlinked_sites$Age == "Adult"),-c(1:3)]+2
juv <- unlinked_sites[which(unlinked_sites$Age == "Juv"),-c(1:3)]+2
```
Get the allele frequencies for adults and juveniles
```{r}
allele_frequencies <- data.frame(Site = colnames(adult), JuvFreq = colSums(juv)/453, AdultFreq = colSums(adult)/129)
head(allele_frequencies)
```
```{r}
plot(allele_frequencies$JuvFreq, allele_frequencies$AdultFreq)
cor.test(allele_frequencies$JuvFreq, allele_frequencies$AdultFreq)
```
```{r}
allele_frequencies1 <- read.csv("allele_frequencies.csv")
MP_effect_sizes <- read.csv("effect_sizes.csv")
```
Join the two
```{r}
MP_effect_sizes2 <- data.frame(paste(MP_effect_sizes$CONTIG, MP_effect_sizes$SNP, sep = "_"), MP_effect_sizes$EffectSize)
colnames(MP_effect_sizes2) <- c("Site","EffectSize")
```
```{r}
effect_and_freq <- merge(MP_effect_sizes2, allele_frequencies, by = "Site")
effect_and_freq <- data.frame(effect_and_freq,
Diff = (effect_and_freq$JuvFreq - effect_and_freq$AdultFreq),
DiffADJ = (effect_and_freq$JuvFreq - effect_and_freq$AdultFreq)/effect_and_freq$AdultFre)
```
Bin the effect sizes
```{r}
dmean <- c()
ese <- c()
bins <- 200
brks <- quantile(effect_and_freq$EffectSize, seq(0,1, length.out = bins+1))
es_mid <- (brks[-1]+brks[-(bins+1)])/2
dmean <- rep(0,bins)
ese <- rep(1, bins)
for (i in 1:bins) {
choice <- effect_and_freq$EffectSize>brks[i] & effect_and_freq$EffectSize<brks[i+1]
dmean[i] <- mean(effect_and_freq$Diff[choice])
ese[i] <- sd(effect_and_freq$Diff[choice])/sqrt(sum(choice))
}
binned_data <- data_frame(MeanEffect = es_mid, MeanChange = dmean)
```
```{r}
af_change_plot_1 <- ggplot(data = binned_data, aes(x = MeanEffect, y = MeanChange/2))+
geom_point(shape=21, size = 2, fill = "white")+
geom_abline()+
theme_minimal()+
xlab("Effect size (200 quantiles)")+
ylab("Mean change in allele frequency")
af_change_plot_1
```
```{r}
effect_and_freq_bin <- effect_and_freq %>% mutate(new_bin = ntile(EffectSize, n=200))
res <- effect_and_freq_bin %>% group_by(new_bin) %>%
summarise(Num = n(), MeanEffect = mean(EffectSize,na.rm=TRUE))
res2 <- effect_and_freq_bin %>% group_by(new_bin) %>%
summarise(Num = n(), MeanChange = mean((JuvFreq-AdultFreq),na.rm=TRUE))
binned_data <- merge(res, res2, by = "new_bin")
```
```{r}
af_change_plot <- ggplot(data = binned_data, aes(x = MeanEffect, y = MeanChange/2))+
geom_point(shape=21, size = 2, fill = "white")+
geom_smooth(method = "lm", se=T) +
theme_minimal()+
xlab("Effect size (200 quantiles)")+
ylab("Mean change in allele frequency")
# ylim(-0.02, 0.02)+
# ggtitle("B")
af_change_plot
```
```{r}
unbinned_plot <- ggplot(data = effect_and_freq, aes(y=Diff, x = EffectSize))+
geom_point(pch=".")+
#geom_smooth(method = "lm", se=T) +
theme_minimal()+
xlab("Effect size")+
ylab("Change in allele frequency")
#ylim(-0.32, 0.32)+
#ggtitle("A")
unbinned_plot
```
```{r}
pq <- effect_and_freq$JuvFreq*(1- effect_and_freq$JuvFreq)
lm_effect <- lm(effect_and_freq$Diff~effect_and_freq$EffectSize)
summary(lm_effect)
lm_effect_freq <- lm(effect_and_freq$Diff~effect_and_freq$EffectSize*pq)
summary(lm_effect_freq)
```
```{r}
af <- (effect_and_freq$AdultAlleles+effect_and_freq$JuvAlleles)/(2*575)
es <- effect_and_freq$EffectSize
maf <- c()
for (i in 1:nrow(effect_and_freq)){
if(af[i]>0.5){
maf[i] <- 1-af[i]
es[i] <- es
}
else{
maf[i] <- af[i]
}
}
pq <- maf*(1- maf)
diff <- effect_and_freq$Diff
lm1 <- lm(diff~es)
pred1 <- predict(lm1, newdata = data.frame(es = c(es)))
plot(pred1, diff, pch=".")
lm2 <- lm(diff~es*pq)
pred2 <- predict(lm2, newdata = data.frame(es = c(es), pq = c(pq)))
plot(pred2, diff, pch=".")
plot(es, pred2, pch=".")
```
`Nb I used mean allele frequency over both generations, as I wanted to capture the frequency of alleles in the missing parents.
Nb I don’t use exactly pq x es but the difference is negligible (graph 1)
To explain, if we assume the focal allele has a fitness of 1 in the homozygotes and 1-s in the hets then the change in allele frequency is
The deficit in the heterozygotes i.e (2pqs x 1/2) / mean fitness.
The x 1/2 is because only half the het alleles are the focal allele. The approximation is assuming mean fitness is 1, which close to true these small fitness effects.
I used the full formulae for my analysis.
```{r}
```
```{r}
library(cowplot)
both_plots <- ggdraw()+
draw_plot(unbinned_plot, x = 0, y = 0.5, height = 0.5)+
draw_plot(af_change_plot, x = 0, y = 0, height = 0.5)
both_plots
both_plots2 <- ggdraw()+
draw_plot(unbinned_plot, x = 0, y = 0, width = 0.5)+
draw_plot(af_change_plot, x = 0.5, y = 0, width = 0.5)
both_plots2
```