Skip to content

Commit

Permalink
refactor handling getting alias for SequenceProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
korikuzma committed Aug 30, 2023
1 parent 812f480 commit af267f0
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 34 deletions.
16 changes: 5 additions & 11 deletions src/ga4gh/vrs/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ def _normalize_allele(input_allele, data_proxy, rle_seq_limit=50):
"""
allele = pydantic_copy(input_allele)

# Temporarily convert SequenceReference to IRI because it makes the code simpler.
# This will be changed back to SequenceReference at the end of the method
sequence_reference = None
if isinstance(allele.location.sequence, models.SequenceReference):
sequence_reference = allele.location.sequence
allele.location.sequence = models.IRI(sequence_reference.refgetAccession)
alias = f"ga4gh:{allele.location.sequence.refgetAccession}"
else:
# IRI
alias = allele.location.sequence.root

# Get reference sequence and interval
ref_seq = SequenceProxy(data_proxy, allele.location.sequence.root)
ref_seq = SequenceProxy(data_proxy, alias)
start = _get_allele_location_pos(allele, use_start=True)
if start is None:
return input_allele
Expand Down Expand Up @@ -145,8 +144,6 @@ def _normalize_allele(input_allele, data_proxy, rle_seq_limit=50):
trim_ival[1], end.pos_type
)
new_allele.state.sequence = models.SequenceString(trim_alleles[1])
if sequence_reference:
new_allele.location.sequence = sequence_reference
return new_allele

# Determine bounds of ambiguity
Expand Down Expand Up @@ -189,9 +186,6 @@ def _normalize_allele(input_allele, data_proxy, rle_seq_limit=50):
if (rle_seq_limit and len_sequence <= rle_seq_limit) or (rle_seq_limit is None):
new_allele.state.sequence = models.SequenceString(new_alleles[1])

if sequence_reference:
new_allele.location.sequence = sequence_reference

return new_allele


Expand Down
117 changes: 94 additions & 23 deletions tests/cassettes/test_normalize_allele.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interactions:
Content-Type:
- application/json
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -64,7 +64,78 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: http://localhost:5000/seqrepo/1/metadata/ga4gh:SQ.0iKlIQk2oZLoeOG9P1riRU6hvL5Ux8TV
response:
body:
string: "{\n \"added\": \"2016-08-27T21:22:36Z\",\n \"aliases\": [\n \"GRCh38:6\",\n
\ \"GRCh38:chr6\",\n \"GRCh38.p1:6\",\n \"GRCh38.p1:chr6\",\n \"GRCh38.p10:6\",\n
\ \"GRCh38.p10:chr6\",\n \"GRCh38.p11:6\",\n \"GRCh38.p11:chr6\",\n
\ \"GRCh38.p12:6\",\n \"GRCh38.p12:chr6\",\n \"GRCh38.p2:6\",\n \"GRCh38.p2:chr6\",\n
\ \"GRCh38.p3:6\",\n \"GRCh38.p3:chr6\",\n \"GRCh38.p4:6\",\n \"GRCh38.p4:chr6\",\n
\ \"GRCh38.p5:6\",\n \"GRCh38.p5:chr6\",\n \"GRCh38.p6:6\",\n \"GRCh38.p6:chr6\",\n
\ \"GRCh38.p7:6\",\n \"GRCh38.p7:chr6\",\n \"GRCh38.p8:6\",\n \"GRCh38.p8:chr6\",\n
\ \"GRCh38.p9:6\",\n \"GRCh38.p9:chr6\",\n \"MD5:5691468a67c7e7a7b5f2a3a683792c29\",\n
\ \"NCBI:NC_000006.12\",\n \"refseq:NC_000006.12\",\n \"SEGUID:WZuaTlR1qIRxrJ5dpG2Z0ydeqX4\",\n
\ \"SHA1:599b9a4e5475a88471ac9e5da46d99d3275ea97e\",\n \"VMC:GS_0iKlIQk2oZLoeOG9P1riRU6hvL5Ux8TV\",\n
\ \"sha512t24u:0iKlIQk2oZLoeOG9P1riRU6hvL5Ux8TV\",\n \"ga4gh:SQ.0iKlIQk2oZLoeOG9P1riRU6hvL5Ux8TV\"\n
\ ],\n \"alphabet\": \"ACGNTY\",\n \"length\": 170805979\n}\n"
headers:
Connection:
- close
Content-Length:
- '975'
Content-Type:
- application/json
Date:
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.0iKlIQk2oZLoeOG9P1riRU6hvL5Ux8TV?start=26090950&end=26090951
response:
body:
string: C
headers:
Connection:
- close
Content-Length:
- '1'
Content-Type:
- text/plain; charset=utf-8
Date:
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -105,7 +176,7 @@ interactions:
Content-Type:
- application/json
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -135,7 +206,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -165,7 +236,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -195,7 +266,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -225,7 +296,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -255,7 +326,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -285,7 +356,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -315,7 +386,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -345,7 +416,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -386,7 +457,7 @@ interactions:
Content-Type:
- application/json
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -427,7 +498,7 @@ interactions:
Content-Type:
- application/json
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:45 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -457,7 +528,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -487,7 +558,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -517,7 +588,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -547,7 +618,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -577,7 +648,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -607,7 +678,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -637,7 +708,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -667,7 +738,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -697,7 +768,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down Expand Up @@ -727,7 +798,7 @@ interactions:
Content-Type:
- text/plain; charset=utf-8
Date:
- Thu, 24 Aug 2023 18:29:30 GMT
- Wed, 30 Aug 2023 13:26:46 GMT
Server:
- Werkzeug/2.2.2 Python/3.10.4
status:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_vrs_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@
"type": "Allele"
}


allele_dict_sequence_reference = {
"location": {
"end": 26090951,
"start": 26090950,
"sequence": {
"type": "SequenceReference",
"refgetAccession": "SQ.0iKlIQk2oZLoeOG9P1riRU6hvL5Ux8TV"
},
"type": "SequenceLocation"
},
"state": {
"sequence": "C",
"type": "LiteralSequenceExpression"
},
"type": "Allele"
}


allele_dict2 = {
"type": "Allele",
"location": {
Expand Down Expand Up @@ -104,6 +123,10 @@ def test_normalize_allele(rest_dataproxy):
allele2 = normalize(allele1, rest_dataproxy)
assert allele1 == allele2

allele1_seq_ref = models.Allele(**allele_dict_sequence_reference)
allele2_seq_ref = normalize(allele1_seq_ref, rest_dataproxy)
assert allele1_seq_ref == allele2_seq_ref

allele1 = models.Allele(**allele_dict2)
allele2 = normalize(allele1, rest_dataproxy, rle_seq_limit=0)
assert allele1 != allele2
Expand Down

0 comments on commit af267f0

Please sign in to comment.