/
repeat_io.py
435 lines (357 loc) · 15.6 KB
/
repeat_io.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
# (C) 2015 Elke Schaper
"""
:synopsis: Input/output for tandem repeats.
.. moduleauthor:: Elke Schaper <elke.schaper@isb-sib.ch>
"""
import Bio.Seq
import random
import logging
import itertools
import os
import re
import shutil
import tempfile
import subprocess
import numpy as np
from tral.repeat import repeat
from tral import configuration
from tral.paths import config_file
CONFIG_GENERAL = configuration.Configuration.instance().config
REPEAT_CONFIG = CONFIG_GENERAL["repeat"]
LOG = logging.getLogger(__name__)
###################### SAVE REPEAT #######################################
def save_repeat_fasta(tandem_repeats, file):
""" save multiple <tandem_repeats> in Fasta format in specified <file>
At current, only one TR per sequence can be defined, as the identifiers
in the dict <tandem_repeats> must be unique.
Parameters: Dict of tandem repeats and identifiers.
e.g. {'ENSP00012': msa1, 'ENSP00013': msa2}
>ID
GHKI
GHKI
GH--
"""
with open(file, 'w', newline='\n') as f:
for identifier, msa in tandem_repeats.items():
f.write(">{0}\n".format(identifier))
f.write("\n".join(msa) + "\n\n")
def save_repeat_stockholm(tandem_repeat, file):
""" save <tandem_repeat> in STOCKHOLM format in specified <file>
Parameters: Tandem repeat MSA.
e.g. ["ACDEF-", "ACCDEF"]
More information in
ftp://selab.janelia.org/pub/software/hmmer3/3.0/Userguide.pdf
STOCKHOLM Format Example:
# STOCKHOLM 1.0
seq1 ACDEF...GHIKL
seq2 ACDEF...GHIKL
seq3 ...EFMNRGHIKL
seq1 MNPQTVWY
seq2 MNPQTVWY
seq3 MNPQT...
//
"""
with open(file, 'w', newline='\n') as fh:
fh.write("# STOCKHOLM 1.0\n")
for i, iMSA in enumerate(tandem_repeat):
fh.write("{} {}\n".format(str(i), iMSA))
fh.write("//")
def save_repeat_treks(tandem_repeats, file):
""" Save multiple `tandem_repeats` in T-REKS format in specified `file`
At current, only one TR per sequence can be defined, as the identifiers
in the dict <tandem_repeats> must be unique.
Parameters: Dict of tandem repeats and identifiers.
e.g. {'ENSP00012': [msa1, begin1], 'ENSP00013': [msa2, begin2]}
T-REKS format example:
>a
Length: 3 residues - nb: 3 from 1 to 10 - Psim:0.8076923076923077 region Length:42
GHKI
GHKI
GH--
**********************
Length: 3 residues - nb: 2 from 21 to 27 - Psim:0.7857142857142857 region Length:22
GHKI
GH--
**********************
>b
Length: 3 residues - nb: 3 from 1 to 10 - Psim:0.8095238095238095 region Length:20
UIR
UIR
UIR
"""
with open(file, 'w', newline='\n') as fh:
for identifier, info in tandem_repeats.items():
fh.write(">{0}\n".format(identifier))
fh.write("Length: from {0} to\n".format(str(info[1])))
fh.write("\n".join(info[0]) + "\n" + "*" * 22 + "\n\n")
############################## READ REPEAT SEQUENCE ######################
def read_fasta(seq_filename, sequence_type='AA'):
""" Read repeat from file in fasta format.
Read repeat from file in fasta format.
Args:
seq_filename (str): Path to the repeats containing fasta file
sequence_type (str): Either "AA" or "DNA"
Returns:
(list of Repeat): A list of Repeat instances.
"""
pat_start = re.compile(r">(.*)")
pat_repeat_unit = re.compile(r"([\w\.\-]+)")
# Our possible parser states:
#
# 1: searching for sequence name
# 2: searching for repeat units
repeats = {}
state = 1
with open(seq_filename, "rt") as infile:
for i, line in enumerate(infile):
LOG.debug("Line {0}: {1}".format(i, line[0:-1]))
if 1 == state:
match = pat_start.match(line)
if match:
LOG.debug(" * (1->2) Found start")
LOG.debug("Start: %s", match.group(1))
name = match.group(1)
repeats[name] = []
state = 2
elif 2 == state:
match = pat_repeat_unit.match(line)
if match:
LOG.debug(" * (2->2) Found Repeat unit")
LOG.debug("Repeat Unit: %s", match.group(1))
repeat_unit = match.group(1)
repeats[name].append(repeat_unit.replace(".", "-").upper())
else:
LOG.debug(" * (2->1) Found NO further Repeat unit")
state = 1
for i_name, iR in repeats.items():
yield iR, sequence_type
################################### SIMULATE SEQUENCE ####################
def evolved_tandem_repeats(l, n, n_samples, sequence_type, job_id='job_id',
mutation_rate=50, tree='star',
indel_rate_per_site=False,
return_type='repeat'):
""" Simulate evolved sequences with ALF.
Simulate evolved sequences with ALF:
Dalquen, D. A., Anisimova, M., Gonnet, G. H. & Dessimoz, C.
ALF--a simulation framework for genome evolution. Molecular Biology and Evolution 29,
1115–1123 (2012).
Args:
l (int): The length of the repeat unit
n (int): The number of repeat units in the tandem repeat
n_samples (int): The number of samples
sequence_type (str): Either "AA" or "DNA"
job_id (str): A tag for files produces with ALF, and result files.
mutation_rate (float): The mutation rate.
tree (str): The type of tree, e.g. "star" or "birthdeath"
indel_rate_per_site (int or False): The indel rate per site.
return_type (str): Either "repeat" or "list"
sequence_length (int): The total length of the simulated sequence
Returns:
Return type depends on ``return_type``: ``Repeat`` or ``Bio.Seq.Seq`` instance.
"""
runfile_template = config_file("data", "ALF", "template.drw")
alf_exec = REPEAT_CONFIG['alfsim']
# create temporary directory
working_dir = tempfile.mkdtemp()
LOG.debug("evolvedTR: Created tempfile: %s", working_dir)
# create working dir
if not os.path.isdir(working_dir):
os.makedirs(working_dir)
# Copy template file and append job specific info
runfilename = "alf.drw"
shutil.copyfile(runfile_template, os.path.join(working_dir, runfilename))
with open(os.path.join(working_dir, runfilename), "a") as runfile:
if indel_rate_per_site:
# insertion rate per site and PAM. E.g. for PAM=40 expect
# aaGainRate*40 insertions per site.
runfile.write(
"aaGainRate := " + str(indel_rate_per_site / mutation_rate) + ";\n")
runfile.write(
"aaLossRate := " + str(indel_rate_per_site / mutation_rate) + ";\n")
runfile.write("maxIndelLength := 50;\n")
runfile.write("indelModel := 'ZIPF';\n")
runfile.write("Z_c := 1.821;\n")
runfile.write("DawgPlacement := true;\n")
runfile.write("uuid := '" + job_id + "';\n")
runfile.write("mname := '" + job_id + "';\n")
runfile.write("protStart := " + str(n_samples) + ";\n")
runfile.write("NSpecies := " + str(n) + ";\n")
runfile.write("minGeneLength := " + str(l) + ";\n")
runfile.write("mutRate := " + str(mutation_rate) + ";\n")
runfile.write("wdir := '" + working_dir + "';\n")
tree_length = n * mutation_rate
# parameters concerning the species tree
if tree == 'star':
# BDTree, ToLSample, Custom
runfile.write("treeType := 'Custom':\n")
runfile.write(
"tree := MakeStarTree(BirthDeathTree(0.1,0.1," + str(n) + ",10),mutRate):\n")
runfile.write("treeLength:= %d ;\n" % tree_length)
else:
# BDTree, ToLSample, Custom
runfile.write("treeType := 'BDTree':\n")
# From the last discussion with Daniel, only the ration of birth to
# death rate matters, if we scale tree to match pam distance
runfile.write("birthRate := 0.01:\n")
runfile.write("deathRate := 0.01:\n")
# for BDTree: should resulting tree be ultrametric, e.g. all leaves
# have same distance to origin?
runfile.write("ultrametric := false:\n")
runfile.write("treeLength:= %d ;\n" % tree_length)
# DANIEL: Is a tree := missing?
if tree not in {'star', 'birthdeath'}:
LOG.warning(
"evolvedTR: tree input %s not known, assuming birthdeath tree",
tree)
# parameters concerning the substitution models
if sequence_type == 'AA':
runfile.write(
"substModels := [SubstitutionModel('CustomP', ['" +
config_file('data', 'ALF', 'lg.dat') +
"'])];\n")
# CHECK! DOES
# substModels := [SubstitutionModel('LG')];
# WORK?
elif sequence_type == 'DNA':
runfile.write(
"substModels := [SubstitutionModel('TN93', [.3, .4, .7],[seq(0.25,4)], true)]:\n")
# CHECK! DO THE EMPIRICAL PARAMETERS MAKE SENSE AT ALL?
# compare to http://people.inf.ethz.ch/ddalquen/alf/ALF_manual.pdf
else: # CODONS
runfile.write("substModels := [SubstitutionModel('CPAM')];\n")
# Determine ALF MSA output file path
if sequence_type == 'AA':
infile = os.path.join(working_dir, job_id, "MSA", "MSA_all_aa.phy")
elif sequence_type == 'DNA':
infile = os.path.join(working_dir, job_id, "MSA", "MSA_all_dna.phy")
else: # CODONS
infile = os.path.join(
working_dir,
job_id,
"MSA",
"MSA_all_codon.phy")
for i in range(10):
with open(os.path.join(working_dir, "out.txt"), "w") as outfile:
alf_process = subprocess.Popen([alf_exec,
runfilename],
cwd=working_dir,
stdout=outfile,
stderr=outfile,
close_fds=True)
alf_process.wait()
if os.path.isfile(infile):
break
else:
LOG.error('ALFSIM was not able to produce simulated sequence.')
LOG.error('Do you have ALFSIM installed and used the correct path in the configuration file?')
return
# shutil.rmtree('/cluster/home/infk/eschaper/spielwiese/')
#shutil.copytree(working_dir, '/cluster/home/infk/eschaper/spielwiese/')
""" Read in MSA data from ALF
The following is a short parser for ALFsim output files
yielding MSAs as lists of strings,
based on a special flavour of Felstein stein MSA files """
# find a repeat unit
pattern_start = re.compile(r"\d+ \d+")
pattern_seq = re.compile(r"\S+[ ]+([A-Z\-]+)")
# Our possible parser states:
#
# state 1: Find beginning of MSA (Felsenstein)
# state 2: Find all repeat units
# protein ::=
# \d \d
# \S/\S repeatunits
#
state = 1
with open(infile, "r") as infile:
for i, line in enumerate(infile):
LOG.debug("Line %d: %s", i, line[0:-1])
if 1 == state: # Find first repeat unit & save begin
search = pattern_start.search(line)
if search:
LOG.debug(" *(1->2) Found MSA start")
state = 2
msa = []
elif 2 == state: # Find all repeat units
search = pattern_seq.search(line)
if search:
LOG.debug(" *(2->2) Found another repeat unit")
msa.append(search.group(1))
else:
LOG.debug(" *(2->1) repeat region finished, yielding.")
state = 1
# YIELD IF WE HAVE FOUND AT LEAST TWO REPEAT UNITS:
if len(msa) > 1:
if return_type == 'repeat':
yield repeat.Repeat(begin=0, msa=msa, sequence_type=sequence_type)
elif return_type == 'list':
yield msa
else:
LOG.debug("YIELD: %s",
"".join(msa).replace('-', ''))
yield Bio.Seq.Seq("".join(msa).replace('-', ''), sequence_type)
# delete temporary directory
try:
shutil.rmtree(working_dir)
except OSError:
pass
def random_sequence(n_samples, sequence_type='AA', return_type='repeat',
equilibrium_frequencies='human', l=0, n=0,
sequence_length=0):
""" Simulate random sequence locally.
Simulate random sequence locally.
Args:
n_samples (int): The number of samples
sequence_type (str): Either "AA" or "DNA"
return_type (str): Either "repeat" or "list"
equilibrium_frequencies (str): Only "human" option available at current
l (int): The length of the repeat unit
n (int): The number of repeat units in the tandem repeat
sequence_length (int): The total length of the simulated sequence
Returns:
Return type depends on ``return_type``.
"""
if sequence_length == 0 and (l == 0 or n == 0):
LOG.error('The specified sequence_length or the product of l and n was'
' set to 0 for random_sequence simulation')
else:
if sequence_length == 0:
sequence_length = l * n
# assert equilibrium_frequencies == 'human'
file = config_file('data', 'Random', "_".join(
[sequence_type, equilibrium_frequencies, '3']) + '.txt')
with open(file, 'r') as f:
a = f.readline()[:-1]
b = [i.split(' ') for i in a.split(' ')]
frequencies = {i[0]: int(i[1]) for i in b}
alphabet = np.unique([i[0] for i in frequencies.keys()])
for _ in range(n_samples):
seed_int = random.randint(1, sum(frequencies.values()))
for key, value in frequencies.items():
seed_int -= value
if seed_int <= 0:
seed = key
break
dimer = [''.join(i) for i in itertools.product(alphabet, repeat=2)]
third_letter_frequencies = {
iD: {
iA: frequencies[
iD +
iA] for iA in alphabet} for iD in dimer}
sequence = seed
for _ in range(sequence_length - 3):
next_int = random.randint(
1, sum(third_letter_frequencies[sequence[-2:]].values()))
for key, value in third_letter_frequencies[
sequence[-2:]].items():
next_int -= value
if next_int <= 0:
sequence += key
break
# return Repeat instances
if return_type == 'repeat' and not l == 0 and not n == 0:
yield [sequence[i * l:(i + 1) * l] for i in range(n)], sequence_type
elif return_type == 'list':
yield sequence
else: # return seqIO instances
yield Bio.Seq.Seq(sequence, sequence_type)