trio行列は1行に1人
my.pedigree <- function(n.init=3,n.iter=20,mean.kid=2){
trio <- matrix(0,n.init,5)
m <- length(trio[1,])
trio[1:n.init,1] <- 1:n.init
trio[1:n.init,4] <- sample(c(-1,1),n.init,replace=TRUE)
trio[,5] <- 0
cnt <- n.init
tmp.f <- function(m,k){
(m/(1-exp(-m))-k)^2
}
xmin <- optimize(tmp.f, c(0, mean.kid), tol = 0.0001, k = mean.kid)
mean.kid. <- xmin[[1]]
for(i in 1:n.iter){
s <- sample(trio[,1],1)
if(length(which(trio[,2:3]==s))==0){
trio <- rbind(trio,c(cnt+1,rep(0,m-1)))
dp <- dpois(1:100,mean.kid.)
dp <- dp/sum(dp)
num.kids <- sample(1:100,1,prob=dp)
tmp <- matrix(0,num.kids,m)
tmp[,1] <- cnt+1+(1:num.kids)
if(trio[s,4]==1){
tmp[,2] <- s
tmp[,3] <- cnt+1
}else{
tmp[,2] <- cnt+1
tmp[,3] <- s
}
tmp[,4] <- sample(c(-1,1),num.kids,replace=TRUE)
tmp[,5] <- trio[s,5] + 1
trio <- rbind(trio,tmp)
trio[cnt+1,4] <- -trio[s,4]
trio[cnt+1,5] <- trio[s,5]
cnt <- cnt+1+num.kids
}else if(trio[s,2] == 0 & trio[s,3] == 0){
trio <- rbind(trio,c(cnt+1,rep(0,m-1)))
trio <- rbind(trio,c(cnt+2,rep(0,m-1)))
trio[cnt+1,4] <- 1
trio[cnt+2,4] <- -1
trio[c(cnt+1,cnt+2),5] <- trio[s,5]-1
trio[s,2] <- cnt+1
trio[s,3] <- cnt+2
cnt <- cnt+2
}
}
trio
}
make.genotype.auto <- function(trio,p.a){
prob <- c(p.a^2,2*p.a*(1-p.a),(1-p.a)^2)
make.genotype(trio,prob)
}
make.genotype.x <- function(trio,p.a){
prob.male <- c(p.a,0,1-p.a)
prob.female <- c(p.a^2,2*p.a*(1-p.a),(1-p.a)^2)
ret <- rep(0,length(trio[,1]))
ord <- order(trio[,5])
for(i in 1:length(ord)){
if(trio[ord[i],2]==0 & trio[ord[i],3]==0){
if(trio[ord[i],4]==1){
ret[ord[i]] <- sample(c(0,1,2),1,prob=prob.male)
}else if(trio[ord[i],4]==-1){
ret[ord[i]] <- sample(c(0,1,2),1,prob=prob.female)
}
}else{
parents <- trio[ord[i],2:3]
pat <- ret[trio[ord[i],2]]/2
mat <- ret[trio[ord[i],3]]/2
if(pat==0.5){
pat <- sample(c(0,1),1)
}
if(mat==0.5){
mat <- sample(c(0,1),1)
}
if(trio[ord[i],4]==1){
ret[ord[i]] <- mat * 2
}else if(trio[ord[i],4]==-1){
ret[ord[i]] <- mat + pat
}
}
}
ret
}
make.genotype.y <- function(trio,p.a){
prob.male <- c(p.a,0,1-p.a)
prob.female <- c(1,0,0)
ret <- rep(0,length(trio[,1]))
ord <- order(trio[,5])
for(i in 1:length(ord)){
if(trio[ord[i],2]==0 & trio[ord[i],3]==0){
if(trio[ord[i],4]==1){
ret[ord[i]] <- sample(c(0,1,2),1,prob=prob.male)
}else if(trio[ord[i],4]==-1){
ret[ord[i]] <- sample(c(0,1,2),1,prob=prob.female)
}
}else{
parents <- trio[ord[i],2:3]
pat <- ret[trio[ord[i],2]]/2
if(pat==0.5){
pat <- sample(c(0,1),1)
}
if(trio[ord[i],4]==1){
ret[ord[i]] <- pat * 2
}else if(trio[ord[i],4]==-1){
ret[ord[i]] <- 0
}
}
}
ret
}
make.genotype.mit <- function(trio,p.a){
prob.male <- c(p.a,0,1-p.a)
prob.female <- c(p.a,0,1-p.a)
ret <- rep(0,length(trio[,1]))
ord <- order(trio[,5])
for(i in 1:length(ord)){
if(trio[ord[i],2]==0 & trio[ord[i],3]==0){
if(trio[ord[i],4]==1){
ret[ord[i]] <- sample(c(0,1,2),1,prob=prob.male)
}else if(trio[ord[i],4]==-1){
ret[ord[i]] <- sample(c(0,1,2),1,prob=prob.female)
}
}else{
parents <- trio[ord[i],2:3]
mat <- ret[trio[ord[i],3]]/2
if(trio[ord[i],4]==1){
ret[ord[i]] <- mat * 2
}else if(trio[ord[i],4]==-1){
ret[ord[i]] <- mat * 2
}
}
}
ret
}
make.genotype <- function(trio,prob){
ret <- rep(0,length(trio[,1]))
ord <- order(trio[,5])
for(i in 1:length(ord)){
if(trio[ord[i],2]==0 & trio[ord[i],3]==0){
ret[ord[i]] <- sample(c(0,1,2),1,prob=prob)
}else{
parents <- trio[ord[i],2:3]
pat <- ret[trio[ord[i],2]]/2
mat <- ret[trio[ord[i],3]]/2
if(pat==0.5){
pat <- sample(c(0,1),1)
}
if(mat==0.5){
mat <- sample(c(0,1),1)
}
ret[ord[i]] <- pat+mat
}
}
ret
}
my.phenotype.score <- function(g,w=c(0,0.5,1)){
ret <- rep(0,length(g))
for(i in 1:3){
ret[g==(i-1)] <- ret[g==(i-1)] + w[i]
}
ret
}
my.phenotype.score.dom <- function(g,comple=FALSE){
if(comple){
return(my.phenotype.score(g,w=c(1,0,0)))
}
my.phenotype.score(g,w=c(0,1,1))
}
my.phenotype.score.rec <- function(g,comple=FALSE){
if(comple){
return(my.phenotype.score(g,w=c(1,1,0)))
}
my.phenotype.score(g,w=c(0,0,1))
}
my.change.sex.val <- function(s){
s[which(s==-1)] <- 2
return(s)
}
library(kinship2)
my.plot.pedigree <- function(my.ped,af,sex.change=TRUE,s.size=1){
if(sex.change){
my.ped[,4] <- my.change.sex.val(my.ped[,4])
}
my.ped <- pedigree(my.ped[,1],my.ped[,2],my.ped[,3],my.ped[,4],af)
plot(my.ped,symbolsize=s.size)
}
n.init <- 5
n.iter <- 20
mean.kid <- 5
my.ped <- my.pedigree(n.init=n.init,n.iter=n.iter,mean.kid=mean.kid)
af.auto <- 0.8
g.auto <- make.genotype.auto(my.ped,af.auto)
par(mfcol=c(1,2))
my.plot.pedigree(my.ped,af=my.phenotype.score.dom(g.auto))
my.plot.pedigree(my.ped,af=my.phenotype.score.dom(g.auto,comple=TRUE))
af.x <- 0.8
g.x <- make.genotype.x(my.ped,af.x)
my.plot.pedigree(my.ped,af=my.phenotype.score.dom(g.x))
my.plot.pedigree(my.ped,af=my.phenotype.score.dom(g.x,comple=TRUE))
my.plot.pedigree(my.ped,af=my.phenotype.score.rec(g.x))
my.plot.pedigree(my.ped,af=my.phenotype.score.rec(g.x,comple=TRUE))
af.y <- 0.3
g.y <- make.genotype.y(my.ped,af.y)
my.plot.pedigree(my.ped,af=my.phenotype.score.dom(g.y))
my.plot.pedigree(my.ped,af=my.phenotype.score.dom(g.y,comple=TRUE))
af.mit <- 0.8
g.mit <- make.genotype.mit(my.ped,af.mit)
my.plot.pedigree(my.ped,af=my.phenotype.score.dom(g.mit))
my.plot.pedigree(my.ped,af=my.phenotype.score.dom(g.mit,comple=TRUE))