-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalisim.py
291 lines (253 loc) · 7.35 KB
/
alisim.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
import argparse
import os
import pickle
import pathlib
import random
import sys
import subprocess
import numpy as np
from pprint import pprint
from Bio import SeqIO
from glob import glob
from tqdm import tqdm
ALPHAS_PATH = os.path.join(os.path.dirname(__file__), "data", "hogenom_alphas.txt")
MAX_ATTEMPTS_DEFAULT = 20
def load_list(listpath):
with open(listpath, "rb") as file:
return pickle.load(file)
def sample_scale(scales):
mean = random.sample(scales, 1)[0]
scale = np.random.normal(loc=mean, scale=mean / 10)
return max(scale, 0.05)
def has_duplicates(alnpath):
seqs = set()
n_seqs = 0
for record in SeqIO.parse(alnpath, "fasta"):
n_seqs += 1
seqs.add(str(record.seq))
return n_seqs != len(seqs)
def trim_alignment(path, seqlen):
# Keep full alignment around
bkppath = path + ".untrimmed"
os.rename(path, bkppath)
aln = SeqIO.parse(bkppath, format="fasta")
with open(path, "w") as out:
SeqIO.write((rec[:seqlen] for rec in aln), out, format="fasta")
def parse_custom_model_name(modelpath):
with open(modelpath, "r") as file:
for line in file:
if line.startswith("frequency"):
return line.split()[1].split("_")[0]
return None
def simulate_alignment(
treefile,
substitution,
gamma,
binary,
custom_model_def,
custom_model_args,
add_indels,
outdir,
length,
max_attempts,
threads,
):
filestem = pathlib.PurePath(treefile).stem
outpath = os.path.join(outdir, f"{filestem}.fa")
success = False
attempt = 1
while not success:
if attempt > max_attempts:
os.remove(outpath)
return treefile, attempt
model_args = f"{substitution}"
if custom_model_args is not None:
model_args += f"+{custom_model_args}"
if args.gamma is not None:
alpha = sample_scale(alphas)
model_args += f"+{gamma}{{{alpha}}}"
indel_args = []
if add_indels:
indel_args = ["--indel", "0.01,0.01", "--indel-size", "GEO{5},GEO{4}"]
threads = 1 # alisim cannot use multiple threads with the indel model
cmd = [
binary,
"--alisim",
os.path.join(outdir, filestem),
"-t",
treefile,
"-m",
model_args,
"-mwopt",
"-af",
"fasta",
"--seqtype",
"AA",
"--length",
f"{length}",
"--threads",
f"{threads}",
*indel_args,
custom_model_def,
]
process = subprocess.Popen(" ".join(cmd), shell=True, stdout=subprocess.PIPE)
_, error = process.communicate()
process.wait()
if error is not None:
return error, None
if add_indels:
trim_alignment(outpath, length)
if args.allow_duplicate_sequences:
break
if not has_duplicates(outpath):
success = True
attempt += 1
if not args.keep_logfiles:
logpath = f"{treefile}.log"
os.remove(logpath)
return None, None
def wrapper(args):
return simulate_alignment(*args)
if __name__ == "__main__":
parser = argparse.ArgumentParser("Alignment simulator")
parser.add_argument(
"trees",
type=str,
help="Path to the directory containing mewick trees",
)
parser.add_argument(
"--outdir",
"-o",
type=str,
help="Path to the output directory",
)
parser.add_argument(
"--length",
"-l",
default=500,
required=False,
type=int,
help="Length of the alignment",
)
parser.add_argument(
"--gamma",
"-g",
default=None,
type=str,
required=False,
help=(
"Gamma model for between-site rate heterogeneity "
"(G[n] for discrete gamma with n categories, GC for continuous gamma)"
),
)
parser.add_argument(
"--substitution",
"-s",
default="LG",
type=str,
required=False,
help=(
"Protein substitution model: "
"classical (LG, WAG, Dayhoff, Blosum62) or mixture (C10, ..., C60)"
),
)
parser.add_argument(
"--custom-model",
"-c",
type=str,
required=False,
default=None,
help=(
"Path to a custom model definition in the nexus format\n "
"(e.g. the UDM models in github.com/dschrempf/EDCluster/"
"Distributions/hogenom/*_lclr_iqtree.nex)"
),
)
parser.add_argument(
"--indels",
action="store_true",
help="Add insertions and deletions to the alignments",
)
parser.add_argument(
"--iqtree",
"-i",
type=str,
required=False,
default="iqtree2",
help=(
"Path to binary of IQTree2 to use to simulate alignments "
"by default it will try to use a `iqtree2` binary in your PATH."
),
)
parser.add_argument(
"--no-summary",
"-n",
action="store_true",
help="If specified suppress the output summarizing which simulation attempts have failed",
)
parser.add_argument(
"--allow-duplicate-sequences",
"-d",
action="store_true",
help="Allow duplicate sequences in the alignments",
)
parser.add_argument(
"--keep-logfiles",
"-k",
action="store_true",
help="Keep IQTree generated log files",
)
parser.add_argument(
"--max-attempts",
"-m",
default=MAX_ATTEMPTS_DEFAULT,
type=int,
required=False,
help="Maximum number of attempts to simulate alignment in case of duplicates",
)
parser.add_argument(
"--processes",
"-p",
default=1,
type=int,
required=False,
help="Number of threads for alisim to use.",
)
args = parser.parse_args()
pprint(args.__dict__, indent=2)
alphas = load_list(ALPHAS_PATH)
os.makedirs(args.outdir)
# Get custom model params if defined
custom_model_def = ""
custom_model_args = ""
if args.custom_model is not None:
model_name = parse_custom_model_name(args.custom_model)
if model_name is None:
raise ValueError(f"{args.custom_model} is not a valid IQTree model file")
custom_model_def = f"-mdef {args.custom_model}"
custom_model_args = model_name
treefiles = glob(f"{args.trees}/*.n*w*k") # matches nwk and newick
failures = []
for treefile in tqdm(treefiles):
error, attempt = simulate_alignment(
treefile,
args.substitution,
args.gamma,
args.iqtree,
custom_model_def,
custom_model_args,
args.indels,
args.outdir,
args.length,
args.max_attempts,
args.processes,
)
if error is not None:
if attempt is None:
print("Error simulating tree: ", error)
sys.exit(1)
failures.append(error)
if len(failures) > 0 and not args.no_summary:
print(f"Failed to simulate {len(failures)} alignments without duplicates:")
for file in failures:
print(f" - {file}")