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.
textwrap: Used for wrapping long domain labels.
os: Used for creating directories if they do not exist.
re: Used for extracting the text part of the domain identifier.
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(ax, label, x, x_left, x_right, y, bracket_height, max_x)
Adds a bracket to the plot labeling a specified region with the given text.
- Parameters:
label (str): The text to be displayed on the bracket.
x (float): The x-coordinate of the label.
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.
max_x (float): The maximum x-coordinate allowed for the label to prevent it from spilling out of the plot axes.
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='origin', type_colors={})
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 origin or 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, ‘origin’ for color based on domain origin, or ‘type’ for color based on domain identifier.
type_colors (dict): A dictionary to store colors for ‘type’ color mode.
- Returns:
dict: The updated type_colors dictionary, only used if color_mode is ‘type’.
- Note:
Although this function does have a return, its primary purpose is to add a rectangle to the provided axis. The return is used to maintain color consistency when plotting multiple domains with the ‘type’ color mode.
- 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=['UniProt', 'manually_defined'])
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 specifying 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 (‘origin’, ‘cycle’, or ‘type’): Whether to color domains by origin, cycle colors, or type. Defaults to ‘type’.
label (list, optional): List of sources for which domains should be labeled. Can include ‘UniProt’, ‘AF’, and ‘manually_defined’. Defaults to labelling UniProt and manually defined domains.
- 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.