forked from cran/PopGenome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_sxsfss.R
265 lines (203 loc) · 10.8 KB
/
calc_sxsfss.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
calc_sxsfss <- function(matrix_pol,populations,outgroup=FALSE,data){
if(outgroup[1]==FALSE){outgroup <- NULL}
#### if only one population is defined
if(length(populations)== 1 & (length(outgroup)==0|length(outgroup)==0)){return(list(NaN))}
#----------------------------------------------------------------------------------
if(length(outgroup)==0){outgroup<-NULL}
npops <- length(populations)
erg <- apply(matrix_pol,2,ispolmisX) # look at the whole matrix
rownames(erg) <- c("bial?","anc")
id <- which(erg["bial?",]==1) # 1 is polymorphic
### only polymorphic sites #### its not necessary because all columns in matrix_pol are polymorph
matrix_pol <- matrix_pol[,id,drop=FALSE]
colnames(matrix_pol) <- id
#-----------------------------#
# ------------------------------
# Check the outgroup
# Check if monomorph or not
# ------------------------------
if(length(outgroup)>0){ # Outgroup is defined
if(is.matrix(matrix_pol[outgroup,])){ outgr <- apply(matrix_pol[outgroup,],2,ispolmisX)}
if(is.vector(matrix_pol[outgroup,])){ outgr <- sapply(matrix_pol[outgroup,],ispolmisX)}
rownames(outgr) <- c("bial?","anc")
}
################################
## Check the populations
## Check if monomorphic or not.
## polqa ######################
popmatrix <- NULL
for(xx in 1:npops){
pop <- apply(matrix_pol[populations[[xx]],,drop=FALSE],2,ispolmisX)
popmatrix <- rbind(popmatrix,pop)
}# End of for
# INIT rownames-#
popnames <- paste("pop",1:npops)
ancnames <- paste("anc",1:npops)
NAMES <- as.vector(mapply(union,popnames,ancnames))
rownames(popmatrix) <- NAMES
colnames(popmatrix) <- id
# ------------- #
ancmatrix <- popmatrix[ancnames,,drop=FALSE]
popmatrix <- popmatrix[popnames,,drop=FALSE]
### polqb ##### ------------------------------------------------------------ # polqb #
if(npops>1){ # more than one population --> calculate poplqb
ohnepopmatrix <- NULL
for(xx in 1:npops){
without <- populations[[xx]]
pops <- unlist(populations)
Xid <- match(without,pops)
ohnepop <- pops[-Xid]
ohnexx <- matrix_pol[ohnepop,,drop=FALSE] # Betrachte die restlichen Pops ausser xx
poprest <- apply(ohnexx,2,ispolmisX)
ohnepopmatrix <- rbind(ohnepopmatrix,poprest)
}
rownames(ohnepopmatrix) <- NAMES
colnames(ohnepopmatrix) <- id
ancmatrixrest <- ohnepopmatrix[ancnames,,drop=FALSE]
popmatrixrest <- ohnepopmatrix[popnames,,drop=FALSE]
}# End if npops > 1 # ------------------------------------------------------ #
#init ---------------------------
comp1 <- vector("list",npops)
comp2 <- vector("list",npops)
comp3 <- vector("list",npops)
comp4 <- vector("list",npops)
comp5 <- vector("list",npops)
#-------------------------------
# attr(SX_SXF_SF_SS,"SX :") <- "I am polymorph rest is monomorph "
# attr(SX_SXF_SF_SS,"SXF:") <- "I am monomorph rest is polymorph "
# attr(SX_SXF_SF_SS,"SF:") <- "I am mono rest is mono with same mono value"
# attr(SX_SXF_SF_SS,"SS:") <- "I am mono rest is mono with different mono value"
# attr(SX_SXF_SF_SS,"SXX:") <- "I am poly rest is poly"
#-------------- Outgroup is defined ---------------------------------------- #
if(length(outgroup)>0){
polc <- outgr[1,] # polymorphic 1 or not 0
polc_anc <- outgr[2,] # outgroup mono value
}
#--------------------------------------------------------------------------- #
if(npops > 1 & length(outgroup)>0){ # mehr als eine Population und Outgroup definiert
for(xx in 1:npops){
polqa <- popmatrix[xx,]
polqa_anc <- ancmatrix[xx,]
polqb <- popmatrixrest[xx,]
polqb_anc <- ancmatrixrest[xx,]
# Wenn keine outgroup definiert polc = NaN
comp1[[xx]] <- as.numeric(names(which(polqa==1 & polqb==0 & polc==0 & (polqb_anc==polc_anc)))) # xx is polymorph rest is (monomorph) and identical with the OUTGROUP !!!
comp2[[xx]] <- as.numeric(names(which(polqa==1 & polqb==0 & polc==0 & polqb_anc!=polc_anc))) # xx is polymorph rest is monomorph, but not identical with the outgroup
comp3[[xx]] <- as.numeric(names(which(polqa==0 & polqb==0 & polc==0 & polqb_anc==polc_anc))) # xx is monomorph rest is monomorph and identical with the outgroup !!!
comp4[[xx]] <- as.numeric(names(which(polqa==1 & polqb==1 & polc==0))) # xx is polymorph , the rest is polymorph, outgroup is monomorph
comp1[[xx]] <- data$biallelic.sites[comp1[[xx]]]
comp2[[xx]] <- data$biallelic.sites[comp2[[xx]]]
comp3[[xx]] <- data$biallelic.sites[comp3[[xx]]]
comp4[[xx]] <- data$biallelic.sites[comp4[[xx]]]
}
} # End if if(npops > 1 & length(outgroup)>0)
if(npops==1 & length(outgroup)>0){ # mehr als eine Population und Outgroup ist nicht definiert
polqa <- popmatrix
polqa_anc <- ancmatrix
comp1[[xx]] <- as.numeric(names(which(polqa==1 & polc==0 ))) # xx is polymorph Outgroup is (monomorph)
comp2[[xx]] <- as.numeric(names(which(polqa==0 & polc==1))) # xx is monomorph Outgroup is polymorph
comp3[[xx]] <- as.numeric(names(which(polqa==0 & polc==0 & polqa_anc==polc_anc))) # xx is monomorph Outgroup is monomorph and have the same monomorph value
comp4[[xx]] <- as.numeric(names(which(polqa==0 & polc==0 & polqa_anc!=polc_anc))) # xx is monomorph Outgroup is monomorph but not with the same monomorph value
comp1[[xx]] <- data$biallelic.sites[comp1[[xx]]]
comp2[[xx]] <- data$biallelic.sites[comp2[[xx]]]
comp3[[xx]] <- data$biallelic.sites[comp3[[xx]]]
comp4[[xx]] <- data$biallelic.sites[comp4[[xx]]]
} # mehr als eine Population, aber keine Outgroup definiert
if(npops > 1 & length(outgroup)==0){ # mehr als eine Population, aber keine Outgroup ist definiert
for(xx in 1:npops){
polqa <- popmatrix[xx,]
polqa_anc <- ancmatrix[xx,]
polqb <- popmatrixrest[xx,]
polqb_anc <- ancmatrixrest[xx,]
comp1[[xx]] <- as.numeric(names(which(polqa==1 & polqb==0 ))) # xx is polymorph rest is (monomorph)
comp2[[xx]] <- as.numeric(names(which(polqa==0 & polqb==1))) # xx is monomorph rest is polymorph
comp3[[xx]] <- as.numeric(names(which(polqa==0 & polqb==0 & polqa_anc==polqb_anc))) # xx is monomorph rest is monomorph and have the same monomorph value
comp4[[xx]] <- as.numeric(names(which(polqa==0 & polqb==0 & polqa_anc!=polqb_anc))) # xx is monomorph rest is monomorph but not with the same monomorph value
# comp5[[xx]] <- as.numeric(names(which(polqa==1 & polqb==1))) # xx is polymorph rest is polymorph
comp1[[xx]] <- data$biallelic.sites[comp1[[xx]]]
comp2[[xx]] <- data$biallelic.sites[comp2[[xx]]]
comp3[[xx]] <- data$biallelic.sites[comp3[[xx]]]
comp4[[xx]] <- data$biallelic.sites[comp4[[xx]]]
# comp5[[xx]] <- data$biallelic.sites[comp5[[xx]]]
}
} # mehr als eine Population, aber keine Outgroup definiert
if(length(outgroup>0) & npops>1){
compout <- vector("list",3)
# ---- OUTGROUP ----------------------------------
# Look at the last population
ONE <- polc==1 & polqa==0 & polqb==0 & (polqb_anc==polqa_anc | npops==1) # Sxo --- Outgroup:polymorph, Populationen: alle identisch
TWO <- polc==1 & polqa==0 & polqb==0 & polqb_anc!=polqa_anc # Sanco --- Outgroup:polymorph, Pops:monomorph, popxxancester untersch..
THREE <- polc==1 & (polqa!=0 | polqb!=0) & (polqa!=-1 & polqb!=-1) # Sanco ---
FOUR <- polc==1 & (polqa!=0 | polqb!=0) & (polqa==-1 | polqb==-1) & (polqa==0 | polqb==0) # sxo---- Outgroup:polymorph,
FIVE <- polc==1 & (polqa!=0 | polqb!=0) & (polqa==-1 | polqb==-1) & (polqa!=0 & polqb!=0) # sanco
SIX <- polc==0 & polqa==0 & polqb==0 & polqb_anc==polqa_anc & polqa_anc!=polc_anc # Sfo --- Outgroup:monomorph, Pops: identisch ! aber unterschiedlich zur Outgroup
compout[[1]] <- as.numeric(names(which( ONE | FOUR ))) #sxo
compout[[2]] <- as.numeric(names(which( TWO | THREE | FIVE ))) #sanco
compout[[3]] <- as.numeric(names(which( SIX ))) #Sfo
compout[[1]] <- data$biallelic.sites[compout[[1]]]
compout[[2]] <- data$biallelic.sites[compout[[2]]]
compout[[3]] <- data$biallelic.sites[compout[[3]]]
compout <- as.matrix(compout)
}# End if Outroup is defined
# --------- RETURN VALUES -------------------------------------- #
if(npops>1 & length(outgroup)>0){
SX <- as.matrix(comp1)
SXF <- as.matrix(comp2)
SF <- as.matrix(comp3)
SS <- as.matrix(comp4)
SX_SXF_SF_SS <- cbind(SX,SXF,SF,SS)
rownames(SX_SXF_SF_SS) <- popnames
colnames(SX_SXF_SF_SS) <- c("SX","SXF","SF","SS")
attr(SX_SXF_SF_SS,"POPULATIONS:") <- ""
OUT <- compout
colnames(OUT) <- "Outgroup"
rownames(OUT) <- c("Sxo","Sanco","Sfo")
attr(OUT,"OUTGROUP:") <- "YES"
return(list(POP=SX_SXF_SF_SS,OUT=OUT))
}
if(npops>1 & length(outgroup)==0){
SX <- as.matrix(comp1)
SXF <- as.matrix(comp2)
SF <- as.matrix(comp3)
SS <- as.matrix(comp4)
# SXX <- as.matrix(comp5)
SX_SXF_SF_SS <- cbind(SX,SXF,SF,SS)
rownames(SX_SXF_SF_SS) <- popnames
colnames(SX_SXF_SF_SS) <- c("SX","SXF","SF","SS")
attr(SX_SXF_SF_SS,"POP:") <- " >1"
attr(SX_SXF_SF_SS,"OUTGROUP") <- "NO"
attr(SX_SXF_SF_SS,"SX :") <- "I am polymorph rest is monomorph "
attr(SX_SXF_SF_SS,"SXF:") <- "I am monomorph rest is polymorph "
attr(SX_SXF_SF_SS,"SF:") <- "I am mono rest is mono with same mono value"
attr(SX_SXF_SF_SS,"SS:") <- "I am mono rest is mono with different mono value"
return(list(POP=SX_SXF_SF_SS))
}
if(npops==1 & length(outgroup)>= 1){
SX <- as.matrix(comp1)
SXF <- as.matrix(comp2)
SF <- as.matrix(comp3)
SS <- as.matrix(comp4)
SX_SXF_SF_SS <- cbind(SX,SXF,SF,SS)
rownames(SX_SXF_SF_SS) <- popnames
colnames(SX_SXF_SF_SS) <- c("SX","SXF","SF","SS")
attr(SX_SXF_SF_SS,"POP:") <- " == 1"
attr(SX_SXF_SF_SS,"OUTGROUP") <- "YES"
attr(SX_SXF_SF_SS,"SX :") <- "I am polymorph Outgroup is monomorph "
attr(SX_SXF_SF_SS,"SXF:") <- "I am monomorph Outgroup is polymorph "
attr(SX_SXF_SF_SS,"SF:") <- "I am mono Outgroup is mono with same mono value"
attr(SX_SXF_SF_SS,"SS:") <- "I am mono Outgroup is mono with different mono value"
return(list(POP=SX_SXF_SF_SS))
}
}# End of Function
######################################################################
### FUNCTION: ISPOLMISX
### Already implemented in a another function # here with other value
######################################################################
ispolmisX <- function(vek){
gapids <- !is.na(vek)
vek <- vek[gapids]
ss <- unique(vek)
if(length(ss)==1) {return(c(0,as.numeric(ss)))} # monomorph
if(length(ss)==2) {return(c(1,NaN))} # biallelic
if(length(ss)==0) {return(c(-1,NaN))} # all are gaps
}