コード例 #1
0
ファイル: test_model.py プロジェクト: hongzhonglu/GEMEditor
    def test_map_metabolites(self, database):
        metabolite = Metabolite("m1")
        metabolite.annotation.add(
            Annotation(identifier="MNXM2", collection="metanetx.chemical"))
        metabolite.annotation.add(
            Annotation(identifier="MNXM215", collection="metanetx.chemical"))

        assert map_by_annotation(database, metabolite) == set([1, 2])
コード例 #2
0
    def test_annotation_property(self):
        reference = Reference()
        reference.pmc = "1234"
        reference.pmid = "54321"
        reference.doi = "5555"

        assert Annotation("pubmed", reference.pmid) in reference.annotation
        assert Annotation("pmc", reference.pmc) in reference.annotation
        assert Annotation("doi", reference.doi) in reference.annotation
コード例 #3
0
 def annotation(self):
     result = set()
     if self.pmid:
         result.add(Annotation("pubmed", self.pmid))
     if self.pmc:
         result.add(Annotation("pmc", self.pmc))
     if self.doi:
         result.add(Annotation("doi", self.doi))
     return result
コード例 #4
0
class TestAnnotation:
    @pytest.fixture(autouse=True)
    def init_objects(self):
        self.annotation1 = Annotation("test_collection", "test_id")
        self.annotation2 = Annotation("test_collection", "test_id")
        self.empty_annotation = Annotation()

    def test_init_(self):
        """ Test that collection and identifier is initialized to None """
        assert self.empty_annotation.collection is None
        assert self.empty_annotation.identifier is None
        assert self.empty_annotation.type == "is"
        assert self.annotation1.collection == "test_collection"
        assert self.annotation1.identifier == "test_id"
        assert self.annotation1.type == "is"

    def test__hash_(self):
        """ Test that annotations containing the same information
        return the same value by __hash__()"""

        assert self.annotation1 is not self.annotation2
        assert self.annotation1.__hash__() == self.annotation2.__hash__()
        assert self.annotation1.__hash__() != self.empty_annotation.__hash__()

    def test_annotation_eq1(self):
        """ Test equality of annotations """
        assert self.annotation1 == self.annotation2
        assert self.annotation1 is not self.annotation2

    def test_annotation_eq2(self):
        """ Test not equality if type is different """

        assert Annotation("a", "a", "is") != Annotation("a", "a", "has")
        assert Annotation("a", "a", "is").__hash__() != Annotation(
            "a", "a", "has").__hash__()

    def test_annotation_ex2(self):
        """ Test class is checked for in annotation equality """
        class Mock:
            def __init__(self, collection=None, identifier=None, type="is"):
                self.collection = collection
                self.identifier = identifier
                self.type = type

        mock = Mock("test_collection", "test_id")
        assert self.annotation1 != mock
        assert self.annotation1.collection == mock.collection
        assert self.annotation1.identifier == mock.identifier

    def test_prevent_setting_of_variables(self):
        """ Test that collection and identifier cannot be set
         in order to assure hash stability """
        with pytest.raises(AttributeError):
            self.annotation1.collection = "test"
        with pytest.raises(AttributeError):
            self.annotation1.identifier = "bla"
コード例 #5
0
ファイル: test_base.py プロジェクト: hongzhonglu/GEMEditor
    def test_get_annotations_from_id(self, database):
        result = database.get_annotations_from_id(1, "metabolite")
        assert result == [Annotation(identifier="MNXM2", collection="metanetx.chemical")]

        result = database.get_annotations_from_id(1, "metabOlite")
        assert result == [Annotation(identifier="MNXM2", collection="metanetx.chemical")]

        result = database.get_annotations_from_id(1, "reaction")
        assert result == [Annotation(identifier="MNXR14892", collection="metanetx.reaction")]

        result = database.get_annotations_from_id(1, "reaCtion")
        assert result == [Annotation(identifier="MNXR14892", collection="metanetx.reaction")]

        with pytest.raises(ValueError):
            database.get_annotations_from_id(1, "pathway")
