Exemplo n.º 1
0
    def compare_two_gene_levels(
        dat_two_cols,
        meta,
        legend_dict,
        colour_map=scatter_colours,
        marker_map=scatter_markers,
    ):

        ax = scatter.scatter_with_colour_and_markers(
            dat_two_cols,
            colour_subgroups=meta.patient_id,
            colour_map=colour_map,
            marker_subgroups=meta.type,
            marker_map=marker_map
        )
        common.add_custom_legend(ax, legend_dict, loc_outside=True)
        ax.set_xlabel('%s (logTPM)' % dat_two_cols.columns[0])
        ax.set_ylabel('%s (logTPM)' % dat_two_cols.columns[1])
        fig = ax.figure
        fig.tight_layout()
        fig.subplots_adjust(right=0.8)

        return fig, ax
        'class': 'patch',
        'edgecolor': 'k',
        'linewidth': 1.,
    }

    lkg = plt_dict[clust_n_ftr]['linkage']
    leg_dict = collections.OrderedDict()
    for k in sorted(cell_line_colours):
        if cell_line_colours[k] in row_colours_all.values:
            leg_dict[k] = dict(leg_entry)
            leg_dict[k].update({'facecolor': cell_line_colours[k]})

    cm = clustering.plot_clustermap(this_dat,
                                    cmap='RdYlBu_r',
                                    metric='correlation',
                                    col_colors=row_colours_all,
                                    col_linkage=lkg,
                                    vmin=-10,
                                    vmax=10)
    cm.fig.set_size_inches((10.9, 8.))

    common.add_custom_legend(cm.ax_heatmap,
                             leg_dict,
                             loc_outside=True,
                             fontsize=14)
    cm.gs.update(bottom=0.3, right=0.79, left=0.01)

    cm.savefig(os.path.join(outdir, "clustermap_ipsc_esc_nsc_fb.png"), dpi=200)
    cm.savefig(os.path.join(outdir, "clustermap_ipsc_esc_nsc_fb.tiff"),
               dpi=200)
