-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunBactDat.R
223 lines (175 loc) · 7.17 KB
/
runBactDat.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
#!/usr/bin/env Rscript
library(ape)
library(BactDating)
## Collect arguments
args = commandArgs(trailingOnly=TRUE)
if ('-r' %in% args || '--random' %in% args) {
randomize = TRUE
args = args[!args %in% c('-r','--random')]
} else {
randomize = FALSE
}
if ('-f' %in% args || '--fixMu' %in% args) {
updateMu = FALSE
args = args[!args %in% c('-f','--fixMu')]
} else {
updateMu = TRUE
}
## Default setting when no arguments passed
if(length(args) < 1) {
args <- c("--help")
}
# Help section
if("--help" %in% args || "-h" %in% args || "-?" %in% args) {
cat("
About: Wrapper for BactDating
Usage: ./runBactDat [OPTIONS] ml.tree
./runBactDat -d outdir/ ml.tree
Options:
-i, --input <file> - ML tree. Dates separated by _ at the end of name.
-r, --random - Randomize dates.
-d, --dir <path> - Directory for the output. Add slash to create new directory.
Default: current working directory
-p, --prefix <str> - Prefix for the output files. Default: input name + BactDating
-t, --sample_times <file> - File with dates.
If absent, it will try to get them from the name (last field separated by '_').
File format: no header, two columns tab separated: id date
-l, --length <int> - Length of the alignment used for the tree.
-m, --model <str> - Model to use. Options: poisson, negbin, strictgamma, relaxedgamma, [mixedgamma]
-n, --nIter <int> - Number of MCMC iterations to perform [10e4]
-M, --initMu <int> - Initial rate of substitutions per genome (not per site),
or zero to use root-to-tip estimate [NA]
-f, --fixMu - If present, don't update the mutation rate [FALSE]
-u, --updateRoot - TRUE/FALSE to update the root (default: FALSE for rooted trees, TRUE for unrooted trees)
-b, --minbralen - Minimum branch length for the input tree (in number of substitutions)
Default: Minimum branch length of tree
-S --initSigma - Initial std on per-branch substitution rate
-A --initAlpha - Initial coalescent time unit
-s, --showProgress - TRUE/FALSE to show progress bar (default: FALSE)
-T, --thinInt - Thinning interval (default: ceiling(nIter/1000))
-h, -?, --help - This help message.
\n")
q(save="no")
}
## Parse arguments (we expect the form --arg value or -a value)
args = paste(args[c(TRUE, FALSE)],args[c(FALSE, TRUE)])
parseArgs <- function(x) strsplit(sub("-*", "", x), " ")
argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
argsL <- as.list(as.character(argsDF$V2))
names(argsL) <- argsDF$V1
#Set the working directory or create a new one
wd = ifelse ("d" %in% names(argsL) || "dir" %in% names(argsL),
argsL[names(argsL) %in% c('d','dir')][[1]],
getwd())
if (strsplit(wd, "")[[1]][length(strsplit(wd,"")[[1]])] == '/'){
if (!dir.exists(file.path(wd))) {dir.create(wd)}
} else {
wd = paste(wd,'/',sep="")
}
input_tree = argsL[names(argsL) %in% c('i','input')][[1]]
alignment_length = as.numeric(argsL[names(argsL) %in% c('l','length')][[1]])
prefix = ifelse ("p" %in% names(argsL) || "prefix" %in% names(argsL),
argsL[names(argsL) %in% c('p','prefix')][[1]],
paste(input_tree,'_BactDating', sep=''))
dates_file = ifelse ("t" %in% names(argsL) || "sample_times" %in% names(argsL),
argsL[names(argsL) %in% c('t','sample_times')][[1]],
FALSE)
model = ifelse ("m" %in% names(argsL) || "model" %in% names(argsL),
argsL[names(argsL) %in% c('m','model')][[1]],
'mixedgamma')
nIter = ifelse ("n" %in% names(argsL) || "nIter" %in% names(argsL),
argsL[names(argsL) %in% c('n','nIter')][[1]],
10e4)
nIter = as.integer(nIter)
initMu = ifelse ("M" %in% names(argsL) || "initMu" %in% names(argsL),
as.numeric(argsL[names(argsL) %in% c('M','initMu')][[1]]),
NA)
updateRoot = ifelse ("u" %in% names(argsL) || "updateRoot" %in% names(argsL),
argsL[names(argsL) %in% c('u','updateRoot')][[1]],
NA)
minbralen = ifelse ("b" %in% names(argsL) || "minbralen" %in% names(argsL),
argsL[names(argsL) %in% c('b','minbralen')][[1]],
0.1)
initAlpha = ifelse ("A" %in% names(argsL) || "initAlpha" %in% names(argsL),
argsL[names(argsL) %in% c('A','initAlpha')][[1]],
NA)
initSigma = ifelse ("S" %in% names(argsL) || "initSigma" %in% names(argsL),
argsL[names(argsL) %in% c('S','initSigma')][[1]],
NA)
showProgress = ifelse ("s" %in% names(argsL) || "showProgress" %in% names(argsL),
argsL[names(argsL) %in% c('s','showProgress')][[1]],
FALSE)
thinInt = ifelse ("T" %in% names(argsL) || "thinInt" %in% names(argsL),
argsL[names(argsL) %in% c('T','thinInt')][[1]],
ceiling(nIter/1000))
thinInt = as.numeric(thinInt)
minbralen = as.numeric(minbralen)
initMu = as.numeric(initMu)
if (!is.na(initAlpha)){initAlpha = as.numeric(initAlpha)}
if (!is.na(initSigma)){initSigma = as.numeric(initSigma)}
tracefile = paste(wd, prefix, '_traces.txt', sep='')
#####################
####### CODE #######
####################
tree = read.tree(input_tree)
# Check whether to update root or not
if (is.na(updateRoot)) {
updateRoot = !is.rooted(tree)
}
# Check if length is in snps/site or total number of snps.
if (sum(tree$edge.length) < 2) {
tree$edge.length=tree$edge.length*alignment_length
}
if (!isFALSE(dates_file)){
dates = read.table(dates_file, header = F, stringsAsFactors = F)
dates = setNames(dates$V2, dates$V1)
} else {
dates = sapply(tree$tip.label, function (x){as.integer(strsplit(x, '_')[[1]][length(strsplit(x, '_')[[1]])])})
}
if (randomize) {
print ('This is gonna be random')
dates_names = names(dates)
dates_random = sample(unname(dates), replace=F)
names(dates_random) = dates_names
dates = dates_random
}
if (isFALSE(minbralen)){
minbralen = min(tree$edge.length)
}
# Run BactDating
print (paste(
'Running BactDating with ',
nIter,
' iterations, with the ',
model,
' model. Starting mutation rate: ',
initMu,
'. Minimum branch length: ',
minbralen,
'. Update the mutation rate: ',
updateMu,
'. Starting Sigma: ',
initSigma,
'. Starting Alpha: ',
initAlpha,
'. Update root: ',
updateRoot,
'. Recording samples every ',
thinInt,
' iterations.',
sep=''
))
res=bactdate(tree = tree,
date = dates,
nbIts = nIter,
model = model,
initMu = initMu,
initSigma = initSigma,
initAlpha = initAlpha,
updateMu = updateMu,
updateRoot = updateRoot,
minbralen = minbralen,
showProgress = showProgress)
# OUTPUT
save(res, file = paste(wd, prefix, '_res.RData', sep=''))
print (res)