-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_experiments.py
336 lines (287 loc) · 10.1 KB
/
test_experiments.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
import unittest
from offsetbasedgraph import Interval, Graph, \
Translation, Block, CriticalPathsMultiPathInterval
from offsetbasedgraph.graphcreators import convert_to_numeric_graph,\
connect_without_flanks, create_initial_grch38_graph, merge_flanks,\
grch38_graph_to_numeric
from offsetbasedgraph.graphutils import create_gene_dicts, \
get_gene_objects_as_intervals
class TestExperiments(unittest.TestCase):
def test_overlapping_alt_loci(self):
chrom_file = "data/chrom.sizes.test"
alt_loci = "data/alt_loci_test"
graph = create_initial_grch38_graph(chrom_file)
numeric_graph, name_translation = convert_to_numeric_graph(graph)
self.assertEqual(len(graph.blocks), 3)
self.assertEqual(len([a for a, v in graph.adj_list.items() if v]), 0)
new_numeric_graph, numeric_translation = \
connect_without_flanks(numeric_graph, alt_loci, name_translation)
correct_graph_structure = Graph(
{
1: Block(1),
2: Block(1),
3: Block(1),
4: Block(1),
5: Block(1),
6: Block(1),
7: Block(1),
8: Block(1),
9: Block(1),
},
{
1: [2, 8],
2: [3, 9],
3: [4],
4: [5],
5: [6],
6: [7],
9: [5],
8: [6]
}
)
self.assertTrue(correct_graph_structure.has_identical_structure(new_numeric_graph))
def test_merge_flanks_2x(self):
# Merge flanks of two alt loci
g = Graph({1: Block(8), 2: Block(3), 3: Block(5)}, {})
# MERGE FIRST ALT LOCUS
intervals = [
Interval(2, 3, [1], g),
Interval(0, 1, [2], g),
Interval(4, 5, [1], g),
Interval(2, 3, [2], g)
]
final_trans = Translation({}, {}, graph=g)
final_trans.graph2 = g
name_trans = Translation({}, {}, graph=g)
new_graph, trans = merge_flanks(intervals, final_trans, g, name_trans)
correct_structure = Graph(
{
1: Block(1),
2: Block(1),
3: Block(1),
4: Block(1),
5: Block(1),
6: Block(1),
7: Block(1),
},
{
1: [2],
2: [3, 6],
3: [4],
6: [4],
4: [5]
}
)
self.assertTrue(new_graph.has_identical_structure(correct_structure))
# MERGE SECOND ALT LOCUS
intervals = [
Interval(1, 3, [1], g),
Interval(0, 2, [3], g),
Interval(5, 6, [1], g),
Interval(4, 5, [3], g)
]
new_graph, trans = merge_flanks(intervals, trans, new_graph, name_trans)
correct_structure = Graph(
{
1: Block(1),
2: Block(1),
3: Block(1),
4: Block(1),
5: Block(1),
6: Block(1),
7: Block(1),
8: Block(1),
9: Block(1),
},
{
1: [2],
2: [3],
3: [8, 4, 9],
4: [5],
5: [6],
6: [7],
8: [5],
9: [6]
}
)
self.assertTrue(new_graph.has_identical_structure(correct_structure))
def test_merge_flanks_2x_case2(self):
# Merge flanks of two alt loci
g = Graph({1: Block(8), 2: Block(3), 3: Block(5)}, {})
# MERGE FIRST ALT LOCUS
intervals = [
Interval(2, 3, [1], g),
Interval(0, 1, [2], g),
Interval(4, 5, [1], g),
Interval(2, 3, [2], g)
]
final_trans = Translation({}, {}, graph=g)
final_trans.graph2 = g
name_trans = Translation({}, {}, graph=g)
new_graph, trans = merge_flanks(intervals, final_trans, g, name_trans)
# MERGE SECOND ALT LOCUS
intervals = [
Interval(2, 4, [1], g),
Interval(0, 2, [3], g),
Interval(5, 6, [1], g),
Interval(4, 5, [3], g)
]
new_graph, trans = merge_flanks(intervals, trans, new_graph, name_trans)
correct_structure = Graph(
{
1: Block(1),
2: Block(1),
3: Block(1),
4: Block(1),
5: Block(1),
6: Block(1),
7: Block(1),
8: Block(1)
},
{
1: [2],
2: [3, 7],
3: [8, 4],
4: [5],
5: [6],
7: [4],
8: [5]
}
)
self.assertTrue(new_graph.has_identical_structure(correct_structure))
def _test_merge_alt_using_cigar(self):
# Case 1
graph = Graph({
"chr1": Block(30),
"chr1_test_alt": Block(10)
},
{});
graph, trans = grch38_graph_to_numeric(graph)
start = 10
end = 20
alt_seq = "CCCTGGGAAA"
main_seq = "CCCGGGTAAA"
cigar = "M3 I1 M3 1D M3"
trans, new_graph = _merge_alt_using_cigar(graph, trans,
"chr1_test_alt",
cigar,
alt_seq,
main_seq,
"chr1",
start,
end,
0,
10)
correct_structure = Graph(
{
1: Block(1),
2: Block(1),
3: Block(1),
4: Block(1),
5: Block(1),
6: Block(1),
7: Block(1),
},
{
1: [2],
2: [3, 6],
3: [4, 7],
4: [5],
6: [3],
7: [4]
}
)
self.assertTrue(new_graph.has_identical_structure(correct_structure))
def _test_merge_alt_using_cigar2(self):
# Case 2, using M with match and mismatches
graph = Graph({
"chr1": Block(31),
"chr1_test_alt": Block(20)
},
{});
graph, trans = grch38_graph_to_numeric(graph)
start = 5
end = 21
alt_seq = "GCCCCTTTTATTTTATTTTA"
main_seq ="G" +"TTTTGTTTTGTTTTA"
cigar = "1M 4I 15M"
trans, new_graph = _merge_alt_using_cigar(graph, trans,
"chr1_test_alt",
cigar,
alt_seq,
main_seq,
"chr1",
start,
end,
0,
20)
correct_structure = Graph(
{
1: Block(1),
2: Block(1),
3: Block(1),
4: Block(1),
5: Block(1),
6: Block(1),
7: Block(1),
},
{
1: [2],
2: [3, 6],
3: [4, 7],
4: [5],
6: [3],
7: [4]
}
)
#self.assertTrue(new_graph.has_identical_structure(correct_structure))
def test_create_gene_dicts(self):
genes_file_name = "data/genes_test.txt"
genes = get_gene_objects_as_intervals(genes_file_name)
alt_loci_genes, gene_name_dict, main_genes = create_gene_dicts(
genes, "data/grch38_alt_loci.txt")
print(alt_loci_genes)
print("-------------")
for k, v in gene_name_dict.items():
print(k, v)
print("------------")
self.assertEqual(len(alt_loci_genes["chr1_KI270762v1_alt"]), 2)
self.assertEqual(len(alt_loci_genes["chr1_GL383518v1_alt"]), 1)
self.assertEqual(len(gene_name_dict["gene1"]), 2)
self.assertEqual(len(gene_name_dict["gene2"]), 2)
self.assertEqual(len(gene_name_dict["gene3"]), 2)
self.assertEqual(
sum([len(gene_list) for gene_list in alt_loci_genes.values()]),
len([g for g in genes if "alt" in
g.transcription_region.start_position.region_path_id]))
def test_parse_genes_from_file_and_translate_to_multipath(self):
genes_file_name = "data/genes_test.txt"
genes = get_gene_objects_as_intervals(genes_file_name)
mpintervals = []
for g in genes:
print(g.exons)
mpinterval = CriticalPathsMultiPathInterval(
g.transcription_region.start_position,
g.transcription_region.end_position,
g.exons
)
mpintervals.append(mpinterval)
first = mpintervals[0]
self.assertEqual(len(first.critical_intervals), 2)
self.assertEqual(first.critical_intervals[0].start_position.offset, 14)
self.assertEqual(first.critical_intervals[0].end_position.offset, 15)
self.assertEqual(first.critical_intervals[1].start_position.offset, 16)
self.assertEqual(first.critical_intervals[1].end_position.offset, 17)
for g in mpintervals:
self.assertEqual(len(g.critical_intervals), 2)
def test_visualization_not_crashes(self):
# Only a simple test to check that
# the visualization does not crash
# TODO: better tests for visualization than this
args = lambda: None
args.alt_locus = "chr1_KI270762v1_alt"
from methods import visualize_alt_locus_wrapper
visualize_alt_locus_wrapper(args, True)
self.assertTrue(True)
if __name__ == "__main__":
unittest.main()