Exemplo n.º 3
0
def nested_pie_chart(dat,
                     facecolours,
                     legend_entries='same',
                     legend_loc='upper right',
                     inner_radius=0.,
                     width_per_level=0.5,
                     startangle=90,
                     ax=None,
                     **wedgeprops):
    """
    Plot a nested pie chart. We ensure (through renormalisation) that the plot represents the nesting faithfully.
    :param dat: Dictionary, number of levels is the number of levels of nested segments.
    In each case, the keys should correspond to an entry of the facecolours dictionary.
    :param facecolours: Dictionary keyed according to dat, with values corresponding to colours.
    Any entry with a None value is omitted from the chart (i.e. a gap is left)
    :param legend_entries: If supplied, this is a dictionary containing the labels to add in a legend. If these are
    identical to the keys used, set this to 'same' (default)
    :param legend_loc: Controls legend placement.
    :param edgecolour:
    :param inner_radius: The radius at which the first level commences. Default is starting at the centre (0).
    :param width_per_level: The width of each level of segments.
    :param startangle:
    :param ax:
    :return:
    """
    if ax is None:
        fig = plt.figure(figsize=(6, 6))
        ax = fig.add_subplot(111)
        ax.set_aspect('equal')

    col = {}
    col.update(facecolours)

    dat_flat = dictionary.nested_dict_to_flat(dat, ordered=True)
    all_keys = []
    for k in dat_flat:
        for t in k:
            if t not in all_keys:
                all_keys.append(t)
    # all_keys = setops.reduce_union(*dat_flat.keys())

    # link keys to their level
    key_level = {}
    for k in dat_flat:
        for i, t in enumerate(k):
            if t not in key_level:
                key_level[t] = i

    # generate a null key
    null_key = generate_null_key(all_keys)

    # the null key shouldn't be plotted
    col[null_key] = None

    n_levels = max(len(k) for k in dat_flat)
    # wherever the levels are unequal, pad to the maximum number
    for k in dat_flat:
        if len(k) < n_levels:
            curr = dat_flat.pop(k)
            new_key = k + tuple([null_key] * (n_levels - len(k)))
            dat_flat[new_key] = curr
            # generate a new null key
            null_key = generate_null_key(all_keys)
            col[null_key] = None

    # reform nested dict
    dat_n = dictionary.flat_dict_to_nested(dat_flat, ordered=True)

    # get the total for each level, ascending the hierarchy
    level_totals = get_level_totals(dat_n)

    # separate segments by level
    dat_flat_n = dictionary.nested_dict_to_flat(dat_n, ordered=True)

    wedgeprops = dict(wedgeprops)
    wedgeprops['width'] = width_per_level

    # plot
    patches = []
    texts = []
    curr_radius = inner_radius
    parent_keys = None

    for i in range(0, n_levels):
        if parent_keys is None:
            child_keys = dat_n.keys()
        else:
            child_keys = []
            for k1 in parent_keys:
                for k2 in dat_flat_n.keys():
                    if (k2[i - 1] == k1) and (k2[i] not in child_keys):
                        child_keys.append(k2[i])

        this_values = [level_totals[k] for k in child_keys]
        this_cols = [col[k] for k in child_keys]
        ps, ts = ax.pie(this_values,
                        colors=this_cols,
                        radius=curr_radius + width_per_level,
                        wedgeprops=wedgeprops,
                        startangle=startangle)
        curr_radius += width_per_level
        # set segments invisible where required
        this_patches = {}
        this_texts = {}
        for p, c, k, t in zip(ps, this_cols, child_keys, ts):
            if c is None:
                p.set_visible(False)
            this_patches[k] = p
            this_texts[k] = t

        patches.append(this_patches)
        texts.append(this_texts)

        parent_keys = child_keys

    if legend_entries is not None:
        if legend_entries == 'same':
            legend_entries = collections.OrderedDict([(k, k)
                                                      for k in all_keys])
        legend_dict = collections.OrderedDict()
        for k, txt in legend_entries.items():
            if k in key_level:
                lvl = key_level[k]
                if k not in patches[lvl]:
                    continue
                ## TODO: is there an easier way to generate these kwargs automatically?
                legend_dict[k] = {
                    'class': 'patch',
                    'facecolor': patches[lvl][k].get_facecolor(),
                    'edgecolor': patches[lvl][k].get_edgecolor(),
                    'linewidth': patches[lvl][k].get_linewidth(),
                }
        legend_dict = {'': legend_dict}
        common.add_custom_legend(ax, legend_dict, loc=legend_loc)

    ax.set_xlim(curr_radius * 1.1 * np.array([-1, 1]))

    return {'ax': ax, 'patches': patches, 'texts': texts}
