Plotting fragmentation output
This module provides functionality to plot the AFprep fragmentation output of a protein.
- Functions:
plot_domain: Plots a rectangle representing a single protein domain
plot_fragment: Plots a rectangle representing a single protein fragment
draw_label: Adds a label to the plot
calculate_tick_freq: Calculates the frequency of x axis ticks based on sequence length
plot_fragmentation_output: Creates and optionally saves a visualization of protein domains and fragments
- Dependencies:
itertools: Used for cycling through colors for the protein domains.
os: Used for creating directories if they do not exist.
matplotlib: Used for plotting the protein domains and fragments.
- alphafragment.plot_fragments.calculate_tick_freq(n)
Calculates the frequency of x axis ticks for a given number of residues.
- Parameters:
n (int): The number of residues in the protein sequence.
- Returns:
int: The frequency of x-axis ticks.
- alphafragment.plot_fragments.draw_label(label, x_left, x_right, y, bracket_height, ax)
Adds a bracket to the plot labelling a specified region with the given text
- Parameters:
label (str): The text to be displayed on the bracket.
x_left (float): The left x-coordinate of the bracket.
x_right (float): The right x-coordinate of the bracket.
y (float): The y-coordinate of the bracket.
bracket_height (float): The height of the bracket.
ax (matplotlib.axes.Axes): The matplotlib axis on which to plot the bracket.
- Note:
This function adds to the provided axis but does not show it. The display is managed by the caller.
- alphafragment.plot_fragments.plot_domain(ax, domain, base_y_position, domain_height, domain_color_cycle, color_mode='type')
Plots a rectangle representing a single protein domain, on a given matplotlib axis. The color of the domain can be either cycled through a set of predefined colors or set based on domain type.
- Parameters:
ax (matplotlib.axes.Axes): The matplotlib axis on which to plot the domain.
domain (Domain): Expected to be a domain type object
base_y_position (float): The base Y-axis position for the domain rectangle.
domain_height (float): The height of the domain rectangle.
domain_color_cycle (itertools.cycle): An itertools.cycle object that cycles through a set of colors for the protein domains.
color_mode (str): ‘cycle’ for cycling colors or ‘type’ for color based on domain type.
- Note:
This function adds to the provided axis but does not show it. The display is managed by the caller.
- alphafragment.plot_fragments.plot_fragment(ax, fragment, index, base_y_position, fragment_height, offset)
Plots a rectangle representing a single protein fragment, on a given matplotlib axis. Plots each fragment with a specified vertical offset to prevent overlap between consecutive fragments.
- Parameters:
ax (matplotlib.axes.Axes): The matplotlib axis on which to plot the fragment.
fragment (tuple): A tuple containing fragment start and end positions.
index (int): The index of the fragment, used to alternate vertical offset.
base_y_position (float): The base Y-axis position for fragment rectangles.
fragment_height (float): The height of the fragment rectangle.
offset (float): The vertical offset for each fragment (to distinguish between overlapping fragments).
- Note:
This function adds to the provided axis but does not show it. The display is managed by the caller.
Adds 0.5 to either end of the fragment to center it on the residue position.
- alphafragment.plot_fragments.plot_fragmentation_output(protein, fragments, save_location=None, figsize=(12, 4), color_mode='type', label=None)
Creates and optionally saves a visualization of protein domains and fragments. Domains are plotted as colored rectangles, and fragments as red rectangles with a vertical offset so they can be distinguished. The resulting plot can be saved to a file by specifiying a save location.
- Parameters:
protein (Protein): The protein object, expected to have ‘domain_list’ and ‘last_res’ attributes.
fragments (list of tuples): A list of tuples, each containing the start and end positions of a fragment.
save_location (str, optional): The directory path where the plot image will be saved. If not provided, the image is not saved.
figsize (tuple, optional): The size of the output figure.
color_mode (‘type’ or ‘cycle): Whether to color domains by type (AlphaFold, UniProt, manually defined), or using a series of colours to distinguish nearby domains
label (list, optional): List of sources for which domains should be labelled. Can include ‘UniProt’, ‘AF’ and ‘manually_defined’. Defaults to None.
- Returns:
matplotlib.figure.Figure: The figure object containing the plot.
- Note:
If save_location is provided and the directory does not exist, it will be created.
x-axis represents the protein sequence position, with 1-based indexing.