From bf0ff6dbc4a1e340eb689ed72933a1d7cc4a3b8b Mon Sep 17 00:00:00 2001 From: haeussma <83341109+haeussma@users.noreply.github.com> Date: Wed, 26 Jun 2024 10:45:39 +0200 Subject: [PATCH 1/3] replaced content with newly generated model --- pyenzyme/model.py | 648 +++++++++++++++++++++++++++------------------- 1 file changed, 376 insertions(+), 272 deletions(-) diff --git a/pyenzyme/model.py b/pyenzyme/model.py index 0c35074..d784817 100644 --- a/pyenzyme/model.py +++ b/pyenzyme/model.py @@ -1,11 +1,13 @@ ## This is a generated file. Do not modify it manually! from __future__ import annotations -from pydantic import BaseModel, Field, ConfigDict -from typing import Optional, Generic, TypeVar + from enum import Enum +from typing import Generic, Optional, TypeVar from uuid import uuid4 +from pydantic import BaseModel, ConfigDict, Field + # Filter Wrapper definition used to filter a list of objects # based on their attributes Cls = TypeVar("Cls") @@ -85,35 +87,32 @@ class EnzymeMLDocument(BaseModel): complexes: list[Complex] = Field(default_factory=list) small_molecules: list[SmallMolecule] = Field(default_factory=list) reactions: list[Reaction] = Field(default_factory=list) + conditions: Optional[ReactionConditions] = Field(default=None) measurements: list[Measurement] = Field(default_factory=list) - equations: list[Equation] = Field(default_factory=list) + equations: list[ODE] = Field(default_factory=list) parameters: list[Parameter] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:EnzymeMLDocument/" + str(uuid4()), + default_factory=lambda: "md:EnzymeMLDocument/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:EnzymeMLDocument", + "md:EnzymeMLDocument", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "name": "schema:title", + "OBO": "http://purl.obolibrary.org/obo/", + "name": "schema:name", "references": { - "@id": "schema:citation", "@type": "@id", }, - "created": "schema:dateCreated", - "modified": "schema:dateModified", - "creators": "schema:creator", }, ) @@ -201,17 +200,17 @@ def filter_measurements(self, **kwargs) -> list[Measurement]: return FilterWrapper[Measurement](self.measurements, **kwargs).filter() - def filter_equations(self, **kwargs) -> list[Equation]: + def filter_equations(self, **kwargs) -> list[ODE]: """Filters the equations attribute based on the given kwargs Args: **kwargs: The attributes to filter by. Returns: - list[Equation]: The filtered list of Equation objects + list[ODE]: The filtered list of ODE objects """ - return FilterWrapper[Equation](self.equations, **kwargs).filter() + return FilterWrapper[ODE](self.equations, **kwargs).filter() def filter_parameters(self, **kwargs) -> list[Parameter]: """Filters the parameters attribute based on the given kwargs @@ -308,19 +307,19 @@ def add_to_creators( def add_to_vessels( self, - id: str, name: str, volume: float, unit: UnitDefinition, - constant: bool = True, + constant: bool, + creator_id: Optional[str] = None, **kwargs, ): params = { - "id": id, "name": name, "volume": volume, "unit": unit, "constant": constant, + "creator_id": creator_id, } if "id" in kwargs: @@ -332,10 +331,9 @@ def add_to_vessels( def add_to_proteins( self, - id: str, name: str, + sequence: str, constant: bool = False, - sequence: Optional[str] = None, vessel_id: Optional[str] = None, ecnumber: Optional[str] = None, organism: Optional[str] = None, @@ -344,10 +342,9 @@ def add_to_proteins( **kwargs, ): params = { - "id": id, "name": name, - "constant": constant, "sequence": sequence, + "constant": constant, "vessel_id": vessel_id, "ecnumber": ecnumber, "organism": organism, @@ -364,11 +361,10 @@ def add_to_proteins( def add_to_complexes( self, - id: str, participants: list[str] = [], **kwargs, ): - params = {"id": id, "participants": participants} + params = {"participants": participants} if "id" in kwargs: params["id"] = kwargs["id"] @@ -379,7 +375,6 @@ def add_to_complexes( def add_to_small_molecules( self, - id: str, name: str, constant: bool = False, vessel_id: Optional[str] = None, @@ -389,7 +384,6 @@ def add_to_small_molecules( **kwargs, ): params = { - "id": id, "name": name, "constant": constant, "vessel_id": vessel_id, @@ -407,19 +401,17 @@ def add_to_small_molecules( def add_to_reactions( self, - id: str, name: str, reversible: bool = False, - kinetic_law: Optional[Equation] = None, + rate_law: Optional[Equation] = None, species: list[ReactionElement] = [], modifiers: list[str] = [], **kwargs, ): params = { - "id": id, "name": name, "reversible": reversible, - "kinetic_law": kinetic_law, + "rate_law": rate_law, "species": species, "modifiers": modifiers, } @@ -433,24 +425,12 @@ def add_to_reactions( def add_to_measurements( self, - id: str, name: str, species: list[MeasurementData] = [], group_id: Optional[str] = None, - ph: Optional[float] = None, - temperature: Optional[float] = None, - temperature_unit: Optional[UnitDefinition] = None, **kwargs, ): - params = { - "id": id, - "name": name, - "species": species, - "group_id": group_id, - "ph": ph, - "temperature": temperature, - "temperature_unit": temperature_unit, - } + params = {"name": name, "species": species, "group_id": group_id} if "id" in kwargs: params["id"] = kwargs["id"] @@ -461,53 +441,40 @@ def add_to_measurements( def add_to_equations( self, - unit: UnitDefinition, - equation_type: EquationType, - equation: str, - species_id: Optional[str] = None, - variables: list[EqVariable] = [], - parameters: list[EqParameter] = [], + species_id: str, + equation: Equation, **kwargs, ): - params = { - "unit": unit, - "equation_type": equation_type, - "equation": equation, - "species_id": species_id, - "variables": variables, - "parameters": parameters, - } + params = {"species_id": species_id, "equation": equation} if "id" in kwargs: params["id"] = kwargs["id"] - self.equations.append(Equation(**params)) + self.equations.append(ODE(**params)) return self.equations[-1] def add_to_parameters( self, - id: str, name: str, - value: Optional[float] = None, - unit: Optional[UnitDefinition] = None, + value: float, + unit: UnitDefinition, + constant: bool, initial_value: Optional[float] = None, upper: Optional[float] = None, lower: Optional[float] = None, stderr: Optional[float] = None, - constant: Optional[bool] = True, **kwargs, ): params = { - "id": id, "name": name, "value": value, "unit": unit, + "constant": constant, "initial_value": initial_value, "upper": upper, "lower": lower, "stderr": stderr, - "constant": constant, } if "id" in kwargs: @@ -529,19 +496,18 @@ class Creator(BaseModel): # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", - default_factory=lambda: "enzml:Creator/" + str(uuid4()), + serialization_alias="@id", default_factory=lambda: "md:Creator/" + str(uuid4()) ) ld_type: list[str] = Field( serialization_alias="@type", - default_factory=lambda: ["enzml:Creator", "schema:person"], + default_factory=lambda: ["md:Creator", "schema:creator"], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", + "OBO": "http://purl.obolibrary.org/obo/", "given_name": "schema:givenName", "family_name": "schema:familyName", "mail": "schema:email", @@ -619,33 +585,32 @@ class Vessel(BaseModel): validate_assigment=True, ) # type: ignore - id: str name: str volume: float unit: UnitDefinition - constant: bool = True + constant: bool + creator_id: Optional[str] = Field(default=None) # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", - default_factory=lambda: "enzml:Vessel/" + str(uuid4()), + serialization_alias="@id", default_factory=lambda: "md:Vessel/" + str(uuid4()) ) ld_type: list[str] = Field( serialization_alias="@type", - default_factory=lambda: ["enzml:Vessel", "OBO:OBI_0400081"], + default_factory=lambda: ["md:Vessel", "OBO:OBI_0400081"], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "id": { + "OBO": "http://purl.obolibrary.org/obo/", + "name": "schema:name", + "volume": "OBO:OBI_0002139", + "creator_id": { "@id": "schema:identifier", "@type": "@id", }, - "name": "schema:name", - "volume": "OBO:OBI_0002139", }, ) @@ -720,10 +685,9 @@ class Protein(BaseModel): validate_assigment=True, ) # type: ignore - id: str name: str + sequence: str constant: bool = False - sequence: Optional[str] = Field(default=None) vessel_id: Optional[str] = Field(default=None) ecnumber: Optional[str] = Field(default=None) organism: Optional[str] = Field(default=None) @@ -732,22 +696,18 @@ class Protein(BaseModel): # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", - default_factory=lambda: "enzml:Protein/" + str(uuid4()), + serialization_alias="@id", default_factory=lambda: "md:Protein/" + str(uuid4()) ) ld_type: list[str] = Field( serialization_alias="@type", - default_factory=lambda: ["enzml:Protein", "schema:Protein"], + default_factory=lambda: ["md:Protein", "schema:Protein"], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "id": { - "@type": "@id", - }, + "OBO": "http://purl.obolibrary.org/obo/", "name": "schema:name", "sequence": "OBO:GSSO_007262", "vessel_id": { @@ -759,7 +719,6 @@ class Protein(BaseModel): "@type": "@id", }, "references": { - "@id": "schema:citation", "@type": "@id", }, }, @@ -836,30 +795,24 @@ class Complex(BaseModel): validate_assigment=True, ) # type: ignore - id: str participants: list[str] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", - default_factory=lambda: "enzml:Complex/" + str(uuid4()), + serialization_alias="@id", default_factory=lambda: "md:Complex/" + str(uuid4()) ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:Complex", + "md:Complex", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "id": { - "@id": "schema:identifier", - "@type": "@id", - }, + "OBO": "http://purl.obolibrary.org/obo/", "participants": { "@type": "@id", }, @@ -937,7 +890,6 @@ class SmallMolecule(BaseModel): validate_assigment=True, ) # type: ignore - id: str name: str constant: bool = False vessel_id: Optional[str] = Field(default=None) @@ -948,31 +900,26 @@ class SmallMolecule(BaseModel): # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:SmallMolecule/" + str(uuid4()), + default_factory=lambda: "md:SmallMolecule/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:SmallMolecule", + "md:SmallMolecule", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "id": { - "@id": "schema:identifier", - "@type": "@id", - }, + "OBO": "http://purl.obolibrary.org/obo/", "name": "schema:name", "vessel_id": { "@id": "schema:identifier", "@type": "@id", }, "references": { - "@id": "schema:citation", "@type": "@id", }, }, @@ -1049,34 +996,28 @@ class Reaction(BaseModel): validate_assigment=True, ) # type: ignore - id: str name: str reversible: bool = False - kinetic_law: Optional[Equation] = Field(default=None) + rate_law: Optional[Equation] = Field(default=None) species: list[ReactionElement] = Field(default_factory=list) modifiers: list[str] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", - default_factory=lambda: "enzml:Reaction/" + str(uuid4()), + serialization_alias="@id", default_factory=lambda: "md:Reaction/" + str(uuid4()) ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:Reaction", + "md:Reaction", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "id": { - "@id": "schema:identifier", - "@type": "@id", - }, + "OBO": "http://purl.obolibrary.org/obo/", "modifiers": { "@type": "@id", }, @@ -1163,7 +1104,7 @@ def add_type_term( def add_to_species( self, species_id: str, - stoichiometry: float, + stoichiometry: Optional[float] = None, **kwargs, ): params = {"species_id": species_id, "stoichiometry": stoichiometry} @@ -1182,25 +1123,25 @@ class ReactionElement(BaseModel): ) # type: ignore species_id: str - stoichiometry: float + stoichiometry: Optional[float] = Field(default=None) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:ReactionElement/" + str(uuid4()), + default_factory=lambda: "md:ReactionElement/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:ReactionElement", + "md:ReactionElement", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", + "OBO": "http://purl.obolibrary.org/obo/", "species_id": { "@type": "@id", }, @@ -1273,64 +1214,130 @@ def add_type_term( self.ld_type.append(term) -class Equation(BaseModel): +class ReactionConditions(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore - unit: UnitDefinition - equation_type: EquationType - equation: str - species_id: Optional[str] = Field(default=None) - variables: list[EqVariable] = Field(default_factory=list) - parameters: list[EqParameter] = Field(default_factory=list) + temperature: Optional[float] = Field(default=None) + temperature_unit: Optional[UnitDefinition] = Field(default=None) + ph: Optional[float] = Field(default=None) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:Equation/" + str(uuid4()), + default_factory=lambda: "md:ReactionConditions/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:Equation", + "md:ReactionConditions", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "species_id": { - "@type": "@id", - }, + "OBO": "http://purl.obolibrary.org/obo/", }, ) - def filter_variables(self, **kwargs) -> list[EqVariable]: - """Filters the variables attribute based on the given kwargs + def set_attr_term( + self, + attr: str, + term: str | dict, + prefix: str | None = None, + iri: str | None = None, + ): + """Sets the term for a given attribute in the JSON-LD object + + Example: + # Using an IRI term + >> obj.set_attr_term("name", "http://schema.org/givenName") + + # Using a prefix and term + >> obj.set_attr_term("name", "schema:givenName", "schema", "http://schema.org") + + # Usinng a dictionary term + >> obj.set_attr_term("name", {"@id": "http://schema.org/givenName", "@type": "@id"}) Args: - **kwargs: The attributes to filter by. + attr (str): The attribute to set the term for + term (str | dict): The term to set for the attribute - Returns: - list[EqVariable]: The filtered list of EqVariable objects + Raises: + AssertionError: If the attribute is not found in the model """ - return FilterWrapper[EqVariable](self.variables, **kwargs).filter() + assert ( + attr in self.model_fields + ), f"Attribute {attr} not found in {self.__class__.__name__}" - def filter_parameters(self, **kwargs) -> list[EqParameter]: - """Filters the parameters attribute based on the given kwargs + if prefix: + validate_prefix(term, prefix) + + add_namespace(self, prefix, iri) + self.ld_context[attr] = term + + def add_type_term( + self, term: str, prefix: str | None = None, iri: str | None = None + ): + """Adds a term to the @type field of the JSON-LD object + + Example: + # Using a term + >> obj.add_type_term("https://schema.org/Person") + + # Using a prefixed term + >> obj.add_type_term("schema:Person", "schema", "https://schema.org/Person") Args: - **kwargs: The attributes to filter by. + term (str): The term to add to the @type field + prefix (str, optional): The prefix to use for the term. Defaults to None. + iri (str, optional): The IRI to use for the term prefix. Defaults to None. - Returns: - list[EqParameter]: The filtered list of EqParameter objects + Raises: + ValueError: If prefix is provided but iri is not + ValueError: If iri is provided but prefix is not """ - return FilterWrapper[EqParameter](self.parameters, **kwargs).filter() + if prefix: + validate_prefix(term, prefix) + + add_namespace(self, prefix, iri) + self.ld_type.append(term) + + +class ODE(BaseModel): + model_config: ConfigDict = ConfigDict( # type: ignore + validate_assigment=True, + ) # type: ignore + + species_id: str + equation: Equation + + # JSON-LD fields + ld_id: str = Field( + serialization_alias="@id", default_factory=lambda: "md:ODE/" + str(uuid4()) + ) + ld_type: list[str] = Field( + serialization_alias="@type", + default_factory=lambda: [ + "md:ODE", + ], + ) + ld_context: dict[str, str | dict] = Field( + serialization_alias="@context", + default_factory=lambda: { + "md": "http://mdmodel.net/", + "schema": "https://schema.org/", + "OBO": "http://purl.obolibrary.org/obo/", + "species_id": { + "@type": "@id", + }, + }, + ) def set_attr_term( self, @@ -1397,76 +1404,38 @@ def add_type_term( add_namespace(self, prefix, iri) self.ld_type.append(term) - def add_to_variables( - self, - id: str, - name: str, - symbol: Optional[str] = None, - **kwargs, - ): - params = {"id": id, "name": name, "symbol": symbol} - - if "id" in kwargs: - params["id"] = kwargs["id"] - - self.variables.append(EqVariable(**params)) - - return self.variables[-1] - - def add_to_parameters( - self, - id: str, - name: str, - symbol: Optional[str] = None, - value: Optional[float] = None, - **kwargs, - ): - params = {"id": id, "name": name, "symbol": symbol, "value": value} - - if "id" in kwargs: - params["id"] = kwargs["id"] - - self.parameters.append(EqParameter(**params)) - - return self.parameters[-1] - class Parameter(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore - id: str name: str - value: Optional[float] = Field(default=None) - unit: Optional[UnitDefinition] = Field(default=None) + value: float + unit: UnitDefinition + constant: bool initial_value: Optional[float] = Field(default=None) upper: Optional[float] = Field(default=None) lower: Optional[float] = Field(default=None) stderr: Optional[float] = Field(default=None) - constant: bool = True # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:Parameter/" + str(uuid4()), + default_factory=lambda: "md:Parameter/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:Parameter", + "md:Parameter", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "id": { - "@id": "schema:identifier", - "@type": "@id", - }, + "OBO": "http://purl.obolibrary.org/obo/", }, ) @@ -1541,35 +1510,27 @@ class Measurement(BaseModel): validate_assigment=True, ) # type: ignore - id: str name: str species: list[MeasurementData] = Field(default_factory=list) group_id: Optional[str] = Field(default=None) - ph: Optional[float] = Field(default=None) - temperature: Optional[float] = Field(default=None) - temperature_unit: Optional[UnitDefinition] = Field(default=None) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:Measurement/" + str(uuid4()), + default_factory=lambda: "md:Measurement/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:Measurement", + "md:Measurement", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "id": { - "@id": "schema:identifier", - "@type": "@id", - }, + "OBO": "http://purl.obolibrary.org/obo/", "group_id": { "@type": "@id", }, @@ -1701,20 +1662,20 @@ class MeasurementData(BaseModel): # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:MeasurementData/" + str(uuid4()), + default_factory=lambda: "md:MeasurementData/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:MeasurementData", + "md:MeasurementData", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", + "OBO": "http://purl.obolibrary.org/obo/", "species_id": { "@type": "@id", }, @@ -1787,46 +1748,57 @@ def add_type_term( self.ld_type.append(term) -class UnitDefinition(BaseModel): +class Equation(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore - id: Optional[str] = Field(default=None) - name: Optional[str] = Field(default=None) - base_units: list[BaseUnit] = Field(default_factory=list) + equation: str + variables: list[EqVariable] = Field(default_factory=list) + parameters: list[EqParameter] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", - default_factory=lambda: "enzml:UnitDefinition/" + str(uuid4()), + serialization_alias="@id", default_factory=lambda: "md:Equation/" + str(uuid4()) ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:UnitDefinition", + "md:Equation", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", + "OBO": "http://purl.obolibrary.org/obo/", }, ) - def filter_base_units(self, **kwargs) -> list[BaseUnit]: - """Filters the base_units attribute based on the given kwargs + def filter_variables(self, **kwargs) -> list[EqVariable]: + """Filters the variables attribute based on the given kwargs Args: **kwargs: The attributes to filter by. Returns: - list[BaseUnit]: The filtered list of BaseUnit objects + list[EqVariable]: The filtered list of EqVariable objects """ - return FilterWrapper[BaseUnit](self.base_units, **kwargs).filter() + return FilterWrapper[EqVariable](self.variables, **kwargs).filter() + + def filter_parameters(self, **kwargs) -> list[EqParameter]: + """Filters the parameters attribute based on the given kwargs + + Args: + **kwargs: The attributes to filter by. + + Returns: + list[EqParameter]: The filtered list of EqParameter objects + """ + + return FilterWrapper[EqParameter](self.parameters, **kwargs).filter() def set_attr_term( self, @@ -1893,56 +1865,69 @@ def add_type_term( add_namespace(self, prefix, iri) self.ld_type.append(term) - def add_to_base_units( + def add_to_variables( self, - kind: UnitType, - exponent: int, - multiplier: Optional[float] = None, - scale: Optional[float] = None, + id: str, + name: str, + symbol: Optional[str] = None, **kwargs, ): - params = { - "kind": kind, - "exponent": exponent, - "multiplier": multiplier, - "scale": scale, - } + params = {"id": id, "name": name, "symbol": symbol} if "id" in kwargs: params["id"] = kwargs["id"] - self.base_units.append(BaseUnit(**params)) + self.variables.append(EqVariable(**params)) - return self.base_units[-1] + return self.variables[-1] + + def add_to_parameters( + self, + id: str, + name: str, + symbol: Optional[str] = None, + value: Optional[float] = None, + **kwargs, + ): + params = {"id": id, "name": name, "symbol": symbol, "value": value} + if "id" in kwargs: + params["id"] = kwargs["id"] -class BaseUnit(BaseModel): + self.parameters.append(EqParameter(**params)) + + return self.parameters[-1] + + +class EqVariable(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore - kind: UnitType - exponent: int - multiplier: Optional[float] = Field(default=None) - scale: Optional[float] = Field(default=None) + id: str + name: str + symbol: Optional[str] = Field(default=None) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:BaseUnit/" + str(uuid4()), + default_factory=lambda: "md:EqVariable/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:BaseUnit", + "md:EqVariable", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", + "OBO": "http://purl.obolibrary.org/obo/", + "id": { + "@type": "@id", + }, }, ) @@ -2012,7 +1997,7 @@ def add_type_term( self.ld_type.append(term) -class EqVariable(BaseModel): +class EqParameter(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore @@ -2020,24 +2005,25 @@ class EqVariable(BaseModel): id: str name: str symbol: Optional[str] = Field(default=None) + value: Optional[float] = Field(default=None) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:EqVariable/" + str(uuid4()), + default_factory=lambda: "md:EqParameter/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:EqVariable", + "md:EqParameter", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", - "OBO": "http://purl.obolibrary.org/obo/", + "md": "http://mdmodel.net/", "schema": "https://schema.org/", + "OBO": "http://purl.obolibrary.org/obo/", "id": { "@type": "@id", }, @@ -2110,36 +2096,161 @@ def add_type_term( self.ld_type.append(term) -class EqParameter(BaseModel): +class UnitDefinition(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore - id: str - name: str - symbol: Optional[str] = Field(default=None) - value: Optional[float] = Field(default=None) + id: Optional[str] = Field(default=None) + name: Optional[str] = Field(default=None) + base_units: list[BaseUnit] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "enzml:EqParameter/" + str(uuid4()), + default_factory=lambda: "md:UnitDefinition/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "enzml:EqParameter", + "md:UnitDefinition", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "enzml": "http://www.enzymeml.org/v2/", + "md": "http://mdmodel.net/", + "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", + }, + ) + + def filter_base_units(self, **kwargs) -> list[BaseUnit]: + """Filters the base_units attribute based on the given kwargs + + Args: + **kwargs: The attributes to filter by. + + Returns: + list[BaseUnit]: The filtered list of BaseUnit objects + """ + + return FilterWrapper[BaseUnit](self.base_units, **kwargs).filter() + + def set_attr_term( + self, + attr: str, + term: str | dict, + prefix: str | None = None, + iri: str | None = None, + ): + """Sets the term for a given attribute in the JSON-LD object + + Example: + # Using an IRI term + >> obj.set_attr_term("name", "http://schema.org/givenName") + + # Using a prefix and term + >> obj.set_attr_term("name", "schema:givenName", "schema", "http://schema.org") + + # Usinng a dictionary term + >> obj.set_attr_term("name", {"@id": "http://schema.org/givenName", "@type": "@id"}) + + Args: + attr (str): The attribute to set the term for + term (str | dict): The term to set for the attribute + + Raises: + AssertionError: If the attribute is not found in the model + """ + + assert ( + attr in self.model_fields + ), f"Attribute {attr} not found in {self.__class__.__name__}" + + if prefix: + validate_prefix(term, prefix) + + add_namespace(self, prefix, iri) + self.ld_context[attr] = term + + def add_type_term( + self, term: str, prefix: str | None = None, iri: str | None = None + ): + """Adds a term to the @type field of the JSON-LD object + + Example: + # Using a term + >> obj.add_type_term("https://schema.org/Person") + + # Using a prefixed term + >> obj.add_type_term("schema:Person", "schema", "https://schema.org/Person") + + Args: + term (str): The term to add to the @type field + prefix (str, optional): The prefix to use for the term. Defaults to None. + iri (str, optional): The IRI to use for the term prefix. Defaults to None. + + Raises: + ValueError: If prefix is provided but iri is not + ValueError: If iri is provided but prefix is not + """ + + if prefix: + validate_prefix(term, prefix) + + add_namespace(self, prefix, iri) + self.ld_type.append(term) + + def add_to_base_units( + self, + kind: UnitType, + exponent: int, + multiplier: Optional[float] = None, + scale: Optional[float] = None, + **kwargs, + ): + params = { + "kind": kind, + "exponent": exponent, + "multiplier": multiplier, + "scale": scale, + } + + if "id" in kwargs: + params["id"] = kwargs["id"] + + self.base_units.append(BaseUnit(**params)) + + return self.base_units[-1] + + +class BaseUnit(BaseModel): + model_config: ConfigDict = ConfigDict( # type: ignore + validate_assigment=True, + ) # type: ignore + + kind: UnitType + exponent: int + multiplier: Optional[float] = Field(default=None) + scale: Optional[float] = Field(default=None) + + # JSON-LD fields + ld_id: str = Field( + serialization_alias="@id", default_factory=lambda: "md:BaseUnit/" + str(uuid4()) + ) + ld_type: list[str] = Field( + serialization_alias="@type", + default_factory=lambda: [ + "md:BaseUnit", + ], + ) + ld_context: dict[str, str | dict] = Field( + serialization_alias="@context", + default_factory=lambda: { + "md": "http://mdmodel.net/", "schema": "https://schema.org/", - "id": { - "@type": "@id", - }, + "OBO": "http://purl.obolibrary.org/obo/", }, ) @@ -2218,13 +2329,6 @@ class DataTypes(Enum): PEAK_AREA = "peak-area" -class EquationType(Enum): - ASSIGNMENT = "assignment" - INITIAL_ASSIGNMENT = "initialAssignment" - ODE = "ode" - RATE_LAW = "rateLaw" - - class UnitType(Enum): AMPERE = "ampere" AVOGADRO = "avogadro" From cf1618c48001bd147150466b2f8d52e434d9eebd Mon Sep 17 00:00:00 2001 From: haeussma <83341109+haeussma@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:35:41 +0200 Subject: [PATCH 2/3] replaced mode, added celsius as unit --- pyenzyme/model.py | 613 +++++++++++++++-------------------- pyenzyme/units/predefined.py | 1 + 2 files changed, 257 insertions(+), 357 deletions(-) diff --git a/pyenzyme/model.py b/pyenzyme/model.py index d784817..23362aa 100644 --- a/pyenzyme/model.py +++ b/pyenzyme/model.py @@ -2,6 +2,7 @@ from __future__ import annotations +from datetime import date, datetime from enum import Enum from typing import Generic, Optional, TypeVar from uuid import uuid4 @@ -87,32 +88,35 @@ class EnzymeMLDocument(BaseModel): complexes: list[Complex] = Field(default_factory=list) small_molecules: list[SmallMolecule] = Field(default_factory=list) reactions: list[Reaction] = Field(default_factory=list) - conditions: Optional[ReactionConditions] = Field(default=None) measurements: list[Measurement] = Field(default_factory=list) - equations: list[ODE] = Field(default_factory=list) + equations: list[Equation] = Field(default_factory=list) parameters: list[Parameter] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:EnzymeMLDocument/" + str(uuid4()), + default_factory=lambda: "enzml:EnzymeMLDocument/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:EnzymeMLDocument", + "enzml:EnzymeMLDocument", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", - "name": "schema:name", + "name": "schema:title", "references": { + "@id": "schema:citation", "@type": "@id", }, + "created": "schema:dateCreated", + "modified": "schema:dateModified", + "creators": "schema:creator", }, ) @@ -200,17 +204,17 @@ def filter_measurements(self, **kwargs) -> list[Measurement]: return FilterWrapper[Measurement](self.measurements, **kwargs).filter() - def filter_equations(self, **kwargs) -> list[ODE]: + def filter_equations(self, **kwargs) -> list[Equation]: """Filters the equations attribute based on the given kwargs Args: **kwargs: The attributes to filter by. Returns: - list[ODE]: The filtered list of ODE objects + list[Equation]: The filtered list of Equation objects """ - return FilterWrapper[ODE](self.equations, **kwargs).filter() + return FilterWrapper[Equation](self.equations, **kwargs).filter() def filter_parameters(self, **kwargs) -> list[Parameter]: """Filters the parameters attribute based on the given kwargs @@ -307,19 +311,19 @@ def add_to_creators( def add_to_vessels( self, + id: str, name: str, volume: float, unit: UnitDefinition, - constant: bool, - creator_id: Optional[str] = None, + constant: bool = True, **kwargs, ): params = { + "id": id, "name": name, "volume": volume, "unit": unit, "constant": constant, - "creator_id": creator_id, } if "id" in kwargs: @@ -331,9 +335,10 @@ def add_to_vessels( def add_to_proteins( self, + id: str, name: str, - sequence: str, constant: bool = False, + sequence: Optional[str] = None, vessel_id: Optional[str] = None, ecnumber: Optional[str] = None, organism: Optional[str] = None, @@ -342,9 +347,10 @@ def add_to_proteins( **kwargs, ): params = { + "id": id, "name": name, - "sequence": sequence, "constant": constant, + "sequence": sequence, "vessel_id": vessel_id, "ecnumber": ecnumber, "organism": organism, @@ -361,10 +367,11 @@ def add_to_proteins( def add_to_complexes( self, + id: str, participants: list[str] = [], **kwargs, ): - params = {"participants": participants} + params = {"id": id, "participants": participants} if "id" in kwargs: params["id"] = kwargs["id"] @@ -375,6 +382,7 @@ def add_to_complexes( def add_to_small_molecules( self, + id: str, name: str, constant: bool = False, vessel_id: Optional[str] = None, @@ -384,6 +392,7 @@ def add_to_small_molecules( **kwargs, ): params = { + "id": id, "name": name, "constant": constant, "vessel_id": vessel_id, @@ -401,17 +410,19 @@ def add_to_small_molecules( def add_to_reactions( self, + id: str, name: str, reversible: bool = False, - rate_law: Optional[Equation] = None, + kinetic_law: Optional[Equation] = None, species: list[ReactionElement] = [], modifiers: list[str] = [], **kwargs, ): params = { + "id": id, "name": name, "reversible": reversible, - "rate_law": rate_law, + "kinetic_law": kinetic_law, "species": species, "modifiers": modifiers, } @@ -425,12 +436,24 @@ def add_to_reactions( def add_to_measurements( self, + id: str, name: str, species: list[MeasurementData] = [], group_id: Optional[str] = None, + ph: Optional[float] = None, + temperature: Optional[float] = None, + temperature_unit: Optional[UnitDefinition] = None, **kwargs, ): - params = {"name": name, "species": species, "group_id": group_id} + params = { + "id": id, + "name": name, + "species": species, + "group_id": group_id, + "ph": ph, + "temperature": temperature, + "temperature_unit": temperature_unit, + } if "id" in kwargs: params["id"] = kwargs["id"] @@ -441,40 +464,53 @@ def add_to_measurements( def add_to_equations( self, - species_id: str, - equation: Equation, + unit: UnitDefinition, + equation_type: EquationType, + equation: str, + species_id: Optional[str] = None, + variables: list[EqVariable] = [], + parameters: list[EqParameter] = [], **kwargs, ): - params = {"species_id": species_id, "equation": equation} + params = { + "unit": unit, + "equation_type": equation_type, + "equation": equation, + "species_id": species_id, + "variables": variables, + "parameters": parameters, + } if "id" in kwargs: params["id"] = kwargs["id"] - self.equations.append(ODE(**params)) + self.equations.append(Equation(**params)) return self.equations[-1] def add_to_parameters( self, + id: str, name: str, - value: float, - unit: UnitDefinition, - constant: bool, + value: Optional[float] = None, + unit: Optional[UnitDefinition] = None, initial_value: Optional[float] = None, upper: Optional[float] = None, lower: Optional[float] = None, stderr: Optional[float] = None, + constant: Optional[bool] = True, **kwargs, ): params = { + "id": id, "name": name, "value": value, "unit": unit, - "constant": constant, "initial_value": initial_value, "upper": upper, "lower": lower, "stderr": stderr, + "constant": constant, } if "id" in kwargs: @@ -496,16 +532,17 @@ class Creator(BaseModel): # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", default_factory=lambda: "md:Creator/" + str(uuid4()) + serialization_alias="@id", + default_factory=lambda: "enzml:Creator/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", - default_factory=lambda: ["md:Creator", "schema:creator"], + default_factory=lambda: ["enzml:Creator", "schema:person"], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", "given_name": "schema:givenName", @@ -585,32 +622,33 @@ class Vessel(BaseModel): validate_assigment=True, ) # type: ignore + id: str name: str volume: float unit: UnitDefinition - constant: bool - creator_id: Optional[str] = Field(default=None) + constant: bool = True # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", default_factory=lambda: "md:Vessel/" + str(uuid4()) + serialization_alias="@id", + default_factory=lambda: "enzml:Vessel/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", - default_factory=lambda: ["md:Vessel", "OBO:OBI_0400081"], + default_factory=lambda: ["enzml:Vessel", "OBO:OBI_0400081"], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", - "name": "schema:name", - "volume": "OBO:OBI_0002139", - "creator_id": { + "id": { "@id": "schema:identifier", "@type": "@id", }, + "name": "schema:name", + "volume": "OBO:OBI_0002139", }, ) @@ -685,9 +723,10 @@ class Protein(BaseModel): validate_assigment=True, ) # type: ignore + id: str name: str - sequence: str constant: bool = False + sequence: Optional[str] = Field(default=None) vessel_id: Optional[str] = Field(default=None) ecnumber: Optional[str] = Field(default=None) organism: Optional[str] = Field(default=None) @@ -696,18 +735,22 @@ class Protein(BaseModel): # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", default_factory=lambda: "md:Protein/" + str(uuid4()) + serialization_alias="@id", + default_factory=lambda: "enzml:Protein/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", - default_factory=lambda: ["md:Protein", "schema:Protein"], + default_factory=lambda: ["enzml:Protein", "schema:Protein"], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", + "id": { + "@type": "@id", + }, "name": "schema:name", "sequence": "OBO:GSSO_007262", "vessel_id": { @@ -719,6 +762,7 @@ class Protein(BaseModel): "@type": "@id", }, "references": { + "@id": "schema:citation", "@type": "@id", }, }, @@ -795,24 +839,30 @@ class Complex(BaseModel): validate_assigment=True, ) # type: ignore + id: str participants: list[str] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", default_factory=lambda: "md:Complex/" + str(uuid4()) + serialization_alias="@id", + default_factory=lambda: "enzml:Complex/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:Complex", + "enzml:Complex", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", + "id": { + "@id": "schema:identifier", + "@type": "@id", + }, "participants": { "@type": "@id", }, @@ -890,6 +940,7 @@ class SmallMolecule(BaseModel): validate_assigment=True, ) # type: ignore + id: str name: str constant: bool = False vessel_id: Optional[str] = Field(default=None) @@ -900,26 +951,31 @@ class SmallMolecule(BaseModel): # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:SmallMolecule/" + str(uuid4()), + default_factory=lambda: "enzml:SmallMolecule/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:SmallMolecule", + "enzml:SmallMolecule", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", + "id": { + "@id": "schema:identifier", + "@type": "@id", + }, "name": "schema:name", "vessel_id": { "@id": "schema:identifier", "@type": "@id", }, "references": { + "@id": "schema:citation", "@type": "@id", }, }, @@ -996,28 +1052,34 @@ class Reaction(BaseModel): validate_assigment=True, ) # type: ignore + id: str name: str reversible: bool = False - rate_law: Optional[Equation] = Field(default=None) + kinetic_law: Optional[Equation] = Field(default=None) species: list[ReactionElement] = Field(default_factory=list) modifiers: list[str] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", default_factory=lambda: "md:Reaction/" + str(uuid4()) + serialization_alias="@id", + default_factory=lambda: "enzml:Reaction/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:Reaction", + "enzml:Reaction", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", + "id": { + "@id": "schema:identifier", + "@type": "@id", + }, "modifiers": { "@type": "@id", }, @@ -1104,7 +1166,7 @@ def add_type_term( def add_to_species( self, species_id: str, - stoichiometry: Optional[float] = None, + stoichiometry: float, **kwargs, ): params = {"species_id": species_id, "stoichiometry": stoichiometry} @@ -1123,23 +1185,23 @@ class ReactionElement(BaseModel): ) # type: ignore species_id: str - stoichiometry: Optional[float] = Field(default=None) + stoichiometry: float # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:ReactionElement/" + str(uuid4()), + default_factory=lambda: "enzml:ReactionElement/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:ReactionElement", + "enzml:ReactionElement", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", "species_id": { @@ -1214,130 +1276,64 @@ def add_type_term( self.ld_type.append(term) -class ReactionConditions(BaseModel): +class Equation(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore - temperature: Optional[float] = Field(default=None) - temperature_unit: Optional[UnitDefinition] = Field(default=None) - ph: Optional[float] = Field(default=None) + unit: UnitDefinition + equation_type: EquationType + equation: str + species_id: Optional[str] = Field(default=None) + variables: list[EqVariable] = Field(default_factory=list) + parameters: list[EqParameter] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:ReactionConditions/" + str(uuid4()), + default_factory=lambda: "enzml:Equation/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:ReactionConditions", + "enzml:Equation", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", + "species_id": { + "@type": "@id", + }, }, ) - def set_attr_term( - self, - attr: str, - term: str | dict, - prefix: str | None = None, - iri: str | None = None, - ): - """Sets the term for a given attribute in the JSON-LD object - - Example: - # Using an IRI term - >> obj.set_attr_term("name", "http://schema.org/givenName") - - # Using a prefix and term - >> obj.set_attr_term("name", "schema:givenName", "schema", "http://schema.org") - - # Usinng a dictionary term - >> obj.set_attr_term("name", {"@id": "http://schema.org/givenName", "@type": "@id"}) + def filter_variables(self, **kwargs) -> list[EqVariable]: + """Filters the variables attribute based on the given kwargs Args: - attr (str): The attribute to set the term for - term (str | dict): The term to set for the attribute + **kwargs: The attributes to filter by. - Raises: - AssertionError: If the attribute is not found in the model + Returns: + list[EqVariable]: The filtered list of EqVariable objects """ - assert ( - attr in self.model_fields - ), f"Attribute {attr} not found in {self.__class__.__name__}" - - if prefix: - validate_prefix(term, prefix) - - add_namespace(self, prefix, iri) - self.ld_context[attr] = term - - def add_type_term( - self, term: str, prefix: str | None = None, iri: str | None = None - ): - """Adds a term to the @type field of the JSON-LD object - - Example: - # Using a term - >> obj.add_type_term("https://schema.org/Person") + return FilterWrapper[EqVariable](self.variables, **kwargs).filter() - # Using a prefixed term - >> obj.add_type_term("schema:Person", "schema", "https://schema.org/Person") + def filter_parameters(self, **kwargs) -> list[EqParameter]: + """Filters the parameters attribute based on the given kwargs Args: - term (str): The term to add to the @type field - prefix (str, optional): The prefix to use for the term. Defaults to None. - iri (str, optional): The IRI to use for the term prefix. Defaults to None. + **kwargs: The attributes to filter by. - Raises: - ValueError: If prefix is provided but iri is not - ValueError: If iri is provided but prefix is not + Returns: + list[EqParameter]: The filtered list of EqParameter objects """ - if prefix: - validate_prefix(term, prefix) - - add_namespace(self, prefix, iri) - self.ld_type.append(term) - - -class ODE(BaseModel): - model_config: ConfigDict = ConfigDict( # type: ignore - validate_assigment=True, - ) # type: ignore - - species_id: str - equation: Equation - - # JSON-LD fields - ld_id: str = Field( - serialization_alias="@id", default_factory=lambda: "md:ODE/" + str(uuid4()) - ) - ld_type: list[str] = Field( - serialization_alias="@type", - default_factory=lambda: [ - "md:ODE", - ], - ) - ld_context: dict[str, str | dict] = Field( - serialization_alias="@context", - default_factory=lambda: { - "md": "http://mdmodel.net/", - "schema": "https://schema.org/", - "OBO": "http://purl.obolibrary.org/obo/", - "species_id": { - "@type": "@id", - }, - }, - ) + return FilterWrapper[EqParameter](self.parameters, **kwargs).filter() def set_attr_term( self, @@ -1404,38 +1400,76 @@ def add_type_term( add_namespace(self, prefix, iri) self.ld_type.append(term) + def add_to_variables( + self, + id: str, + name: str, + symbol: Optional[str] = None, + **kwargs, + ): + params = {"id": id, "name": name, "symbol": symbol} + + if "id" in kwargs: + params["id"] = kwargs["id"] + + self.variables.append(EqVariable(**params)) + + return self.variables[-1] + + def add_to_parameters( + self, + id: str, + name: str, + symbol: Optional[str] = None, + value: Optional[float] = None, + **kwargs, + ): + params = {"id": id, "name": name, "symbol": symbol, "value": value} + + if "id" in kwargs: + params["id"] = kwargs["id"] + + self.parameters.append(EqParameter(**params)) + + return self.parameters[-1] + class Parameter(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore + id: str name: str - value: float - unit: UnitDefinition - constant: bool + value: Optional[float] = Field(default=None) + unit: Optional[UnitDefinition] = Field(default=None) initial_value: Optional[float] = Field(default=None) upper: Optional[float] = Field(default=None) lower: Optional[float] = Field(default=None) stderr: Optional[float] = Field(default=None) + constant: bool = True # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:Parameter/" + str(uuid4()), + default_factory=lambda: "enzml:Parameter/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:Parameter", + "enzml:Parameter", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", + "id": { + "@id": "schema:identifier", + "@type": "@id", + }, }, ) @@ -1510,27 +1544,35 @@ class Measurement(BaseModel): validate_assigment=True, ) # type: ignore + id: str name: str species: list[MeasurementData] = Field(default_factory=list) group_id: Optional[str] = Field(default=None) + ph: Optional[float] = Field(default=None) + temperature: Optional[float] = Field(default=None) + temperature_unit: Optional[UnitDefinition] = Field(default=None) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:Measurement/" + str(uuid4()), + default_factory=lambda: "enzml:Measurement/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:Measurement", + "enzml:Measurement", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", + "id": { + "@id": "schema:identifier", + "@type": "@id", + }, "group_id": { "@type": "@id", }, @@ -1662,18 +1704,18 @@ class MeasurementData(BaseModel): # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:MeasurementData/" + str(uuid4()), + default_factory=lambda: "enzml:MeasurementData/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:MeasurementData", + "enzml:MeasurementData", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", "species_id": { @@ -1748,57 +1790,46 @@ def add_type_term( self.ld_type.append(term) -class Equation(BaseModel): +class UnitDefinition(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore - equation: str - variables: list[EqVariable] = Field(default_factory=list) - parameters: list[EqParameter] = Field(default_factory=list) + id: Optional[str] = Field(default=None) + name: Optional[str] = Field(default=None) + base_units: list[BaseUnit] = Field(default_factory=list) # JSON-LD fields ld_id: str = Field( - serialization_alias="@id", default_factory=lambda: "md:Equation/" + str(uuid4()) + serialization_alias="@id", + default_factory=lambda: "enzml:UnitDefinition/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:Equation", + "enzml:UnitDefinition", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", }, ) - def filter_variables(self, **kwargs) -> list[EqVariable]: - """Filters the variables attribute based on the given kwargs - - Args: - **kwargs: The attributes to filter by. - - Returns: - list[EqVariable]: The filtered list of EqVariable objects - """ - - return FilterWrapper[EqVariable](self.variables, **kwargs).filter() - - def filter_parameters(self, **kwargs) -> list[EqParameter]: - """Filters the parameters attribute based on the given kwargs + def filter_base_units(self, **kwargs) -> list[BaseUnit]: + """Filters the base_units attribute based on the given kwargs Args: **kwargs: The attributes to filter by. Returns: - list[EqParameter]: The filtered list of EqParameter objects + list[BaseUnit]: The filtered list of BaseUnit objects """ - return FilterWrapper[EqParameter](self.parameters, **kwargs).filter() + return FilterWrapper[BaseUnit](self.base_units, **kwargs).filter() def set_attr_term( self, @@ -1865,69 +1896,56 @@ def add_type_term( add_namespace(self, prefix, iri) self.ld_type.append(term) - def add_to_variables( - self, - id: str, - name: str, - symbol: Optional[str] = None, - **kwargs, - ): - params = {"id": id, "name": name, "symbol": symbol} - - if "id" in kwargs: - params["id"] = kwargs["id"] - - self.variables.append(EqVariable(**params)) - - return self.variables[-1] - - def add_to_parameters( + def add_to_base_units( self, - id: str, - name: str, - symbol: Optional[str] = None, - value: Optional[float] = None, + kind: UnitType, + exponent: int, + multiplier: Optional[float] = None, + scale: Optional[float] = None, **kwargs, ): - params = {"id": id, "name": name, "symbol": symbol, "value": value} + params = { + "kind": kind, + "exponent": exponent, + "multiplier": multiplier, + "scale": scale, + } if "id" in kwargs: params["id"] = kwargs["id"] - self.parameters.append(EqParameter(**params)) + self.base_units.append(BaseUnit(**params)) - return self.parameters[-1] + return self.base_units[-1] -class EqVariable(BaseModel): +class BaseUnit(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore - id: str - name: str - symbol: Optional[str] = Field(default=None) + kind: UnitType + exponent: int + multiplier: Optional[float] = Field(default=None) + scale: Optional[float] = Field(default=None) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:EqVariable/" + str(uuid4()), + default_factory=lambda: "enzml:BaseUnit/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:EqVariable", + "enzml:BaseUnit", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", - "id": { - "@type": "@id", - }, }, ) @@ -1997,7 +2015,7 @@ def add_type_term( self.ld_type.append(term) -class EqParameter(BaseModel): +class EqVariable(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore @@ -2005,23 +2023,22 @@ class EqParameter(BaseModel): id: str name: str symbol: Optional[str] = Field(default=None) - value: Optional[float] = Field(default=None) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:EqParameter/" + str(uuid4()), + default_factory=lambda: "enzml:EqVariable/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:EqParameter", + "enzml:EqVariable", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", "id": { @@ -2096,161 +2113,36 @@ def add_type_term( self.ld_type.append(term) -class UnitDefinition(BaseModel): +class EqParameter(BaseModel): model_config: ConfigDict = ConfigDict( # type: ignore validate_assigment=True, ) # type: ignore - id: Optional[str] = Field(default=None) - name: Optional[str] = Field(default=None) - base_units: list[BaseUnit] = Field(default_factory=list) + id: str + name: str + symbol: Optional[str] = Field(default=None) + value: Optional[float] = Field(default=None) # JSON-LD fields ld_id: str = Field( serialization_alias="@id", - default_factory=lambda: "md:UnitDefinition/" + str(uuid4()), - ) - ld_type: list[str] = Field( - serialization_alias="@type", - default_factory=lambda: [ - "md:UnitDefinition", - ], - ) - ld_context: dict[str, str | dict] = Field( - serialization_alias="@context", - default_factory=lambda: { - "md": "http://mdmodel.net/", - "schema": "https://schema.org/", - "OBO": "http://purl.obolibrary.org/obo/", - }, - ) - - def filter_base_units(self, **kwargs) -> list[BaseUnit]: - """Filters the base_units attribute based on the given kwargs - - Args: - **kwargs: The attributes to filter by. - - Returns: - list[BaseUnit]: The filtered list of BaseUnit objects - """ - - return FilterWrapper[BaseUnit](self.base_units, **kwargs).filter() - - def set_attr_term( - self, - attr: str, - term: str | dict, - prefix: str | None = None, - iri: str | None = None, - ): - """Sets the term for a given attribute in the JSON-LD object - - Example: - # Using an IRI term - >> obj.set_attr_term("name", "http://schema.org/givenName") - - # Using a prefix and term - >> obj.set_attr_term("name", "schema:givenName", "schema", "http://schema.org") - - # Usinng a dictionary term - >> obj.set_attr_term("name", {"@id": "http://schema.org/givenName", "@type": "@id"}) - - Args: - attr (str): The attribute to set the term for - term (str | dict): The term to set for the attribute - - Raises: - AssertionError: If the attribute is not found in the model - """ - - assert ( - attr in self.model_fields - ), f"Attribute {attr} not found in {self.__class__.__name__}" - - if prefix: - validate_prefix(term, prefix) - - add_namespace(self, prefix, iri) - self.ld_context[attr] = term - - def add_type_term( - self, term: str, prefix: str | None = None, iri: str | None = None - ): - """Adds a term to the @type field of the JSON-LD object - - Example: - # Using a term - >> obj.add_type_term("https://schema.org/Person") - - # Using a prefixed term - >> obj.add_type_term("schema:Person", "schema", "https://schema.org/Person") - - Args: - term (str): The term to add to the @type field - prefix (str, optional): The prefix to use for the term. Defaults to None. - iri (str, optional): The IRI to use for the term prefix. Defaults to None. - - Raises: - ValueError: If prefix is provided but iri is not - ValueError: If iri is provided but prefix is not - """ - - if prefix: - validate_prefix(term, prefix) - - add_namespace(self, prefix, iri) - self.ld_type.append(term) - - def add_to_base_units( - self, - kind: UnitType, - exponent: int, - multiplier: Optional[float] = None, - scale: Optional[float] = None, - **kwargs, - ): - params = { - "kind": kind, - "exponent": exponent, - "multiplier": multiplier, - "scale": scale, - } - - if "id" in kwargs: - params["id"] = kwargs["id"] - - self.base_units.append(BaseUnit(**params)) - - return self.base_units[-1] - - -class BaseUnit(BaseModel): - model_config: ConfigDict = ConfigDict( # type: ignore - validate_assigment=True, - ) # type: ignore - - kind: UnitType - exponent: int - multiplier: Optional[float] = Field(default=None) - scale: Optional[float] = Field(default=None) - - # JSON-LD fields - ld_id: str = Field( - serialization_alias="@id", default_factory=lambda: "md:BaseUnit/" + str(uuid4()) + default_factory=lambda: "enzml:EqParameter/" + str(uuid4()), ) ld_type: list[str] = Field( serialization_alias="@type", default_factory=lambda: [ - "md:BaseUnit", + "enzml:EqParameter", ], ) ld_context: dict[str, str | dict] = Field( serialization_alias="@context", default_factory=lambda: { - "md": "http://mdmodel.net/", + "enzml": "http://www.enzymeml.org/v2/", "schema": "https://schema.org/", "OBO": "http://purl.obolibrary.org/obo/", + "id": { + "@type": "@id", + }, }, ) @@ -2329,6 +2221,13 @@ class DataTypes(Enum): PEAK_AREA = "peak-area" +class EquationType(Enum): + ASSIGNMENT = "assignment" + INITIAL_ASSIGNMENT = "initialAssignment" + ODE = "ode" + RATE_LAW = "rateLaw" + + class UnitType(Enum): AMPERE = "ampere" AVOGADRO = "avogadro" diff --git a/pyenzyme/units/predefined.py b/pyenzyme/units/predefined.py index 729de81..2591f3a 100644 --- a/pyenzyme/units/predefined.py +++ b/pyenzyme/units/predefined.py @@ -144,6 +144,7 @@ def dimensionless(): kelvin = UnitDefinition(base_units=[Unit.kelvin()])._get_name() K = UnitDefinition(base_units=[Unit.kelvin()])._get_name() celsius = UnitDefinition(base_units=[Unit.celsius()])._get_name() +C = UnitDefinition(base_units=[Unit.celsius()])._get_name() ## Ontology From b2bb4f1b552283b3a34297468c2a6264a2f74b48 Mon Sep 17 00:00:00 2001 From: Jan Range Date: Wed, 26 Jun 2024 12:56:54 +0200 Subject: [PATCH 3/3] remove unused import --- pyenzyme/model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyenzyme/model.py b/pyenzyme/model.py index 23362aa..b1205c8 100644 --- a/pyenzyme/model.py +++ b/pyenzyme/model.py @@ -2,7 +2,6 @@ from __future__ import annotations -from datetime import date, datetime from enum import Enum from typing import Generic, Optional, TypeVar from uuid import uuid4