コード例 #6
0
    def test_parsing_of_valid_metabolite(self):
        parent_node = ET.fromstring(valid_metabolite_with_annotation)
        metabolites = parse_metabolites(parent_node)
        assert len(metabolites) == 1

        metabolite = metabolites[0]
        assert metabolite.id == "13GLUCAN"
        assert metabolite.name == "1,3-beta-D-Glucan"
        assert metabolite.compartment == "c"
        assert metabolite.formula == "C18H32O16"
        assert metabolite.charge == 0
        assert len(metabolite.annotation) == 3

        assert Annotation("inchi", "InChI=1S/C18H32O16/c19-1-4-7(22)10(25)11(26)17(31-4)34-15-9(24)6(3-21)32-18(13(15)28)33-14-8(23)5(2-20)30-16(29)12(14)27/h4-29H,1-3H2/t4-,5-,6-,7-,8-,9-,10+,11-,12-,13-,14+,15+,16-,17+,18+/m1/s1") in metabolite.annotation
        assert Annotation("chebi", "CHEBI:37671") in metabolite.annotation
        assert Annotation("kegg.compound", "C00965") in metabolite.annotation
コード例 #7
0
ファイル: test_base.py プロジェクト: hongzhonglu/GEMEditor
    def test_get_metabolite_from_id(self, database):
        result = database.get_metabolite_from_id(1)
        assert result.formula == "H2O"
        assert result.name == "Water"
        assert Annotation(identifier="MNXM2", collection="metanetx.chemical") in result.annotation

        result = database.get_metabolite_from_id(50)
        assert result is None
コード例 #8
0
ファイル: test_tables.py プロジェクト: hongzhonglu/GEMEditor
 def setup_items(self):
     self.table = AnnotationTable()
     self.collection = "test collection"
     self.identifier = "test identifier"
     self.type = "is"
     self.annotation = Annotation(collection=self.collection,
                                  identifier=self.identifier,
                                  type=self.type)
コード例 #9
0
    def test_annotations(self):
        # Setup metabolites
        met1 = Metabolite("met1")
        annotation1 = Annotation(collection="chebi", identifier="test1")
        annotation2 = Annotation(collection="chebi", identifier="test2")
        met1.annotation.update((annotation1, annotation2))

        met2 = Metabolite("met2")
        annotation3 = Annotation(collection="chebi", identifier="test3")
        met2.annotation.add(annotation3)

        # Run
        mapping = get_annotation_to_item_map([met1, met2])

        assert mapping[annotation1] == [met1]
        assert mapping[annotation2] == [met1]
        assert mapping[annotation3] == [met2]
コード例 #10
0
ファイル: test_base.py プロジェクト: hongzhonglu/GEMEditor
    def setup_items(self):
        self.parent = QWidget()
        self.widget = AnnotationDisplayWidget(self.parent)
        self.empty_reaction = Reaction()

        self.annotated_reaction = Reaction()
        self.annotation = Annotation("chebi", "CHEBI:123454")
        self.annotated_reaction.annotation.add(self.annotation)
        self.model = Model()
コード例 #11
0
ファイル: test_base.py プロジェクト: hongzhonglu/GEMEditor
    def test_save_state(self):
        self.widget.set_item(self.annotated_reaction, self.model)
        new_annotation = Annotation("kegg", "C91233")

        self.widget.dataTable.update_row_from_item(new_annotation)
        assert self.widget.dataTable.rowCount() == 2
        self.widget.save_state()
        assert self.annotated_reaction.annotation == set(
            [self.annotation, new_annotation])
コード例 #12
0
ファイル: test_model.py プロジェクト: hongzhonglu/GEMEditor
    def test_map_metabolite_by_annotation(self, database, progress):
        model = Model()
        met1 = Metabolite("m1")
        model.add_metabolites([met1])
        met1.annotation.add(
            Annotation(identifier="MNXM2", collection="metanetx.chemical"))

        update_metabolite_database_mapping(database, model, progress)
        assert model.database_mapping[met1] == 1