Exemplo n.º 4
0
def line_plot_pvalues_slope(
    pvals,
    slopes,
    cmap=plt.get_cmap('Reds'),
    alpha=None,
    log_scale=True,
    vmin=None,
    vmax=None,
):
    """
    Plot summarising pvalue and correlation slope simultaneously using position (slope) and colour (pval).
    Hard coded into vertical orientation (TODO: make this a parameter??)
    :param pvals: DataFrame. Index and columns will be used for ticklabels. Suggest using -log10
    :param slopes: DataFrame, must match pvals
    :param pct_to_size_func:
    :param cmap: cmap used to represent
    :param alpha: If supplied, highlight all results with p < alpha.
    :param log_scale: If True (default), convert p values to -log10 scale.
    :param vmin: For pvalue shading. If not supplied, min of data will be used
    :param vmax: For pvalue shading. If not supplied, max of data will be used
    :return:
    """
    if sorted(pvals.index) != sorted(slopes.index):
        raise AttributeError("Index of pvals and concords must match")
    if sorted(pvals.columns) != sorted(slopes.columns):
        raise AttributeError("Columns of pvals and concords must match")

    if alpha is None:
        alpha = -1.

    signif = pvals < alpha

    if log_scale:
        pvals = -np.log10(pvals.astype(float))

    slopes = slopes.loc[pvals.index, pvals.columns]

    if vmin is None:
        vmin = pvals.values.min()
    if vmax is None:
        vmax = pvals.values.max()

    ny, nx = pvals.shape
    markers = common.get_best_marker_map(nx)

    gs = plt.GridSpec(nrows=2, ncols=1, height_ratios=[1, 19])
    fig = plt.figure(figsize=(1.5 * nx, .5 * ny))
    ax = fig.add_subplot(gs[1])
    ax.invert_yaxis()

    cax = fig.add_subplot(gs[0])

    plogp_norm = colors.Normalize(vmin=vmin, vmax=vmax)
    plogp_sm = plt.cm.ScalarMappable(cmap=cmap, norm=plogp_norm)

    for i, col in enumerate(slopes.columns):
        ew = [1.5 if t else 0.7 for t in signif[col].values]
        ax.scatter(slopes[col],
                   range(ny),
                   c=[plogp_sm.to_rgba(t) for t in pvals[col].values],
                   s=60,
                   edgecolor='k',
                   linewidths=ew,
                   marker=markers[i])

    ax.set_xlabel('Slope', fontsize=12)
    ax.set_yticks(range(ny))
    ax.set_yticklabels(pvals.index, fontsize=12)
    # ax.set_xlim([50, 100])
    plogp_sm.set_array(pvals)
    fig.colorbar(plogp_sm, cax=cax, orientation='horizontal')
    cax.xaxis.set_label_position('top')
    cax.set_xlabel(r'$-\log_{10}(p)$')

    type_attrs = {
        'class': 'line',
        'linestyle': 'none',
        'markeredgecolor': 'k',
        'markeredgewidth': 1.,
        'markerfacecolor': 'none',
        'markersize': 8
    }

    leg_dict = {}
    for i, col in enumerate(pvals.columns):
        leg_dict[col] = dict(type_attrs)
        leg_dict[col].update({'marker': markers[i]})

    common.add_custom_legend(ax,
                             leg_dict,
                             loc_outside=True,
                             loc_outside_horiz='right')
    gs.update(left=0.2, bottom=0.1, right=0.72, top=0.95, hspace=0.12)

    return {'fig': fig, 'ax': ax, 'gs': gs, 'cax': cax}
Exemplo n.º 5
0
    leg_type_dict['FFPE']['marker'] = '^'
    leg_type_dict['GIC']['marker'] = 'o'

    for k in tax_dict:
        this_leg_dict = collections.OrderedDict()
        # FIXME: we use the actual results to determine subgroup earlier, here it's hardcoded
        for pid in subgroups[k]:
            t = dict()
            t.update(leg_kwargs)
            t['facecolor'] = cmap[pids.index(pid)]
            this_leg_dict[pid] = t
        if k == tax_dict.keys()[-1]:
            leg_dicts[k] = collections.OrderedDict()
            leg_dicts[k]['#1'] = this_leg_dict
            leg_dicts[k]['#2'] = leg_type_dict
        else:
            leg_dicts[k] = this_leg_dict
        common.add_custom_legend(
            tax_dict[k].ax,
            leg_dicts[k],
            loc_outside=True,
            loc_outside_horiz='right',
            loc_outside_vert='top',
            bbox_to_anchor=(0.7, 1.),
            frameon=False
        )

    fig.subplots_adjust(left=0.0, right=.95, wspace=0.)
    fig.savefig(os.path.join(outdir, 'ternary_plot_classification.png'), dpi=200)
    fig.savefig(os.path.join(outdir, 'ternary_plot_classification.tiff'), dpi=200)
