-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHENe_christine.R
68 lines (48 loc) · 1.81 KB
/
HENe_christine.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
###### METHOD 2: Heterozygote excess Ne ######
## Christine ##
# missing: confidence intervals / bootstrapping / jackknifing
getSampleSize_byLocus <- function(theObject)
{
K<-length([email protected])
ends<-cumsum([email protected])
starts<-c(0,ends[-length(ends)])+1
sample_sizes<-numeric(K)
i=4
for (i in 1:K){
sample_sizes[i]<-sum(na.rm=T,theObject@tab[,starts[i]:ends[i]])
}
return(sample_sizes)
}
require(adegenet)
for (l in 1:length(genind.crit)) {
genind.by.locus <- genind.crit[[l]]
K <- length(genind.by.locus)
sample_sizes <- getSampleSize_byLocus(genObj)
p <- num.allele <- Hexp <- no.het.per.allele <- Hobs <- d <- vector("list", length=K)
w <- d.per.locus <- dw <- num.alleles.per.locus <- 0
p <- vector("list", length=K)
num.allele <- vector("list", length=K)
num.alleles.per.locus <- 0
Hexp <- vector("list", length=K)
no.het.per.allele <- vector("list", length=K)
Hobs <- vector("list", length=K)
d <- vector("list", length=K)
w <- 0
d.per.locus <- 0
dw <- 0
#j <- 1
#i <- 1
for (j in 1:K) {
for (i in 1:ncol(genind.by.locus[[j]]@tab)) {
num.alleles.per.locus[j] <- sum(genind.by.locus[[j]]@tab, na.rm=T) # total number of alleles for locus 1
num.allele[[j]][i] <- sum(genind.by.locus[[j]]@tab[,i], na.rm=T) # number of occurrences of allele 1 of locus 1
p[[j]][i] <- num.allele[[j]][i] / num.alleles.per.locus[j] # allele frequency
Hexp[[j]][i] <- 2*p[[j]][i]*(1-p[[j]][i])*2*sample_sizes[j] / (2*sample_sizes[j]-1) # expected heterozygosity
no.het.per.allele[[j]][i] <- length(genind.by.locus[[j]]@tab[,i][!is.na(genind.by.locus[[j]]@tab[,i]) & genind.by.locus[[j]]@tab[,i] == 1])
Hobs[[j]][i] <- no.het.per.allele[[j]][i] / num.allele[[j]][i]
d[[j]][i] <- (Hobs[[j]][i]-Hexp[[j]][i]) / Hexp[[j]][i]
}
}
D[l] <- sum(dw)/sum(w)
Nb[l] <- 1/2*D + 1/(2*(D+1))
}