-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_uta_database.py
More file actions
505 lines (427 loc) · 15.4 KB
/
test_uta_database.py
File metadata and controls
505 lines (427 loc) · 15.4 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
"""Test UTA data source."""
from urllib.parse import urlparse
import pytest
from cool_seq_tool.schemas import Strand
from cool_seq_tool.sources.uta_database import (
GenomicTxData,
GenomicTxMetadata,
ParseResult,
TxExonAlnData,
)
@pytest.fixture(scope="module")
def tx_exon_aln_data():
"""Create test fixture for tx_exon_aln_data test."""
return TxExonAlnData(
hgnc="BRAF",
ord=14,
tx_ac="NM_004333.4",
tx_start_i=1802,
tx_end_i=1921,
alt_ac="NC_000007.13",
alt_start_i=140453074,
alt_end_i=140453193,
alt_strand=Strand.NEGATIVE,
alt_aln_method="splign",
tx_exon_id=780494,
alt_exon_id=1927263,
)
@pytest.fixture(scope="module")
def data_from_result():
"""Create test fixture for data from result"""
params = {
"gene": "BRAF",
"strand": Strand.NEGATIVE,
"tx_pos_range": (1802, 1921),
"alt_pos_range": (140453074, 140453193),
"alt_aln_method": "splign",
"tx_exon_id": 780494,
"alt_exon_id": 1927263,
}
return GenomicTxData(**params)
@pytest.mark.asyncio
async def test_get_cds_start_end(test_db):
"""Test that get_cds_start_end works correctly."""
expected = (61, 2362)
resp = await test_db.get_cds_start_end("NM_004333.4")
assert resp == expected
resp = await test_db.get_cds_start_end("ENST00000288602.6")
assert resp == expected
resp = await test_db.get_cds_start_end("NM_004333.999")
assert resp is None
@pytest.mark.asyncio
async def test_get_newest_assembly_ac(test_db):
"""Test that get_newest_assembly_ac works correctly."""
resp = await test_db.get_newest_assembly_ac("NC_000007.13")
assert resp == ["NC_000007.14"]
resp = await test_db.get_newest_assembly_ac("NC_000011.9")
assert resp == ["NC_000011.10"]
resp = await test_db.get_newest_assembly_ac("NC_000011.10")
assert resp == ["NC_000011.10"]
resp = await test_db.get_newest_assembly_ac("ENST00000288602")
assert resp == ["ENST00000288602"]
resp = await test_db.get_newest_assembly_ac("NC_0000077.1")
assert resp == []
@pytest.mark.asyncio
async def test_validate_genomic_ac(test_db):
"""Test that validate_genomic_ac"""
resp = await test_db.validate_genomic_ac("NC_000007.13")
assert resp is True
resp = await test_db.validate_genomic_ac("NC_000007.17")
assert resp is False
@pytest.mark.asyncio
async def test_validate_gene_exists(test_db):
"""Test validate_gene_symbol"""
resp = await test_db.gene_exists("TPM3")
assert resp is True
resp = await test_db.gene_exists("dummy gene")
assert resp is False
@pytest.mark.asyncio
async def test_validate_transcript_exists(test_db):
"""Tests validate_transcript"""
resp = await test_db.transcript_exists("NM_152263.3")
assert resp is True
resp = await test_db.transcript_exists("NM_152263 3")
assert resp is False
@pytest.mark.asyncio
async def test_get_ac_descr(test_db):
"""Test that get_ac_descr works correctly."""
resp = await test_db.get_ac_descr("NC_000007.13")
assert resp is not None
resp = await test_db.get_ac_descr("NC_000007.14")
assert resp is None
@pytest.mark.asyncio
async def test_get_tx_exon_aln_data(test_db, tx_exon_aln_data):
"""Test that get_tx_exon_aln_data"""
resp = await test_db.get_tx_exon_aln_data(
"NM_004333.4", 140453136, 140453136, alt_ac="NC_000007.13", use_tx_pos=False
)
assert resp == [tx_exon_aln_data]
resp = await test_db.get_tx_exon_aln_data(
"NM_004333.4", 140453136, 140453136, alt_ac=None, use_tx_pos=False
)
assert resp == [tx_exon_aln_data]
resp = await test_db.get_tx_exon_aln_data(
"NM_004333.4", 1860, 1860, alt_ac=None, use_tx_pos=True
)
assert resp == [
TxExonAlnData(
hgnc="BRAF",
ord=14,
tx_ac="NM_004333.4",
tx_start_i=1802,
tx_end_i=1921,
alt_ac="NC_000007.13",
alt_start_i=140453074,
alt_end_i=140453193,
alt_strand=Strand.NEGATIVE,
alt_aln_method="splign",
tx_exon_id=780494,
alt_exon_id=1927263,
),
TxExonAlnData(
hgnc="BRAF",
ord=14,
tx_ac="NM_004333.4",
tx_start_i=1802,
tx_end_i=1921,
alt_ac="NC_000007.14",
alt_start_i=140753274,
alt_end_i=140753393,
alt_strand=Strand.NEGATIVE,
alt_aln_method="splign",
tx_exon_id=780494,
alt_exon_id=6619850,
),
]
@pytest.mark.asyncio
async def test_data_from_result(test_db, tx_exon_aln_data, data_from_result):
"""Test that data_from_result works correctly."""
resp = test_db.data_from_result(tx_exon_aln_data)
assert resp == data_from_result
@pytest.mark.asyncio
async def test_mane_c_genomic_data(test_db):
"""Test that get_mane_c_genomic_data works correctly."""
resp = await test_db.get_mane_c_genomic_data(
"NM_001374258.1", None, 140753335, 140753335
)
expected_params = {
"gene": "BRAF",
"strand": Strand.NEGATIVE,
"tx_pos_range": (2087, 2206),
"alt_pos_range": (140753274, 140753393),
"alt_aln_method": "splign",
"tx_exon_id": 8439617,
"alt_exon_id": 9353476,
"coding_start_site": 226,
"coding_end_site": 2650,
"pos_change": (58, 61),
"alt_pos_change_range": (140753335, 140753335),
"tx_ac": "NM_001374258.1",
"alt_ac": "NC_000007.14",
}
assert resp == GenomicTxMetadata(**expected_params)
# Test example where sorting of tx_exon_aln_mv is needed
resp = await test_db.get_mane_c_genomic_data(
"NM_000077.5", "NC_000009.12", 21971186, 21971187
)
expected_params = {
"gene": "CDKN2A",
"strand": Strand.NEGATIVE,
"tx_pos_range": (180, 487),
"alt_pos_range": (21970901, 21971208),
"alt_aln_method": "splign",
"tx_exon_id": 8314723,
"alt_exon_id": 8960507,
"coding_start_site": 30,
"coding_end_site": 501,
"pos_change": (21, 285),
"alt_pos_change_range": (21971187, 21971186),
"tx_ac": "NM_000077.5",
"alt_ac": "NC_000009.12",
}
assert resp == GenomicTxMetadata(**expected_params)
# Test case where chromosomal accession is not provided
resp = await test_db.get_mane_c_genomic_data(
"NM_000077.5", None, 21971186, 21971187
)
assert resp == GenomicTxMetadata(**expected_params)
@pytest.mark.asyncio
async def test_get_genomic_tx_data(test_db, genomic_tx_data):
"""Test that get_genomic_tx_data works correctly."""
# Positive strand transcript
resp = await test_db.get_genomic_tx_data("NM_004327.3", (3595, 3596))
expected_params = {
"gene": "BCR",
"strand": Strand.POSITIVE,
"tx_pos_range": (3476, 3608),
"alt_pos_range": (23295023, 23295155),
"alt_aln_method": "splign",
"tx_exon_id": 956565,
"alt_exon_id": 6619783,
"tx_ac": "NM_004327.3",
"alt_ac": "NC_000022.11",
"pos_change": (119, 12),
"alt_pos_change_range": (23295142, 23295143),
}
assert resp == GenomicTxMetadata(**expected_params)
# Negative strand transcript
resp = await test_db.get_genomic_tx_data("NM_004333.4", (2144, 2145))
expected_params = {
"gene": "BRAF",
"strand": Strand.NEGATIVE,
"tx_pos_range": (2053, 2188),
"alt_pos_range": (140739811, 140739946),
"alt_aln_method": "splign",
"tx_exon_id": 780496,
"alt_exon_id": 6619852,
"tx_ac": "NM_004333.4",
"alt_ac": "NC_000007.14",
"pos_change": (91, 43),
"alt_pos_change_range": (140739855, 140739854),
}
assert resp == GenomicTxMetadata(**expected_params)
@pytest.mark.asyncio
async def test_get_ac_from_gene(test_db):
"""Test that get_ac_from_gene works correctly."""
resp = await test_db.get_ac_from_gene("BRAF")
assert resp == ["NC_000007.14", "NC_000007.13"]
resp = await test_db.get_ac_from_gene("HRAS")
assert resp == ["NC_000011.10", "NC_000011.9"]
resp = await test_db.get_ac_from_gene("dummy")
assert resp == []
@pytest.mark.asyncio
async def test_get_gene_from_ac(test_db):
"""Tet that get_gene_from_ac works correctly."""
resp = await test_db.get_gene_from_ac("NC_000007.13", 140453136, None)
assert resp == ["BRAF"]
resp = await test_db.get_gene_from_ac("NC_000007.14", 140753336, None)
assert resp == ["BRAF"]
resp = await test_db.get_gene_from_ac("NC_000007.13", 55249071, None)
assert resp == ["EGFR", "EGFR-AS1"]
resp = await test_db.get_gene_from_ac("NC_0000078.1", 140453136, None)
assert resp is None
@pytest.mark.asyncio
async def test_get_transcripts_from_gene(test_db):
"""Test that get_transcripts works correctly."""
resp = await test_db.get_transcripts(start_pos=2145, end_pos=2145, gene="BRAF")
assert len(resp) == 32
# using no start/end pos
resp = await test_db.get_transcripts(gene="BRAF")
assert len(resp) == 32
# using 0 start/end pos
resp = await test_db.get_transcripts(gene="BRAF", start_pos=0, end_pos=0)
assert len(resp) == 32
# using 0 genomic start/end pos
resp = await test_db.get_transcripts(
gene="BRAF", start_pos=0, end_pos=0, use_tx_pos=False
)
assert len(resp) == 0
# using gene with genomic pos
resp = await test_db.get_transcripts(
gene="BRAF", start_pos=140753336, end_pos=140753336, use_tx_pos=False
)
assert len(resp) == 16
resp = await test_db.get_transcripts(
gene="BRAF", start_pos=140453136, end_pos=140453136
)
assert len(resp) == 0
# No gene and no alt_ac
resp = await test_db.get_transcripts(start_pos=140453136, end_pos=140453136)
assert len(resp) == 0
@pytest.mark.asyncio
async def test_get_chr_assembly(test_db):
"""Test that get_chr_assembly works correctly."""
resp = await test_db.get_chr_assembly("NC_000007.13")
assert resp == ("chr7", "GRCh37")
resp = await test_db.get_chr_assembly("NC_000007.14")
assert resp is None
# Invalid ac
resp = await test_db.get_chr_assembly("NC_00000714")
assert resp is None
@pytest.mark.asyncio
async def test_p_to_c_ac(test_db):
"""Test that p_to_c_ac works correctly."""
resp = await test_db.p_to_c_ac("NP_004324.2")
assert resp == ["NM_004333.4", "NM_004333.5", "NM_004333.6"]
resp = await test_db.p_to_c_ac("NP_064502.9")
assert resp == ["NM_020117.9", "NM_020117.10", "NM_020117.11"]
resp = await test_db.p_to_c_ac("NP_004324.22")
assert resp == []
@pytest.mark.asyncio
async def test_get_alt_ac_start_or_end(
test_db, tpm3_1_8_start_genomic, tpm3_1_8_end_genomic
):
"""Test that get_alt_ac_start_or_end works correctly."""
resp = await test_db.get_alt_ac_start_or_end("NM_152263.3", 117, 234, None)
assert resp[0] == tpm3_1_8_start_genomic
assert resp[1] is None
resp = await test_db.get_alt_ac_start_or_end("NM_152263.3", 822, 892, None)
assert resp[0] == tpm3_1_8_end_genomic
assert resp[1] is None
resp = await test_db.get_alt_ac_start_or_end("NM_152263.63", 822, 892, None)
assert resp[0] is None
assert (
resp[1] == "Unable to find a result where NM_152263.63 has "
"transcript coordinates 822 and 892 between an exon's "
"start and end coordinates"
)
@pytest.mark.asyncio
async def test_get_mane_transcripts_from_genomic_pos(test_db):
"""Test that get_mane_transcripts_from_genomic_pos works correctly"""
resp = await test_db.get_transcripts_from_genomic_pos("NC_000007.14", 140753336)
assert set(resp) == {
"NM_001354609.1",
"NM_001354609.2",
"NM_001374244.1",
"NM_001374258.1",
"NM_001378467.1",
"NM_001378468.1",
"NM_001378469.1",
"NM_001378470.1",
"NM_001378471.1",
"NM_001378472.1",
"NM_001378473.1",
"NM_001378474.1",
"NM_001378475.1",
"NM_004333.4",
"NM_004333.5",
"NM_004333.6",
}
# invalid pos
resp = await test_db.get_transcripts_from_genomic_pos("NC_000007.14", 150753336)
assert resp == []
# invalid ac
resp = await test_db.get_transcripts_from_genomic_pos("NC_000007.14232", 140753336)
assert resp == []
@pytest.mark.parametrize(
("raw_url", "expected"),
[
# Username + password
(
"postgresql://user:pass@localhost:5432/dbname",
{
"scheme": "postgresql",
"username": "user",
"password": "pass",
"hostname": "localhost",
"port": 5432,
"database": "dbname",
"sanitized_url": "postgresql://user:***@localhost:5432/dbname",
},
),
# Username with null password
(
"postgresql://user@localhost/dbname",
{
"scheme": "postgresql",
"username": "user",
"password": None,
"hostname": "localhost",
"port": None,
"database": "dbname",
"sanitized_url": "postgresql://user@localhost/dbname",
},
),
# Password is "0"
(
"postgresql://user:0@localhost/dbname",
{
"scheme": "postgresql",
"username": "user",
"password": "0",
"hostname": "localhost",
"port": None,
"database": "dbname",
"sanitized_url": "postgresql://user:***@localhost/dbname",
},
),
# Empty password
(
"postgresql://user:@localhost/dbname",
{
"scheme": "postgresql",
"username": "user",
"password": "",
"hostname": "localhost",
"port": None,
"database": "dbname",
"sanitized_url": "postgresql://user@localhost/dbname",
},
),
# No username
(
"postgresql://localhost:5432/dbname",
{
"scheme": "postgresql",
"username": None,
"password": None,
"hostname": "localhost",
"port": 5432,
"database": "dbname",
"sanitized_url": "postgresql://localhost:5432/dbname",
},
),
# With query params
(
"postgresql://user:secret@localhost/dbname?query#fragment",
{
"scheme": "postgresql",
"username": "user",
"password": "secret",
"hostname": "localhost",
"port": None,
"database": "dbname",
"sanitized_url": "postgresql://user:***@localhost/dbname?query#fragment",
},
),
],
)
async def test_parsed_url(raw_url, expected):
parsed_result = ParseResult(urlparse(raw_url))
assert parsed_result.scheme == expected["scheme"]
assert parsed_result.username == expected["username"]
assert parsed_result.password == expected["password"]
assert parsed_result.hostname == expected["hostname"]
assert parsed_result.port == expected["port"]
assert parsed_result.database == expected["database"]
assert parsed_result.sanitized_url == expected["sanitized_url"]