def plot_biplot(dat,
                meta,
                dims,
                scatter_colours,
                scatter_markers,
                annotate_features_radius=None,
                annotate_features_quantile=None,
                adjust_annotation=True,
                adjust_annotation_kwargs=None,
                **kwargs):
    """
    :param dat:
    :param meta: pd.DataFrame, must have columns entitled `type` and `patient_id`
    :param dims:
    :param scatter_colours:
    :param scatter_markers:
    :param annotate_features_radius: If supplied, this is the biplot radius outside of which we annotate genes (by
    symbol).
    :param **kwargs: Passed to pca.biplot()
    :return:
    """
    if annotate_features_radius is not None and annotate_features_quantile is not None:
        raise AttributeError(
            "Supply EITHER annotate_features_radius OR annotate_features_quantile."
        )

    if annotate_features_quantile is not None:
        assert 0 < annotate_features_quantile < 1, "annotate_features_quantile must be between 0 and 1 (not inclusive)."

    if adjust_annotation_kwargs is None:
        adjust_annotation_kwargs = {}

    sample_colours = meta.patient_id.map(scatter_colours.get).to_dict()
    sample_markers = meta.type.map(scatter_markers.get).to_dict()

    res = pca.biplot(dat,
                     plot_dims=dims,
                     sample_colours=sample_colours,
                     sample_markers=sample_markers,
                     **kwargs)

    sample_x, sample_y = res['sample_data']
    feat_x, feat_y = res['feature_data']
    ax = res['ax']
    fig = res['fig']

    typ_ix, typ = meta.type.factorize()

    # connect patients
    for pid in meta.patient_id.unique():
        ix = meta.patient_id == pid
        for t0, t1 in itertools.combinations(typ, 2):
            # draw all possible connections between these two cell types (in one direction only)
            ix0 = meta.index[ix & (meta.type == t0)]
            ix1 = meta.index[ix & (meta.type == t1)]

            for a, b in itertools.product(ix0, ix1):
                ax.plot([
                    sample_x[meta.index == a][0], sample_x[meta.index == b][0]
                ], [
                    sample_y[meta.index == a][0], sample_y[meta.index == b][0]
                ],
                        lw=1.5,
                        color=scatter_colours[pid],
                        zorder=9)

    # custom legend outside of plot
    line_kwargs = {
        'class': 'line',
        'markerfacecolor': 'none',
        'markeredgecolor': 'k',
        'markeredgewidth': 1.0,
        'linestyle': 'none'
    }
    patch_kwargs = {'class': 'patch', 'edgecolor': 'k', 'linewidth': 1.}
    legend_dict = {
        'Patient': collections.OrderedDict(),
        'Cell type': collections.OrderedDict()
    }
    for pid in consts.PIDS:
        ll = dict(patch_kwargs)
        ll['facecolor'] = scatter_colours[pid]
        legend_dict['Patient'][pid] = ll
    for t in typ:
        pp = dict(line_kwargs)
        pp['marker'] = scatter_markers[t]
        legend_dict['Cell type'][t] = pp

    res['legend_dict'] = legend_dict

    common.add_custom_legend(ax, legend_dict, loc_outside=True)
    fig.tight_layout()
    fig.subplots_adjust(right=0.8)

    selected = None

    if annotate_features_radius is not None:
        # annotate most influential genes
        selected = pca.highlight_biplot_features(feat_x, feat_y,
                                                 annotate_features_radius, ax)

    if annotate_features_quantile is not None:
        rad = (feat_x**2 + feat_y**2)**.5
        cut = sorted(rad)[int(len(rad) * annotate_features_quantile)]
        selected = rad >= cut

    if selected is not None:
        genes_selected = dat.index[selected]
        symbols_selected = reference_genomes.ensembl_to_gene_symbol(
            genes_selected)

        # add gene symbol annotations
        text_handles = []
        for ix, gs in zip(np.where(selected)[0], symbols_selected):
            if not pd.isnull(gs):
                text_handles.append(
                    ax.text(feat_x[ix], feat_y[ix], gs, zorder=10))
        # rearrange them to avoid overlaps
        if adjust_annotation:
            adjuster.adjust_text_radial_plus_repulsion(
                text_handles, **adjust_annotation_kwargs)

    return fig, ax, res