コード例 #13
0
ファイル: annotation.py プロジェクト: hongzhonglu/GEMEditor
def parse_miriam_string(miriam_link, type):
    """ Parse the miriam annotation contained in node """

    split_url = urlsplit(miriam_link)
    # Remove leading slash and split path in collection and identifier
    collection, identifier = split_url.path.lstrip("/").split("/", 1)
    if split_url.netloc != "identifiers.org":
        raise ValueError("{0} does not start with http://identifiers.org/".format(miriam_link))
    else:
        return Annotation(collection=collection, identifier=identifier, type=type)
コード例 #14
0
ファイル: test_base.py プロジェクト: hongzhonglu/GEMEditor
    def test_add_annotation(self):
        receiver = MockSlot()

        self.widget.set_item(self.annotated_reaction, self.model)
        self.widget.changed.connect(receiver.slot)
        assert receiver.called is False
        assert receiver.last_caller is None

        new_annotation = Annotation("kegg", "C91233")

        self.widget.dataTable.update_row_from_item(new_annotation)
        assert receiver.called is True
        assert receiver.last_caller is self.widget
コード例 #15
0
    def test_add_references(self):
        root = Element("root")
        add_references(root, self.model)

        references_list_node = root.find(ge_listOfReferences)
        assert references_list_node is not None
        assert len(references_list_node) == 1

        reference_node = references_list_node.find(ge_reference)
        assert reference_node is not None
        assert len(reference_node) == 2

        author_list_node = reference_node.find(ge_listOfAuthors)
        assert author_list_node is not None
        assert len(author_list_node) == 2

        first_author_node = author_list_node.find(ge_author)
        assert first_author_node is not None
        assert first_author_node.get("firstname") == self.first_author.firstname
        assert first_author_node.get("lastname") == self.first_author.lastname
        assert first_author_node.get("initials") == self.first_author.initials

        second_author_node = first_author_node.getnext()
        assert second_author_node is not None
        assert second_author_node.get("firstname") == self.second_author.firstname
        assert second_author_node.get("lastname") == self.second_author.lastname
        assert second_author_node.get("initials") == self.second_author.initials

        annotation = annotate_element_from_xml(reference_node)
        assert len(annotation) == 3
        assert Annotation("pubmed", self.test_pmid) in annotation
        assert Annotation("pmc", self.test_pmc) in annotation
        assert Annotation("doi", self.test_doi) in annotation

        assert reference_node.attrib == {"url": self.test_url,
                                         "year": self.test_year,
                                         "title": self.test_title,
                                         "journal": self.test_journal,
                                         "id": self.test_ref_id}
コード例 #16
0
ファイル: test_model.py プロジェクト: hongzhonglu/GEMEditor
    def test_map_reaction_by_annotation(self, database, progress):
        model = Model()
        met1 = Metabolite("m1")
        met2 = Metabolite("m2")
        model.add_metabolites([met1, met2])

        reaction = Reaction("r1")
        reaction.add_metabolites({met1: -1, met2: 1})
        reaction.annotation.add(
            Annotation(identifier="MNXR14892", collection="metanetx.reaction"))
        model.add_reactions([reaction])

        update_reaction_database_mapping(database, model, progress)

        assert model.database_mapping[reaction] == 1
コード例 #17
0
    def setup_items(self):

        self.test_id = "test_id"
        self.test_name = "test_name"
        self.test_formula = "H2O"
        self.test_charge = 0
        self.test_compartment = "t"
        self.metabolite = Metabolite(id=self.test_id,
                                     name=self.test_name,
                                     formula=self.test_formula,
                                     charge=self.test_charge,
                                     compartment=self.test_compartment)

        self.annotation1 = Annotation("chebi", "CHEBI:37671")
        self.metabolite.annotation.add(self.annotation1)

        self.model = Model("Test")
        self.model.add_metabolites([self.metabolite])
        self.root = Element("root")
