/
mrbayes.py
533 lines (418 loc) · 14.2 KB
/
mrbayes.py
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/python
""" wrapper to make simple calls to mb """
import os
import sys
import subprocess
import pandas as pd
from ipyrad.assemble.utils import Params
from ipyrad.assemble.utils import IPyradError
try:
import toytree
except ImportError:
pass
# template for standard tree inference
NEX_TEMPLATE_1 = """\
#NEXUS
log start filename={outname}.log replace;
execute {nexus};
begin mrbayes;
set autoclose=yes nowarn=yes;
lset nst=6 rates=gamma;
{constraints}
mcmcp ngen={ngen} nrun={nruns} nchains={nchains};
mcmcp relburnin=yes burninfrac=0.25;
mcmcp samplefreq={samplefreq} printfreq=10000;
mcmcp filename={outname};
mcmc;
sump filename={outname};
sumt filename={outname};
end;
"""
# template for clock model tree inference
# https://www.groundai.com/project/molecular-clock-dating-using-mrbayes/
TEMPLATE_2_DICT = {
"brlenspr": "clock:birthdeath",
"clockratepr": "lognorm(-7,0.6)",
"clockvarpr": "tk02",
"tk02varpr": "exp(1.0)",
"samplestrat": "diversity",
"sampleprob": "0.1",
"speciationpr": "beta(2, 200)",
"treeagepr": "offsetexp(1,5)",
}
NEX_TEMPLATE_2 = """\
#NEXUS
log start filename={outname}.log replace;
execute {nexus};
begin mrbayes;
set autoclose=yes nowarn=yes;
lset nst=6 rates=gamma;
prset brlenspr={brlenspr};
prset clockratepr={clockratepr};
prset clockvarpr={tk02varpr};
prset tk02varpr={tk02varpr};
prset samplestrat={samplestrat};
prset sampleprob={sampleprob};
prset speciationpr={speciationpr};
prset extinctionpr={extinctionpr};
prset treeagepr={treeagepr};
{constraints}
mcmcp ngen={ngen} nrun={nruns} nchains={nchains};
mcmcp relburnin=yes burninfrac=0.25;
mcmcp samplefreq={samplefreq};
mcmcp printfreq=10000 diagnfr=5000;
mcmcp filename={outname};
mcmc;
sump filename={outname};
sumt filename={outname};
end;
"""
# template for clock model tree inference
# https://www.groundai.com/project/molecular-clock-dating-using-mrbayes/
TEMPLATE_3_DICT = {
"brlenspr": "clock:uniform",
"clockvarpr": "igr",
"igrvarpr": "exp(10.0)",
"clockratepr": "normal(0.01,0.005)",
"topologypr": "uniform"
}
NEX_TEMPLATE_3 = """\
#NEXUS
[log block]
log start filename={outname}.log replace;
[data block]
execute {nexus};
[tree block]
{treeblock}
[mb block]
begin mrbayes;
set autoclose=yes nowarn=yes;
lset nst=6 rates=gamma;
prset brlenspr={brlenspr};
prset clockvarpr={clockvarpr};
prset igrvarpr={igrvarpr};
prset clockratepr={clockratepr};
prset topologypr={topologypr};
{constraints}
mcmcp ngen={ngen} nrun={nruns} nchains={nchains};
mcmcp relburnin=yes burninfrac=0.25;
mcmcp samplefreq={samplefreq};
mcmcp printfreq=10000 diagnfr=5000;
mcmcp filename={outname};
mcmc;
sump filename={outname};
sumt filename={outname} contype=allcompat;
end;
[log block]
log stop filename={outname}.log append;
"""
class MrBayes(object):
"""
MrBayes analysis utility function for running simple commands.
Parameters:
-----------
data: str
The phylip formated sequence file (.phy from ipyrad).
name: str
The name for this run. An alias for '-n'.
workdir: str
The output directory for results. An alias for '-w'.
force: bool
Overwrite/rm any existing mb results with this workdir/name prefix.
Additional optional parameters
-------------------------------
ngen: int
Number of MCMC generations to run.
sfreq: int
Frequency to sample from MCMC chain.
burnin: int
Number of generations to run before sampling starts.
clock_model: int
0, 1, or 2 define a set of parameters for a relaxed molecular clock
analysis that can then be further modified.
constraints: dict or ToyTree
A dictionary mapping {constraint_names: [list of tips]}. To
constrain an entire tree you can enter a tree and a dict will
automatically be built to describe the tree structure.
Attributes:
-----------
params: dict
parameters for this mb run
cmd:
returns the command string to run mb
Functions:
----------
run()
submits a mrbayes job locally or on an ipyparallel client cluster.
"""
# init object for params
def __init__(
self,
data,
name="test",
workdir="analysis-mb",
clock_model=False,
constraints=None,
**kwargs):
# path attributes
self._kwargs = {}
self._kwargs.update(kwargs)
# check workdir
if workdir:
workdir = os.path.abspath(os.path.expanduser(workdir))
else:
workdir = os.path.abspath(os.path.curdir)
if not os.path.exists(workdir):
os.makedirs(workdir)
# entered args
self.clock_model = clock_model
self.constraints = constraints
self.name = name
self.workdir = workdir
self.data = os.path.abspath(os.path.expanduser(data))
self.nexus = os.path.join(self.workdir, self.name + ".nex")
self.binary = ""
self._get_binary(self._kwargs.get("binary"))
self.params = Params()
defaults = {
"ngen": 100000,
"nruns": "1",
"nchains": 4,
"samplefreq": 1000,
}
if self.clock_model == 1:
defaults.update(TEMPLATE_2_DICT)
elif self.clock_model == 2:
defaults.update(TEMPLATE_3_DICT)
for i, j in defaults.items():
setattr(self.params, i, j)
# set params (overrides defaults)
for key in self._kwargs:
setattr(self.params, key, self._kwargs[key])
# attributes
self.rasync = None
self.stdout = None
self.stderr = None
# results files
self.trees = Params()
runs = ("" if int(self.params.nruns) < 2 else "run1.")
self.trees.constre = os.path.join(
self.workdir, "{}.nex.con.tre".format(self.name))
self.trees.posttre = os.path.join(
self.workdir, "{}.nex.{}t".format(self.name, runs))
self.trees.pstat = os.path.join(
self.workdir, "{}.nex.pstat".format(self.name))
self._write_nexus_file()
# check attribute for existing results at this name.
if self.result_files:
print(
"Existing results loaded for run [{}], see .trees attribute."
.format(len(self.result_files), self.name)
)
def _write_nexus_file(self, write=True):
"""
Write a mrbayes block to a copy of the NEXUS file.
"""
# get parameters for this model type
cwargs = self.params.__dict__.copy()
# always add I/O args
cwargs["nexus"] = self.data
cwargs["outname"] = self.nexus
# is the tree topology fixed?
if isinstance(self.constraints, toytree.Toytree.ToyTree):
self._fixed_tree()
cwargs["treeblock"] = self.treeblock
cwargs["topologypr"] = self.params.topologypr
cwargs["constraints"] = ""
elif isinstance(self.constraints, dict):
# set constraints from dict
cwargs["constraints"] = self._get_constraint_str()
cwargs["treeblock"] = ""
else:
cwargs["constraints"] = ""
cwargs["treeblock"] = ""
# expand NEXUS usign string formatting
if self.clock_model == 1:
self._nexstring = NEX_TEMPLATE_2.format(**cwargs)
elif self.clock_model == 2:
self._nexstring = NEX_TEMPLATE_3.format(**cwargs)
else:
self._nexstring = NEX_TEMPLATE_1.format(**cwargs)
# write the NEX string
with open(self.nexus, 'w') as out:
out.write(self._nexstring)
def print_command(self):
print("{} {}".format(self.binary, self.nexus))
def print_nexus_string(self):
"update nexus string and print"
self._write_nexus_file(write=False)
print(self._nexstring)
@property
def command(self):
return "{} {}".format(self.binary, self.nexus)
@property
def nexus_string(self):
"update nexus string and return"
self._write_nexus_file(write=False)
return self._nexstring
@property
def result_files(self):
"returns a list of files that have finished structure"
resfiles = [i[1] for i in self.trees if os.path.exists(i[1])]
return resfiles
@property
def convergence_stats(self):
if not os.path.exists(self.trees.pstat):
print("no stats available")
else:
stats = pd.read_csv(
self.trees.pstat,
sep="\t",
skiprows=1,
index_col=0,
)
return stats
def run(
self,
ipyclient=None,
quiet=False,
force=False,
block=False,
):
"""
Submits mrbayes job to run. If no ipyclient object is provided then
the function will block until the mb run is finished. If an ipyclient
is provided then the job is sent to a remote engine and an asynchronous
result object is returned which can be queried or awaited until it
finishes.
Parameters
-----------
ipyclient:
Not yet supported...
quiet:
suppress print statements
force:
overwrite existing results files with this job name.
block:
will block progress in notebook until job finishes, even if job
is running on a remote ipyclient.
"""
# check for input data file
if not os.path.exists(self.data):
raise IPyradError("data file not found {}".format(self.data))
# stop before trying in mrbayes
if force:
for key, oldfile in self.trees:
if os.path.exists(oldfile):
os.remove(oldfile)
if os.path.exists(self.trees.pstat):
print("Error Files Exist: set a new name or use Force flag.\n{}"
.format(self.trees.pstat))
return
# rewrite nexus file in case params have been updated
self._write_nexus_file()
# submit it
if not ipyclient:
self.stdout = _call_mb([self.binary, self.nexus])
else:
# find all hosts and submit job to host with most available engines
lbview = ipyclient.load_balanced_view()
self.rasync = lbview.apply(
_call_mb, [self.binary, self.nexus])
# initiate random seed
if not quiet:
if not ipyclient:
print("job {} finished successfully".format(self.name))
else:
if block:
print("job {} running".format(self.name))
ipyclient.wait()
if self.rasync.successful():
print(
"job {} finished successfully"
.format(self.name))
else:
self.rasync.get()
else:
print("job {} submitted to cluster".format(self.name))
def _get_binary(self, search_first=None):
""" find binaries available"""
# check for binary
list_binaries = [
search_first,
os.path.join(sys.prefix, "bin", "mb"),
]
list_binaries = [i for i in list_binaries if i]
# check user binary first, then backups
for binary in list_binaries:
proc = subprocess.Popen(
["which", binary],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()
# update the binary
if proc[0]:
self.binary = binary
# if none then raise error
if not proc[0]:
raise Exception(
"cannot find mb; "
"run 'conda install mrbayes -c conda-forge -c bioconda'")
def _get_constraints_from_tree(self):
"""
Returns a dictionary mapping node idx to a list of names. This can
be entered to the ipa.mb object as a constraints argument to constrain
an entire tree.
"""
constraints = {}
for node in self.constraints.treenode.traverse():
# get all tips descendant
lvs = [i.name for i in node.get_leaves()]
if len(lvs) > 1:
# constraint string
constraints[node.idx] = lvs
return constraints
def _fixed_tree(self):
"""
Based on a thread on GitHub it is recommended that for fixing a
tree you simply set the parameters affecting topoology to zero and
set the starting tree to your desired tree. This is better than
setting a constraint on every node.
https://github.com/NBISweden/MrBayes/issues/38
"""
# write a tree block in the NEXUS
self.treeblock = (
"begin trees;\n tree fixedtree = {}\nend;".
format(self.constraints.write(tree_format=9))
)
# set topologypr to use fixed tree
self.params.topologypr = "fixed(fixedtree)"
def _get_constraint_str(self):
"""
constraint <constraint name> <probability-x> = <list of taxa>
constraint example 100 = taxon_2 taxon_3;
prset topologypr = example;
"""
# self.constraints is a dictionary
constraints = []
for key in self.constraints:
# const str
constraint = "constraint idx-{} 100 = {};".format(
key, " ".join(self.constraints[key])
)
# setting string
setit = "prset topologypr = constraints (idx-{});".format(key)
constraints.append(constraint)
constraints.append(setit)
# return empty string if no constraints
return "\n".join(constraints)
def _call_mb(command_list):
""" call the command as sps """
proc = subprocess.Popen(
command_list,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE
)
comm = proc.communicate()
if proc.returncode:
raise IPyradError(comm[0].decode())
return comm[0].decode()