Exemplo n.º 7
0
                            figsize=(5.5, 6.5))
    big_ax = common.add_big_ax_to_subplot_fig(fig)
    for i, pw in enumerate(pw_to_keep):
        ax = axs[i]
        ax.bar(range(len(pids)),
               df.loc[df.pathway == pw, 'z'],
               color=[patient_colours[pid] for pid in pids],
               edgecolor='k',
               linewidth=0.8)
        ax.set_title(pw)
        ax.set_ylim([-6, 6])
        ax.axhline(0, c='k', linestyle='--', alpha=0.6, linewidth=0.8)
        ax.xaxis.set_ticks(range(len(pids)))

    axs[-1].xaxis.set_ticklabels(pids, rotation=90)
    axs[-1].set_xlim([-.5, len(pids) - .5])
    # add manual legend
    legend_dict = collections.OrderedDict([(pid, {
        'class': 'patch',
        'facecolor': patient_colours[pid],
        'edgecolor': 'k',
        'linewidth': 0.5
    }) for pid in pids])
    common.add_custom_legend(axs[0], {'': legend_dict},
                             loc_outside=True,
                             loc_outside_vert='top')
    big_ax.set_ylabel('IPA inferred z value')
    fig.subplots_adjust(bottom=0.06, left=0.1, right=0.8, top=0.95, hspace=0.3)
    fig.savefig(os.path.join(outdir, "selected_pathways_s1_ipa_z_values.png"),
                dpi=200)
Exemplo n.º 8
0
    outdir = output.unique_output_dir()

    pids = consts.PIDS

    # generate standalone legend
    legend_dict = collections.OrderedDict([
        (pid, {'class': 'patch', 'edgecolor': 'k', 'facecolor': patient_colours[pid], 'linewidth': 1.})
        for pid in pids
    ])
    legend_dict = {'': legend_dict}
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.axis('off')
    fig.set_facecolor('w')
    common.add_custom_legend(ax, legend_dict, loc='center')
    fig.set_size_inches((1.4, 2.4))
    fig.savefig(os.path.join(outdir, "cytoscape_legend.png"), dpi=200)
    fig.savefig(os.path.join(outdir, "cytoscape_legend.tiff"), dpi=200)

    # load exported pathways
    ipa_pathway_export_fn = os.path.join(HGIC_LOCAL_DIR, 'current/input_data/ipa_pathways/ipa_exported_pathways_symbols.csv')
    ipa_pathways = {}
    with open(ipa_pathway_export_fn, 'r') as f:
        c = csv.reader(f)
        for row in c:
            ipa_pathways[row[0].decode('utf-8')] = row[2:]  # the second entry is the 'anonymisation number' for sending to externals


    # Cytoscape session
    cy_session = cyto.CytoscapeSession()
