# Written by Jay Taylor (3/7/2018) # Modified by Ti Eriksson (last 11/29/2018) ################################################################### # Relatedness statistics for haplodiploid colonies (7 March 2018) # This code generates random samples of worker genotypes assuming different degrees of # relatedness between the parents. # Four unlinked microsatellite loci are simulated, using allele frequencies supplied # by the user. # Scenarios simulated: I and II # Load packages and function files library(related) source("functions.r") # Defined parameters Nsim = 1000 # number of simulations used to estimate each p-value Nloci = 4 # number of microsatellite loci mf = 2 # number of male mates per queen # Read in allele frequencies freqs <- read.table("allele_freqs.txt", header=TRUE, sep="\t") locus1 <- subset(freqs, locus == 1, select=c(allele, freq)) locus2 <- subset(freqs, locus == 2, select=c(allele, freq)) locus3 <- subset(freqs, locus == 3, select=c(allele, freq)) locus4 <- subset(freqs, locus == 4, select=c(allele, freq)) allele_list <- list(locus1,locus2,locus3,locus4) # Read in colony relatedness statistics cdata <- read.table("colonies.txt", header=TRUE, sep="\t") Ncolonies <- length(cdata$colony) # number of colonies in data set # Relatedness p-values Pval1 = numeric(Ncolonies) Pval2 = numeric(Ncolonies) for (col in 1:Ncolonies) { Nworkers <- cdata$n[col] Nqueens <- cdata$q[col] Ndrones <- mf*Nqueens meanR <- cdata$R[col] # mean pairwise R for this colony names = c(1:Nworkers) ######################################################################################################## # Scenario I: q unrelated queens each mated to mf unrelated fathers meanRsim1 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the unrelated queens and drones queen <- matrix(0,Nqueens,2*Nloci) for (q in 1:Nqueens) { genotype <- diploid_genotype(Nloci, allele_list) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Nworkers,2*Nloci) for (w in 1:Nworkers) { q <- runif(1,1,Nqueens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output1 <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim1[sim] <- mean(output1$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval1[col] <- (rank(c(meanR,meanRsim1))[1])/(Nsim + 1) ######################################################################################################## ######################################################################################################## # Scenario II: q queens that all share a common mother and mf unrelated fathers meanRsim2 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the grand-queen and her male mates grandqueen <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,mf,Nloci) for (d in 1:mf) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nqueens,2*Nloci) for (q in 1:Nqueens) { qg <- grandqueen d <- runif(1,1,mf) # choose the father (granddrone) dg <- granddrone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Nworkers,2*Nloci) for (w in 1:Nworkers) { q <- runif(1,1,Nqueens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output2 <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim2[sim] <- mean(output2$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval2[col] <- (rank(c(meanR,meanRsim2))[1])/(Nsim + 1) ######################################################################################################## } as.data.frame(Pval1) as.data.frame(Pval2) ######################################################################################################## # Relatedness statistics for haplodiploid colonies (7 March 2018) # This code generates random samples of worker genotypes assuming different degrees of # relatedness between the parents. # Four unlinked microsatellite loci are simulated, using allele frequencies supplied # by the user. # Scenarios simulated: III-VIII # Load packages and function files library(related) source("functions.r") # Defined parameters Nsim = 1000 # number of simulations used to estimate each p-value Nloci = 4 # number of microsatellite loci mf = 2 # number of male mates per queen grandqueen2<-matrix(ncol=8,nrow=2) grandqueen3<-matrix(ncol=8,nrow=3) grandqueen4<-matrix(ncol=8,nrow=4) grandqueen5<-matrix(ncol=8,nrow=5) grandqueen6<-matrix(ncol=8,nrow=6) grandqueen7<-matrix(ncol=8,nrow=7) grandqueen8<-matrix(ncol=8,nrow=8) # Read in allele frequencies freqs <- read.table("allele_freqs.txt", header=TRUE, sep="\t") locus1 <- subset(freqs, locus == 1, select=c(allele, freq)) locus2 <- subset(freqs, locus == 2, select=c(allele, freq)) locus3 <- subset(freqs, locus == 3, select=c(allele, freq)) locus4 <- subset(freqs, locus == 4, select=c(allele, freq)) allele_list <- list(locus1,locus2,locus3,locus4) # Read in colony relatedness statistics cdata <- read.table("colonies.txt", header=TRUE, sep="\t") Ncolonies <- length(cdata$colony) # number of colonies in data set # Relatedness p-values Pval3 = numeric(Ncolonies) Pval4 = numeric(Ncolonies) Pval5 = numeric(Ncolonies) Pval6 = numeric(Ncolonies) Pval7 = numeric(Ncolonies) Pval8 = numeric(Ncolonies) Pval9 = numeric(Ncolonies) for (col in 1:Ncolonies) { Nworkers <- cdata$n[col] Nqueens <- cdata$q[col] Ndrones <- mf*Nqueens meanR <- cdata$R[col] # mean pairwise R for this colony names = c(1:Nworkers) ######################################################################################################## # Scenario III: q queens that share 2 common mothers and mf unrelated fathers meanRsim2 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the two grand-queens and their male mates grandqueen2[1,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen2[2,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,2*mf,Nloci) for (d in 1:(2*mf)) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nqueens,2*Nloci) for (q in 1:Nqueens) { g <- runif(1,1,2) # choose the mother (grandquuen) qg <- grandqueen2[g,1:(2*Nloci)] d <- runif(1,1,mf) # choose the father (granddrone) dg <- granddrone[(g-1)*mf+d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Nworkers,2*Nloci) for (w in 1:Nworkers) { q <- runif(1,1,Nqueens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim2[sim] <- mean(output$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval3[col] <- (rank(c(meanR,meanRsim2))[1])/(Nsim + 1) ######################################################################################################## ######################################################################################################## # Scenario 4: q queens that share 3 common mothers and mf unrelated fathers meanRsim3 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the two grand-queens and their male mates grandqueen3[1,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen3[2,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen3[3,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,3*mf,Nloci) for (d in 1:(3*mf)) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nqueens,2*Nloci) for (q in 1:Nqueens) { g <- runif(1,1,3) # choose grandqueen qg <- grandqueen3[g,1:(2*Nloci)] d <- runif(1,1,mf) # choose granddrone dg <- granddrone[(g-1)*mf+d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Nworkers,2*Nloci) for (w in 1:Nworkers) { q <- runif(1,1,Nqueens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim3[sim] <- mean(output$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval4[col] <- (rank(c(meanR,meanRsim3))[1])/(Nsim + 1) ######################################################################################################## ######################################################################################################## # Scenario 5: q queens that share 4 common mothers and mf unrelated fathers meanRsim4 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the two grand-queens and their male mates grandqueen4[1,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen4[2,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen4[3,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen4[4,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,4*mf,Nloci) for (d in 1:(4*mf)) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nqueens,2*Nloci) for (q in 1:Nqueens) { g <- runif(1,1,3) # choose grandqueen qg <- grandqueen4[g,1:(2*Nloci)] d <- runif(1,1,mf) # choose granddrone dg <- granddrone[(g-1)*mf+d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Nworkers,2*Nloci) for (w in 1:Nworkers) { q <- runif(1,1,Nqueens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim4[sim] <- mean(output$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval5[col] <- (rank(c(meanR,meanRsim4))[1])/(Nsim + 1) ######################################################################################################## ######################################################################################################## # Scenario 6: q queens that share 5 common mothers and mf unrelated fathers meanRsim5 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the two grand-queens and their male mates grandqueen5[1,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen5[2,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen5[3,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen5[4,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen5[5,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,5*mf,Nloci) for (d in 1:(5*mf)) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nqueens,2*Nloci) for (q in 1:Nqueens) { g <- runif(1,1,3) # choose grandqueen qg <- grandqueen5[g,1:(2*Nloci)] d <- runif(1,1,mf) # choose granddrone dg <- granddrone[(g-1)*mf+d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Nworkers,2*Nloci) for (w in 1:Nworkers) { q <- runif(1,1,Nqueens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim5[sim] <- mean(output$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval6[col] <- (rank(c(meanR,meanRsim5))[1])/(Nsim + 1) ######################################################################################################## ######################################################################################################## # Scenario 7: q queens that share 6 common mothers and mf unrelated fathers meanRsim6 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the two grand-queens and their male mates grandqueen6[1,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen6[2,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen6[3,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen6[4,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen6[5,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen6[6,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,6*mf,Nloci) for (d in 1:(6*mf)) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nqueens,2*Nloci) for (q in 1:Nqueens) { g <- runif(1,1,3) # choose grandqueen qg <- grandqueen6[g,1:(2*Nloci)] d <- runif(1,1,mf) # choose granddrone dg <- granddrone[(g-1)*mf+d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Nworkers,2*Nloci) for (w in 1:Nworkers) { q <- runif(1,1,Nqueens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim6[sim] <- mean(output$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval7[col] <- (rank(c(meanR,meanRsim6))[1])/(Nsim + 1) ######################################################################################################## ######################################################################################################## # Scenario 8: q queens that share 7 common mothers and mf unrelated fathers meanRsim7 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the two grand-queens and their male mates grandqueen7[1,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen7[2,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen7[3,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen7[4,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen7[5,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen7[6,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen7[7,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,7*mf,Nloci) for (d in 1:(7*mf)) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nqueens,2*Nloci) for (q in 1:Nqueens) { g <- runif(1,1,3) # choose grandqueen qg <- grandqueen7[g,1:(2*Nloci)] d <- runif(1,1,mf) # choose granddrone dg <- granddrone[(g-1)*mf+d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Nworkers,2*Nloci) for (w in 1:Nworkers) { q <- runif(1,1,Nqueens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim7[sim] <- mean(output$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval8[col] <- (rank(c(meanR,meanRsim7))[1])/(Nsim + 1) ######################################################################################################## ######################################################################################################## # Scenario 9: q queens that share 8 common mothers and mf unrelated fathers meanRsim8 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the two grand-queens and their male mates grandqueen8[1,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen8[2,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen8[3,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen8[4,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen7[5,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen8[6,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen8[7,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen8[8,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,8*mf,Nloci) for (d in 1:(8*mf)) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nqueens,2*Nloci) for (q in 1:Nqueens) { g <- runif(1,1,3) # choose grandqueen qg <- grandqueen8[g,1:(2*Nloci)] d <- runif(1,1,mf) # choose granddrone dg <- granddrone[(g-1)*mf+d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Nworkers,2*Nloci) for (w in 1:Nworkers) { q <- runif(1,1,Nqueens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim8[sim] <- mean(output$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval9[col] <- (rank(c(meanR,meanRsim8))[1])/(Nsim + 1) ######################################################################################################## } as.data.frame(Pval3) as.data.frame(Pval4) as.data.frame(Pval5) as.data.frame(Pval6) as.data.frame(Pval7) as.data.frame(Pval8) as.data.frame(Pval9) ######################################################################################################## # Relatedness statistics for haplodiploid colonies (7 March 2018) # This code generates random samples of daughter.queen genotypes assuming different degrees of # relatedness between the parents. # Four unlinked microsatellite loci are simulated, using allele frequencies supplied # by the user. # Load packages and function files library(related) source("functions.r") # Defined parameters Nsim = 1000 # number of simulations used to estimate each p-value Nloci = 4 # number of microsatellite loci mf = 2 # number of male mates per queen # Read in allele frequencies freqs <- read.table("allele_freqs.txt", header=TRUE, sep="\t") locus1 <- subset(freqs, locus == 1, select=c(allele, freq)) locus2 <- subset(freqs, locus == 2, select=c(allele, freq)) locus3 <- subset(freqs, locus == 3, select=c(allele, freq)) locus4 <- subset(freqs, locus == 4, select=c(allele, freq)) allele_list <- list(locus1,locus2,locus3,locus4) # Read in colony relatedness statistics: all nestmate matrilines are siblings. cdata <- read.table("colonies_inferred matrilines_2 motherqueens_somecolonies.txt", header=TRUE, sep="\t") Ncolonies <- length(cdata$colony) # number of colonies in data set # Relatedness p-values Pval1 = numeric(Ncolonies) Pval2 = numeric(Ncolonies) for (col in 1:Ncolonies) { Ndaughter.queens <- cdata$n[col] Nmother.queens <- cdata$q[col] Ndrones <- mf*Nmother.queens meanR <- cdata$R[col] # mean pairwise R for this colony names = c(1:Ndaughter.queens) ######################################################################################################## # Scenario I: q unrelated mother queens each mated to mf unrelated fathers meanRsim1 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the unrelated queens and drones mother.queen <- matrix(0,Nmother.queens,2*Nloci) for (q in 1:Nmother.queens) { genotype <- diploid_genotype(Nloci, allele_list) mother.queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (daughter.queen) genotypes daughter.queen <- matrix(0,Ndaughter.queens,2*Nloci) for (w in 1:Ndaughter.queens) { q <- runif(1,1,Nmother.queens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- mother.queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) daughter.queen[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,daughter.queen) # data frame with daughter.queen names and genotypes output1 <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim1[sim] <- mean(output1$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval1[col] <- (rank(c(meanR,meanRsim1))[1])/(Nsim + 1) ######################################################################################################## ######################################################################################################## # Scenario II: q mother queens that all share a common mother (grand-queen) and mf unrelated fathers (grand-drones) # (i.e. all daughter queens are first cousins) meanRsim2 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the grand-queen and her male mates grandqueen <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,mf,Nloci) for (d in 1:mf) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the mother queens and their unrelated male mates mother.queen <- matrix(0,Nmother.queens,2*Nloci) for (q in 1:Nmother.queens) { qg <- grandqueen d <- runif(1,1,mf) # choose the father (granddrone) dg <- granddrone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) mother.queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (daughter queen) genotypes daughter.queen <- matrix(0,Ndaughter.queens,2*Nloci) for (w in 1:Ndaughter.queens) { q <- runif(1,1,Nmother.queens) # choose the motherqueen d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- mother.queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) daughter.queen[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,daughter.queen) # data frame with daughter.queen names and genotypes output2 <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim2[sim] <- mean(output2$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval2[col] <- (rank(c(meanR,meanRsim2))[1])/(Nsim + 1) ######################################################################################################## } as.data.frame(Pval1) as.data.frame(Pval2) ######################################################################################################## # Relatedness statistics for haplodiploid colonies (7 March 2018) # This code generates random samples of worker genotypes assuming different degrees of # relatedness between the parents. # Four unlinked microsatellite loci are simulated, using allele frequencies supplied # by the user. # Load packages and function files library(related) source("functions.r") # Defined parameters Nsim = 1000 # number of simulations used to estimate each p-value Nloci = 4 # number of microsatellite loci mf = 2 # number of male mates per queen grandqueen2<-matrix(ncol=8,nrow=2) grandqueen3<-matrix(ncol=8,nrow=3) grandqueen8<-matrix(ncol=8,nrow=8) # Read in allele frequencies freqs <- read.table("allele_freqs.txt", header=TRUE, sep="\t") locus1 <- subset(freqs, locus == 1, select=c(allele, freq)) locus2 <- subset(freqs, locus == 2, select=c(allele, freq)) locus3 <- subset(freqs, locus == 3, select=c(allele, freq)) locus4 <- subset(freqs, locus == 4, select=c(allele, freq)) allele_list <- list(locus1,locus2,locus3,locus4) # Read in colony relatedness statistics cdata <- read.table("colonies_inferred matrilines_1 motherqueen.txt", header=TRUE, sep="\t") Ncolonies <- length(cdata$colony) # number of colonies in data set # Relatedness p-values Pval3 = numeric(Ncolonies) Pval4 = numeric(Ncolonies) for (col in 1:Ncolonies) { Ndaugther.queens <- cdata$n[col] Nmother.queens <- cdata$q[col] Ndrones <- mf*Nmother.queens meanR <- cdata$R[col] # mean pairwise R for this colony names = c(1:Ndaugther.queens) ######################################################################################################## # Scenario III: q mother queens that share 2 common mothers and mf unrelated fathers meanRsim3 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the two grand-queens and their male mates grandqueen2[1,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen2[2,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,2*mf,Nloci) for (d in 1:(2*mf)) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nmother.queens,2*Nloci) for (q in 1:Nmother.queens) { g <- runif(1,1,2) # choose the mother (grandquuen) qg <- grandqueen2[g,1:(2*Nloci)] d <- runif(1,1,mf) # choose the father (granddrone) dg <- granddrone[(g-1)*mf+d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Ndaugther.queens,2*Nloci) for (w in 1:Ndaugther.queens) { q <- runif(1,1,Nmother.queens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim3[sim] <- mean(output$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval3[col] <- (rank(c(meanR,meanRsim3))[1])/(Nsim + 1) ######################################################################################################## ######################################################################################################## # Scenario 4: q queens that share 3 common mothers and mf unrelated fathers meanRsim4 <- numeric(Nsim) # This array will contain the simulated mean pairwise R's for (sim in 1:Nsim) { # Sample the two grand-queens and their male mates grandqueen3[1,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen3[2,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) grandqueen3[3,1:(2*Nloci)] <- diploid_genotype(Nloci, allele_list) granddrone <- matrix(0,3*mf,Nloci) for (d in 1:(3*mf)) { genotype <- haploid_genotype(Nloci, allele_list) granddrone[d,1:Nloci] <- t(genotype) } # Sample the queens and their unrelated male mates queen <- matrix(0,Nmother.queens,2*Nloci) for (q in 1:Nmother.queens) { g <- runif(1,1,3) # choose the mother (grandquuen) qg <- grandqueen3[g,1:(2*Nloci)] d <- runif(1,1,mf) # choose the father (granddrone) dg <- granddrone[(g-1)*mf+d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) queen[q,1:(2*Nloci)] <- t(genotype) } drone <- matrix(0,Ndrones,Nloci) for (d in 1:Ndrones) { genotype <- haploid_genotype(Nloci, allele_list) drone[d,1:Nloci] <- t(genotype) } # Sample the offspring (worker) genotypes worker <- matrix(0,Ndaugther.queens,2*Nloci) for (w in 1:Ndaugther.queens) { q <- runif(1,1,Nmother.queens) # choose the mother d <- runif(1,mf*(q-1)+1,mf*q) # choose the father qg <- queen[q,1:(2*Nloci)] dg <- drone[d,1:Nloci] genotype <- offspring_genotype(Nloci, qg, dg) worker[w,1:(2*Nloci)] <- t(genotype) } # Calculate pairwise relatedness gdata <- data.frame(names,worker) # data frame with worker names and genotypes output <- coancestry(gdata, allele.freqs=allele_list, quellergt=1) meanRsim4[sim] <- mean(output$relatedness[[10]]) # calculate and save the mean pairwise R } # Calculate the p-value of the observed mean pairwise R for this colony relative to the simulated values Pval4[col] <- (rank(c(meanR,meanRsim4))[1])/(Nsim + 1) ######################################################################################################## } as.data.frame(Pval3) as.data.frame(Pval4) #end#