-
Notifications
You must be signed in to change notification settings - Fork 14
/
pairwise.fst.R
159 lines (146 loc) · 4.56 KB
/
pairwise.fst.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
###################################################
#'
#' Estimates pairwise FSTs according to Nei (1987)
#'
#' Estimate pairwise FSTs according to Nei (1987)
#'
#' FST are calculated using Nei (87) equations for FST', as described in the note
#' section of \link{basic.stats}
#'
#'
#' @usage pairwise.neifst(dat,diploid=TRUE)
#'
#' @param dat A data frame containing population of origin as the first column and
#' multi-locus genotypes in following columns
#' @param diploid whether the data is from a diploid (default) or haploid organism
#'
#' @return A matrix of pairwise FSTs
#'
#' @references Nei, M. (1987) Molecular Evolutionary Genetics. Columbia University Press
#'
#'
#' @author Jerome Goudet \email{jerome.goudet@@unil.ch}
#'
#' @seealso \link{pairwise.WCfst} \link{genet.dist} \link{basic.stats}
#'
#'
#' @examples
#'
#' data(gtrunchier)
#' pairwise.neifst(gtrunchier[,-2],diploid=TRUE)
#'
#' @export
#'
#########################################################################
pairwise.neifst <- function(dat,diploid=TRUE){
dat<-dat[order(dat[,1]),]
pops<-unique(dat[,1])
npop<-length(pops)
fstmat <- matrix(nrow=npop,ncol=npop,dimnames=list(pops,pops))
if (is.factor(dat[,1])) {
dat[,1]<-as.numeric(dat[,1])
pops<-as.numeric(pops)
}
for(a in 2:npop){
for(b in 1:(a-1)){
subdat <- dat[dat[,1] == pops[a] | dat[,1]==pops[b],]
fstmat[a,b]<-fstmat[b,a]<- basic.stats(subdat,diploid=diploid)$overall[8]
}
}
fstmat
}
####################################################################
###################################################
#'
#' Estimates pairwise FSTs according to Weir and Cockerham (1984)
#'
#' Estimates pairwise FSTs according to Weir and Cockerham (1984)
#'
#' @usage pairwise.WCfst(dat,diploid=TRUE)
#'
#' @param dat A data frame containing population of origin as the first column
#' and multi-locus genotypes in following columns
#' @param diploid whether the data is from a diploid (default) or haploid organism
#'
#' @return A matrix of pairwise FSTs
#'
#' @details FST are calculated using Weir & Cockerham (1984) equations for FST', as described in the note section of \link{wc}
#'
#' @references
#' Weir, B.S. (1996) Genetic Data Analysis II. Sinauer Associates.
#'
#' \href{https://onlinelibrary.wiley.com/doi/10.1111/j.1558-5646.1984.tb05657.x}{Weir B.S. and Cockerham C.C. (1984)} Estimating F-Statistics for the Analysis of Population Structure. Evolution 38:1358
#'
#'
#' @author Jerome Goudet \email{jerome.goudet@@unil.ch}
#'
#' @seealso \link{pairwise.neifst} \link{genet.dist}
#'
#'
#' @examples
#'
#' data(gtrunchier)
#' pairwise.WCfst(gtrunchier[,-2],diploid=TRUE)
#'
#' @export
#'
#########################################################################
pairwise.WCfst <- function(dat,diploid=TRUE){
dat<-dat[order(dat[,1]),]
pops<-unique(dat[,1])
npop<-length(pops)
fstmat <- matrix(nrow=npop,ncol=npop,dimnames=list(pops,pops))
if (is.factor(dat[,1])) {
dat[,1]<-as.numeric(dat[,1])
pops<-as.numeric(pops)
}
for(a in 2:npop){
for(b in 1:(a-1)){
subdat <- dat[dat[,1] == pops[a] | dat[,1]==pops[b],]
fstmat[a,b]<-fstmat[b,a]<- wc(subdat,diploid=diploid)$FST
}
}
fstmat
}
####################################################################
#' Estimates pairwise betas according to Weir and Goudet (2017)
#'
#' Estimates pairwise betas according to Weir and Goudet (2017)
#'
#' @usage pairwise.betas(dat,diploid=TRUE)
#'
#' @param dat A data frame containing population of origin as the first column
#' and multi-locus genotypes in following columns
#' @param diploid whether the data is from a diploid (default) or haploid organism
#'
#' @return a matrix of pairwise betas
#'
#' @author Jerome Goudet \email{jerome.goudet@@unil.ch}
#'
#' \href{https://www.genetics.org/content/206/4/2085}{Weir, BS and Goudet J. 2017} A Unified Characterization
#' of Population Structure and Relatedness. Genetics (2017) 206:2085
#' @examples
#'
#' data(gtrunchier)
#' pairwise.betas(gtrunchier[,-2],diploid=TRUE)
#'
#' @export
#'
pairwise.betas<-function(dat,diploid=TRUE){
if (is.genind(dat)) dat<-genind2hierfstat(dat)
dat<-dat[order(dat[,1]),]
pops<-unique(dat[,1])
npop<-length(pops)
fstmat <- matrix(nrow=npop,ncol=npop,dimnames=list(pops,pops))
if (is.factor(dat[,1])) {
dat[,1]<-as.numeric(dat[,1])
pops<-as.numeric(pops)
}
for(a in 2:npop){
for(b in 1:(a-1)){
subdat <- dat[dat[,1] == pops[a] | dat[,1]==pops[b],]
fstmat[a,b]<-fstmat[b,a]<- betas(subdat,diploid=diploid)$betaW
}
}
fstmat
}