-
Notifications
You must be signed in to change notification settings - Fork 0
/
splice_sim.py
319 lines (266 loc) · 9.18 KB
/
splice_sim.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
import argparse
import itertools
import random
import sys
def random_seq(n):
seq = []
for _ in range(n): seq.append(random.choice('ACGT'))
return ''.join(seq)
def random_intron(n, canonical=True):
if canonical: return 'GT' + random_seq(n-4) + 'AG'
else: return 'AA' + random_seq(n-4) + 'TT'
def make_snps(seq):
pass
def make_indels(seq):
pass
def generate_reads(seq, rsize):
for i in range(len(seq) -rsize + 1): yield seq[i:i+rsize]
def write_fasta(fp, name, seq, wrap=True):
print(f'>{name}', file=fp)
if wrap == False:
print(seq, file=fp)
return
for i in range(0, len(seq), 80):
print(seq[i:i+80], file=fp)
parser = argparse.ArgumentParser(description='splicing data simulator')
parser.add_argument('root', help='path to the project directory')
parser.add_argument('--seed', type=int, default=1,
help='random seed [%(default)i]')
parser.add_argument('--rsize', type=int, default=100,
help='read size [%(default)i]')
parser.add_argument('--flank', type=int, default=100,
help='flanking sequence upstream and downstream [%(default)i]')
parser.add_argument('--exon', type=int, default=100,
help='non-variable exon size [%(default)i]')
parser.add_argument('--intron', type=int, default=100,
help='non-variable intron size [%(default)i]')
parser.add_argument('--vintron', nargs=2, default=[5, 40],
help='variable intron size min & max %(default)s')
parser.add_argument('--vexon', nargs=2, default=[1, 40],
help='variable exon size min & max %(default)s')
arg = parser.parse_args()
random.seed(arg.seed)
# globals
cid = 0 # unique chromosome id
rid = 0 # unique read id
#-----------------------------------------------------------------------------
# Experiment 1
# Junk sequences: none of the sequences align
# [junk]
gfp = open(f'{arg.root}/junk.genome.fa', 'w')
rfp = open(f'{arg.root}/junk.reads.fa', 'w')
ifp = open(f'{arg.root}/junk.info.txt', 'w')
cid += 1
for _ in range(arg.exon):
rid += 1
read = random_seq(arg.rsize)
write(fasta(rfp, f'c{cid}.r{rid}', read, wrap=False)
gfp.close()
rfp.close()
ifp.close()
#-----------------------------------------------------------------------------
# Experiment 2
# Vanilla alignment: create and align all reads
# [exon]---intron---[exon]
gfp = open(f'{arg.root}/vanilla.genome.fa', 'w')
rfp = open(f'{arg.root}/vanilla.reads.fa', 'w')
ifp = open(f'{arg.root}/vanilla.info.txt', 'w')
cid += 1
f1 = random_seq(arg.flank)
e1 = random_seq(arg.exon)
i1 = random_intron(arg.intron)
e2 = random_seq(arg.exon)
f2 = random_seq(arg.flank)
write_fasta(gfp, f'c{cid}', f1 + e1 + i1 + e2 + f2)
for read in generate_reads(e1 + e2, arg.rsize):
rid += 1
write_fasta(rfp, f'c{cid}.r{rid}', read, wrap=False)
ibeg = arg.flank + arg.exon + 1
iend = ibeg + arg.intron -1
text = f'intron-{arg.intron}'
print(f'c{cid}', 'intron', ibeg, iend, text, file=ifp)
gfp.close()
rfp.close()
ifp.close()
#-----------------------------------------------------------------------------
# Experiment 3
# Extra sequences: reads polluted with junk
# [exon]---intron---[exon]
# [junk]
gfp = open(f'{arg.root}/extra.genome.fa', 'w')
rfp = open(f'{arg.root}/extra.reads.fa', 'w')
ifp = open(f'{arg.root}/extra.info.txt', 'w')
cid += 1
f1 = random_seq(arg.flank)
e1 = random_seq(arg.exon)
i1 = random_intron(arg.intron)
e2 = random_seq(arg.exon)
f2 = random_seq(arg.flank)
write_fasta(gfp, f'c{cid}', f1 + e1 + i1 + e2 + f2)
for read in generate_reads(e1 + e2, arg.rsize):
rid += 1
write_fasta(rfp, f'c{cid}.r{rid}', read, wrap=False)
ibeg = arg.flank + arg.exon + 1
iend = ibeg + arg.intron -1
text = f'intron-{arg.intron}'
print(f'c{cid}', 'intron', ibeg, iend, text, file=ifp)
for _ in range(arg.exon *2):
rid += 1
read = random_seq(arg.rsize)
write(fasta(rfp, f'c{cid}.r{rid}', read, wrap=False)
gfp.close()
rfp.close()
ifp.close()
#-----------------------------------------------------------------------------
# Experiment 4
# how do intron lengths affect alignment?
# [exon]---v.intron---[exon]
vimin = int(arg.vintron[0])
vimax = int(arg.vintron[1])
gfp = open(f'{arg.root}/var_intron.genome.fa', 'w')
rfp = open(f'{arg.root}/var_intron.reads.fa', 'w')
ifp = open(f'{arg.root}/var_intron.info.txt', 'w')
for ilen in range(vimin, vimax +1):
cid += 1
f1 = random_seq(arg.flank)
e1 = random_seq(arg.exon)
iv = random_intron(ilen)
e2 = random_seq(arg.exon)
f2 = random_seq(arg.flank)
write_fasta(gfp, f'c{cid}', f1 + e1 + iv + e2 + f2)
for read in generate_reads(e1 + e2, arg.rsize):
rid += 1
write_fasta(rfp, f'c{cid}.r{rid}', read, wrap=False)
ibeg = arg.flank + arg.exon + 1
iend = ibeg + ilen
text = f'vanilla-{ilen}'
print(f'c{cid}', 'intron', ibeg, iend, text, file=ifp)
gfp.close()
rfp.close()
ifp.close()
#-----------------------------------------------------------------------------
# Experiment 5
# how does middle exon length affect alignment?
# [exon]---intron---[v.exon]---intron---[exon]
vemin = int(arg.vexon[0])
vemax = int(arg.vexon[1])
gfp = open(f'{arg.root}/var_exon.genome.fa', 'w')
rfp = open(f'{arg.root}/var_exon.reads.fa', 'w')
ifp = open(f'{arg.root}/var_exon.info.txt', 'w')
for elen in range(vemin, vemax + 1):
cid += 1
f1 = random_seq(arg.flank)
e1 = random_seq(arg.exon)
i1 = random_intron(arg.intron)
ev = random_seq(elen)
i2 = random_intron(arg.intron)
e2 = random_seq(arg.exon)
f2 = random_seq(arg.flank)
write_fasta(gfp, f'c{cid}', f1 + e1 + i1 + ev + i2 + e2 + f2)
for read in generate_reads(e1 + ev + e2, arg.rsize):
rid += 1
write_fasta(rfp, f'c{cid}.r{rid}', read, wrap=False)
i1b = arg.flank + arg.exon
i1e = i1b + arg.intron
i2b = arg.flank + arg.exon + elen
i2e = i2b + arg.intron
text = f'exon-{elen}'
print(f'c{cid}', 'intron', i1b, i1e, text, file=ifp)
print(f'c{cid}', 'intron', i2b, i2e, text, file=ifp)
gfp.close()
rfp.close()
ifp.close()
#-----------------------------------------------------------------------------
# Experiment 6
# how does initial exon length affect alignment?
# [v.exon]---intron---[exon]
gfp = open(f'{arg.root}/var_einit.genome.fa', 'w')
rfp = open(f'{arg.root}/var_einit.reads.fa', 'w')
ifp = open(f'{arg.root}/var_einit.info.txt', 'w')
for elen in range(vemin, vemax + 1):
cid += 1
f1 = random_seq(arg.flank)
ev = random_seq(elen)
i1 = random_intron(arg.intron)
e2 = random_seq(arg.exon)
f2 = random_seq(arg.flank)
write_fasta(gfp, f'c{cid}', f1 + ev + i1 + e2 + f2)
for read in generate_reads(ev + e2, arg.rsize):
rid += 1
write_fasta(rfp, f'c{cid}.r{rid}', read, wrap=False)
ibeg = arg.flank + elen
iend = ibeg + arg.intron
text = f'einit-{elen}'
print(f'c{cid}', 'intron', ibeg, iend, text, file=ifp)
gfp.close()
rfp.close()
ifp.close()
#-----------------------------------------------------------------------------
# Experiment 7
# how does terminal exon length affect alignment?
# [exon]---intron---[v.exon]
gfp = open(f'{arg.root}/var_eterm.genome.fa', 'w')
rfp = open(f'{arg.root}/var_eterm.reads.fa', 'w')
ifp = open(f'{arg.root}/var_eterm.info.txt', 'w')
for elen in range(vemin, vemax + 1):
cid += 1
f1 = random_seq(arg.flank)
e1 = random_seq(arg.exon)
i1 = random_intron(arg.intron)
ev = random_seq(elen)
f2 = random_seq(arg.flank)
write_fasta(gfp, f'c{cid}', f1 + e1 + i1 + ev + f2)
for read in generate_reads(e1 + ev, arg.rsize):
rid += 1
write_fasta(rfp, f'c{cid}.r{rid}', read, wrap=False)
ibeg = arg.exon
iend = ibeg + arg.intron
text = f'eterm-{elen}'
print(f'c{cid}', 'intron', ibeg, iend, text, file=ifp)
gfp.close()
rfp.close()
ifp.close()
#-----------------------------------------------------------------------------
# Experiment
# how does trans-splicing affect alignment
# [ts.exon]---intron---[exon]
#-----------------------------------------------------------------------------
# Experiment
# how does poly-A tail affect alignment?
# [exon]---intron---[exon][poly-A]
#-----------------------------------------------------------------------------
# Experiment
# how do 5' artefacts affect alignment?
# [v.artefact][exon]---intron---[exon]
#-----------------------------------------------------------------------------
# Experiment
# how do 3' artefacts affect alignment?
# [exon]---intron---[exon][v.artefact]
#-----------------------------------------------------------------------------
# Experiment
# how do 5' internal artefacts affect alignment?
# [exon][v.artefact]---intron---[exon]
#-----------------------------------------------------------------------------
# Experiment
# how do 3' internal artefacts affect alignment?
# [exon]---intron---[v.artefact][exon]
#-----------------------------------------------------------------------------
# Experiment
# how do substitutions affect alignment?
# [s.exon]---intron---[s.exon]
#-----------------------------------------------------------------------------
# Experiment
# how do indels affect alignment?
# [i.exon]---intron---[i.exon]
#-----------------------------------------------------------------------------
# Experiment
# how does low complexity affect alignment?
# [lc.exon]---intron---[lc.exon]
#-----------------------------------------------------------------------------
# Experiment
# how does duplication affect alignment?
# [exon1]---intron---[exon2] ... [exon1]---intron---[exon2]
#-----------------------------------------------------------------------------
# Experiment
# how do non-canonical splice sites affect alignment?
# [exon]---nc.intron---[exon]