- Brief comments in English.
Na<-4
Pa<-c(0.3,0.2,0.1,0.4)
Pa<-Pa/sum(Pa)
Pa<-runif(Na)
install.packages("MCMCpack")
library(MCMCpack)
Pa<-rdirichlet(1,rep(1,Na))
Pa
sum(Pa)
Ns<-4
NaS<-c(3,4,5,2)
PaS<-list()
for(i in 1:Ns){
PaS[[i]]<-c(rdirichlet(1,rep(1,NaS[i])))
}
NaS
PaS
hap1<-hap2<-rep(0,Ns)
for(i in 1:Ns){
hap1[i]<-sample(1:NaS[i],1,prob=PaS[[i]])
hap2[i]<-sample(1:NaS[i],1,prob=PaS[[i]])
}
hap1
hap2
MakeHaplotype<-function(NaS,PaS,N){
H<-matrix(0,N,length(NaS))
for(i in 1:length(NaS)){
H[,i]<-sample(1:NaS[i],N,prob=PaS[[i]])
}
H
}
N<-2
Haps<-MakeHaplotype(NaS,PaS,N)
Haps
Dg<-runif(Ns-1)
rands.crossing<-runif(length(Dg))
Cross<-which((Dg-rands.crossing)>0)
Cross
hap1<-Haps[1,]
hap2<-Haps[2,]
rec.hap1<-hap1
rec.hap2<-hap2
if(length(Cross)>0){
for(i in 1:length(Cross)){
tmp.tail1<-rec.hap1[(Cross[i]+1):Ns]
tmp.tail2<-rec.hap2[(Cross[i]+1):Ns]
rec.hap1[(Cross[i]+1):Ns]<-tmp.tail2
rec.hap2[(Cross[i]+1):Ns]<-tmp.tail1
}
}
Cross
hap1
hap2
rec.hap1
rec.hap2
Rec.Haplotype<-function(hap1,hap2,d){
Ns<-length(hap1)
rands.crossing<-runif(length(d))
Cross<-which((d-rands.crossing)>0)
rec.hap1<-hap1
rec.hap2<-hap2
if(length(Cross)>0){
for(i in 1:length(Cross)){
tmp.tail1<-rec.hap1[(Cross[i]+1):Ns]
tmp.tail2<-rec.hap2[(Cross[i]+1):Ns]
rec.hap1[(Cross[i]+1):Ns]<-tmp.tail2
rec.hap2[(Cross[i]+1):Ns]<-tmp.tail1
}
}
matrix(c(rec.hap1,rec.hap2),byrow=TRUE,nrow=2)
}
rec.haps<-Rec.Haplotype(Haps[1,],Haps[2,],Dg)
rec.haps
father<-MakeHaplotype(NaS,PaS,2)
mother<-MakeHaplotype(NaS,PaS,2)
father
mother
father.gamates<-Rec.Haplotype(father[1,],father[2,],Dg)
mother.gamates<-Rec.Haplotype(mother[1,],mother[2,],Dg)
from.father<-father.gamates[sample(1:2,1),]
from.mother<-mother.gamates[sample(1:2,1),]
offspring<-matrix(c(from.father,from.mother),byrow=TRUE,nrow=2)
offspring
father
mother
offspring
MakeOffspring<-function(father,mother,d){
father.gamates<-Rec.Haplotype(father[1,],father[2,],d)
mother.gamates<-Rec.Haplotype(mother[1,],mother[2,],d)
from.father<-father.gamates[sample(1:2,1),]
from.mother<-mother.gamates[sample(1:2,1),]
offspring<-matrix(c(from.father,from.mother),byrow=TRUE,nrow=2)
offspring
}
offspring<-MakeOffspring(father,mother,Dg)
father
mother
offspring
M.Oyako.Trio<-matrix(c(0,0,0,0,1,2),byrow=TRUE,ncol=2)
M.Oyako.Trio
M.3gen<-matrix(c(0,0,0,0,0,0,0,0,1,2,3,4,5,6),byrow=TRUE,ncol=2)
M.3gen
M.cousins<-matrix(c(0,0,0,0,1,2,0,0,1,2,0,0,3,4,5,6),byrow=TRUE,ncol=2)
M.cousins
sim.G<-list()
for(i in 1:n.rows){
tmp.father<-M.cousins[i,1]
tmp.mother<-M.cousins[i,2]
if(tmp.father==0){
father.genotype<-MakeHaplotype(NaS,PaS,2)
}else{
father.genotype<-sim.G[[tmp.father]]
}
if(tmp.mother==0){
mother.genotype<-MakeHaplotype(NaS,PaS,2)
}else{
mother.genotype<-sim.G[[tmp.mother]]
}
sim.G[[i]]<-MakeOffspring(father.genotype,mother.genotype,Dg)
}
sim.G
M.cousins
Simulate.Genotype<-function(M,NaS,PaS,Dg){
sim.G<-list()
for(i in 1:n.rows){
tmp.father<-M.cousins[i,1]
tmp.mother<-M.cousins[i,2]
if(tmp.father==0){
father.genotype<-MakeHaplotype(NaS,PaS,2)
}else{
father.genotype<-sim.G[[tmp.father]]
}
if(tmp.mother==0){
mother.genotype<-MakeHaplotype(NaS,PaS,2)
}else{
mother.genotype<-sim.G[[tmp.mother]]
}
sim.G[[i]]<-MakeOffspring(father.genotype,mother.genotype,Dg)
}
sim.G
}
sim.G<-Simulate.Genotype(M.cousins,NaS,PaS,Dg)
sim.G
num.Chromosomes<-22
NaS.list<-list()
PaS.list<-list()
Dg.list<-list()
for(i in 1:num.Chromosomes){
NaS.list[[i]]<-NaS
PaS.list[[i]]<-PaS
Dg.list[[i]]<-Dg
}
sim.Gs<-list()
for(i in 1:num.Chromosomes){
sim.Gs[[i]]<-Simulate.Genotype(M.cousins,NaS.list[[i]],PaS.list[[i]],Dg.list[[i]])
}
Simulate.Genotype.Multi<-function(M,NaS.list,PaS.list,Dg.list){
num.Chromosomes<-length(NaS.list)
sim.Gs<-list()
for(i in 1:num.Chromosomes){
sim.Gs[[i]]<-Simulate.Genotype(M.cousins,NaS.list[[i]],PaS.list[[i]],Dg.list[[i]])
}
sim.Gs
}
sim.Gs<-Simulate.Genotype.Multi(M.cousins,NaS.list,PaS.list,Dg.list)
Niter<-10
sim.Gs.list<-list()
for(i in 1:Niter){
sim.Gs.list[[i]]<-Simulate.Genotype.Multi(M.cousins,NaS.list,PaS.list,Dg.list)
}