forked from cran/PopGenome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYach.stats.R
200 lines (149 loc) · 5.97 KB
/
Yach.stats.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
setGeneric("Achaz.stats", function(object,new.populations=FALSE,new.outgroup=FALSE,subsites=FALSE) standardGeneric("Achaz.stats"))
setMethod("Achaz.stats", "GENOME",
function(object,new.populations,new.outgroup,subsites){
region.names <- [email protected]
n.region.names <- length(region.names)
# object@Pop_Recomb$sites <- "ALL"
# object@Pop_Recomb$calculated <- TRUE
if(missing(new.populations)){
npops <- length(object@populations)
# object@Pop_Sweeps$Populations <- object@populations
}else{
npops <- length(new.populations)
# object@Pop_Sweeps$Populations <- new.populations
}
# bial <- object@biallelics
# Init
init <- matrix(,n.region.names,npops)
Achaz <- init
# Names
nam <- paste("pop",1:npops)
# rownames(CL) <- region.names
colnames(Achaz) <- nam
# Populations
if(!missing(new.populations)){
NEWPOP <- TRUE
populations <- vector("list",npops)
}else{
NEWPOP <- FALSE
}
# Outgroup
if(!missing(new.outgroup)){
NEWOUT <- TRUE
}else{
NEWOUT <- FALSE
}
## PROGRESS #########################
progr <- progressBar()
#####################################
for(xx in 1:n.region.names){
### if Subsites ----------------------------------
bial <- popGetBial(object,xx)
if(subsites[1]!=FALSE){
if(subsites=="transitions" & length(bial!=0)){
tran <- which([email protected]@transitions[[xx]]==TRUE)
bial <- bial[,tran,drop=FALSE]
#object@Pop_Recomb$sites <- "transitions"
}
if(subsites=="transversions" & length(bial!=0)){
transv <- which([email protected]@transitions[[xx]]==FALSE)
bial <- bial[,transv,drop=FALSE]
#object@Pop_Recomb$sites <- "transversions"
}
if(subsites=="syn" & length(bial!=0)){
syn <- which([email protected]@synonymous[[xx]]==TRUE)
bial <- bial[,syn,drop=FALSE]
#object@Pop_Recomb$sites <- "synonymous"
}
if(subsites=="nonsyn" & length(bial!=0)){
nonsyn <- which([email protected]@synonymous[[xx]]==FALSE)
bial <- bial[,nonsyn,drop=FALSE]
#object@Pop_Recomb$sites <- "nonsynonymous"
}
if(subsites=="intron" & length(bial!=0)){
intron <- which([email protected]@IntronSNPS[[xx]]==TRUE)
#if(length(intron)==0){
# intron <- [email protected]@GeneSNPS[[xx]] & [email protected]@ExonSNPS[[xx]]
#}
bial <- bial[,intron,drop=FALSE]
#object@Pop_Recomb$sites <- "introns"
}
if(subsites=="utr" & length(bial!=0)){
utr <- which([email protected]@UTRSNPS[[xx]]==TRUE)
bial <- bial[,utr,drop=FALSE]
#object@Pop_Recomb$sites <- "utr"
}
if(subsites=="exon" & length(bial!=0)){
exon <- which([email protected]@ExonSNPS[[xx]]==TRUE)
bial <- bial[,exon,drop=FALSE]
#object@Pop_Recomb$sites <- "exon"
}
if(subsites=="coding" & length(bial!=0)){
#coding <- which(!is.na([email protected]@synonymous[[xx]]))
coding <- which([email protected]@CodingSNPS[[xx]]==TRUE)
bial <- bial[,coding,drop=FALSE]
#object@Pop_Recomb$sites <- "coding"
}
if(subsites=="gene" & length(bial!=0)){
gene <- which([email protected]@GeneSNPS[[xx]])
bial <- bial[,gene,drop=FALSE]
#object@Pop_Recomb$sites <- "gene"
}
}# End if subsites
############### ---------------------------------
if(length(bial)!=0){ # if a biallelic position exists
# OUTGROUP
## Outgroup
if(NEWOUT){
if(is.character(new.outgroup)){
outgroup <- match(new.outgroup,rownames(bial))
naids <- which(!is.na(outgroup))
outgroup <- outgroup[naids]
}else{
outgroup <- new.outgroup
ids <- which(outgroup>dim(bial)[1])
if(length(ids)>0){outgroup <- outgroup[-ids]}
}
if(length(outgroup)==0){outgroup <- FALSE}
}else{
outgroup <- [email protected]@outgroup[[xx]]
}
# --------------------------------------
if(NEWPOP){ # wenn neu Populationen definiert
for(yy in 1:npops){
if(is.character(new.populations[[yy]])){
#populations[[yy]] <- match(new.populations[[yy]],rownames(object@DATA[[xx]]@matrix_pol))
populations[[yy]] <- match(new.populations[[yy]],rownames(bial))
naids <- which(!is.na(populations[[yy]]))
populations[[yy]] <- populations[[yy]][naids]
}else{
populations[[yy]] <- new.populations[[yy]]
ids <- which(populations[[yy]]>dim(bial)[1])
if(length(ids)>0){ populations[[yy]] <- populations[[yy]][-ids]}
}
}
#----------------------#
temp <- delNULLpop(populations)
populations <- temp$Populations
popmissing <- temp$popmissing
#----------------------#
if(length(populations)==0){next} # Keine Population vorhanden
}else{
populations <- [email protected]@populations[[xx]] # if there is no new population
}
if(NEWPOP) {if(length(popmissing)!=0){respop <- (1:npops)[-popmissing]}else{respop <- 1:npops}} # nur die Populationen, die existieren
if(!NEWPOP) {if(length([email protected]@popmissing[[xx]])!=0){popmissing <- [email protected]@popmissing[[xx]];respop <- (1:npops)[-popmissing]}else{respop <- 1:npops}}
# important for Coalescent Simulation
# change@Pop_Linkage[[xx]] <- list(Populations=populations,Outgroup=NULL)
# ------------------- fill detail slots
res <- compute_intern_achaz(bial,populations,outgroup)
Achaz[xx,respop] <- res
# -------------------
# PROGRESS #######################################################
progr <- progressBar(xx,n.region.names, progr)
###################################################################
}
}
object@Yach <- Achaz
return(object)
})