Skip to content

Commit

Permalink
Merge pull request #229 from ga4gh/range_serialization
Browse files Browse the repository at this point in the history
Minor changes to how direct and indirect ranges are serialized
  • Loading branch information
toneillbroad authored Aug 16, 2023
2 parents 0879eab + 80a5b67 commit db1486c
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/ga4gh/core/_internal/identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
is_identifiable,
getattr_in,
get_pydantic_root,
is_pydantic_custom_str_type
is_pydantic_custom_type
)

__all__ = "ga4gh_digest ga4gh_identify ga4gh_serialize is_ga4gh_identifier parse_ga4gh_identifier".split()
Expand Down Expand Up @@ -197,9 +197,9 @@ def identify_all(
if input_obj is None:
return None
output_obj = input_obj
if is_pydantic_custom_str_type(input_obj):
if is_pydantic_custom_type(input_obj):
val = export_pydantic_model(input_obj)
if is_curie_type(val) and is_ga4gh_identifier(val):
if isinstance(val, str) and is_curie_type(val) and is_ga4gh_identifier(val):
val = parse_ga4gh_identifier(val)["digest"]
output_obj = val
elif is_pydantic_instance(input_obj):
Expand Down
3 changes: 3 additions & 0 deletions src/ga4gh/core/_internal/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def get_pydantic_root(obj: Union[Any, RootModel]) -> Any:
return obj


def is_pydantic_custom_type(obj: RootModel) -> bool:
return isinstance(obj, RootModel)

def is_pydantic_custom_str_type(obj: RootModel) -> bool:
return isinstance(obj, RootModel) and isinstance(obj.root, str)

Expand Down
4 changes: 3 additions & 1 deletion src/ga4gh/vrs/_internal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class IRI(RootModel):
class SequenceReference(BaseModel):
model_config = ConfigDict(
extra='allow',
use_enum_values=True
)
id: Optional[str] = Field(
None,
Expand Down Expand Up @@ -295,7 +296,6 @@ class ReferenceLengthExpression(BaseModel):




class LiteralSequenceExpression(BaseModel):
model_config = ConfigDict(
extra='allow',
Expand All @@ -315,6 +315,7 @@ class LiteralSequenceExpression(BaseModel):
class Mapping(BaseModel):
model_config = ConfigDict(
extra='allow',
use_enum_values=True
)
id: Optional[str] = Field(
None,
Expand Down Expand Up @@ -481,6 +482,7 @@ class ga4gh:
class CopyNumberChange(BaseModel):
model_config = ConfigDict(
extra='allow',
use_enum_values=True
)
id: Optional[str] = Field(
None,
Expand Down
10 changes: 5 additions & 5 deletions src/ga4gh/vrs/extras/variation_normalizer_rest_dp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def to_hgvs(self, vo, namespace="refseq"):
Use this method if you don't have UTA installed locally or are unable
to reach the public UTA database due to port issues.
"""
vo = vo.model_dump()
vo = vo.model_dump(exclude_none=True)
data = dict(
variation = vo,
namespace = namespace
variation=vo,
namespace=namespace
)
r = requests.post(
url = f"{self.api}/vrs_allele_to_hgvs",
url=f"{self.api}/vrs_allele_to_hgvs",
json=data,
headers={ "Content-Type": "application/json; charset=utf-8" }
headers={"Content-Type": "application/json; charset=utf-8"}
)
if r.status_code == 200:
return r.json().get("variations", [])
Expand Down
7 changes: 2 additions & 5 deletions src/ga4gh/vrs/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
def _normalize_allele(allele, data_proxy):
sequence = SequenceProxy(data_proxy, allele.location.sequence)

ival = (allele.location.start.value, allele.location.end)
ival = (allele.location.start, allele.location.end)

_allele_state = allele.state.type
_states_with_sequence = ["SequenceState", "LiteralSequenceExpression"]
_states_with_sequence = ["ReferenceLengthExpression", "LiteralSequenceExpression"]
if _allele_state in _states_with_sequence:
alleles = (None, allele.state.sequence)
elif _allele_state == "RepeatedSequenceExpression" and \
allele.state.seq_expr.type in _states_with_sequence:
alleles = (None, allele.state.seq_expr.sequence)
else:
alleles = (None, "")

Expand Down
2 changes: 1 addition & 1 deletion tests/validation/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ def flatten_tests(vts):

@pytest.mark.parametrize("cls,data,fn,exp", flatten_tests(validation_tests))
def test_validation(cls, data, fn, exp):
o = models[cls](**data)
o = getattr(models, cls)(**data)
fx = fxs[fn]
assert exp == fx(o)

0 comments on commit db1486c

Please sign in to comment.