示例#1
0
                              wspace=0.05,
                              top=0.8,
                              bottom=0)
    f2_ax3 = fig2.add_subplot(spec2[0, 0])
    f2_ax4 = fig2.add_subplot(spec2[1, 0])
    cuts_colorbar_ax = fig2.add_subplot(spec1[0, 0])

    # add gene model
    properties_dict = {
        'file': 'test.bed',
        'color': 'grey',
        'labels': 'on',
        'style': 'UCSC',
        'fontsize': 9
    }
    bed = tracks.BedTrack(properties_dict)

    # axis 1 is the project
    # axis 2 is the tss enrichment
    # axis 3 is the heatmap
    # axis 4 is the gene model
    cut_norm = mcolors.Normalize(vmin=0, vmax=heatmap.max().max())

    f2_ax3.imshow(heatmap,
                  aspect='auto',
                  cmap=newcmp,
                  extent=(heatmap.columns.min(), heatmap.columns.max(), 0, 1),
                  norm=cut_norm)
    f2_ax3.set_xlim(START, END)
    f2_ax3.xaxis.set_ticklabels('')
    f2_ax3.yaxis.set_ticklabels('')
track_config = dict(file=GENCODE_BED,
                    arrow_length=1000,
                    arrow_interval=10,
                    file_type='bed',
                    height=3,
                    color='black',
                    merge_transcripts=False,
                    labels=True,
                    section_name='',
                    prefered_name='gene_name',
                    global_max_row=False,
                    font_size=8,
                    style="UCSC",
                    all_labels_inside=False,
                    labels_in_margin=True)
tk = pygtk.BedTrack(track_config)

# In[3]:

#track_config = dict(file='/lab/data/reference/human/hg19/annot/gencode.v19.annotation.gtf.gz', file_type='bed', height=3, color='black', line_width=0.1, merge_transcripts=False, labels=True, section_name='', prefered_name='gene_name', global_max_row=False, font_size=8, style="UCSC", all_labels_inside=False, labels_in_margin=True)
#tk = pygtk.BedTrack(track_config)

# In[30]:

CLUSTER_NAMES = '/lab/work/porchard/sn-muscle-project/cluster-names.txt'
cluster_names = pd.read_csv(CLUSTER_NAMES, sep='\t').astype(str)
cluster_colors = cmap_utils.make_colormap_dict(
    cluster_names.new_name.to_list())
cluster_names = dict(zip(cluster_names.old_name, cluster_names.new_name))

