This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
forked from rbsmith896/rbsmith_bsf_paternity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOLD_MultiplePaternitySim.R
345 lines (262 loc) · 11.1 KB
/
OLD_MultiplePaternitySim.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
#RB Smith
#BSF MSc Project
#Multiple Paternity Simulation Script
install.packages("AlphaSimR")
library(AlphaSimR)
rm(list = ls())
#This first part creates a base population (with SNPs) and simulates
#some example clutches manually. After, there are some generalized functions
#to do this automatically with set parameters
#Once multi-allelic microsatellites are encoded, relationships can be
#estimated using the pairwise relatedness coefficient r_xy, where r_xy = 2*theta_xy,
#and theta_xy is the probability that the two individuals have the same allele
#at a given locus
#With known allele frequencies, we can simulate genotypes of full sibs, half sibs,
#and unrelated pairs to get distributions of r_xy for each pairing, and then
#calculate the error rates that full-sibs are mis-classified as half-sibs and vice versa
#creating the parents, with each chromosome corresponding to a different
#microsat locus (since they're in free recombination, as far as I can tell)
#Each locus is defined by 2 sites, for 4 possible alleles
founderPop = runMacs(nInd=4, nChr=15, segSites=2)
SP = SimParam$new(founderPop)
#pulling the genetic map, and setting the location of both loci on each chromosome
#to the same value, so there's no recombination between them,
#then storing it as the new genetic map
GM <- SP$genMap
for(i in 1:founderPop@nChr){
GM[[i]][2] <- GM[[i]][1]
}
SP$switchGenMap(GM)
#now to set the haplotypes at each locus
#let's start by assuming all the loci have 3 alleles at equal frequencies
alleleDist <- list(c(1,0),c(0,1),c(1,1),c(0,0))
Haplos <- pullSegSiteHaplo(founderPop)
#this makes sure that all the haplotypes are sampled from the distribution above
#this distribution can be different for different loci, but for now we're assuming
#it's the same for all
for(ind in 1:founderPop@nInd){
for(loc in 1:founderPop@nChr){
for(hap in 0:1){
allele <- sample(alleleDist,1)
Haplos[(2*ind)-1+hap,(2*loc)-1] <- allele[[1]][1]
Haplos[(2*ind)-1+hap,(2*loc)] <- allele[[1]][2]
}
}
}
setMarkerHaplo(founderPop,Haplos)
#now let's make a function that takes this funny binary-encoded microsat system
#and converts it into a simple matrix of genotypes (with the microsat alleles)
#scored as 1, 2, 3 and 4
scoreMicrosats <- function(Clutch){
ClutchHaplo <- pullSegSiteHaplo(Clutch)
output <- data.frame(matrix(nrow = nrow(ClutchHaplo), ncol = ncol(ClutchHaplo)/2))
rownames(output) <- rownames(ClutchHaplo)
colnames(output) <- c(1:Clutch@nChr)
for(i in 1:nrow(output)){
for(j in 1:ncol(output)){
x1 <- ClutchHaplo[i,2*j-1]
x2 <- ClutchHaplo[i,2*j]
if(x1==1 & x2==0){
output[i,j] <- 1
}
if(x1==0 & x2==1){
output[i,j] <- 2
}
if(x1==1 & x2==1){
output[i,j] <- 3
}
if(x1==0 & x2==0){
output[i,j] <- 4
}
}
}
return(output)
}
#test
scoreMicrosats(founderPop)
#now for the function that makes the Clutches
#Clutch-making function, that takes:
#a) the number of offspring
#b) the number of sires (indicated by the size of the basePop - individual 1 is the mother, the rest are fathers)
#c) the relative contribution of the primary sire
#and produce a matrix of offspring genotypes
makeClutch <- function(basePop, clutchSize, percentSire1){
numSires <- nInd(basePop) - 1
sire1Brood <- round(percentSire1 * clutchSize, digits=0)
altSireBrood <- round(((1 - percentSire1)/(numSires - 1))*clutchSize, digits=0)
totalClutch <- sire1Brood + altSireBrood*(numSires-1)
if(totalClutch != clutchSize){
sire1Brood <- sire1Brood + (clutchSize - totalClutch)
}
Clutch <- makeCross(pop = basePop, crossPlan = matrix(c(1, 2), ncol = 2), nProgeny = sire1Brood)
for(i in 3:(numSires+1)){
altcross <- makeCross(pop = basePop, crossPlan = matrix(c(1, i), ncol = 2), nProgeny = altSireBrood)
Clutch <- mergePops(list(Clutch,altcross))
}
return(Clutch)
}
#now let's see if we're able to discern full sibs from half sibs.
#We'll make 2 pairs of clutches, one of full sibs and one of half sibs
basePop = newPop(founderPop)
fullSib1 <- makeCross(pop = basePop, crossPlan = matrix(c(1, 2), ncol = 2), nProgeny = 50)
fullSib2 <- makeCross(pop = basePop, crossPlan = matrix(c(1, 2), ncol = 2), nProgeny = 50)
fullSibs <- mergePops(list(fullSib1,fullSib2))
halfSib1 <- makeCross(pop = basePop, crossPlan = matrix(c(1, 2), ncol = 2), nProgeny = 50)
halfSib2 <- makeCross(pop = basePop, crossPlan = matrix(c(1, 3), ncol = 2), nProgeny = 50)
halfSibs <- mergePops(list(halfSib1,halfSib2))
#now we need a function to get all the pairwise relationships between 2 populations
#but importantly, NOT among individuals in the same population
getPairwiseRelationships <- function(Clutch1, Clutch2){
Haplos1 <- scoreMicrosats(Clutch1)
Haplos2 <- scoreMicrosats(Clutch2)
output <- data.frame(matrix(nrow = Clutch1@nInd, ncol = Clutch2@nInd))
rownames(output) <- c(1:Clutch1@nInd)
colnames(output) <- c(1:Clutch2@nInd)
for(i in 1:nrow(output)){
for(j in 1:ncol(output)){
rel <- 0
for(loc in 1:ncol(Haplos1)){
#kind of confusing here, but basically just pulling the 4 microsatellite
#genotypes, two for the first individual (i11 and i12) and 2 for the second
#individual (i21 and i22)
i11 <- Haplos1[2*i-1,loc]
i12 <- Haplos1[2*i,loc]
i21 <- Haplos2[2*j-1,loc]
i22 <- Haplos2[2*j,loc]
#then if there are 2 matches across individuals
#(which can happen 1 of 2 ways), score it as a 2
if((i11==i21)&(i12==i22) | (i11==i22)&(i21==i12)){
rel <- rel+2
}
#otherwise, if there are ANY matches across individuals,
#score it was a 1
else if(i11==i21 | i11==i22 | i12==i21 | i12==i22){
rel <- rel+1
}
#the only other case is there are no matches, so scored a 0
}
#once you do that for all the loci, divide by the number of loci
#and store the value in the corrsponding slot of the relationship matrix
output[i,j] <- rel/Clutch1@nChr
}
}
return(output)
}
fullsibpairs <- getPairwiseRelationships(fullSib1,fullSib2)
halfsibpairs <- getPairwiseRelationships(halfSib1,halfSib2)
hist(as.matrix(fullsibpairs))
hist(as.matrix(halfsibpairs))
#this is a problem: the coefficients of relationship aren't different enough
#between full sibs and half sibs to be able to differentiate them
#need to figure out a better way to assess relationships
#OLDER THINGS
#I guess now we need a function to calculate relatedness between all individuals
#in a population?
getRelationships <- function(Clutch){
Haplos <- scoreMicrosats(Clutch)
output <- data.frame(matrix(nrow = Clutch@nInd, ncol = Clutch@nInd))
rownames(output) <- c(1:Clutch@nInd)
colnames(output) <- c(1:Clutch@nInd)
for(i in 1:nrow(output)){
for(j in 1:ncol(output)){
rel <- 0
for(loc in 1:ncol(Haplos)){
#kind of confusing here, but basically just pulling the 4 microsatellite
#genotypes, two for the first individual (i11 and i12) and 2 for the second
#individual (i21 and i22)
i11 <- Haplos[2*i-1,loc]
i12 <- Haplos[2*i,loc]
i21 <- Haplos[2*j-1,loc]
i22 <- Haplos[2*j,loc]
#then if there are 2 matches across individuals
#(which can happen 1 of 2 ways), score it as a 2
if((i11==i21)&(i12==i22) | (i11==i22)&(i21==i12)){
rel <- rel+2
}
#otherwise, if there are ANY matches across individuals,
#score it was a 1
else if(i11==i21 | i11==i22 | i12==i21 | i12==i22){
rel <- rel+1
}
#the only other case is there are no matches, so scored a 0
}
#once you do that for all the loci, divide by the number of loci
#and store the value in the corrsponding slot of the relationship matrix
output[i,j] <- rel/Clutch@nChr
}
}
return(output)
}
#and let's test
rel_fullSibs <- getRelationships(fullSibs)
rel_halfSibs <- getRelationships(halfSibs)
#and let's make a population and a sample clutch of 100
basePop = newPop(founderPop)
sampleClutch <- makeClutch(basePop, clutchSize = 100, percentSire1 <- .8)
clutchGeno <- scoreMicrosats(sampleClutch)
#now we can get measures of relatedness
#Sampling function, which takes a certain number of random offspring
#from a population object of offspring
#I couldn't figure out a nice way to select a sub-population without a phenotype,
#but the atttrition function can be finagled to do something similar.
#Not the most elegant, but it works for now
takeSample <- function(Clutch, sampNum){
Samp <- attrition(Clutch, 1 - (sampNum / nInd(sampleClutch)))
while(nInd(Samp) != sampNum){
Samp <- attrition(Clutch, 1 - (sampNum / nInd(sampleClutch)))
}
return(Samp)
}
samp <- takeSample(sampleClutch, 15)
#Generalized functions
#example
#example
samp
#OLD STUFF, PROBABLY DELETE LATER
basePopHaplo = pullSegSiteHaplo(basePop)
basePopHaplo[, 1:5]
basePopHaplo
basePopGeno = pullSegSiteGeno(basePop)
basePopGeno[, 1:5]
basePopGeno
#single paternity model 1: female (ind1) mates with one male (ind2)
M1cross12 = makeCross(pop = basePop, crossPlan = matrix(c(1, 2), ncol = 2), nProgeny = 1000)
#multiple paternity model 2, equal shares: female (ind1) mates with all 3 males equally
M2cross12 = makeCross(pop = basePop, crossPlan = matrix(c(1, 2), ncol = 2), nProgeny = 340)
M2cross13 = makeCross(pop = basePop, crossPlan = matrix(c(1, 3), ncol = 2), nProgeny = 330)
M2cross14 = makeCross(pop = basePop, crossPlan = matrix(c(1, 4), ncol = 2), nProgeny = 330)
#multiple paternity model 3: female (ind1) mates with one male (ind2) with some contributions from the other males (ind3, ind4)
M3cross12 = makeCross(pop = basePop, crossPlan = matrix(c(1, 2), ncol = 2), nProgeny = 800)
M3cross13 = makeCross(pop = basePop, crossPlan = matrix(c(1, 3), ncol = 2), nProgeny = 100)
M3cross14 = makeCross(pop = basePop, crossPlan = matrix(c(1, 4), ncol = 2), nProgeny = 100)
#progeny genotypes for model 1
M1cross12Geno = pullSegSiteGeno(M1cross12)
M1Geno <- M1cross12Geno
dim(M1Geno)
#progeny genotypes for model 2
M2cross12Geno = pullSegSiteGeno(M2cross12)
M2cross13Geno = pullSegSiteGeno(M2cross13)
M2cross14Geno = pullSegSiteGeno(M2cross14)
M2Geno <- rbind(M2cross12Geno,M2cross13Geno,M2cross14Geno)
dim(M2Geno)
#progeny genotypes for model 3
M3cross12Geno = pullSegSiteGeno(M3cross12)
M3cross13Geno = pullSegSiteGeno(M3cross13)
M3cross14Geno = pullSegSiteGeno(M3cross14)
M3Geno <- rbind(M3cross12Geno,M3cross13Geno,M3cross14Geno)
dim(M3Geno)
#getting correlations among model 1 offspring
M1GenoT <- t(M1Geno)
M1indCor = cor(M1GenoT)
corCols = hcl.colors(n = 21, palette = "RdYlBu",rev = TRUE)
image(M1indCor, xlab = "Individual", ylab = "Individual", axes = FALSE, col = corCols, main = "Model 1")
#then for model 2
M2GenoT <- t(M2Geno)
M2indCor = cor(M2GenoT)
corCols = hcl.colors(n = 21, palette = "RdYlBu",rev = TRUE)
image(M2indCor, xlab = "Individual", ylab = "Individual", axes = FALSE, col = corCols, main = "Model 2")
#and for model 3
M3GenoT <- t(M3Geno)
M3indCor = cor(M3GenoT)
corCols = hcl.colors(n = 21, palette = "RdYlBu",rev = TRUE)
image(M3indCor, xlab = "Individual", ylab = "Individual", axes = FALSE, col = corCols, main = "Model 3")