Exemplo n.º 9
0
    def plot_legend(self, figsize=None):
        """
        Generate a figure showing the interpretation of the various colours / markers
        :return:
        """
        the_fig_kws = dict(self.fig_kws)
        if self.dmr_comparison_groups is None:
            if figsize is None:
                height = min(2., self.n_comparison_groups / 3.)
                figsize = (4., height)
            the_fig_kws['figsize'] = figsize
            fig = plt.figure(**the_fig_kws)
            # no legend
            gs = plt.GridSpec(nrows=2, ncols=1)
            dm_ax = fig.add_subplot(gs[0])
            de_ax = fig.add_subplot(gs[1])
            leg_ax = None
        else:
            figsize = (5.5, 2.)
            the_fig_kws['figsize'] = figsize
            fig = plt.figure(**the_fig_kws)
            gs = plt.GridSpec(nrows=2, ncols=2, width_ratios=[5, 1])
            dm_ax = fig.add_subplot(gs[0, 0])
            de_ax = fig.add_subplot(gs[1, 0])
            leg_ax = fig.add_subplot(gs[:, 1], frameon=False)
            leg_ax.tick_params(labelcolor='none',
                               top='off',
                               bottom='off',
                               left='off',
                               right='off')
            leg_ax.grid(False)

        de_vmin = self.de_vmin or -5
        de_vmax = self.de_vmax or 5
        dm_vmin = self.dm_vmin or -8
        dm_vmax = self.dm_vmax or 8

        for_heatmap = {
            'de': {
                'vmin': de_vmin,
                'vmax': de_vmax,
                'cmap': self.de_direction_colour,
                'ax': de_ax,
                'label': "DE log2(fold change)",
            },
            'dm': {
                'vmin': dm_vmin,
                'vmax': dm_vmax,
                'cmap': self.dm_direction_colour,
                'ax': dm_ax,
                'label': r"DM median $\Delta$M",
            },
        }

        heatmaps = {}
        for k, d in for_heatmap.items():
            if isinstance(d['cmap'], colors.LinearSegmentedColormap):
                the_cmap = d['cmap']
            else:
                the_cmap = colors.LinearSegmentedColormap.from_list(k, [
                    d['cmap'](t)
                    for t in np.linspace(d['vmin'], d['vmax'], 256)
                ],
                                                                    N=256)
            heatmaps[k] = d['ax'].pcolor(
                [np.linspace(d['vmin'], d['vmax'], 257)] * 2,
                [np.zeros(257), np.ones(257)],
                [np.linspace(d['vmin'], d['vmax'], 257)] * 2,
                cmap=the_cmap)
            d['ax'].yaxis.set_ticks([])
            d['ax'].set_xlabel(d['label'], fontsize=14)

        # custom legend (if we have the groups needed to plot it)
        leg = None
        hleg = None
        if self.dmr_comparison_groups is not None:
            all_groups = sorted(
                setops.reduce_union(
                    *(t.keys() for t in self.dmr_comparison_groups.values())))
            type_attrs = {
                'class': 'line',
                'linestyle': 'none',
                'markeredgecolor': 'k',
                'markeredgewidth': 1.,
                'markerfacecolor': 'none',
                'markersize': 20
            }

            leg_dict = {}
            for nm in all_groups:
                leg_dict[nm] = dict(type_attrs)
                leg_dict[nm]['markerfacecolor'] = self.colours.get(nm)
                leg_dict[nm]['marker'] = self.markers.get(nm)
                leg_dict[nm]['alpha'] = self.alpha.get(nm)
                leg_dict[nm]['markersize'] = self.size.get(nm)
            leg = common.add_custom_legend(leg_ax,
                                           leg_dict,
                                           loc='center',
                                           fontsize=14)
            hleg = leg_ax.get_legend()
            hleg.set_frame_on(False)

        gs.update(bottom=0.3,
                  top=0.98,
                  left=0.04,
                  right=0.95,
                  wspace=0.05,
                  hspace=2.)

        return {
            'fig': fig,
            'gs': gs,
            'legend_objects': leg,
            'legend': hleg,
            'heatmaps': heatmaps,
            'dm_ax': dm_ax,
            'de_ax': de_ax,
            'leg_ax': leg_ax
        }
        for j in sorted(cmap):
            if cmap[j] in row_colours_all[k].values:
                this_leg_dict[j] = dict(leg_entry)
                this_leg_dict[j].update({'facecolor': cmap[j]})
        leg_dict[k] = this_leg_dict

    gc = clustering.plot_clustermap(this_dat,
                                    cmap='RdYlBu_r',
                                    metric='correlation',
                                    col_colors=row_colours_all,
                                    col_linkage=lkg,
                                    vmin=-10,
                                    vmax=10)
    common.add_custom_legend(gc.ax_heatmap,
                             leg_dict,
                             loc_outside=True,
                             fontsize=14,
                             frameon=False)
    gc.fig.set_size_inches((9.4, 8.5))
    # reduce cbar ticks
    cbar = gc.ax_heatmap.collections[0].colorbar
    cbar.set_ticks([-8, 0, 8])

    plt.setp(gc.cax.yaxis.get_ticklabels(), fontsize=12)
    plt.setp(gc.ax_col_colors.yaxis.get_ticklabels(), fontsize=12)
    gc.cax.yaxis.set_ticks_position('left')
    gc.cax.set_title('M value')

    # move the legend down a little
    leg = gc.ax_heatmap.get_legend()
    bb = leg.get_bbox_to_anchor().inverse_transformed(leg.axes.transAxes)
