The utils package#

These are utility functions that are used for some glycan specific tasks.

The iupac module#

Functions to work with the IUPAC glycan nomenclature.

class glycosylator.utils.iupac.IUPACParser[source]#

Bases: object

A parser for condensed IUPAC glycan nomenclature strings. This class will generate a list of connecting glycan segments from a string from which a Molecule can be built.

parse(string)[source]#

Parse a string of IUPAC glycan nomenclature into a list of glycan segments.

Parameters:

string (str) – The IUPAC glycan nomenclature string.

Returns:

A list of tuples where each segment is a tuple of (residue1, residue2, linkage).

Return type:

list

reset()[source]#

Reset the parser.

class glycosylator.utils.iupac.IUPACStringMaker[source]#

Bases: object

This class generates IUPAC/SNFG strings from a list of glycan molecule.

beta_alpha_mismatch_pattern = 'b-(\\w+)\\((a)'#
beta_alpha_mismatch_replacement = '\\1(b'#
double_beta_pattern = '(b-(\\w+\\(b))'#
double_beta_replacement = '\\2'#
terminal_alpha_pattern = '([\\]\\)]\\w+)$'#
terminal_alpha_replacement = '\\1(a1-'#
terminal_beta_pattern = 'b-(\\w+)$'#
terminal_beta_replacement = '\\1(b1-'#
write_string(glycan: Glycan, add_terminal_conformation: bool = True) str[source]#

Write the IUPAC string for a glycan molecule.