コード例 #18
0
    def get_annotations_from_id(self, identifier, entry_type, get_all=False):
        """ Get all annotations from a database identifier

        Parameters
        ----------
        identifier: str or int
        entry_type: str, "Metabolite" or "Reaction"
        get_all: bool, Get all annotations irrespective if active or not

        Returns
        -------
        list of Annotation objects
        """

        # Run query depending on the specified type
        if entry_type.lower() == "metabolite":
            if get_all:
                self.cursor.execute(query_all_annotation_from_metabolite_id,
                                    (str(identifier), ))
            else:
                self.cursor.execute(query_annotation_from_metabolite_id,
                                    (str(identifier), ))
        elif entry_type.lower() == "reaction":
            if get_all:
                self.cursor.execute(query_all_annotation_from_reaction_id,
                                    (str(identifier), ))
            else:
                self.cursor.execute(query_annotation_from_reaction_id,
                                    (str(identifier), ))
        else:
            raise ValueError(
                "Unexpected entry_type: '{0!s}'".format(entry_type))

        # Return annotations
        annotations = [
            Annotation(collection, identifier)
            for collection, identifier in self.cursor.fetchall()
        ]
        return annotations
コード例 #19
0
ファイル: test_base.py プロジェクト: hongzhonglu/GEMEditor
    def test_get_reaction_from_id(self, database):
        result = database.get_reaction_from_id(1)
        assert Annotation(identifier="MNXR14892", collection="metanetx.reaction") in result.annotation

        assert database.get_reaction_from_id(15) is None
コード例 #20
0
 def get_annotation(self):
     """ Return an annotation object from the user input """
     return Annotation(collection=self._get_collection(),
                       identifier=self.annotationLineEdit.text())
コード例 #21
0
    def test_annotation_eq2(self):
        """ Test not equality if type is different """

        assert Annotation("a", "a", "is") != Annotation("a", "a", "has")
        assert Annotation("a", "a", "is").__hash__() != Annotation(
            "a", "a", "has").__hash__()
コード例 #22
0
 def init_objects(self):
     self.annotation1 = Annotation("test_collection", "test_id")
     self.annotation2 = Annotation("test_collection", "test_id")
     self.empty_annotation = Annotation()
コード例 #23
0
from GEMEditor.model.classes.annotation import Annotation
from GEMEditor.rw import *
from GEMEditor.rw.annotation import add_miriam, add_qbiol_bag, add_rdf_annotation, annotate_xml_from_model, \
    parse_miriam_string, annotate_element_from_xml
from GEMEditor.rw.test.ex_annotation import valid_annotation, valid_annotation_id, valid_annotation_provider, \
    invalid_annotation1, invalid_annotation2, valid_annotation_xml
from lxml.etree import Element


class MockElement:
    def __init__(self):
        self.id = "test_id"
        self.annotation = set()


is_annotation = Annotation("collection", "identifier")
has_annotation = Annotation("collection2", "identifier2", "has")


class TestAddMiriam:
    @pytest.fixture(autouse=True)
    def setup_items(self):
        self.node = Element("test")
        self.annotation = is_annotation

    def test_addition(self):
        """ Test the addition of a valid annotation """
        return_value = add_miriam(self.node, self.annotation)

        assert self.node.find(rdf_li).attrib == {
            rdf_resource: "http://identifiers.org/collection/identifier"
コード例 #24
0
ファイル: test_model.py プロジェクト: hongzhonglu/GEMEditor
    def test_empty_result_if_not_in_db(self, database):
        metabolite = Metabolite("m1")
        metabolite.annotation.add(
            Annotation(identifier="MNXM2000", collection="metanetx.chemical"))

        assert map_by_annotation(database, metabolite) == set()