forked from cran/PopGenome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmismatch.R
54 lines (42 loc) · 1.48 KB
/
mismatch.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
mismatch <- function(matrix_pol,populations,THETA){
npops <- length(populations)
popnames <- paste("pop",1:npops)
if(npops!=length(THETA)){warning("You have to choose a theta value for every popualtion ")}
RE <- matrix(NaN,3,npops)
rownames(RE) <- c("MDSD","MDG1","MDG2")
colnames(RE) <- popnames
for(xx in 1:npops){
if(length(populations[[xx]])==0){next;}
thetaT <- THETA[xx]
popmatrix <- matrix_pol[populations[[xx]],,drop=FALSE]
poplength <- length(populations[[xx]])
if(poplength>1){seqpairs <- combn(poplength,2)}else{seqpairs<-rbind(1,1)}# only one sequence
ncomb <- choose(poplength,2)
res <- apply(seqpairs,2,function(x){
seq1 <- popmatrix[x[1],]
seq2 <- popmatrix[x[2],]
id <- which(!is.na(seq1) & !is.na(seq2) & seq1!=seq2)
freq1 <- length(id)
moment <- freq1 - thetaT
freq2 <- moment*moment
freq3 <- moment*moment*moment
freq4 <- moment*moment*moment*moment
return(c(freq2,freq3,freq4))
})
rownames(res) <- c("freq2","freq3","freq4")
E <- rowSums(res)
s2 <- E["freq2"]
g1 <- E["freq3"]
g2 <- E["freq4"]
value <- s2/(ncomb-1)
if(!is.na(value)){
s <- sqrt(s2/(ncomb-1))
RE["MDSD",xx] <- s
}else{s <- FALSE}
if(s){
RE["MDG1",xx] <- g1*ncomb/((ncomb-2)*((ncomb-1)*s*s*s))
RE["MDG2",xx] <- g2* (ncomb-1) * ncomb /((ncomb-3)*(ncomb-2)*(ncomb-1)*s*s*s*s)-(3*(ncomb-1)*(ncomb-1))/((ncomb-3)*(ncomb-2))
}
}# END for over all POPS
return(RE)
}