Exemplo n.º 11
0
        legend_dict['Hyper'][k] = {
            'class': 'patch',
            'facecolor': patches[1][k].get_facecolor(),
            'edgecolor': patches[1][k].get_edgecolor(),
            'linewidth': patches[1][k].get_linewidth(),
        }
    for k in to_plot['Hypomethylated'].keys()[::-1]:
        legend_dict['Hypo'][k] = {
            'class': 'patch',
            'facecolor': patches[1][k].get_facecolor(),
            'edgecolor': patches[1][k].get_edgecolor(),
            'linewidth': patches[1][k].get_linewidth(),
        }

    common.add_custom_legend(axs[int(len(pids) / 2.)][-1],
                             legend_dict,
                             loc_outside=True)
    # gs.update(left=0.1, right=0.99, bottom=0.03, top=0.9, hspace=0.3, wspace=0.3)

    main_fig_bounds = {'left': 0.1, 'bottom': 0.02, 'top': 0.9, 'right': 0.72}
    gs.update(hspace=0.06, wspace=0.06, **main_fig_bounds)
    big_ax.set_position([
        main_fig_bounds['left'],
        main_fig_bounds['bottom'],
        main_fig_bounds['right'] - main_fig_bounds['left'],
        main_fig_bounds['top'] - main_fig_bounds['bottom'],
    ])
    fig.savefig(os.path.join(outdir,
                             "dmr_direction_effect_size_pie_array.png"),
                dpi=200)
