Source code for glypy.structure.link

import itertools
from uuid import uuid4
try:
    from collections.abc import Iterable
except ImportError:
    from collections import Iterable

from glypy.composition import Composition
from glypy.utils import uid, basestring, make_struct
from .base import SaccharideBase, SubstituentBase
from .constants import UnknownPosition, LinkageType


default_parent_loss = Composition({"O": 1, "H": 1})
default_child_loss = Composition(H=1)


linkage_configuration = make_struct("linkage_configuration", ("parent", "child", "parent_position", "child_position"))





class LinkMaskContext(object):
    '''
    A context manager for masking and unmasking |Link| objects on a residue
    '''
    def __init__(self, residue, attach=False):
        self.residue = residue
        self.links = [link for link in residue.links.values()]
        self.attach = attach

    def __enter__(self):
        self.mask() if not self.attach else self.unmask()

    def __exit__(self, exc_type, exc_value, traceback):
        self.mask() if self.attach else self.unmask()

    def mask(self):
        """
        For each |Link| captured, if that link is attched, call its
        :meth:`break_link` method with `refund=True`
        """
        for link in self.links:
            if link.is_attached():
                link.break_link(refund=True)

    def unmask(self):
        """
        For each |Link| captured, if that link is not attched, call its
        :meth:`apply` method
        """
        for link in self.links:
            if not link.is_attached():
                link.apply()