forked from cran/PopGenome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpair_linkdisequ_FAST.R
67 lines (45 loc) · 1.66 KB
/
pair_linkdisequ_FAST.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
pair_linkdisequ_FAST <- function(bial1, bial2, populations1, populations2){
if(length(colnames(bial1))>1){
# only need for Distance !!
combinations <- NULL
vek <- 1:dim(bial1)[2]
vek2 <- 1:dim(bial2)[2]
for(xx in vek){
val <- rep(xx,dim(bial2)[2])
mat <- rbind(val,vek2)
combinations <- cbind(combinations,mat)
}
}
# --------------------------------
npops <- length(populations1)
reslist <- vector("list",npops)
for(yy in 1:npops){
bial1_sub <- bial1[populations1[[yy]],,drop=FALSE]
bial2_sub <- bial2[populations2[[yy]],,drop=FALSE]
colsums1 <- colSums(bial1_sub)
colsums2 <- colSums(bial2_sub)
res <- .Call("R2_between_C",bial1_sub,colsums1, bial2_sub, colsums2)
R2 <- res[[1]]
M <- res[[2]]
P <- apply(M,1,function(x){return(fisher.test( matrix(x,ncol=2,byrow=TRUE) )$p.value )
})
# Distance
if(length(colnames(bial1_sub))>1){
names1 <- as.numeric(colnames(bial1_sub))
names2 <- as.numeric(colnames(bial2_sub))
d_dist <- apply(combinations,2,function(x){return( abs(names1[x[1]]-names2[x[2]]) )})
}else{
d_dist <- rep(NaN,dim(combinations)[2])
}
res <- rbind(R2, P , d_dist)
rownames(res) <- c("R2","P","Distance")
# colnames(res) <- apply(combinations,2,function(x){return(paste(x[1],"/",x[2],sep=""))})
# rownames(res) <- c("d_raw","d_prime","r2","r","d_dist")
# colnames(res) <- apply(combinations,2,function(x){return(paste(x[1],"/",x[2],sep=""))})
reslist[[yy]] <- res
}# End of iteration over pops
reslist <- as.matrix(reslist)
rownames(reslist) <- paste("pop",1:npops)
colnames(reslist) <- "Linkdisequilibrium"
return(reslist)
}