utilities_for_plotting_genomic_signal_tracks

Author

Saideep Gona

Published

2023-09-20

Utilities for plotting genomic signal tracks

This post contains function for plotting genomic signal tracks as well as additional utilities to make working with this kind of data easier.

Required packages

Code
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

Plotting function

The following is a general purpose plotting function for plotting signal tracks. Just supply a dictionary of vectors to create multiple subplots corresponding to a single interval. We should continually update this function as needed.

Code
def plot_tracks(tracks, interval, vlines=None, font_size=18, height=1.5):
    '''
    tracks: dictionary of signal tracks (1-d vectors) and titles
    interval: [chrom, start, end]  , should be a list/tuple
    vlines: vertical line coordinates to be plotted, for example TSS sites. Should be in absolute coordinates 
    '''
    import matplotlib.pyplot as plt
    import seaborn as sns

    fig, axes = plt.subplots(len(tracks), 1, figsize=(20, height * len(tracks)), sharex=True)

    for ax, (title, y) in zip(axes, tracks.items()):
        ax.fill_between(np.linspace(interval[1], interval[2], num=len(y)), y)
        ax.set_title(title, fontsize=font_size)
        if vlines == None:
            pass
        else:
            ax.vlines(list(vlines),0, max(y), color="red")
        sns.despine(top=True, right=True, bottom=True)

    ax.set_xlabel("_".join([str(x) for x in interval]))
    plt.tight_layout()
Code
# Create an interval spanning the signal window. Window length should be a multiple of track length.
example_interval = ['chr6', 100000, 100000+896*10]

# Store signal tracks as vectors in a dictionary
plotting_dictionary = {
    "Example1":np.random.normal(size=(896,)),
    "Example2":np.random.normal(size=(896,))
}

# Supply inputs to plot
plot_tracks(plotting_dictionary, example_interval)