-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.R
194 lines (155 loc) · 6.91 KB
/
model.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
#### Recolonization model run script ####
## Jason Selwyn, Chris Bird
## Last Mod: 2019-18-02
rm(list=ls())
#read in command line arguments
arguments <- commandArgs(trailingOnly=TRUE)
parameters <- arguments[1]
outDir <- arguments[2]
library(parallel)
library(abind)
library(snow)
library(ggplot2)
#### Source Functions ####
source("model_functions.R")
source(parameters)
#### User Inputs from Parameters File ####
BS <- NUM_BOOTSTRAPS
RUN.MONTH <- MONTHS*MODEL_DURATION # Number of 12 months * number of years to run model
initial.females <- round(seq(min_f_number,max_f_number,length.out=f_bins)) # vector of number of starting female lionfish
proportion.successful.recruits <- seq(min_prop,max_prop,length.out=prop_bins)
np <- NP-1 #Number of processors to use
Demo.param <- c(1-ADULT.MORT,1-JUVI.MORT,1-ADULT.FRAC,BIN) #mortalities
if(!exists("FE.sd")){FE.sd <- 0}
RPR <- c(ADULT.FEM.FRAC,ADULT.FEM.MAT,FE,ME,DE,ML,DL,FE.sd,K) #Demographic parameters used to calculate recruit per individual in monthly time steps
#### Setup Data ####
if(exists("source.hap")){
haplotype.sources <- list(source.hap)
names(haplotype.sources) <- paste(source.name,'Haplotypes',sep='.')
}
if(exists("source.theta")){
all.thetas<-source.theta+(2:-2)*source.theta.sd
for(i in all.thetas){
if(i < 0.5){
all.thetas[which(all.thetas == i)] <- 0.5
}
}
source.dist<-as.list(all.thetas)
names(source.dist)<-paste(source.name,'theta',all.thetas,sep='.')
} else if(exists("source.thetas")){
all.thetas<-source.thetas
source.dist<-as.list(all.thetas)
names(source.dist)<-paste(source.name,'theta',all.thetas,sep='.')
}
if(exists("all.thetas")){
#CEB calculate how large to make matrix to hold haplotypes
HapMatrixLen <- round(1.25 * max(sapply(1:1000, function(x) length(rinfall(max(all.thetas),max(initial.females))))))
}
if(exists("source.hap") ){
if(exists("source.theta") || exists("source.thetas")){
sources <- c(source.dist,haplotype.sources)
} else {
sources <- c(haplotype.sources)
}
} else {
if(exists("source.theta") || exists("source.thetas")){
sources <- c(source.dist)
} else {
stop("Space Invaders Haulted, no source data provided")
}
}
haplotype.destinations<-list(destination.hap)
names(haplotype.destinations) <- destination.name
destinations<-c(haplotype.destinations)
#name lists and pack them together into one data structure
if(exists("dest.gendiv")){
names(dest.gendiv) <- c("gendiv","stdev")
dest.params <- dest.gendiv
} else if(exists("dest.theta")){
names(dest.theta) <- "theta"
dest.params <- dest.theta
} else {
dest.params <- NULL
}
for.workers<-ls()
#### Build MPI Cluster ####
cluster <- makeCluster(np,type="SOCK",outfile = paste(outDir,"/debug.txt",sep=""))
clusterExport(cl=cluster, as.list(for.workers),
envir=environment())
clusterEvalQ(cluster, {library(abind)})
#### Run the model ####
for(v in proportion.successful.recruits){
clusterExport(cl=cluster, list('v'),envir=environment())
for(S in 1:length(sources) ){
clusterExport(cl=cluster, list('S'),envir=environment())
s<-parallel::parSapply(cl=cluster, initial.females, function(x) replicate(BS, run.Model(FEMALE.START=x,hap.num.start.freq=sources[[S]],RUN.MONTH,Demo.param,RPR,verbose=FALSE,variable.RPR=v,THIN=thin,HapMatrixLen=HapMatrixLen),
simplify = "array"), simplify = 'array')
# s<-sapply(initial.females, function(x) replicate(BS, run.Model(FEMALE.START=x,hap.num.start.freq=sources[[S]],RUN.MONTH,Demo.param,RPR,F,variable.RPR=v),
# simplify = "array"), simplify = 'array')
print('Finished Cluster Simulations')
print(paste("s",format(object.size(s),units="Mb"),sep=' = '))
s<-s[,!apply(s,2,remove.0.haps),,]
print('Summary part 1 finished')
print(paste("s",format(object.size(s),units="Mb"),sep=' = '))
s<-aperm(s, c(1,2,4,3))
print('Summary part 2 finished')
print(paste("s",format(object.size(s),units="Mb"),sep=' = '))
names(dim(s))<-c('Months','Haplotypes','Initial Females','Bootstrap')
print('Summary part 3 finished')
print(paste("s",format(object.size(s),units="Mb"),sep=' = '))
f<-array(apply(s,c(3,4),freq.summary),dim=dim(s))
print('Summary finished')
print(paste("f",format(object.size(f),units="Mb"),sep=' = '))
save.image(paste('./', outDir,'/', names(sources)[S],'-rpr-',v,'_source.RData',sep=''))
print('Model Image Saved')
#### Make Model Overview Plots ####
#Problems with this resulting in this error:
#/home/apps/R/gcc/3.3.2/lib64/R/bin/BATCH: line 60: 94950 Killed ${R_HOME}/bin/R -f ${in} ${opts} ${R_BATCH_OPTIONS} > ${out} 2>&1
# pdf(paste('./', outDir,'/', names(sources)[S],'-rpr-',v,'-plots.pdf',sep=''),width=100,height=100,onefile = T)
#
# total.plot<-plotting.model(s,initial.females,type='total',mem.redux=T)
# print(total.plot)
# frequency.plot<-plotting.model(f,initial.females,type='freq',mem.redux=T)
# print(frequency.plot)
#
# dev.off()
#
# rm(s)
# rm(total.plot)
# rm(frequency.plot)
# gc()
#
# print('Finished simulation plots')
#### Demographic Plots ####
pdf(paste('./', outDir,'/', names(sources)[S],'-rpr-',v,'-demographic_plots.pdf',sep=''),onefile = T)
#loop through number of starting females
for(numfem in initial.females){
tmp_s <- s[,,which(initial.females == numfem),]
#this takes mean pop size across all bootstrap at each time point
plot(apply(tmp_s[,,], 1, sum)/dim(tmp_s)[3], main=paste(names(sources)[S],'-rpr-',v," Colonists = ", numfem, sep=""),xlab="Months Since Colonization",ylab="Destination Population Size" )
#print(demoplot)
}
dev.off()
#### Statistical Analysis ####
pdf(paste('./', outDir,'/', names(sources)[S],'-rpr-',v,'-statistical_plots.pdf',sep=''),onefile = T)
for(D in 1:length(destinations)){
stats.output<-model.statistics(destination.haplotypes=destinations[[D]],model.freq=f,dest.params=dest.params)
statistical.plots<-plotting.statistics(stats.output,start.females=initial.females,title=paste(names(sources)[S],'to',names(destinations)[D]))
print(statistical.plots[[1]])
print(statistical.plots[[2]])
print(statistical.plots[[3]])
print(statistical.plots[[4]])
print(statistical.plots[[5]])
write.csv(statistical.plots[[6]],paste('./', outDir,'/', names(sources)[S],'-to-',names(destinations)[D],'-rpr-',v,'-plot.data.csv',sep=''))
}
dev.off()
print('Finished statistical plots')
rm(s)
rm(f)
rm(statistical.plots)
gc()
print(paste('Finished',names(sources)[S]))
}
}
#### Stop Cluster ####
stopCluster(cluster)