# In[51]:
示例#3
0
def plot_tracks(region, track_configs, outfig_name, **kwargs):
    if len(track_configs) < 2:
        raise Exception(
            "Invalid track numbers... We only accepted more than one track right now..."
        )

    figsize = kwargs.get("figsize", (16, 9))
    fig, axs = plt.subplots(len(track_configs),
                            1,
                            sharex='col',
                            figsize=figsize)
    plt.subplots_adjust(left=0.1, right=0.8, bottom=0.1, top=0.9)

    for i in range(0, len(track_configs)):
        ax = axs[i]
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.spines['left'].set_visible(False)

        infile_type = track_configs[i]['file'].split(".")[-1]

        if infile_type == 'cool':
            if track_configs[i].get('section_name') is None:
                track_configs[i]['section_name'] = 'HiC'
            if track_configs[i].get('region') is None:
                track_configs[i]['region'] = "{}:{}-{}".format(
                    region[0], region[1], region[2])

            tk = pygtk.HiCMatrixTrack(track_configs[i])
            tk.plot(ax, region[0],
                    int(region[1]) + track_configs[i]['depth'],
                    int(region[2]) - track_configs[i]['depth'])
            ax.set_yticks([])

            axins = inset_axes(ax,
                               width="1%",
                               height="100%",
                               loc='lower left',
                               bbox_to_anchor=(0, 0, 1, 1),
                               bbox_transform=ax.transAxes,
                               borderpad=0)
            cobar = fig.colorbar(tk.img, ax=ax, cax=axins)
            cobar.ax.yaxis.set_ticks_position('left')

            if tk.properties.get('title') is not None:
                draw_track_title(ax, tk.properties['title'])

        if infile_type == 'arcs' or infile_type == 'links':
            if track_configs[i].get('section_name') is None:
                track_configs[i]['section_name'] = 'Links'
            elif track_configs[i].get('section_name') == 'clusters':

                if track_configs[i].get('color') is None \
                 or not is_colormap(track_configs[i].get('color')):
                    track_configs[i]['color'] = default_colormap

                if track_configs[i].get('clusters') is None:
                    tmp_pd = pd.read_csv(track_configs[i].get('file'),
                                         sep="\t",
                                         header=None)
                    track_configs[i]['min_value'] = 1
                    track_configs[i]['max_value'] = tmp_pd.iloc[:, 6].max()
                    n_clusters = track_configs[i]['max_value'] - track_configs[
                        i]['min_value'] + 1
                else:
                    track_configs[i]['min_value'] = 1
                    track_configs[i]['max_value'] = track_configs[i].get(
                        'clusters')
                    n_clusters = track_configs[i].get('clusters')

            tk = pygtk.LinksTrack(track_configs[i])
            tk.plot(ax, region[0], region[1], region[2])
            ax.set_yticks([])

            axins = inset_axes(ax,
                               width="1%",
                               height="100%",
                               loc='lower left',
                               bbox_to_anchor=(0, 0, 1, 1),
                               bbox_transform=ax.transAxes,
                               borderpad=0)
            if tk.properties.get('section_name') == 'clusters':
                ticks = np.linspace(tk.properties.get('min_value'),
                                    tk.properties.get('max_value'), n_clusters)
                bounds = np.append((ticks - 0.5),
                                   (tk.properties.get('max_value') + 0.5))
                cobar = fig.colorbar(tk.colormap,
                                     ax=ax,
                                     cax=axins,
                                     ticks=ticks,
                                     boundaries=bounds)
            else:
                cobar = fig.colorbar(tk.colormap, ax=ax, cax=axins)
            cobar.ax.yaxis.set_ticks_position('left')

            if tk.properties.get('title') is not None:
                draw_track_title(ax, tk.properties['title'])

        if infile_type == 'bigwig' or infile_type == 'bw':
            if track_configs[i].get('section_name') is None:
                track_configs[i]['section_name'] = 'BigWig'
            tk = pygtk.BigWigTrack(track_configs[i])
            tk.plot(ax, region[0], region[1], region[2])

            if tk.properties.get('title') is not None:
                draw_track_title(ax, tk.properties['title'])

        if infile_type == 'gtf':
            if track_configs[i].get('section_name') is None:
                track_configs[i]['section_name'] = 'Gene'
            tk = pygtk.GtfTrack(track_configs[i])
            tk.plot(ax, region[0], region[1], region[2])
            ax.set_yticks([])

            if tk.properties.get('title') is not None:
                draw_track_title(ax, tk.properties['title'])

        if infile_type == 'bed':
            if track_configs[i].get('section_name') is None:
                track_configs[i]['section_name'] = 'Bed'
            elif track_configs[i].get('section_name') == 'clusters':

                if track_configs[i].get('color') is None \
                 or not is_colormap(track_configs[i].get('color')):
                    track_configs[i]['color'] = default_colormap

                if track_configs[i].get('clusters') is None:
                    tmp_pd = pd.read_csv(track_configs[i].get('file'),
                                         sep="\t",
                                         header=None)
                    track_configs[i]['min_value'] = 1
                    track_configs[i]['max_value'] = tmp_pd.iloc[:, 4].max()
                    n_clusters = track_configs[i]['max_value'] - track_configs[
                        i]['min_value'] + 1
                else:
                    track_configs[i]['min_value'] = 1
                    track_configs[i]['max_value'] = track_configs[i].get(
                        'clusters')
                    n_clusters = track_configs[i].get('clusters')

            tk = pygtk.BedTrack(track_configs[i])
            tk.plot(ax, region[0], region[1], region[2])
            ax.set_yticks([])

            if is_colormap(tk.properties['color']):
                axins = inset_axes(
                    ax,
                    width="1%",  # width = 1% of parent_bbox width
                    height="100%",  # height : 100%
                    loc='lower left',
                    bbox_to_anchor=(0, 0, 1, 1),
                    bbox_transform=ax.transAxes,
                    borderpad=0)

                if tk.properties.get('section_name') == 'clusters':
                    ticks = np.linspace(tk.properties.get('min_value'),
                                        tk.properties.get('max_value'),
                                        n_clusters)
                    bounds = np.append((ticks - 0.5),
                                       (tk.properties.get('max_value') + 0.5))
                    cobar = fig.colorbar(tk.colormap,
                                         ax=ax,
                                         cax=axins,
                                         ticks=ticks,
                                         boundaries=bounds)
                else:
                    cobar = fig.colorbar(tk.colormap, ax=ax, cax=axins)
                cobar.ax.yaxis.set_ticks_position('left')

            if tk.properties.get('title') is not None:
                draw_track_title(ax, tk.properties['title'])

    plt.savefig(outfig_name)