Skip to content

relation

autocorpus.ac_bioc.relation ¤

This module defines the BioCRelation class.

Classes¤

BioCRelation(id=str(), infons=dict(), nodes=list()) dataclass ¤

Bases: DataClassJsonMixin

A class representing a BioC relation.

Functions¤
from_xml(elem) classmethod ¤

Create a BioCRelation instance from an XML element.

Parameters:

Name Type Description Default
elem Element

An XML element containing the relation data.

required

Returns:

Name Type Description
BioCRelation BioCRelation

An instance of BioCRelation created from the XML element.

Source code in autocorpus/ac_bioc/relation.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@classmethod
def from_xml(cls, elem: ET.Element) -> BioCRelation:
    """Create a BioCRelation instance from an XML element.

    Args:
        elem (ET.Element): An XML element containing the relation data.

    Returns:
        BioCRelation: An instance of BioCRelation created from the XML element.
    """
    from .node import BioCNode

    infons = {e.attrib["key"]: e.text for e in elem.findall("infon")}
    nodes = [BioCNode.from_xml(n) for n in elem.findall("node")]
    return cls(
        id=elem.attrib.get("id", ""),
        infons={k: v for k, v in infons.items() if v is not None},
        nodes=nodes,
    )
to_xml() ¤

Convert the BioCRelation instance to an XML element.

Returns:

Type Description
Element

ET.Element: An XML element representation of the BioCRelation instance.

Source code in autocorpus/ac_bioc/relation.py
21
22
23
24
25
26
27
28
29
30
31
32
33
def to_xml(self) -> ET.Element:
    """Convert the BioCRelation instance to an XML element.

    Returns:
        ET.Element: An XML element representation of the BioCRelation instance.
    """
    elem = ET.Element("relation", {"id": self.id})
    for k, v in self.infons.items():
        infon = ET.SubElement(elem, "infon", {"key": k})
        infon.text = v
    for node in self.nodes:
        elem.append(node.to_xml())
    return elem