def subtree_of(subtree, tree, exact=False, tolerance=0): ''' Test to see if `subtree` is included in `tree` anywhere. Returns the node id number of the first occurence of `subtree` included in `tree` or |None| if it is not found. Parameters ---------- subtree: Glycan The structure to search for. The search attempts to match the complete structure of subtree. tree: Glycan The sturcture to search in. The search iterates over each residue in `tree` and calls a comparator function, comparing the `subtree` to the substructure rooted at that residue. exact: bool If |True|, use :func:`exact_ordering_inclusion` to compare nodes. Otherwise use :func:`topological_inclusion`. Defaults to |False|. Returns ------- |int| or |None| if no match ''' if exact: comparator = exact_ordering_inclusion else: comparator = topological_inclusion tree_root = root(subtree) for node in tree: if comparator(tree_root, node): return node.id return None
def __init__(self, structure, figure=None, ax=None, layout=buchheim, symbol_nomenclature=cfg_symbols, **kwargs): self.structure = structure self.root = DrawTreeNode(root(structure)) self.figure = figure self.ax = ax self.layout_scheme = layout self.symbol_nomenclature = symbol_nomenclature
def plot(tree, at=(1, 1), ax=None, orientation='h', center=False, label=False, symbol_nomenclature='cfg', layout='balanced', **kwargs): ''' Draw the parent outlink position and the child anomer symbol Parameters ---------- tree: Glycan or Monosaccharide The saccharide structure to draw. orientation: str A string corresponding to `h` or `horizontal` will draw the glycan horizontally from right to left. `v` or `vertical` will draw the glycan from bottom to top. Defaults to `h` at: tuple The x, y coordinates at which to draw the glycan's reducing_end. Defaults to `(0, 0)` ax: :class:`matplotlib.axes.Axis` A matplotlib axis on which to draw. Useful for adding glycans to existing figures. If an axis is not provided, a new figure will be created and used. center: bool Should the plot limits be centered around this glycan? Defaults to |False| but will be be set to |True| if `ax` is |None| label: bool Should the bond annotations for `tree` be drawn? Defaults to |False| scale: (float, float) or float Node scale coefficients. Pass a pair to scale x and y dimensions respectively. stretch: (float, float) or float Edge length coefficients. Pass a pair to scale x and y dimensions respectively. Order is affected by `orientation` ''' scale = kwargs.get("scale", DEFAULT_SYMBOL_SCALE_FACTOR) if isinstance(symbol_nomenclature, basestring): symbol_nomenclature = nomenclature_map.get(symbol_nomenclature) if isinstance(layout, basestring): layout = layout_map.get(layout) tree_root = root(tree) dtree = DrawTreeNode(tree_root) if layout == topological: for node in breadth_first_traversal(dtree): node.mask_special_cases = False layout(dtree) if layout != topological: dtree.fix_special_cases() fig = None # Create a figure if no axes are provided if ax is None: fig, ax = plt.subplots() at = (0, 0) center = True ax.axis('off') dtree.draw(at=at, ax=ax, scale=scale, label=False, symbol_nomenclature=symbol_nomenclature, **kwargs) dtree.axes = ax dtree.data['orientation'] = orientation if label: dtree.draw_linkage_annotations( at=at, ax=ax, scale=scale, symbol_nomenclature=symbol_nomenclature, **kwargs) if orientation in {"h", "horizontal"}: dtree.transform(mtransforms.Affine2D().rotate_deg(90)) dtree._rotate_text(-90) # If the figure is stand-alone, center it if fig is not None or center: xmin, xmax, ymin, ymax = dtree.extrema(at=at) ax.set_xlim(xmin - 2, xmax + 2) ax.set_ylim(ymin - 2, ymax + 2) ax.autoscale_view() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) return (dtree, ax)
def pack(self): d = self.processor(''.join(self.sequence_chunks)) name = self.defline root_node = root(d) return NamedGlycan(name=name, root=root_node, index_method=None)