forked from cran/PopGenome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathehh.R
77 lines (54 loc) · 2.03 KB
/
ehh.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
#Cai JJ (2008) PGEToolbox: A Matlab toolbox for population genetics and evolution
#Journal of Heredity Jul-Aug;99(4):438-40. doi:10.1093/jhered/esm127
#modified (unused)
ehh <- function(hapldata,n1,n2){
if(missing(n1) & missing(n2)){
n1 <- floor(dim(hapldata)[2]/2)
n2 <- n1
}
core <- hapldata[,n1:n2,drop=FALSE]
values <- my_unique(core)
# return(values)
n <- values$numHap # number of haplotypes
nsites <- dim(hapldata)[2] # number of sites
data_left <- hapldata[, 1:(n1-1),drop=FALSE]
data_right <- hapldata[, (n2+1):nsites,drop=FALSE]
m2 <- nsites - (n2-n1) # size of core (sites)
h <- matrix(1,nrow=n,ncol=m2)
rownames(h) <- rownames(values$uniquematrix)
hvar <- matrix(0,nrow=n,ncol=m2)
rownames(hvar) <- rownames(values$uniquematrix)
core_p <- rep(0,n)
names(core_p) <- rownames(values$uniquematrix)
for(k in 1:n){
idx <- values$idx==k # Welche sind Haplotyp k ?
core_p[k] <- mean(idx) # Frequenz des Haplotypen k ---> (Anzahl k)/(Alle Sequenzen)
dl <- data_left [idx,,drop=FALSE] #
dr <- data_right[idx,,drop=FALSE] #
# Check the left side of the core
for(i in 1:(n1-1)){
dlfenster <- dl[,i:dim(dl)[2],drop=FALSE]
# print(dlfenster)
val <- my_unique(dlfenster) # von links nach rechts --> verkleiner die Fenster
z <- sum(val$sizHap)
if(z>1){
p <- val$sizHap/z # relative Frequency of Haplotypes
a <- sum(p^2)
h[k,i] <- (a-1/z)/(1-1/z) #
hvar[k,i] <- (2*(z-1)/z^3)*(2*(z-2)*(sum(p^3)-a^2) + a - a^2 )
}
}
# Check the ride side of the core
for(j in (n2+1):m2){
val <- my_unique(dr[,1:(j-n1),drop=FALSE]) #
z <- sum(val$sizHap)
if(z>1){
p <- val$sizHap/z
a <- sum(p^2)
h[k,j] <- (a-1/z)/(1-1/z)
hvar[k,j] <-(2*(z-1)/z^3)*(2*(z-2)*(sum(p^3)-a^2) + a - a^2 )
}
}
} # End of for 1:n
return(list(h=h,hvar=hvar,core_p=core_p))
}