Skip to content

Commit

Permalink
Add interface with EnzymeML
Browse files Browse the repository at this point in the history
  • Loading branch information
torogi94 committed Nov 2, 2023
1 parent 1fcefb7 commit bad0d70
Show file tree
Hide file tree
Showing 17 changed files with 2,692 additions and 114 deletions.
2,457 changes: 2,457 additions & 0 deletions nmrpy/data_objects-copy.py

Large diffs are not rendered by default.

56 changes: 52 additions & 4 deletions nmrpy/data_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pickle
from ipywidgets import SelectMultiple
from sdRDM import DataModel
from sdRDM.base.importedmodules import ImportedModules
from nmrpy.datamodel.core import *


Expand Down Expand Up @@ -247,12 +248,11 @@ def __init__(self, *args, **kwargs):
self._flags = {
"ft": False,
}
self.fid_object = FID(
self.fid_object = FIDObject(
raw_data=[(str(datum)) for datum in self.data],
processed_data=[],
nmr_parameters=Parameters(),
processing_steps=ProcessingSteps(),
peak_identities=[],
)

def __str__(self):
Expand Down Expand Up @@ -1416,9 +1416,11 @@ def assign_identities(self):
containing species defined in EnzymeML. When satisfied with
assignment, press Assign button to apply.
"""

raise NotImplementedError
widget_title = "Assign identities for {}".format(self.id)
self._assigner_widget = IdentityAssigner(fid=self, title=widget_title)
self._assigner_widget = IdentityAssigner(
fid=self, title=widget_title, available_species=[]
)

def clear_identities(self):
"""
Expand Down Expand Up @@ -1483,6 +1485,41 @@ def data_model(self):
del self.__data_model
print("The current data model has been deleted.")

@property
def enzymeml_document(self):
return self.__enzymeml_document

@enzymeml_document.setter
def enzymeml_document(self, enzymeml_document: DataModel):
if not isinstance(enzymeml_document, DataModel):
raise AttributeError(
f"Parameter `enzymeml_document` has to be of type `sdrdm.DataModel`, got {type(enzymeml_document)} instead."
)
self.__enzymeml_document = enzymeml_document
self.__enzymeml_document.modified = datetime.now()

@enzymeml_document.deleter
def enzymeml_document(self):
del self.__enzymeml_document
print("The current EnzymeML document has been deleted.")

@property
def enzymeml_library(self):
return self.__enzymeml_library

@enzymeml_library.setter
def enzymeml_library(self, enzymeml_library: ImportedModules):
if not isinstance(enzymeml_library, ImportedModules):
raise AttributeError(
f"Parameter `enzymeml_library` has to be of type `sdrdm.base.importedmodules.ImportedModules`, got {type(enzymeml_library)} instead."
)
self.__enzymeml_library = enzymeml_library

@enzymeml_library.deleter
def enzymeml_library(self):
del self.__enzymeml_library
print("The current EnzymeML library has been deleted.")

def __str__(self):
return "FidArray of {} FID(s)".format(len(self.data))

Expand Down Expand Up @@ -1647,6 +1684,17 @@ def add_fids(self, fids):
except AttributeError as e:
print(e)

def parse_enzymeml_document(self, path_to_enzymeml_document) -> None:
"""Parse an EnzymeML document and its library from specified
file path.
Args:
path_to_enzymeml_document (str): Path to file containing an EnzymeML document
"""
self.enzymeml_document, self.enzymeml_library = DataModel.parse(
path_to_enzymeml_document
)

@classmethod
def from_data(cls, data):
"""
Expand Down
5 changes: 2 additions & 3 deletions nmrpy/datamodel/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .nmrpy import NMRpy
from .experiment import Experiment
from .fid import FID
from .fidobject import FIDObject
from .parameters import Parameters
from .processingsteps import ProcessingSteps
from .identity import Identity
Expand All @@ -16,11 +16,10 @@
from .identifiertypes import IdentifierTypes

__doc__ = ""

__all__ = [
"NMRpy",
"Experiment",
"FID",
"FIDObject",
"Parameters",
"ProcessingSteps",
"Identity",
Expand Down
30 changes: 5 additions & 25 deletions nmrpy/datamodel/core/citation.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import sdRDM

from typing import List, Optional
from pydantic import Field
from typing import Any, List, Optional
from pydantic import AnyUrl, Field
from sdRDM.base.listplus import ListPlus
from sdRDM.base.utils import forge_signature, IDGenerator

from pydantic import AnyUrl
from typing import Any

from .term import Term
from .publicationtypes import PublicationTypes
from .person import Person
from .identifiertypes import IdentifierTypes
from .term import Term
from .publication import Publication
from .subjects import Subjects
from .publicationtypes import PublicationTypes
from .person import Person


@forge_signature
Expand Down Expand Up @@ -111,7 +107,6 @@ def add_to_authors(
identifier_type (): Recognized identifier for the person.. Defaults to None
identifier_value (): Value of the identifier for the person.. Defaults to None
"""

params = {
"last_name": last_name,
"first_name": first_name,
Expand All @@ -121,12 +116,9 @@ def add_to_authors(
"identifier_type": identifier_type,
"identifier_value": identifier_value,
}

if id is not None:
params["id"] = id

self.authors.append(Person(**params))

return self.authors[-1]

def add_to_keywords(
Expand All @@ -147,19 +139,15 @@ def add_to_keywords(
term_cv_reference (): Reference to the `CV.id` of a controlled vocabulary that has been defined for this dataset.. Defaults to None
value (): Value of the term, if applicable.. Defaults to None
"""

params = {
"name": name,
"accession": accession,
"term_cv_reference": term_cv_reference,
"value": value,
}

if id is not None:
params["id"] = id

self.keywords.append(Term(**params))

return self.keywords[-1]

def add_to_topics(
Expand All @@ -180,19 +168,15 @@ def add_to_topics(
term_cv_reference (): Reference to the `CV.id` of a controlled vocabulary that has been defined for this dataset.. Defaults to None
value (): Value of the term, if applicable.. Defaults to None
"""

params = {
"name": name,
"accession": accession,
"term_cv_reference": term_cv_reference,
"value": value,
}

if id is not None:
params["id"] = id

self.topics.append(Term(**params))

return self.topics[-1]

def add_to_related_publications(
Expand All @@ -215,18 +199,14 @@ def add_to_related_publications(
year (): Year of publication.. Defaults to None
doi (): The DOI pointing to the publication.. Defaults to None
"""

params = {
"type": type,
"title": title,
"authors": authors,
"year": year,
"doi": doi,
}

if id is not None:
params["id"] = id

self.related_publications.append(Publication(**params))

return self.related_publications[-1]
4 changes: 1 addition & 3 deletions nmrpy/datamodel/core/cv.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import sdRDM

from typing import Optional
from pydantic import Field
from pydantic import AnyUrl, Field
from sdRDM.base.utils import forge_signature, IDGenerator

from pydantic import AnyUrl


@forge_signature
class CV(sdRDM.DataModel):
Expand Down
18 changes: 6 additions & 12 deletions nmrpy/datamodel/core/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
from pydantic import Field
from sdRDM.base.listplus import ListPlus
from sdRDM.base.utils import forge_signature, IDGenerator


from .fidarray import FIDArray
from .fid import FID
from .parameters import Parameters
from .fidarray import FIDArray
from .processingsteps import ProcessingSteps
from .identity import Identity
from .fidobject import FIDObject


@forge_signature
Expand All @@ -36,7 +34,7 @@ class Experiment(sdRDM.DataModel):
multiple=True,
)

fid: List[FID] = Field(
fid: List[FIDObject] = Field(
description="A single NMR spectrum.",
default_factory=ListPlus,
multiple=True,
Expand All @@ -57,28 +55,24 @@ def add_to_fid(
id: Optional[str] = None,
) -> None:
"""
This method adds an object of type 'FID' to attribute fid
This method adds an object of type 'FIDObject' to attribute fid
Args:
id (str): Unique identifier of the 'FID' object. Defaults to 'None'.
id (str): Unique identifier of the 'FIDObject' object. Defaults to 'None'.
raw_data (): Complex spectral data from numpy array as string of format `{array.real}+{array.imag}j`.. Defaults to ListPlus()
processed_data (): Processed data array.. Defaults to ListPlus()
nmr_parameters (): Contains commonly-used NMR parameters.. Defaults to None
processing_steps (): Contains the processing steps performed, as well as the parameters used for them.. Defaults to None
peak_identities (): Container holding and mapping integrals resulting from peaks and their ranges to EnzymeML species.. Defaults to ListPlus()
"""

params = {
"raw_data": raw_data,
"processed_data": processed_data,
"nmr_parameters": nmr_parameters,
"processing_steps": processing_steps,
"peak_identities": peak_identities,
}

if id is not None:
params["id"] = id

self.fid.append(FID(**params))

self.fid.append(FIDObject(**params))
return self.fid[-1]
4 changes: 2 additions & 2 deletions nmrpy/datamodel/core/fidarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@forge_signature
class FIDArray(sdRDM.DataModel):
"""Container for processing of multiple spectra. Must reference the respective `FID` objects by `id`. {Add reference back. Setup time for experiment, Default 0.5}"""
"""Container for processing of multiple spectra. Must reference the respective `FIDObject` by `id`. {Add reference back. Setup time for experiment, Default 0.5}"""

id: Optional[str] = Field(
description="Unique identifier of the given object.",
Expand All @@ -17,7 +17,7 @@ class FIDArray(sdRDM.DataModel):
)

fids: List[str] = Field(
description="List of `FID.id` belonging to this array.",
description="List of `FIDObject.id` belonging to this array.",
multiple=True,
default_factory=ListPlus,
)
15 changes: 4 additions & 11 deletions nmrpy/datamodel/core/fid.py → nmrpy/datamodel/core/fidobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
from pydantic import Field
from sdRDM.base.listplus import ListPlus
from sdRDM.base.utils import forge_signature, IDGenerator

from pydantic.types import FrozenSet

from .parameters import Parameters
from .processingsteps import ProcessingSteps
from .identity import Identity
from .identity import Identity, AssociatedRanges


@forge_signature
class FID(sdRDM.DataModel):
class FIDObject(sdRDM.DataModel):
"""Container for a single NMR spectrum."""

id: Optional[str] = Field(
description="Unique identifier of the given object.",
default_factory=IDGenerator("fidINDEX"),
default_factory=IDGenerator("fidobjectINDEX"),
xml="@id",
)

Expand Down Expand Up @@ -64,7 +61,7 @@ def add_to_peak_identities(
name: Optional[str] = None,
species_id: Optional[str] = None,
associated_peaks: List[float] = ListPlus(),
associated_ranges: List[FrozenSet] = ListPlus(),
associated_ranges: List[AssociatedRanges] = ListPlus(),
associated_integrals: List[float] = ListPlus(),
id: Optional[str] = None,
) -> None:
Expand All @@ -79,18 +76,14 @@ def add_to_peak_identities(
associated_ranges (): Sets of ranges belonging to the given peaks. Defaults to ListPlus()
associated_integrals (): Integrals resulting from the given peaks and ranges of a species. Defaults to ListPlus()
"""

params = {
"name": name,
"species_id": species_id,
"associated_peaks": associated_peaks,
"associated_ranges": associated_ranges,
"associated_integrals": associated_integrals,
}

if id is not None:
params["id"] = id

self.peak_identities.append(Identity(**params))

return self.peak_identities[-1]
Loading

0 comments on commit bad0d70

Please sign in to comment.