/
astral.py
280 lines (222 loc) · 7.65 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
275
276
277
278
279
280
#!/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 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 conda-forge
"""
class Astral:
"""
Wrapper to run simple astral analyses on a list of gene trees.
The input can be either a file with newick trees on separate lines,
or a list of newick strings, or a list of toytree objects, or a DataFrame
containing a column labeled .tree.
Parameters
===========
data (str or list)
name (str)
workdir (str)
bootsfile (str or None)
...
"""
def __init__(
self,
data,
name="test",
workdir="analysis-astral",
bootsfile=None,
imap=None,
annotation=3,
gene_resampling=False,
nboots=None,
binary=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.
"""
# check for toytree
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.\n"
"You can use 'conda install openjdk -c conda-forge"
)
# check for astral jarfile in userspec
if self.binary is not None:
if os.path.exists(self.binary):
return
# check for astral jarfile in eaton-lab conda install location
else:
self.binary = os.path.join(sys.prefix, "bin", "astral.5.7.1.jar")
if os.path.exists(self.binary):
return
# if you get here an install was not found and you are in trouble.
raise IPyradError(
"astral binary not found. Please either specify a binary if\n"
"astral is already installed, or install with conda by using:\n"
"'conda install astral3 -c conda-forge -c eaton-lab'"
)
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
elif isinstance(data, (str, bytes)):
# could be a CSV file from treeslider or ipcoal
data = pd.read_csv(data)
treelist = data[data.tree.notna()].tree.tolist()
# or it could be a file with newicks on lines
# ...
# assume this is the treeslider dataframe output with .tree column
elif isinstance(data, pd.DataFrame):
treelist = data[data.tree.notna()].tree.tolist()
else:
raise IPyradError("input format should be list or DataFrame.")
# 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.1.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))