def test_should_read_lines(self): actual = read((line for line in [ "format-version: 1.2\n", "another-format-version: 1.3\n", "[Term]\n", "id: GO:0015137\n", "name: citrate transmembrane transporter activity\n", "namespace: molecular_function\n", '''def: "Enables the transfer of citrate from one side of a membrane to the other." [GOC:ai]\n''', """synonym: "tricarboxylate transport protein" RELATED [] {comment="WIkipedia:Mitochondrial_carrier"}\n""", 'xref: reactome:R-HSA-433104 "NACT co-transports trivalent citrate and a sodium ion"\n', "is_a: GO:0015142 ! tricarboxylic acid transmembrane transporter activity\n", "is_a: GO:0042895 ! antibiotic transmembrane transporter activity\n", "relationship: part_of GO:0015746 ! citrate transport\n", "is_obsolete: true ! this is just to test a boolean\n", "[Typedef]\n", "id: ends_during\n", "name: ends_during\n", "namespace: external\n", "xref: RO:0002093\n", ])) expected = OboDocument() expected.header.format_version = "1.2" expected.header.another_format_version = ["1.3"] term = OboTerm() term.id = "GO:0015137" term.name = "citrate transmembrane transporter activity" term.namespace = "molecular_function" term.def_ = OboDef( '''Enables the transfer of citrate from one side of a membrane to the other.''', [OboXref("GOC:ai", None)]) term.synonym = ['"tricarboxylate transport protein" RELATED []'] term.xref = OboXref( 'reactome:R-HSA-433104', "NACT co-transports trivalent citrate and a sodium ion") term.is_a = ["GO:0015142", "GO:0042895"] term.relationship = ["part_of GO:0015746"] term.is_obsolete = True term._qualifiers["synonym"] = { "comment": "WIkipedia:Mitochondrial_carrier" } typedef = OboTypedef() typedef.id = "ends_during" typedef.name = "ends_during" typedef.namespace = "external" typedef.xref = OboXref("RO:0002093", None) expected.terms = [term] expected.typedefs = [typedef] self.assertDocumentEquals(actual, expected)
def test_supports_single_relax_unique_identifier_assumption_for_namespace_but_does_not_validate_content(self): actual = read((line for line in [ "relax-unique-identifier-assumption-for-namespace: an invalid relax-unique-identifier-assumption-for-namespace"])) expected = OboDocument() expected.header.relax_unique_identifier_assumption_for_namespace = [ "an invalid relax-unique-identifier-assumption-for-namespace"] self.assertDocumentEquals(actual, expected)
def test_supports_multiple_treat_xrefs_as_genus_differentia_but_does_not_validate_content(self): actual = read((line for line in ["treat-xrefs-as-genus-differentia: an invalid treat-xrefs-as-genus-differentia\n", "treat-xrefs-as-genus-differentia: ID \"value\"\n"])) expected = OboDocument() expected.header.treat_xrefs_as_genus_differentia = ["an invalid treat-xrefs-as-genus-differentia", 'ID "value"'] self.assertDocumentEquals(actual, expected)
def test_supports_multiple_owl_axioms(self): actual = read( (line for line in ["owl-axioms: an axiom\n", "owl-axioms: another axiom\n"])) expected = OboDocument() expected.header.owl_axioms = ["an axiom", "another axiom"] self.assertDocumentEquals(actual, expected)
def test_supports_multiple_remarks(self): actual = read( (line for line in ["remark: some comment\n", "remark: some other comment\n"])) expected = OboDocument() expected.header.remark = ["some comment", "some other comment"] self.assertDocumentEquals(actual, expected)
def test_supports_multiple_relax_unique_label_assumption_for_namespace_but_does_not_validate_content(self): actual = read((line for line in [ "relax-unique-label-assumption-for-namespace: an invalid relax-unique-label-assumption-for-namespace\n", "relax-unique-label-assumption-for-namespace: ID \"value\"\n"])) expected = OboDocument() expected.header.relax_unique_label_assumption_for_namespace = [ "an invalid relax-unique-label-assumption-for-namespace", 'ID "value"'] self.assertDocumentEquals(actual, expected)
def test_supports_single_treat_xrefs_as_reverse_genus_differentia_but_does_not_validate_content(self): actual = read( (line for line in ["treat-xrefs-as-reverse-genus-differentia: an invalid treat-xrefs-as-reverse-genus-differentia"])) expected = OboDocument() expected.header.treat_xrefs_as_reverse_genus_differentia = [ "an invalid treat-xrefs-as-reverse-genus-differentia"] self.assertDocumentEquals(actual, expected)
def test_add_typedef(self): actual = OboDocument() typedef = actual.add_typedef() typedef.id = TestDocument.A_TYPEDEF_ID expected = OboDocument() expected_typedef = OboTypedef() expected_typedef.id = TestDocument.A_TYPEDEF_ID expected.typedefs = [expected_typedef] self.assertDocumentEquals(actual, expected)
def test_add_term(self): actual = OboDocument() term = actual.add_term() term.id = TestDocument.A_TERM_ID expected = OboDocument() expected_term = OboTerm() expected_term.id = TestDocument.A_TERM_ID expected.terms = [expected_term] self.assertDocumentEquals(actual, expected)
def test_supports_multiple_idspace_but_does_not_validate_content(self): actual = read((line for line in ["idspace: an invalid idspace\n", "idspace: ID \"value\"\n"])) expected = OboDocument() expected.header.idspace = ["an invalid idspace", 'ID "value"'] self.assertDocumentEquals(actual, expected)
def test_supports_single_idspace_but_does_not_validate_content(self): actual = read((line for line in ["idspace: an invalid idspace"])) expected = OboDocument() expected.header.idspace = ["an invalid idspace"] self.assertDocumentEquals(actual, expected)
def test_supports_invalid_date(self): """This test should not pass, the date should be validated""" actual = read((line for line in ["date: some invalid date"])) expected = OboDocument() expected.header.date = "some invalid date" self.assertDocumentEquals(actual, expected)
def __init__(self): self.document = OboDocument() self.scope = self.document.header self.qualifiers = {} self.xrefs = []
def test_supports_format_version(self): actual = read((line for line in ["format-version: 1.2"])) expected = OboDocument() expected.header.format_version = "1.2" self.assertDocumentEquals(actual, expected)
def test_supports_single_remark(self): actual = read((line for line in ["remark: some comment"])) expected = OboDocument() expected.header.remark = ["some comment"] self.assertDocumentEquals(actual, expected)
def test_supports_default_namespace(self): actual = read((line for line in ["default-namespace: hello"])) expected = OboDocument() expected.header.default_namespace = "hello" self.assertDocumentEquals(actual, expected)
def test_supports_single_property_value_but_does_not_validate_content(self): actual = read((line for line in ["property_value: an invalid property_value"])) expected = OboDocument() expected.header.property_value = ["an invalid property_value"] self.assertDocumentEquals(actual, expected)
def test_supports_multiple_treat_xrefs_as_relationship_but_does_not_validate_content(self): actual = read((line for line in ["treat-xrefs-as-relationship: an invalid treat-xrefs-as-relationship\n", "treat-xrefs-as-relationship: ID \"value\"\n"])) expected = OboDocument() expected.header.treat_xrefs_as_relationship = ["an invalid treat-xrefs-as-relationship", 'ID "value"'] self.assertDocumentEquals(actual, expected)
def test_supports_single_treat_xrefs_as_is_a_but_does_not_validate_content(self): actual = read((line for line in ["treat-xrefs-as-is_a: an invalid treat-xrefs-as-is_a"])) expected = OboDocument() expected.header.treat_xrefs_as_is_a = ["an invalid treat-xrefs-as-is_a"] self.assertDocumentEquals(actual, expected)
def test_supports_date(self): actual = read((line for line in ["date: 22:05:1987 10:54"])) expected = OboDocument() expected.header.date = "22:05:1987 10:54" self.assertDocumentEquals(actual, expected)
def test_supports_ontology(self): actual = read((line for line in ["ontology: hello"])) expected = OboDocument() expected.header.ontology = "hello" self.assertDocumentEquals(actual, expected)
def test_supports_single_import_but_does_not_validate_url(self): actual = read((line for line in ["import: an invalid import"])) expected = OboDocument() expected.header.import_ = ["an invalid import"] self.assertDocumentEquals(actual, expected)
def test_supports_auto_generated_by(self): actual = read((line for line in ["auto-generated-by: myself"])) expected = OboDocument() expected.header.auto_generated_by = "myself" self.assertDocumentEquals(actual, expected)
def test_supports_multiple_imports_but_does_not_validate_url(self): actual = read((line for line in ["import: an invalid import\n", "import: http://someurl/\n"])) expected = OboDocument() expected.header.import_ = ["an invalid import", "http://someurl/"] self.assertDocumentEquals(actual, expected)
def test_supports_single_synonymtypedef_but_does_not_validate_content(self): actual = read((line for line in ["synonymtypedef: an invalid synonymtypedef"])) expected = OboDocument() expected.header.synonymtypedef = ["an invalid synonymtypedef"] self.assertDocumentEquals(actual, expected)
def test_supports_multiple_synonymtypedef_but_does_not_validate_content(self): actual = read( (line for line in ["synonymtypedef: an invalid synonymtypedef\n", "synonymtypedef: ID \"value\"\n"])) expected = OboDocument() expected.header.synonymtypedef = ["an invalid synonymtypedef", 'ID "value"'] self.assertDocumentEquals(actual, expected)
def test_supports_single_owl_axiom(self): actual = read((line for line in ["owl-axioms: an axiom"])) expected = OboDocument() expected.header.owl_axioms = ["an axiom"] self.assertDocumentEquals(actual, expected)
def test_supports_multiple_treat_xrefs_as_equivalent_but_does_not_validate_content(self): actual = read((line for line in ["treat-xrefs-as-equivalent: an invalid treat-xrefs-as-equivalent\n", "treat-xrefs-as-equivalent: ID\n"])) expected = OboDocument() expected.header.treat_xrefs_as_equivalent = ["an invalid treat-xrefs-as-equivalent", 'ID'] self.assertDocumentEquals(actual, expected)
def test_supports_default_relationship_id_prefix(self): actual = read( (line for line in ["default-relationship-id-prefix: prefix"])) expected = OboDocument() expected.header.default_relationship_id_prefix = "prefix" self.assertDocumentEquals(actual, expected)
class OboDocumentBuilder: def __init__(self): self.document = OboDocument() self.scope = self.document.header self.qualifiers = {} self.xrefs = [] def term(self): self.scope = self.document.add_term() def add_xref(self, xref, description): self.xrefs.append(OboXref(xref, description)) def def_tag_value(self, value): definition = OboDef(value, self.xrefs) self.xrefs = [] self.scope.def_ = definition self._process_qualifiers("def_") def typedef(self): self.scope = self.document.add_typedef() def xref_tag(self): if len(self.xrefs) != 1: raise OboDocumentBuildingError.expected_only_one_xref(self.xrefs) self.scope.xref = self.xrefs.pop() self._process_qualifiers("xref") def tag_value_pair(self, tag, value): attribute = OboDocumentBuilder._extract_attribute(tag) self._ensure_attribute_is_defined(attribute) self._set_attribute_value(tag, attribute, value) self._process_qualifiers(attribute) def _process_qualifiers(self, attribute): if len(self.qualifiers) > 0: self.scope.add_qualifiers(attribute, self.qualifiers) self.qualifiers = {} def boolean_tag_value_pair(self, tag, boolean_value): attribute = OboDocumentBuilder._extract_attribute(tag) self._set_boolean_attribute_value(tag, attribute, boolean_value) self._process_qualifiers(attribute) def qualifier(self, id, value): self.qualifiers[id] = value def _set_attribute_value(self, tag, attribute, value): current_value = self.scope.__dict__.get(attribute) if current_value is None: self.scope.__dict__[attribute] = value return if type(current_value) == list: current_value.append(value) return if current_value != value: raise OboDocumentBuildingError.invalid_tag_pair_merge( tag, current_value, value) def _set_boolean_attribute_value(self, tag, attribute, value): current_value = self.scope.__dict__.get(attribute) if current_value is None: self.scope.__dict__[attribute] = value return if type(current_value) == list: raise OboDocumentBuildingError.unexpected_boolean_value( tag, current_value, value) if current_value != value: raise OboDocumentBuildingError.invalid_tag_pair_merge( tag, current_value, value) def _ensure_attribute_is_defined(self, attribute): if attribute not in self.scope.__dict__: self.scope.__dict__[attribute] = [] @staticmethod def _extract_attribute(tag): attribute = tag.replace("-", "_") if attribute == "version": return "data_version" if attribute == "import": return "import_" if attribute == "def": return "def_" return attribute