/
bucky.py
683 lines (572 loc) · 21.7 KB
/
bucky.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#!/usr/bin/env python
"ipyrad.analysis wrapper for parallel BUCKy concordance tree analyses"
from __future__ import print_function
from builtins import range
import os
import glob
import time
import numpy as np
import subprocess as sps
from collections import Counter
from ipyrad.analysis.utils import progressbar, Params
from ipyrad.assemble.utils import IPyradError
MISSING_IMPORTS = """
To use the ipa.bucky module you must install two additional
libraries which can be done with the following conda command.
conda install -c ipyrad bucky
conda install -c bioconda mrbayes
"""
class Bucky(object):
"""
Bucky analysis utility function for creating input files, setting parameters,
and submitting jobs to run on a parallel cluster. Converts loci/alleles
file format data to nexus file format, The main functions are write_nex_files()
and run()
Parameters:
-----------
name:
A name for this analysis that will be a prefix for all output files.
data:
A .loci or .alleles.loci file produced by ipyrad.
workdir:
A directory for all output files. Default is "./analysis-bucky"
samples:
A list of samples to include in the analysis. If empty then all samples
present in the loci file will be used, and loci will only be included
in the analysis if data is present for all samples.
mb_mcmc_ngen:
....
mb_mcmc_burnin:
...
mb_mcmc_sample_freq:
...
bucky_alpha:
...
bucky_nchains:
...
bucky_nreps:
...
bucky_niter:
...
Attributes:
...u
"""
def __init__(self, name, data, workdir=None, **kwargs):
## store attributes
self.name = name
self.data = data
self._kwargs = {
"minsnps": 0,
"maxloci": None,
"seed": None,
"mb_mcmc_ngen": int(1e6),
"mb_mcmc_burnin": int(1e5),
"mb_mcmc_sample_freq": int(1e3),
"bucky_alpha": [0.1, 1.0, 10.0],
"bucky_nchains": 4,
"bucky_nreps": 4,
"bucky_niter": int(1e6),
"copied": False,
"samples": None,
}
self._kwargs.update(kwargs)
## check binaries
self.check_binaries()
## check workdir
if workdir:
self.workdir = os.path.realpath(
os.path.abspath(os.path.expanduser(workdir)))
else:
self.workdir = os.path.realpath(
os.path.join(os.path.curdir, "analysis-bucky"))
if not os.path.exists(self.workdir):
os.makedirs(self.workdir)
## set bucky parameters with defaults
self.params = Params()
notparams = set(["workdir", "samples", "copied"])
for key in set(self._kwargs.keys()) - notparams:
self.params[key] = self._kwargs[key]
## set defaults
self.params.seed = np.random.randint(1e9)
if self._kwargs["seed"]:
self.params.seed = self._kwargs["seed"]
self.samples = []
if self._kwargs["samples"]:
self.samples = set(self._kwargs["samples"])
self._alleles = 0
if ".alleles" in self.data:
self._alleles = 1
## results files
self.files = Params()
self.files.data = data
self.files.nexfiles = []
self.files.sumfiles = []
self.files.buckyfiles = []
## accessible results
## get from property functions
self.results = Params()
self.results.concordance_trees = Params()
self.results.population_trees = Params()
self.results.concordance_factors = Params()
## during init, if results files exist then make them accessible
## otherwise they are only set when they are made.
#_buckies = glob.glob(os.path.join(self.workdir, self.name, "*.bucky*"))
## ...
def check_binaries(self):
"check for structure and clumpp"
for binary in ['mb', 'mbsum', 'bucky']:
cmd = ["which", binary]
proc = sps.Popen(cmd, stdout=sps.PIPE, stderr=sps.PIPE)
stdout = proc.communicate()[0]
if not stdout:
raise IPyradError(MISSING_IMPORTS)
@property
def _results(self):
"""
parse results from .bucky results files for this {workdir}/{name}.
"""
pass
@property
def _trees(self):
"""
parse trees from .bucky results files for this {workdir}/{name}.
"""
pass
def copy(self, name):
"""
Returns a new bucky object with the same parameters as its parent. A
new name must be provided that is different from the parent name.
"""
pass
def write_nexus_files(self, force=False, quiet=False):
"""
Write nexus files to {workdir}/{name}/[0-N].nex, If the directory already
exists an exception will be raised unless you use the force flag which
will remove all files in the directory.
Parameters:
-----------
force (bool):
If True then all files in {workdir}/{name}/*.nex* will be removed.
"""
# clear existing files
existing = glob.glob(os.path.join(self.workdir, self.name, "*.nex"))
if any(existing):
if force:
for rfile in existing:
os.remove(rfile)
else:
path = os.path.join(self.workdir, self.name)
raise IPyradError(EXISTING_NEX_FILES.format(path))
## parse the loci or alleles file
with open(self.files.data) as infile:
loci = iter(infile.read().strip().split("|\n"))
## use entered samples or parse them from the file
if not self.samples:
with open(self.files.data) as infile:
samples = set(
(i.split()[0] for i in infile.readlines() if "//" not in i)
)
else:
samples = set(self.samples)
## keep track of how many loci pass filtering
totn = len(samples)
nloci = 0
## this set is just used for matching, then we randomly
## subsample for real within the locus so it varies
if self._alleles:
msamples = {i + rbin() for i in samples}
else:
msamples = samples
## write subsampled set of loci
for loc in loci:
## get names and seqs from locus
dat = loc.split("\n")[:-1]
try:
names = [i.split()[0] for i in dat]
snames = set(names)
seqs = np.array([list(i.split()[1]) for i in dat])
except IndexError:
print(ALLELESBUGFIXED)
continue
## check name matches
if len(snames.intersection(msamples)) == totn:
## prune sample names if alleles. Done here so it is randomly
## different in every locus which allele is selected from
## each sample (e.g., 0 or 1)
if self._alleles:
_samples = [i + rbin() for i in samples]
else:
_samples = samples
## re-order seqs to be in set order
seqsamp = seqs[[names.index(tax) for tax in _samples]]
## resolve ambiguities randomly if .loci file otherwise
## sample one of the alleles if .alleles file.
if not self._alleles:
seqsamp = _resolveambig(seqsamp)
## find parsimony informative sites
if _count_PIS(seqsamp, self.params.minsnps):
## keep the locus
nloci += 1
## remove empty columns given this sampling
copied = seqsamp.copy()
copied[copied == "-"] == "N"
rmcol = np.all(copied == "N", axis=0)
seqsamp = seqsamp[:, ~rmcol]
## write nexus file
if self._alleles:
## trim off the allele number
samps = [i.rsplit("_", 1)[0] for i in _samples]
mdict = dict(zip(samps, ["".join(i) for i in seqsamp]))
else:
mdict = dict(zip(_samples, ["".join(i) for i in seqsamp]))
self._write_nex(mdict, nloci)
## quit early if using maxloci
if nloci == self.params.maxloci:
break
## print data size
if not quiet:
path = os.path.join(self.workdir, self.name)
path = path.replace(os.path.expanduser("~"), "~")
print("wrote {} nexus files to {}".format(nloci, path))
def run(self, steps=None, ipyclient=None, force=False, quiet=False):
"""
Submits an ordered list of jobs to a load-balancer to complete
the following tasks, and reports a progress bar:
(1) Write nexus files for each locus
(2) Run mrBayes on each locus to get a posterior of gene trees
(3) Run mbsum (a bucky tool) on the posterior set of trees
(4) Run Bucky on the summarized set of trees for all alpha values.
Parameters:
-----------
ipyclient (ipyparallel.Client())
A connected ipyparallel Client object used to distribute jobs
force (bool):
Whether to overwrite existing files with the same name and workdir
if they exist. Default is False.
quiet (bool):
Whether to suppress progress information. Default is False.
steps (list):
A list of integers of steps to perform. This is useful if a
job was interrupted, or you created a new bucky object copy,
or you wish to run an analysis under a new set of parameters,
after having run it once. For example, if you finished running
steps 1 and 2 (write nexus files and infer mrbayes posteriors),
but you want to rerun steps 3 and 4 with new settings, then you
could enter `steps=[3,4]` and also `force=True` to run steps 3
and 4 with a new set of parameters. Default argument is None
which means run all steps.
"""
## require ipyclient
if not ipyclient:
raise IPyradError("an ipyclient object is required")
## check the steps argument
if not steps:
steps = [1, 2, 3, 4]
if isinstance(steps, (int, str)):
steps = [int(i) for i in [steps]]
if isinstance(steps, list):
if not all(isinstance(i, int) for i in steps):
raise IPyradError("steps must be a list of integers")
## run steps ------------------------------------------------------
## TODO: wrap this function so it plays nice when interrupted.
if 1 in steps:
self.write_nexus_files(force=force, quiet=quiet)
if 2 in steps:
self.run_mrbayes(force=force, quiet=quiet, ipyclient=ipyclient)
if 3 in steps:
self.run_mbsum(force=force, quiet=quiet, ipyclient=ipyclient)
if 4 in steps:
self.run_bucky(force=force, quiet=quiet, ipyclient=ipyclient)
## make sure jobs are done if waiting (TODO: maybe make this optional)
ipyclient.wait()
def _write_nex(self, mdict, nlocus):
"""
function that takes a dictionary mapping names to sequences,
and a locus number, and writes it as a NEXUS file with a mrbayes
analysis block given a set of mcmc arguments.
"""
## create matrix as a string
max_name_len = max([len(i) for i in mdict])
namestring = "{:<" + str(max_name_len + 1) + "} {}\n"
matrix = ""
for i in mdict.items():
matrix += namestring.format(i[0], i[1])
## ensure dir
minidir = os.path.realpath(os.path.join(self.workdir, self.name))
if not os.path.exists(minidir):
os.makedirs(minidir)
## write nexus block
handle = os.path.join(minidir, "{}.nex".format(nlocus))
with open(handle, 'w') as outnex:
outnex.write(NEXBLOCK.format(**{
"ntax": len(mdict),
"nchar": len(list(mdict.values())[0]),
"matrix": matrix,
"ngen": self.params.mb_mcmc_ngen,
"sfreq": self.params.mb_mcmc_sample_freq,
"burnin": self.params.mb_mcmc_burnin,
}))
def run_mbsum(self, ipyclient, force=False, quiet=False):
"""
Sums two replicate mrbayes runs for each locus
"""
minidir = os.path.realpath(os.path.join(self.workdir, self.name))
trees1 = glob.glob(os.path.join(minidir, "*.run1.t"))
trees2 = glob.glob(os.path.join(minidir, "*.run2.t"))
## clear existing files
existing = glob.glob(os.path.join(self.workdir, self.name, "*.sumt"))
if any(existing):
if force:
for rfile in existing:
os.remove(rfile)
else:
path = os.path.join(self.workdir, self.name)
raise IPyradError(EXISTING_SUMT_FILES.format(path))
## load balancer
lbview = ipyclient.load_balanced_view()
## submit each to be processed
asyncs = []
for tidx in range(len(trees1)):
rep1 = trees1[tidx]
rep2 = trees2[tidx]
outname = os.path.join(minidir, str(tidx) + ".sumt")
rasync = lbview.apply(call_mbsum, *(rep1, rep2, outname))
asyncs.append(rasync)
## track progress
start = time.time()
printstr = "sum replicate runs"
while 1:
ready = [i.ready() for i in asyncs]
if not quiet:
progressbar(sum(ready), len(ready), start, printstr)
if len(ready) == sum(ready):
if not quiet:
print("")
break
else:
time.sleep(0.1)
## check success
for rasync in asyncs:
if not rasync.successful():
raise IPyradError(rasync.result())
def run_mrbayes(self, ipyclient, force=False, quiet=False):
"""
calls the mrbayes block in each nexus file.
"""
## get all the nexus files for this object
minidir = os.path.realpath(os.path.join(self.workdir, self.name))
nexus_files = glob.glob(os.path.join(minidir, "*.nex"))
## clear existing files
existing = glob.glob(os.path.join(minidir, "*.nex.*"))
if any(existing):
if force:
for rfile in existing:
os.remove(rfile)
else:
raise IPyradError(EXISTING_NEXdot_FILES.format(minidir))
## load balancer
lbview = ipyclient.load_balanced_view()
## submit each to be processed
asyncs = []
for nex in nexus_files:
rasync = lbview.apply(call_mb, nex)
asyncs.append(rasync)
## track progress
start = time.time()
printstr = "infer gene-tree posteriors"
while 1:
ready = [i.ready() for i in asyncs]
if not quiet:
progressbar(sum(ready), len(ready), start, printstr)
if len(ready) == sum(ready):
if not quiet:
print("")
break
else:
time.sleep(0.1)
## check success
for rasync in asyncs:
if not rasync.successful():
raise IPyradError(rasync.result())
def run_bucky(self, ipyclient, force=False, quiet=False, subname=False):
"""
Runs bucky for a given set of parameters and stores the result
to the ipa.bucky object. The results will be stored by default
with the name '{name}-{alpha}' unless a argument is passed for
'subname' to customize the output name.
Parameters:
-----------
subname (str):
A custom name prefix for the output files produced by the bucky
analysis and output into the {workdir}/{name} directory.
force (bool):
If True then existing result files with the same name prefix
will be overwritten.
quiet (bool):
If True the progress bars will be suppressed.
ipyclient (ipyparallel.Client)
An active ipyparallel client to distribute jobs to.
"""
## check for existing results files
minidir = os.path.realpath(os.path.join(self.workdir, self.name))
infiles = glob.glob(os.path.join(minidir, "*.sumt"))
outroot = os.path.realpath(os.path.join(self.workdir, self.name))
## build alpha list
if isinstance(self.params.bucky_alpha, list):
alphas = self.params.bucky_alpha
else:
alphas = [self.params.bucky_alpha]
## load balancer
lbview = ipyclient.load_balanced_view()
## submit each to be processed
asyncs = []
for alpha in alphas:
pathname = os.path.join(outroot, "CF-a" + str(alpha))
if (os.path.exists(pathname)) and (force != True):
print("BUCKy results already exist for this filepath. " + \
"Use force to overwrite")
else:
args = [
alpha,
self.params.bucky_nchains,
self.params.bucky_nreps,
self.params.bucky_niter,
pathname,
infiles]
rasync = lbview.apply(call_bucky, *args)
asyncs.append(rasync)
## track progress
start = time.time()
printstr = "infer CF posteriors"
while 1:
ready = [i.ready() for i in asyncs]
if not quiet:
progressbar(sum(ready), len(ready), start, printstr)
if len(ready) == sum(ready):
if not quiet:
print("")
break
else:
time.sleep(0.1)
## check success
for rasync in asyncs:
if not rasync.successful():
raise IPyradError(rasync.result())
#################################################
def call_bucky(alpha, nchains, nreps, niter, outname, infiles):
"call bucky binary command"
# build command string
cmd = [
"bucky",
"-a", str(alpha),
"-c", str(nchains),
"-k", str(nreps),
"-n", str(int(niter)),
"-o", outname,
]
for ifile in infiles:
cmd.append(ifile)
# call bucky
proc = sps.Popen(cmd, stderr=sps.STDOUT, stdout=sps.PIPE)
stdout = proc.communicate()
if proc.returncode:
return stdout
def call_mb(infile):
"call mrbayes on a nex file"
# call mrbayes
cmd = ['mb', infile]
proc = sps.Popen(cmd, stderr=sps.STDOUT, stdout=sps.PIPE)
stdout = proc.communicate()
# check for errors
if proc.returncode:
return stdout
def call_mbsum(rep1, rep2, outname):
"calls mbsum for each pair of replicates"
cmd = [
"mbsum",
"-n", "0",
"-o", outname,
rep1,
rep2,
]
proc = sps.Popen(cmd, stderr=sps.STDOUT, stdout=sps.PIPE)
stdout = proc.communicate()
## check for errors
if proc.returncode:
return stdout
def rbin():
"returns a random binomial as a string with an underscore before it"
return "_{}".format(np.random.binomial(1, 0.5))
def _resolveambig(subseq):
"""
Randomly resolves iupac hetero codes. This is a shortcut
for now, we could instead use the phased alleles in RAD loci.
"""
N = []
for col in subseq:
rand = np.random.binomial(1, 0.5)
N.append([_AMBIGS[i][rand] for i in col])
return np.array(N)
def _count_PIS(seqsamp, N):
""" filters for loci with >= N PIS """
counts = [Counter(col) for col in seqsamp.T if not ("-" in col or "N" in col)]
pis = [i.most_common(2)[1][1] > 1 for i in counts if len(i.most_common(2))>1]
if sum(pis) >= N:
return sum(pis)
else:
return 0
## GLOBALS
NEXBLOCK = """\
#NEXUS
begin data;
dimensions ntax={ntax} nchar={nchar};
format datatype=dna interleave=yes gap=- missing=N;
matrix
{matrix}
;
begin mrbayes;
set autoclose=yes nowarn=yes;
lset nst=6 rates=gamma;
mcmc ngen={ngen} samplefreq={sfreq} printfreq={ngen};
sump burnin={burnin};
sumt burnin={burnin};
end;
"""
EXISTING_NEX_FILES = """\
Nexus files linked to this object (i.e., same workdir & name)
already exist at {}.
To remove the existing files and write new files use the
argument force=True.
"""
EXISTING_NEXdot_FILES = """\
Result files linked to this object (i.e., same workdir & name)
already exist at {}.
To remove the existing files and write new mb result files use
the argument force=True.
"""
EXISTING_SUMT_FILES = """\
Sumtree (sumt) files linked to this object (i.e., same workdir & name)
already exist at {}.
To remove the existing files and write new sumt files use the
argument force=True.
"""
ALLELESBUGFIXED = """\
Warning: encountered an error in the alleles file format. This
is a bug that was fixed in v.0.7.2. Rerun step 7 on this data
set to ensure that the alleles file is properly formatted.
"""
## a dictionary mapping ambiguous characters
_AMBIGS = {
"R": ("G", "A"),
"K": ("G", "T"),
"S": ("G", "C"),
"Y": ("T", "C"),
"W": ("T", "A"),
"M": ("C", "A"),
"A": ("A", "A"),
"T": ("T", "T"),
"G": ("G", "G"),
"C": ("C", "C"),
"-": ("-", "-"),
"N": ("N", "N"),
}