Skip to content

Commit

Permalink
fix: ensure Entity objects return type as str (#434)
Browse files Browse the repository at this point in the history
close #433

* Add back `use_enum_values` config . I removed in #417 because of a
copy/paste I had done in the Adjacency class which left me confused.
* `type` field should be a literal string, not enum
  • Loading branch information
korikuzma authored Jul 18, 2024
1 parent f78fc6e commit 03425d3
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 77 deletions.
2 changes: 1 addition & 1 deletion notebooks/getting_started/3_Basic_Models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
30 changes: 15 additions & 15 deletions notebooks/getting_started/5_Exploring_the_CnvTranslator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,16 @@
"data": {
"text/plain": [
"{'id': 'ga4gh:CX.0M5VkV5v504_laQURFMEsqzZGcOF9YEw',\n",
" 'type': <VrsType.CN_CHANGE: 'CopyNumberChange'>,\n",
" 'type': 'CopyNumberChange',\n",
" 'digest': '0M5VkV5v504_laQURFMEsqzZGcOF9YEw',\n",
" 'location': {'id': 'ga4gh:SL.GSJAEJXFDz7Nq6VlJj5NTEku48MmteUU',\n",
" 'type': <VrsType.SEQ_LOC: 'SequenceLocation'>,\n",
" 'type': 'SequenceLocation',\n",
" 'digest': 'GSJAEJXFDz7Nq6VlJj5NTEku48MmteUU',\n",
" 'sequenceReference': {'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'sequenceReference': {'type': 'SequenceReference',\n",
" 'refgetAccession': 'SQ.eK4D2MosgK_ivBkgi6FVPg5UXs1bYESm'},\n",
" 'start': 45002866,\n",
" 'end': 45015056},\n",
" 'copyChange': <CopyChange.EFO_0030067: 'efo:0030067'>}"
" 'copyChange': 'efo:0030067'}"
]
},
"execution_count": 4,
Expand Down Expand Up @@ -225,16 +225,16 @@
"data": {
"text/plain": [
"{'id': 'ga4gh:CX.0BN4vrqPrLPAZYsQEAPnG4IS8AYeBGe1',\n",
" 'type': <VrsType.CN_CHANGE: 'CopyNumberChange'>,\n",
" 'type': 'CopyNumberChange',\n",
" 'digest': '0BN4vrqPrLPAZYsQEAPnG4IS8AYeBGe1',\n",
" 'location': {'id': 'ga4gh:SL.tydo6UFL8Y60L5Me3k8AJfljURO9vYn9',\n",
" 'type': <VrsType.SEQ_LOC: 'SequenceLocation'>,\n",
" 'type': 'SequenceLocation',\n",
" 'digest': 'tydo6UFL8Y60L5Me3k8AJfljURO9vYn9',\n",
" 'sequenceReference': {'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'sequenceReference': {'type': 'SequenceReference',\n",
" 'refgetAccession': 'SQ.KEO-4XBcm1cxeo_DIQ8_ofqGUkp4iZhI'},\n",
" 'start': 75502957,\n",
" 'end': 76045032},\n",
" 'copyChange': <CopyChange.EFO_0030070: 'efo:0030070'>}"
" 'copyChange': 'efo:0030070'}"
]
},
"execution_count": 5,
Expand Down Expand Up @@ -292,12 +292,12 @@
"data": {
"text/plain": [
"{'id': 'ga4gh:CN.O_QHImmfErh9jDFkJaypPPvUmnj7EM70',\n",
" 'type': <VrsType.CN_COUNT: 'CopyNumberCount'>,\n",
" 'type': 'CopyNumberCount',\n",
" 'digest': 'O_QHImmfErh9jDFkJaypPPvUmnj7EM70',\n",
" 'location': {'id': 'ga4gh:SL.hBVWalem_rNclxjmUuT9CHbEGCdlqW9L',\n",
" 'type': <VrsType.SEQ_LOC: 'SequenceLocation'>,\n",
" 'type': 'SequenceLocation',\n",
" 'digest': 'hBVWalem_rNclxjmUuT9CHbEGCdlqW9L',\n",
" 'sequenceReference': {'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'sequenceReference': {'type': 'SequenceReference',\n",
" 'refgetAccession': 'SQ.HxuclGHh0XCDuF8x6yQrpHUBL7ZntAHc'},\n",
" 'start': 85623,\n",
" 'end': 57073230},\n",
Expand Down Expand Up @@ -346,12 +346,12 @@
"data": {
"text/plain": [
"{'id': 'ga4gh:CN.WDzlT9oUq4IcQrVRWGH0dZnARnFBotCS',\n",
" 'type': <VrsType.CN_COUNT: 'CopyNumberCount'>,\n",
" 'type': 'CopyNumberCount',\n",
" 'digest': 'WDzlT9oUq4IcQrVRWGH0dZnARnFBotCS',\n",
" 'location': {'id': 'ga4gh:SL.H1Zh5xdBqamBjwVE9orWdY_uBkpEMH1V',\n",
" 'type': <VrsType.SEQ_LOC: 'SequenceLocation'>,\n",
" 'type': 'SequenceLocation',\n",
" 'digest': 'H1Zh5xdBqamBjwVE9orWdY_uBkpEMH1V',\n",
" 'sequenceReference': {'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'sequenceReference': {'type': 'SequenceReference',\n",
" 'refgetAccession': 'SQ.5ZUqxCmDDgN4xTRbaSjN8LwgZironmB8'},\n",
" 'start': 46111352,\n",
" 'end': 46119948},\n",
Expand Down Expand Up @@ -385,7 +385,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
46 changes: 22 additions & 24 deletions notebooks/getting_started/6_Upcoming_features.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,16 @@
"data": {
"text/plain": [
"{'id': 'ga4gh:VA.LK_4rOVxyEwrEpaOVd-BDFV0ocbO5vgV',\n",
" 'type': <VrsType.ALLELE: 'Allele'>,\n",
" 'type': 'Allele',\n",
" 'digest': 'LK_4rOVxyEwrEpaOVd-BDFV0ocbO5vgV',\n",
" 'location': {'id': 'ga4gh:SL.nA5-KovovkH-5p3LF1657nkkeWFwrInI',\n",
" 'type': <VrsType.SEQ_LOC: 'SequenceLocation'>,\n",
" 'type': 'SequenceLocation',\n",
" 'digest': 'nA5-KovovkH-5p3LF1657nkkeWFwrInI',\n",
" 'sequenceReference': {'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'sequenceReference': {'type': 'SequenceReference',\n",
" 'refgetAccession': 'SQ.aUiQCzCPZ2d0csHbMSbh2NzInhonSXwI'},\n",
" 'start': 80656509,\n",
" 'end': 80656510},\n",
" 'state': {'type': <VrsType.LIT_SEQ_EXPR: 'LiteralSequenceExpression'>,\n",
" 'sequence': 'TT'}}"
" 'state': {'type': 'LiteralSequenceExpression', 'sequence': 'TT'}}"
]
},
"execution_count": 4,
Expand Down Expand Up @@ -226,9 +225,9 @@
"data": {
"text/plain": [
"{'id': 'ga4gh:SL.nA5-KovovkH-5p3LF1657nkkeWFwrInI',\n",
" 'type': <VrsType.SEQ_LOC: 'SequenceLocation'>,\n",
" 'type': 'SequenceLocation',\n",
" 'digest': 'nA5-KovovkH-5p3LF1657nkkeWFwrInI',\n",
" 'sequenceReference': {'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'sequenceReference': {'type': 'SequenceReference',\n",
" 'refgetAccession': 'SQ.aUiQCzCPZ2d0csHbMSbh2NzInhonSXwI'},\n",
" 'start': 80656509,\n",
" 'end': 80656510}"
Expand Down Expand Up @@ -261,7 +260,7 @@
{
"data": {
"text/plain": [
"{'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
"{'type': 'SequenceReference',\n",
" 'refgetAccession': 'SQ.aUiQCzCPZ2d0csHbMSbh2NzInhonSXwI'}"
]
},
Expand Down Expand Up @@ -293,7 +292,7 @@
"data": {
"text/plain": [
"{'id': None,\n",
" 'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'type': 'SequenceReference',\n",
" 'label': None,\n",
" 'description': None,\n",
" 'alternativeLabels': None,\n",
Expand Down Expand Up @@ -399,9 +398,9 @@
"data": {
"text/plain": [
"{'id': 'refseq:NC_000005.10',\n",
" 'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'label': 'GRCh38:chr5',\n",
" 'alternativeLabels': ['GRCh38:5'],\n",
" 'type': 'SequenceReference',\n",
" 'label': 'GRCh38:5',\n",
" 'alternativeLabels': ['GRCh38:chr5'],\n",
" 'refgetAccession': 'SQ.aUiQCzCPZ2d0csHbMSbh2NzInhonSXwI'}"
]
},
Expand Down Expand Up @@ -435,12 +434,12 @@
"data": {
"text/plain": [
"{'id': 'ga4gh:SL.nA5-KovovkH-5p3LF1657nkkeWFwrInI',\n",
" 'type': <VrsType.SEQ_LOC: 'SequenceLocation'>,\n",
" 'type': 'SequenceLocation',\n",
" 'digest': 'nA5-KovovkH-5p3LF1657nkkeWFwrInI',\n",
" 'sequenceReference': {'id': 'refseq:NC_000005.10',\n",
" 'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'label': 'GRCh38:chr5',\n",
" 'alternativeLabels': ['GRCh38:5'],\n",
" 'type': 'SequenceReference',\n",
" 'label': 'GRCh38:5',\n",
" 'alternativeLabels': ['GRCh38:chr5'],\n",
" 'refgetAccession': 'SQ.aUiQCzCPZ2d0csHbMSbh2NzInhonSXwI'},\n",
" 'start': 80656509,\n",
" 'end': 80656510}"
Expand All @@ -465,20 +464,19 @@
"data": {
"text/plain": [
"{'id': 'ga4gh:VA.LK_4rOVxyEwrEpaOVd-BDFV0ocbO5vgV',\n",
" 'type': <VrsType.ALLELE: 'Allele'>,\n",
" 'type': 'Allele',\n",
" 'digest': 'LK_4rOVxyEwrEpaOVd-BDFV0ocbO5vgV',\n",
" 'location': {'id': 'ga4gh:SL.nA5-KovovkH-5p3LF1657nkkeWFwrInI',\n",
" 'type': <VrsType.SEQ_LOC: 'SequenceLocation'>,\n",
" 'type': 'SequenceLocation',\n",
" 'digest': 'nA5-KovovkH-5p3LF1657nkkeWFwrInI',\n",
" 'sequenceReference': {'id': 'refseq:NC_000005.10',\n",
" 'type': <VrsType.SEQ_REF: 'SequenceReference'>,\n",
" 'label': 'GRCh38:chr5',\n",
" 'alternativeLabels': ['GRCh38:5'],\n",
" 'type': 'SequenceReference',\n",
" 'label': 'GRCh38:5',\n",
" 'alternativeLabels': ['GRCh38:chr5'],\n",
" 'refgetAccession': 'SQ.aUiQCzCPZ2d0csHbMSbh2NzInhonSXwI'},\n",
" 'start': 80656509,\n",
" 'end': 80656510},\n",
" 'state': {'type': <VrsType.LIT_SEQ_EXPR: 'LiteralSequenceExpression'>,\n",
" 'sequence': 'TT'}}"
" 'state': {'type': 'LiteralSequenceExpression', 'sequence': 'TT'}}"
]
},
"execution_count": 11,
Expand Down Expand Up @@ -507,7 +505,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions src/ga4gh/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .pydantic import (
is_pydantic_instance, is_curie_type, pydantic_copy
)
from .domain_models import CommonDomainType
from . import entity_models, domain_models

__all__ = [
Expand All @@ -36,6 +37,7 @@
"is_pydantic_instance",
"is_curie_type",
"pydantic_copy",
"CommonDomainType",
"entity_models",
"domain_models"
]
Expand Down
34 changes: 17 additions & 17 deletions src/ga4gh/core/domain_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* `import ga4gh.core`, and refer to models using the fully-qualified
module name, e.g., `ga4gh.core.domain_models.Gene`
"""
from typing import Literal, Union, List
from enum import Enum
from typing import Literal, Union, List

from pydantic import Field, RootModel

Expand All @@ -33,8 +33,8 @@ class CommonDomainType(str, Enum):
class Phenotype(DomainEntity):
"""An observable characteristic or trait of an organism."""

type: Literal[CommonDomainType.PHENOTYPE] = Field(
CommonDomainType.PHENOTYPE,
type: Literal["Phenotype"] = Field(
CommonDomainType.PHENOTYPE.value,
description=f'MUST be "{CommonDomainType.PHENOTYPE.value}".'
)

Expand All @@ -44,17 +44,17 @@ class Disease(DomainEntity):
of all or part of an organism and is not immediately due to any external injury.
"""

type: Literal[CommonDomainType.DISEASE] = Field(
CommonDomainType.DISEASE,
type: Literal["Disease"] = Field(
CommonDomainType.DISEASE.value,
description=f'MUST be "{CommonDomainType.DISEASE.value}".'
)


class TraitSet(DomainEntity):
"""A set of phenotype and/or disease concepts that together constitute a condition."""

type: Literal[CommonDomainType.TRAIT_SET] = Field(
CommonDomainType.TRAIT_SET,
type: Literal["TraitSet"] = Field(
CommonDomainType.TRAIT_SET.value,
description=f'MUST be "{CommonDomainType.TRAIT_SET.value}".'
)
traits: List[Union[Disease, Phenotype]] = Field(
Expand All @@ -76,26 +76,26 @@ class Condition(RootModel):
class TherapeuticAction(DomainEntity):
"""A therapeutic action taken that is intended to alter or stop a pathologic process."""

type: Literal[CommonDomainType.TR_ACTION] = Field(
CommonDomainType.TR_ACTION,
type: Literal["TherapeuticAction"] = Field(
CommonDomainType.TR_ACTION.value,
description=f'MUST be "{CommonDomainType.TR_ACTION.value}".'
)


class TherapeuticAgent(DomainEntity):
"""An administered therapeutic agent that is intended to alter or stop a pathologic process."""

type: Literal[CommonDomainType.TR_AGENT] = Field(
CommonDomainType.TR_AGENT,
type: Literal["TherapeuticAgent"] = Field(
CommonDomainType.TR_AGENT.value,
description=f'MUST be "{CommonDomainType.TR_AGENT.value}".'
)


class TherapeuticSubstituteGroup(DomainEntity):
"""A group of therapeutic procedures that may be treated as substitutes for one another."""

type: Literal[CommonDomainType.TR_SUB] = Field(
CommonDomainType.TR_SUB,
type: Literal["TherapeuticSubstituteGroup"] = Field(
CommonDomainType.TR_SUB.value,
description=f'MUST be "{CommonDomainType.TR_SUB.value}".'
)
substitutes: List[Union[TherapeuticAction, TherapeuticAgent]] = Field(
Expand All @@ -110,8 +110,8 @@ class CombinationTherapy(DomainEntity):
performed in combination.
"""

type: Literal[CommonDomainType.TR_COMB] = Field(
CommonDomainType.TR_COMB,
type: Literal["CombinationTherapy"] = Field(
CommonDomainType.TR_COMB.value,
description=f'MUST be "{CommonDomainType.TR_COMB.value}".'
)
components: List[Union[TherapeuticSubstituteGroup, TherapeuticAction, TherapeuticAgent]] = Field(
Expand All @@ -136,7 +136,7 @@ class TherapeuticProcedure(RootModel):
class Gene(DomainEntity):
"""A basic physical and functional unit of heredity."""

type: Literal[CommonDomainType.GENE] = Field(
CommonDomainType.GENE,
type: Literal["Gene"] = Field(
CommonDomainType.GENE.value,
description=f'MUST be "{CommonDomainType.GENE.value}".'
)
6 changes: 5 additions & 1 deletion src/ga4gh/core/entity_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Any, Dict, Annotated, Optional, Union, List
from enum import Enum

from pydantic import BaseModel, Field, RootModel, StringConstraints, model_serializer
from pydantic import BaseModel, Field, RootModel, StringConstraints, model_serializer, ConfigDict

from ga4gh.core import GA4GH_IR_REGEXP

Expand Down Expand Up @@ -119,6 +119,8 @@ class Coding(BaseModel):
class ConceptMapping(BaseModel):
"""A mapping to a concept in a terminology or code system."""

model_config = ConfigDict(use_enum_values=True)

coding: Coding = Field(..., description="A structured representation of a code for a defined concept in a terminology or code system.")
relation: Relation = Field(..., description="A mapping relation between concepts as defined by the Simple Knowledge Organization System (SKOS).")

Expand All @@ -144,6 +146,8 @@ class Expression(BaseModel):
variation include the HGVS and ISCN nomenclatures.
"""

model_config = ConfigDict(use_enum_values=True)

syntax: Syntax = Field(..., description="The syntax used to describe the variation. The value should be one of the supported syntaxes.")
value: str = Field(..., description="The expression of the variation in the specified syntax. The value should be a valid expression in the specified syntax.")
syntax_version: Optional[str] = Field(None, description="The version of the syntax used to describe the variation. This is particularly important for HGVS expressions, as the syntax has evolved over time.")
Expand Down
2 changes: 2 additions & 0 deletions src/ga4gh/vrs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

from .normalize import normalize
from .enderef import vrs_deref, vrs_enref
from .models import VrsType
from . import models

__all__ = [
"normalize",
"vrs_deref",
"vrs_enref",
"VrsType",
"models"
]

Expand Down
Loading

0 comments on commit 03425d3

Please sign in to comment.