forked from PeterEmmrich/Heterosis-GRN-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnatSelect.r
101 lines (95 loc) · 4.33 KB
/
natSelect.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
#have edited lines 16 and 23 for the allele matrix
natural.diploid.selection<-function(ntwk.list,reps,max.popn,cull.frac,gene.list,startStates,func="avg",envmts,emph=NULL){
#ntwk.list is expected to be a list of boolean networks
n<-length(ntwk.list)
phenotype<-vector('numeric',n)
if (is.null(emph)){
emph<-rep(1,length(envmts))
}
#current startStates that are input here should have the same length as the original gametes
for (i in 1:n){
env.positions<-grep("e",ntwk.list[[i]]$genes)
#calculate the startStates for this diploid
new.startStates<-matrix(0,length(ntwk.list[[i]]$genes),reps)
for (j in 1:length(ntwk.list[[i]]$genes)){
if (sum(j==env.positions)>0){#set env startStates
#which envnode is this?
envnode<-as.integer(strsplit(strsplit(ntwk.list[[i]]$genes[j],split="_")[[1]][1],split="e")[[1]][2])
new.startStates[j,]<-startStates[envnode,]
}
else{
allele<-as.integer(strsplit(strsplit(ntwk.list[[i]]$genes[j],split="_")[[1]][1],split="g")[[1]][2])#this is what might also be called the gene 'group'
new.startStates[j,]<-startStates[allele,]
}
}
#convert startStates to list form
new.startStates<-as.list(as.data.frame(new.startStates))
phenotype[i]<-calculate.diploid.phenotype(ntwk.list[[i]],reps,new.startStates,gene.list=gene.list,func=func,envmts,emph)
}
num<-min(max.popn,ceiling(n*(1-cull.frac)))
ordered.phtype<-sort(phenotype,decreasing=TRUE)
threshold<-ordered.phtype[num]
keep<-which(phenotype>=threshold)[1:num]
new.ntwk.list<-vector('list',num)
for (i in 1:num){
new.ntwk.list[[i]]<-ntwk.list[[keep[i]]]
}
#cat("average phenotype of this generation: ",mean(phenotype),"\n")
#cat("average phenotype of selected networks: ",mean(phenotype[keep]),"\n")
return(list(phenotypes=phenotype,ntwks=new.ntwk.list,keep=keep,selectedAvg=mean(phenotype[keep]),wholeAvg=mean(phenotype)))
}
natural.selection.MH<-function(ntwk.list,reps,max.popn,cull.frac,gene.list,startStates,func="avg",envmts,emph=NULL,old.pheno){#not suitable for diploids atm
#ntwk.list is expected to be a list of boolean networks
n<-length(ntwk.list)
phenotype<-vector('numeric',n)
if (is.null(emph)){
emph<-rep(1,length(envmts))
}
#current startStates that are input here should have the same length as the original gametes
for (i in 1:n){
env.positions<-grep("e",ntwk.list[[i]]$genes)
#calculate the startStates for this diploid
new.startStates<-matrix(0,length(ntwk.list[[i]]$genes),reps)
for (j in 1:length(ntwk.list[[i]]$genes)){
if (sum(j==env.positions)>0){#set env startStates
#which envnode is this?
envnode<-as.integer(strsplit(strsplit(ntwk.list[[i]]$genes[j],split="_")[[1]][1],split="e")[[1]][2])
new.startStates[j,]<-startStates[envnode,]
}
else{
allele<-as.integer(strsplit(strsplit(ntwk.list[[i]]$genes[j],split="_")[[1]][1],split="g")[[1]][2])#this is what might also be called the gene 'group'
new.startStates[j,]<-startStates[allele,]
}
}
#convert startStates to list form
new.startStates<-as.list(as.data.frame(new.startStates))
phenotype[i]<-calculate.diploid.phenotype(ntwk.list[[i]],reps,new.startStates,gene.list=gene.list,func=func,envmts,emph)
}
num<-min(max.popn,ceiling(n*(1-cull.frac)))
ordered.phtype<-sort(phenotype,decreasing=TRUE)
#we find out the average selected phenotype of the previous generation.
#then pick a network, accept it with exponential probability depending on the difference between it's pheno type and the old average - definitely select if pheno is better.
#keep doing this until we've selected max.popn networks to keep.
#Trying different things out atm
kept<-0
keep<-vector('numeric',num)
new.ntwk.list<-vector('list',num)
while (kept<num){
rand<-runif(1)
new.ntwk.index<-sample(1:length(phenotype),1)
new.pheno<-phenotype[new.ntwk.index]
if (exp(1000*(new.pheno-old.pheno))>rand){
keep[kept+1]<-new.ntwk.index
new.ntwk.list[[kept+1]]<-ntwk.list[[new.ntwk.index]]
kept<-kept+1
}
else{
keep[kept+1]<-which(phenotype==ordered.phtype[kept+1])[1]
new.ntwk.list[[kept+1]]<-ntwk.list[[keep[kept+1]]]
kept<-kept+1
}
}
#cat("average phenotype of this generation: ",mean(phenotype),"\n")
#cat("average phenotype of selected networks: ",mean(phenotype[keep]),"\n")
return(list(phenotypes=phenotype,ntwks=new.ntwk.list,keep=keep,selectedPhenos=phenotype[keep],selectedAvg=mean(phenotype[keep]),wholeAvg=mean(phenotype)))
}