-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLDNe.R
76 lines (54 loc) · 1.62 KB
/
LDNe.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
### METHOD 1: Linkage disequilibrium Ne ###
# problems: we dont have a formula to calculate r hat, only its expectation
# missing: implementation that accounts for missing data
# missing: confidence intervals
require(adegenet)
require(multiNe)
harmonic<-function(vec) 1/mean(1/vec)
data(nancycats)
K<-length([email protected])
getHeterozygoteNumbers <- function(theObject)
{
K<-length([email protected])
ends<-cumsum([email protected])
starts<-c(0,ends[-length(ends)])+1
heterozygote_numbers<-list()
i=1
for (i in 1:K){
heterozygote_numbers[[i]]<-colSums(na.rm=T,theObject@tab[,starts[i]:ends[i]]==0.5)/colSums(!is.na(theObject@tab[,starts[i]:ends[i]]))
}
return(heterozygote_numbers)
}
sample_sizes <- getSampleSize_byLocus(nancycats)
s<-num.alleles<-Expect_r2<-w<-matrix(NA,nrow=K,ncol=K)
N=0
NonS=0
for(i in 1:(K-1)){
for(j in (i+1):K){
s[i,j] <- harmonic(c(sample_sizes[i],sample_sizes[j]))
num.alleles[i,j] <- ([email protected][i]-1)*([email protected][j]-1)
if(s[i,j]>=30)
{
Expect_r2[i,j] <- 1/s[i,j] + 3.19/s[i,j]^2
} else {
Expect_r2[i,j] <- 0.0018 + 0.907/s[i,j] + 4.44/s[i,j]^2
}
}
}
w=num.alleles*s^2
N=sum(num.alleles,na.rm=T)
NonS=sum(num.alleles/s,na.rm=T)
S=1/(NonS)*N
W=sum(w,na.rm=T)
R2_hat=sum(w*Expect_r2,na.rm=T)/W
if(S>=30)
{
Expect_R2 <- 1/S + 3.19/S^2
R2_drift <- R2_hat - Expect_R2
Ne <- 1/(2*R2_drift)*(1/3 + sqrt(1/9 - 2.76*R2_drift))
} else {
Expect_R2 <- 0.0018 + 0.907/S + 4.44/S^2
R2_drift <- R2_hat - Expect_R2
Ne <- 1/(2*R2_drift)*(1/3 + sqrt(0.94864 - 2.08*R2_drift))
}
NOT WORKING!!!