/
astral.py
274 lines (223 loc) · 7.61 KB
/
astral.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
#!/usr/bin/env python
"""
Wrapper tool to conveniently call astral from ipa
"""
# py2/3 compat
from __future__ import print_function
from builtins import range
import os
import sys
import tempfile
import requests
import subprocess as sps
import pandas as pd
from ..assemble.utils import IPyradError
# import toytree
try:
import toytree
except ImportError:
pass
_MISSING_TOYTREE = """
You are missing required packages to use ipa.bpp().
First run the following conda install command:
conda install toytree -c eaton-lab
"""
class Astral:
"""
Wrapper to run simple astral analyses on a list of gene trees.
"""
def __init__(
self,
data,
name="test",
workdir="analysis-astral",
bootsfile=None,
imap=None,
annotation=1,
gene_resampling=False,
nboots=None,
**kwargs):
# i/o
self.name = name
self.data = data
self._tmptrees = None
self.workdir = os.path.realpath(os.path.expanduser(workdir))
self.binary = None
self.mapfile = None
# results
self.tree = None
self.quartet_score = None
self.treefile = os.path.join(self.workdir, self.name + ".tre")
self.logfile = os.path.join(self.workdir, self.name + ".log")
# params
self.bootsfile = bootsfile
self.imap = imap
self.gene_resampling = gene_resampling
self.annotation = annotation
self.nboots = nboots
# prep
self._check_binary()
self._check_args()
self._parse_data_to_tmpfile()
self._write_mapfile()
# the kwargs for astral
self._kwargs = {
"-i": self._tmptrees,
"-o": self.treefile,
"-a": self.mapfile,
"-b": self.bootsfile,
"-t": self.annotation,
"-r": self.nboots,
"-g": self.gene_resampling,
}
def _write_mapfile(self):
"""
species_name:individual_1,individual_2,...
"""
if self.imap:
self.mapfile = os.path.join(
self.workdir,
"{}.imap.txt".format(self.name),
)
maplines = [
"{}:{}".format(i, ",".join(j))
for (i, j) in self.imap.items()
]
with open(self.mapfile, 'w') as out:
out.write("\n".join(maplines))
def _check_args(self):
"""
Check for bad or incompatible arguments
"""
# check for workdir
if not os.path.exists(self.workdir):
os.makedirs(self.workdir)
# check bootstrap args
if self.bootsfile:
# cannot do bootstrapping unless bootsfile exists
assert os.path.exists(self.bootsfile), "bootsfile not found"
# # cannot have more replicates than the length of boots
# with open(self.bootsfile, 'r') as infile:
# nboots = sum(1 for i in infile)
# assert nboots >= self.nboots, (
# "nboots ({}) cannot be > number of bootstrap trees ({})"
# .format(nboots, self.nboots))
# # not bootstrapping
# else:
# # cannot do gene-resampling unless bootstrapping
# assert self.gene_resampling is None, (
# "cannot do gene resampling unless using bootsfile")
def _get_command(self):
"""
When the command is run we also save stderr to a log file.
"""
# base command
cmd = ["java", "-jar", self.binary]
# additional args
for key, val in self._kwargs.items():
if val is not None:
if val is True:
cmd.extend([str(key)])
elif val is False:
pass
else:
cmd.extend([str(key), str(val)])
return cmd
def print_command(self):
print(" ".join(self._get_command()))
def _check_binary(self):
"""
Check that java is installed and get a tmp binary if needed.
"""
if not sys.modules.get("toytree"):
raise ImportError(_MISSING_TOYTREE)
# check for java
cmd = ["which", "java"]
proc = sps.Popen(cmd, stderr=sps.STDOUT, stdout=sps.PIPE)
comm = proc.communicate()
if proc.returncode:
print(comm[0])
if not comm[0]:
raise IPyradError(
"java must be installed or loaded to use Astral.")
# paths
uzip = "https://github.com/smirarab/ASTRAL/raw/master/Astral.5.7.3.zip"
dzip = "Astral.5.7.3.zip"
adir = "Astral"
jbin = "astral.5.7.3.jar"
# look for existing binary in tmpdir
self.binary = os.path.join(
tempfile.gettempdir(), adir, jbin,
)
# check that bpp is installed and in path
cmd = ['which', self.binary]
proc = sps.Popen(cmd, stderr=sps.PIPE, stdout=sps.PIPE)
comm = proc.communicate()[0]
if comm:
return
# bpp not found in /tmp, download it.
res = requests.get(uzip, allow_redirects=True)
tmpzip = os.path.join(tempfile.gettempdir(), dzip)
with open(tmpzip, 'wb') as tz:
tz.write(res.content)
# decompress tar file
cmd = ["unzip", tmpzip, "-d", tempfile.gettempdir()]
proc = sps.Popen(cmd, stderr=sps.PIPE, stdout=sps.PIPE)
comm = proc.communicate()
if proc.returncode:
print(comm[0], comm[1])
# check that binary now can be found
cmd = ['which', self.binary]
proc = sps.Popen(cmd, stderr=sps.PIPE, stdout=sps.PIPE)
comm = proc.communicate()[0]
if comm:
return
raise IPyradError("astral binary not found.")
def _parse_data_to_tmpfile(self):
"""
Input can be a CSV file or DataFrame or list to
a bootfile that points the location of many bootstrap tree files.
"""
# shorthand code
data = self.data
# data is a .tree series possiblly with NaN values.
if isinstance(data, list):
treelist = data
# it is a filepath string
if isinstance(data, (str, bytes)):
data = pd.read_csv(data)
treelist = data[data.tree.notna()].tree.tolist()
# assume this is the treeslider dataframe output with .tree column
if isinstance(data, pd.DataFrame):
treelist = data[data.tree.notna()].tree.tolist()
# write to tmpfile
self._tmptrees = os.path.join(self.workdir, "tmptrees.txt")
with open(self._tmptrees, 'w') as out:
out.write("\n".join(treelist))
def run(self):
"""
Call Astral command ()
"""
print("[astral.5.7.3.jar]")
# setup the comamnd
proc = sps.Popen(
self._get_command(),
stderr=sps.STDOUT,
stdout=sps.PIPE,
)
comm = proc.communicate()
if proc.returncode:
print("Astral Error:\n", comm[0].decode())
raise IPyradError(
"Astral Error: your command string was:\n{}"
.format(" ".join(self._get_command())))
# store stderr to logfile
with open(self.logfile, 'w') as out:
out.write(comm[0].decode())
# cleanup
if os.path.exists(self._tmptrees):
os.remove(self._tmptrees)
# try loading the tree result
self.tree = toytree.tree(self.treefile)
# report result file
print("inferred tree written to ({})".format(self.treefile))