-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq_gen.py
434 lines (367 loc) · 12.8 KB
/
seq_gen.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
#! /usr/bin/python
# randomized motif-embedding sequence generator
# by: jake martinez (jrm98)
import random
import argparse
import time
from pprint import pprint
def main():
opt = argparse.ArgumentParser()
opt.add_argument('-w', default=8, type=int, help='motif width')
opt.add_argument('-N', default=20, type=int, help='number of sequences')
opt.add_argument('-n', default=100, type=int, help='sequence length')
opt.add_argument('-e', default=0, type=int, help='max # of SNPs in motif')
opt.add_argument('--eprob', default=.65, type=float, help='probability of a SNP existing in the motif')
opt.add_argument('--maxloc', default=-1, type=int, help='max starting location for motifs (-1 represents the end of the sequence)')
opt.add_argument('--minloc', default=0, type=int, help='min starting location for motifs')
opt.add_argument('-P', default=1, type=float, help='probability motif is present in sequence')
opt.add_argument('-o', default=None, type=str, help='output filename')
opt.add_argument('--dir', default='', type=str, help='output directory')
opt.add_argument('--dyad', help='spaced dyad mode (overrides nmotifs parameter, -w option becomes the total width of the combined dyad)', action='store_true')
opt.add_argument('--nmotifs', default=1, type=int, help='max number of distinct motifs in a single sequence')
opt.add_argument('--nsites', default=1, type=int, help='max number of appearances of the motif in a single sequence (not implemented)')
opt.add_argument('--negative', help='used to generate a sequence for a negative discriminative set', action='store_true')
opt.add_argument('--template', default=None, type=str, help='template sequence file path')
opt.add_argument('--bgprob', default=None, type=str, help='background probability file path')
opt.add_argument('--markov', default=None, type=str, help='markov probability file')
opt.add_argument('-v', help='turn on output messages', action='store_true')
opt.add_argument('--verbose', help='turn on output messages', action='store_true')
args=opt.parse_args()
N = args.N
n = args.n
e = args.e
ep = args.eprob
P = args.P
w = args.w
minloc = args.minloc
maxloc = args.maxloc
output = args.o
directory = args.dir
negative = args.negative
nmotifs = args.nmotifs
dyad = args.dyad
tmp = args.template
bg = args.bgprob
mkv = args.markov
verbose = args.verbose or args.v
# create a default filename if none was given
if output is None:
output = 'seq_w'+str(w)+'_N'+str(N)+'_n'+str(n)
if e != 0:
output += '_e'+str(e)
if ep != .65:
output += '_ep'+str(int(ep*100))
if P != 1:
output += '_P'+str(int(ep*100))
output += '-'+str(int(time.time()))+'.fasta'
if directory is None:
directory = ''
if tmp is not None:
print('seq_gen: loading from template...')
template = load_template(tmp,verbose=verbose)
if verbose:
print('seq_gen: loaded statistical model: ')
pprint(template)
generate(w,N,n,e,ep,P,minloc,maxloc,output,directory,negative,
nmotifs,dyad,markov=template,verbose=verbose)
#show the Markov model generated from the synthetic sequence
if verbose:
gen_template = load_template(directory+output,save=False,verbose=verbose)
print('seq_gen: statistical model of output: ')
pprint(gen_template)
#get background probs from template?
# gen_bgprob = load_bgprob(directory+output)
# for key in gen_bgprob:
# print(' '+key+': '+str(bgprob[key])+' => '+str(gen_bgprob[key]))
elif mkv is not None:
print('seq_gen: loading from Markov file...')
markov = load_markov(mkv)
if verbose:
print('seq_gen: loaded statistical model: ')
pprint(markov)
generate(w,N,n,e,ep,P,minloc,maxloc,output,directory,negative,
nmotifs,dyad,markov=markov,verbose=verbose)
#show the Markov model generated from the synthetic sequence
if verbose:
gen_template = load_template(directory+output,save=False,verbose=verbose)
print('seq_gen: statistical model of output: ')
pprint(gen_template)
elif bg is not None:
print('seq_gen: loading from bgprob file...')
bgprob = load_bgprob(bg)
if verbose:
print('seq_gen: loaded statistical model: ')
pprint(bgprob)
generate(w,N,n,e,ep,P,minloc,maxloc,output,directory,negative,
nmotifs,dyad,bgprob=bgprob,verbose=verbose)
#show the background probability model generated from the synthetic sequence
if verbose:
gen_bgprob = load_bgprob(directory+output)
print('seq_gen: statistical model of output: ')
for key in gen_bgprob:
print(' '+key+': '+str(bgprob[key])+' => '+str(gen_bgprob[key]))
else:
generate(w,N,n,e,ep,P,minloc,maxloc,output,directory,negative,
nmotifs,dyad,verbose=verbose)
def generate(w,N,n,e=0,ep=.65,P=1,minloc=0,maxloc=-1,
output='seq.fasta',directory='',negative=False,
nmotifs=1,dyad=False,bgprob={'A':.25,'T':.25,'C':.25,'G':.25},
markov=None,verbose=False):
# look for potential errors in input parameters
psum = bgprob['A'] + bgprob['T'] + bgprob['C'] + bgprob['G']
if psum > 1.0 or psum < .99:
print('seq_gen: invalid background probabilities')
return
if len(directory) > 0 and directory[-1:] != '/':
directory += '/'
# reflect input parameters in output
if verbose:
if negative and not dyad:
print('seq_gen: NEGATIVE=true')
elif dyad:
print('seq_gen: DYAD=true')
print('seq_gen: w='+str(w)+'\tN='+str(N)+'\tn='+str(n)+'\te='+str(e)+'\tP='+str(P))
else:
print('seq_gen: w='+str(w)+'\tN='+str(N)+'\tn='+str(n)+'\te='+str(e)+'\tP='+str(P))
print('\toutput => '+directory+output)
sequences = []
motif = []
if dyad:
w1 = w - random.randint(2,w-2) #requires total motif width of at least 4
w2 = w - w1
motif.append(gen_seq(w1))
motif.append(gen_seq(w2))
if verbose:
print('seq_gen: motifs generated...')
for i in range(N):
sequence = gen_seq(n,bgprob=bgprob,template=markov)
result = embed_motif(sequence, motif, maxerror=e, errorprob=ep, P=P, minstart=minloc, maxstart=maxloc, nmotifs=2)
sequences.append((result,motif,i))
# print result, motif, i
else:
for i in range(nmotifs):
motif.append(gen_seq(w))
if verbose:
print('seq_gen: motifs generated...')
for i in range(N):
sequence = gen_seq(n,bgprob=bgprob,template=markov)
if negative:
result = embed_motif(sequence, motif, maxerror=0, errorprob=0, P=0, minstart=minloc, maxstart=maxloc)
else:
result = embed_motif(sequence, motif, maxerror=e, errorprob=ep, P=P, minstart=minloc, maxstart=maxloc, nmotifs=nmotifs)
sequences.append((result,motif,i))
# print result, motif, i
if verbose:
print('seq_gen: sequences generated...')
write_to_fasta(directory+output, sequences, negative)
if verbose:
print('seq_gen: wrote sequences to file...')
return (sequences,motif)
# generates a string from the alphabet 'ACGT' of the given length iteratively
def gen_seq(length,bgprob={'A':.25,'T':.25,'C':.25,'G':.25},template=None):
if template is None:
seq = ''
for i in range(length):
vals = [float(x) for x in bgprob.values()]
m = sum(vals)
r = random.uniform(0,m)
total = 0.0
for c in bgprob:
if total + float(bgprob[c]) >= r:
seq += c
break
total += float(bgprob[c])
return seq
else:
seq = ''
for i in range(length):
if i == 0:
vals = [float(x) for x in bgprob.values()]
m = sum(vals)
r = random.uniform(0,m)
total = 0.0
for c in bgprob:
if total + float(bgprob[c]) >= r:
seq += c
break
total += float(bgprob[c])
else:
m = sum([float(x) for x in template[seq[-1]].values()])
r = random.uniform(0,m)
total = 0.0
for c in template[seq[-1]]:
if total + float(template[seq[-1]][c]) >= r:
seq += c
break
total += float(template[seq[-1]][c])
return seq
#return ''.join(random.SystemRandom().choice('ACGT') for _ in range(length))
# embeds motif in the given sequence
def embed_motif(seq, motif, maxerror=0, errorprob=.20, minstart=0, maxstart=-1, P=1,nmotifs=1):
if random.random() < P and (maxstart > minstart or maxstart == -1):
if maxstart < 0:
maxstart = len(seq) - len(motif[0]) - 1
# pick starting location for motif
loc = [random.randint(minstart,maxstart)]
# introduce artificial error in motif
errors = [0]
errorind = [[]]
r = random.random()
while r < errorprob and errors[0] < maxerror:
# choose location for snp, if already snp pick another loc
snp = random.randint(0,len(motif[0])-1)
while snp in errorind[0]:
snp = random.randint(0,len(motif[0])-1)
# remove the correct bp from the options for replacement
letters = 'ACGT'.replace(motif[0][snp],'')
# replace bp for another random bp
motif[0] = motif[0][:snp] \
+ random.SystemRandom().choice(letters) \
+ motif[0][snp+1:]
errors[0] += 1
errorind[0].append(snp)
r = random.random()
# embed modified motif in sequence
newseq = seq[:loc[0]] + motif[0] + seq[loc[0] + len(motif[0]):]
# sort snp locations in motif before returning
errorind[0].sort()
# try to add more motifs
if nmotifs > 1 and random.random() < P:
res = embed_motif(newseq, motif[1:],maxerror,errorprob,loc[0]+len(motif[0]),maxstart,P,nmotifs-1)
newseq = res[0]
loc += res[1]
errors += res[3]
errorind += res[4]
return (newseq, loc, motif, errors, errorind)
else:
return (seq, [-1], None, [None], [None])
# writes a set of sequences to a fasta file
def write_to_fasta(filename, sequences, negative):
with open(filename, 'w') as f:
for sequence in sequences:
if negative:
f.write('>artificial'+str(sequence[2])+'-'+str(int(time.time()))+' NEGATIVE sequence'+'\n')
else:
f.write('>artificial'+str(sequence[2])+'-'+str(int(time.time()))+' seq w/ embedded motif:'+str(sequence[1])+' start_loc:'+str(sequence[0][1])+' errors:'+str(sequence[0][3])+' errorind:'+str(sequence[0][4])+'\n')
linelen = 80
line = sequence[0][0]
for i in range(0, len(line), linelen):
f.write(line[i:i+linelen]+'\n')
f.write('\n\n')
pass
def load_template(filename, order=1, save=True, verbose=False):
model = build_markov(order)
order_prob = {key:0 for key in model}
threshold = 0
seq_name = ""
with open(filename, "r") as f:
text = f.read().split("\n")
file_length = len(text)
ln_num = -1
while ln_num < file_length - 1:
# retrieve line
ln_num += 1
line = text[ln_num]
if len(line) == 0:
# ignore blank lines
continue
if line[0] == '>':
# beginning of new sequence, save sequence name
seq_name = line[1:]
continue
# retrieve sequence
sequence = ""
while ln_num < file_length and len(text[ln_num]) != 0:
sequence += line
ln_num += 1
seq_len = len(sequence)
# search sequence and update markov model
pos = 0
while pos < seq_len - (order+1):
match = sequence[pos:pos+order]
order_prob[match] += 1
model[match][sequence[pos+order+1]] += 1
pos += 1
final_markov = model
for key in final_markov:
subtotal = sum([final_markov[key][x] for x in final_markov[key]])
for c in final_markov[key]:
c_freq = final_markov[key][c]
final_markov[key][c] = float(c_freq) / float(subtotal)
if verbose:
print('seq_gen: loaded template.')
if save:
fname = 'mkv_'+str(int(time.time()*100))+'.txt'
save_markov(final_markov, fname)
if verbose:
print('seq_gen: saved template to '+fname)
return final_markov
def save_markov(markov, filename):
with open(filename, "w") as f:
for k in markov:
for c in markov[k]:
f.write("P(%s|%s) = %f\n" % (c, k, markov[k][c]))
# loads markov model from file, wont work if file doesn't have all possibilities
def load_markov(filename):
markov = dict()
with open(filename, "r") as f:
text = f.read().replace(" ","")
text = text.replace("P(","")
text = text.replace("|",",")
text = text.replace(")=",",") # each line should now be c,k,v
lines = text.split()
for line in lines:
print(line)
if line == '':
continue
ckv = line.split(",")
c = ckv[0]
k = ckv[1]
v = ckv[2]
if k not in markov:
markov[k] = dict()
markov[k][c] = v
return markov
# bgprob file format:
# A=.25
# T=.25
# C=.25
# G=.25
def load_bgprob(filename):
with open(filename, 'r') as f:
text = f.read().split('\n')
prob = {'A':.25,'T':.25,'C':.25,'G':.25}
for line in text:
if '=' not in line:
continue
line = line.replace(' ','')
if len(line) < 1:
continue
if line[0] == 'A':
prob['A'] = float(line.split('=')[1])
elif line[0] == 'T':
prob['T'] = float(line.split('=')[1])
elif line[0] == 'C':
prob['C'] = float(line.split('=')[1])
elif line[0] == 'G':
prob['G'] = float(line.split('=')[1])
else:
print('seq_gen: unexpected line format in bgprob file')
return prob
def build_markov(order=1):
chars = ['A','G','C','T']
if order == 1:
model = {x:{x:0 for x in chars} for x in chars}
return model
perm = chars
for i in range(1,order):
perm = permutations(perm)
return {x:{x:0 for x in chars} for x in perm}
def permutations(perm):
chars = ['A','G','C','T']
res = []
for c in chars:
res += [x+c for x in perm]
return res
if __name__ == '__main__':
main()