Parameters:
  • glycan (Glycan) – The glycan molecule to write the IUPAC string for.

  • add_terminal_conformation (bool) – If True, adds the terminal conformation as last(a1- or last(b1- to the string.

Returns:

The IUPAC string for the glycan molecule.

Return type:

str

glycosylator.utils.iupac.make_iupac_string(glycan, add_terminal_conformation: bool = True)[source]#

Make an IUPAC/SNFG string from a glycan molecule.

Parameters:
  • glycan (Glycan) – The glycan molecule to make the IUPAC string for.

  • add_terminal_conformation (bool) – If True, adds the terminal conformation as last(a1- or last(b1- to the end of the string.

Returns:

The IUPAC string for the glycan molecule.

Return type:

str

Make a link id string in Glycosylator format from a Bond object.

Parameters:

bond (Bond) – The bond from which to make an id string.

Returns:

The linkage id string in Glycosylator format.

Return type:

str

glycosylator.utils.iupac.make_snfg_string(glycan, add_terminal_conformation: bool = True)#

Make an IUPAC/SNFG string from a glycan molecule.

Parameters:
  • glycan (Glycan) – The glycan molecule to make the IUPAC string for.

  • add_terminal_conformation (bool) – If True, adds the terminal conformation as last(a1- or last(b1- to the end of the string.

Returns:

The IUPAC string for the glycan molecule.

Return type:

str

glycosylator.utils.iupac.parse_iupac(string)[source]#

Parse a string of IUPAC/SNFG glycan nomenclature into a list of glycan segments.

Parameters:

string (str) – The IUPAC glycan nomenclature string.

Returns:

A list of tuples where each segment is a tuple of (residue1, residue2, linkage).

Return type:

list

glycosylator.utils.iupac.parse_snfg(string)#

Parse a string of IUPAC/SNFG glycan nomenclature into a list of glycan segments.

Parameters:

string (str) – The IUPAC glycan nomenclature string.

Returns:

A list of tuples where each segment is a tuple of (residue1, residue2, linkage).

Return type:

list

The visual module#

Visualizations for glycosylator

class glycosylator.utils.visual.Canvas[source]#

Bases: object

A canvas of 2D slots for placing nodes.

root_nodes#

The root nodes on the canvas.

Type:

list

all_nodes#

The nodes on the canvas (not regarding connectivity or hierarchy).

Type:

list

add(node: Node, x: int = None, y: int = None)[source]#

Add a node at a specific location on the canvas.

Parameters:
  • node (Node) – The node to place.

  • x (int) – The x coordinate to place the node at.

  • y (int) – The y coordinate to place the node at.

flip(horizontal: bool = False, vertical: bool = True)[source]#

Flip the canvas horizontally or vertically.

Parameters:
  • horizontal (bool) – Whether to flip the canvas horizontally.

  • vertical (bool) – Whether to flip the canvas vertically.

classmethod from_dict(_Canvas__dict)[source]#

Make a canvas from a dictionary.

Parameters:

__dict (dict) – The dictionary to make the canvas from.

Returns:

canvas – The canvas made from the dictionary.

Return type:

Canvas

classmethod from_glycan(glycan)[source]#
get_clashing_nodes() iter[source]#

Get an iterator of nodes that are clashing with each other.

get_xlim()[source]#

Get the x limits of the canvas.

get_ylim()[source]#

Get the y limits of the canvas.

has_node(node_or_id)[source]#

Check if a node is on the canvas.

Parameters:

node_or_id (Node or str) – The node or id to check for.

icicle_layout()[source]#

Make an icicle layout of the nodes.

is_free(x: int, y: int) bool[source]#

Check if a slot is free.

Parameters:
  • x (int) – The x coordinate of the slot.

  • y (int) – The y coordinate of the slot.

Returns:

is_free – Whether the slot is free.

Return type:

bool

property leaves: list#

The leaves of the canvas.

remove(node: Node)[source]#

Remove a node from the canvas.

Parameters:

node (Node) – The node to remove.

render(ax, draw_edge_labels: bool = True, pretty_labels: bool = True, small_labels: bool = False, label_kws: dict = None, edge_kws: dict = None, adjust_axes: bool = True)[source]#

Render the canvas on a matplotlib axis. This requires that all nodes have a matplotlib patch associated with them.

Parameters:
  • ax (matplotlib.axes.Axes) – The axis to render the canvas on.

  • draw_edge_labels (bool) – Whether to draw the id labels of the connecting edges.

  • pretty_labels (bool) – Whether to use the pretty format for the edge labels. Including greek letters and arrows.

  • small_labels (bool) – Whether to use the small format for the edge labels, without arrows.

  • label_kws (dict) – The keyword arguments to pass to the edge label drawing function.

  • edge_kws (dict) – The keyword arguments to pass to matplotlib.patches.ConnectionPatch.

rotate90(clockwise: bool = True)[source]#

Rotate the canvas 90 degrees.

Parameters:

clockwise (bool) – Whether to rotate the canvas clockwise.

scale(factor: float)[source]#

Scale the canvas by a factor.

tree_layout()[source]#

Make a tree layout of the nodes.

property xlim: tuple#

The x limits of the canvas.

property ylim: tuple#

The y limits of the canvas.

class glycosylator.utils.visual.GlycanViewer2D(glycan)[source]#

Bases: object

A 2D scematic viewer for glycan structures

disable_glycowork()[source]#

Disable the use of GlycoWork for drawing (if available)

enable_glycowork()[source]#

Enable the use of GlycoWork for drawing (if available)

class glycosylator.utils.visual.Node(id, icon=None)[source]#

Bases: object

A node in a tree structure.

Parameters:
  • id (str) – The id of the node.

  • icon (np.ndarray) – The icon to represent the node in SNFG format.

id#

The id of the node.

Type:

str

icon#

The icon of the node.

Type:

np.ndarray

children#

The children of the node.

Type:

list

parent#

The parent of the node.

Type:

Node

canvas#

The canvas that the node is placed on.

Type:

Canvas

x#

The x coordinate of the node.

Type:

int

y#

The y coordinate of the node.

Type:

int

add_child(child: Node, x: int = None, y: int = None)[source]#

Add a child node to this node.

Parameters:
  • child (Node) – The child node to add.

  • x (int) – The x coordinate of the child node.

  • y (int) – The y coordinate of the child node.

property depth#

The depth of the node.

get_ancestors() list[source]#

Get the ancestors of this node.

get_descendants() list[source]#

Get the descendants of this node.

get_left_cousins()[source]#

Get the left cousins of this node.

get_left_siblings()[source]#

Get the left siblings of this node.

get_right_cousins()[source]#

Get the right cousins of this node.

get_right_siblings()[source]#

Get the right siblings of this node.

has_descendant(node: Node) bool[source]#

Check if a node is a descendant of this node.

has_valid_placement() bool[source]#

Check if a node has a valid placement on the canvas. Placements are valid if there is no other node at the same position.

move_to(x: int, y: int)[source]#

Move this node to (x, y) on the canvas.

place(canvas: Canvas, x: int, y: int)[source]#

Place this node at (x, y) on the canvas.

Parameters:
  • canvas (Canvas) – The canvas to place the node on.

  • x (int) – The x coordinate of the node.

  • y (int) – The y coordinate of the node.

remove_child(child: Node)[source]#

Remove a child node from this node.

shift_down(dx, dy)[source]#

Shift this node and all children by dx and dy.

shift_up(dx, dy)[source]#

Shift this node and all parents by dx and dy. This will also affet all siblings and their children.

property siblings: list#

Sibling nodes that share the same parent.

class glycosylator.utils.visual.ScaffoldViewer2D(scaffold, bar_color: str = 'xkcd:sandy', bar_height: int = 6, height: float = 20, figsize: tuple = None)[source]#

Bases: object

A 2D schematic for a protein scaffold

Parameters:
  • scaffold (Scaffold) – The protein scaffold to visualize.

  • bar_color (str) – The color to use for the bar representing the protein sequence.

  • bar_height (int) – The height of the bar representing the protein sequence.

  • height (float) – The total height of each subplot (chain) in the figure.

draw_glycans(scalar: float = 1)[source]#

Draw the glycans

Parameters:

scalar (float) – A scalar to use for artificially upscaling the glycans.

draw_glycosylation_sites(n_linked_color='xkcd:sky blue', o_linked_color='xkcd:medium pink', draw_legend: bool = True)[source]#

Highlight glycosylation sites

Parameters:
  • n_linked_color (str) – The color to use for N-linked glycosylation sites.

  • o_linked_color (str) – The color to use for O-linked glycosylation sites.

  • draw_legend (bool) – Whether to draw a legend for the glycosylation sites.

get_figure()[source]#

Get the figure of the viewer

save(filename: str, dpi: int = 300, **kwargs)[source]#

Save the figure to a file

Parameters:
  • filename (str) – The filename to save to.

  • dpi (int) – The resolution of the image.

show()[source]#

Show the figure

title(t: str)[source]#

Set the title of the figure

Parameters:

t (str) – The title to set

glycosylator.utils.visual.USE_GLYCOWORK = False#

Whether to use GlycoDraw from GlycoWork to draw glycan structures

glycosylator.utils.visual.dont_use_glycowork()[source]#

Don’t use GlycoDraw from GlycoWork to draw glycan structures

glycosylator.utils.visual.edge_label_defaults = {'alpha': 1.0, 'clip_on': False, 'font_color': 'k', 'font_size': 7, 'font_weight': 'normal', 'horizontalalignment': 'center', 'rotate': True}#

Default edge label drawing parameters

glycosylator.utils.visual.use_glycowork()[source]#

Use GlycoDraw from GlycoWork to draw glycan structures