Exemplo n.º 12
0
        'class': 'patch',
        'edgecolor': 'none',
        'linewidth': 1.5,
        'facecolor': x
    }

    leg_dict = collections.OrderedDict()
    leg_dict['#colours'] = collections.OrderedDict()
    leg_dict['#pvalue'] = collections.OrderedDict()
    leg_dict['#colours']['All'] = leg_entry_fun('0.3')
    leg_dict['#colours']['Favourable'] = leg_entry_fun(outcome_colours['fav'])
    leg_dict['#colours']['Unfavourable'] = leg_entry_fun(
        outcome_colours['unfav'])
    leg_dict['#pvalue']['p < %.2f' % p_threshold] = leg_entry_fun('none')
    leg_dict['#pvalue']['p < %.2f' % p_threshold]['edgecolor'] = 'k'
    common.add_custom_legend(ax, leg_dict, fontsize=14, loc='lower right')

    fig.tight_layout()
    fig.savefig(os.path.join(outdir, 'linregress_value_with_age_bar.png'),
                dpi=200)
    fig.savefig(os.path.join(outdir, 'linregress_value_with_age_bar.pdf'))

    jitter = 0.2
    figsize = (9.7, 6.4)
    if nvar % 2 == 0:
        fig, axs = plt.subplots(nrows=2,
                                ncols=nvar / 2,
                                sharex=True,
                                figsize=figsize)
    else:
        fig, axs = plt.subplots(nrows=2,
Exemplo n.º 13
0
def plot_clustermap(obj,
                    quantile_norm,
                    method='average',
                    metric='correlation',
                    n_gene_by_mad=5000,
                    n_gene_for_heatmap=500,
                    fmin=0.05,
                    fmax=0.95,
                    eps=0.01,
                    cell_line_colours=None):
    if cell_line_colours is None:
        cell_line_colours = {
            'FB': '#fff89e',  # yellow
            'GBM (this study)': '#e6e6e6',  # light gray
            'GBM': '#4d4d4d',  # dark grey
            'ESC': '#ff7777',  # light red
            'iPSC': '#990000',  # dark red
            'iPSC (this study)': '#fdc086',  # orange
            'NSC': '#006600',  # dark green
            'iNSC (this study)': '#7fc97f',  # green
        }

    the_dat = np.log2(obj.data + eps)

    if quantile_norm is not None:
        the_dat = transformations.quantile_normalisation(the_dat,
                                                         method=quantile_norm)
    the_mad = transformations.median_absolute_deviation(the_dat).sort_values(
        ascending=False)
    cc, st, leg_dict = construct_colour_array_legend_studies(obj.meta)

    # linkage
    lkg = hc.linkage(
        the_dat.loc[the_mad.index[:n_gene_by_mad]].transpose(),
        method=method,
        metric=metric,
    )

    # ref line colours
    for k, v in cell_line_colours.items():
        cc.loc[obj.meta.type == k, 'Cell type'] = v
    # our line colours
    cc.loc[obj.meta.batch.str.contains('wtchg') & (obj.meta.type == 'iPSC'), 'Cell type'] = \
    cell_line_colours['iPSC (this study)']

    # get appropriate clims
    the_dat = the_dat.loc[the_mad.index[:n_for_heatmap]]
    the_dat_flat = np.sort(the_dat.values.flatten())
    vmin = the_dat_flat[int(len(the_dat_flat) * fmin)] - 0.5
    vmax = the_dat_flat[int(len(the_dat_flat) * fmax)] + 0.5

    gc = clustering.plot_clustermap(
        the_dat.loc[the_mad.index[:n_gene_for_heatmap]],
        cmap='RdBu_r',
        col_linkage=lkg,
        col_colors=cc,
        vmin=vmin,
        vmax=vmax,
    )

    leg_entry = {
        'class': 'patch',
        'edgecolor': 'k',
        'linewidth': 1.,
    }
    leg_dict2 = collections.OrderedDict()
    leg_dict2['Cell type'] = collections.OrderedDict()

    for k in sorted(cell_line_colours):
        if k.replace(' (this study)', '') in obj.meta.type.unique():
            leg_dict2['Cell type'][k] = dict(leg_entry)
            leg_dict2['Cell type'][k].update(
                {'facecolor': cell_line_colours[k]})

    leg_dict2['Study'] = {}
    for k, v in leg_dict['Study'].items():
        leg_dict2['Study'][k] = dict(leg_entry)
        leg_dict2['Study'][k].update({'facecolor': v})

    common.add_custom_legend(gc.ax_heatmap,
                             leg_dict2,
                             loc_outside=True,
                             fontsize=14)
    format_clustermap(gc)

    return gc
Exemplo n.º 14
0
    lkg = plt_dict['linkage']
    leg_dict = collections.OrderedDict()
    leg_dict['Type'] = collections.OrderedDict()
    for k in sorted(descriptor_colours):
        leg_dict['Type'][k] = dict(leg_entry)
        leg_dict['Type'][k].update({'facecolor': descriptor_colours[k]})
    leg_dict['Patient'] = collections.OrderedDict()
    for k in sorted(pid_colours):
        leg_dict['Patient'][k] = dict(leg_entry)
        leg_dict['Patient'][k].update({'facecolor': pid_colours[k]})

    common.add_custom_legend(
        plt_dict['col_colour_ax'],
        leg_dict,
        loc_outside=True,
        loc_outside_horiz='right',
        loc_outside_vert='top'
        if clustering_metric == 'correlation' else 'bottom',
        bbox_to_anchor=(7.5, 1. if clustering_metric == 'correlation' else 0.),
        frameon=False)
    gs = plt_dict['gridspec']
    gs.update(right=0.5, bottom=0.1)
    plt_dict['dendrogram_ax'].set_frame_on(False)
    plt_dict['fig'].savefig(os.path.join(
        outdir, "all_samples_dendrogram_%d_probes_%s.png" %
        (clust_n_ftr, clustering_metric)),
                            dpi=200)

    # Again, again! But with only 019 samples this time
    row_colours = row_colours_all.loc[mdat_019.columns, ['Type']]
    plt_dict = hc_plot_dendrogram(mdat_019,
Exemplo n.º 15
0
                          'iNSC',
                          horizontalalignment='center',
                          verticalalignment='center',
                          transform=cg.ax_col_colors.transAxes)

    cg.ax_col_colors.text(0.75,
                          0.5,
                          'GIC',
                          horizontalalignment='center',
                          verticalalignment='center',
                          transform=cg.ax_col_colors.transAxes)

    # add custom legend
    leg_ax = cg.ax_col_dendrogram
    leg_dict = dict([(k, {
        'class': 'patch',
        'facecolor': function_colours[k],
        'edgecolor': 'k',
        'linewidth': 1.
    }) for k in function_to_gene])
    leg_dict = {'': leg_dict}
    leg = common.add_custom_legend(leg_ax, legend_dict=leg_dict)
    leg_ax.legend(loc='upper center', bbox_to_anchor=(0.6, 1.7), handles=leg)

    cg.savefig(os.path.join(outdir,
                            "gag_genes_logtpm_clustermap_row_by_function.png"),
               dpi=200)
    cg.savefig(os.path.join(
        outdir, "gag_genes_logtpm_clustermap_row_by_function.tiff"),
               dpi=200)