def plot_error_profiles_total(options, data, fieldsize):
    xpositions = np.arange(fieldsize)

    for sample, _ in options['profiles']:
        profile = data[sample][COLFORSTATS]

        fig = plt.figure(figsize=(3.5, 2.8))

        subst_freq = []
        ins_freq = []
        del_freq = []

        for fn in range(fieldsize):
            inserted = (profile[fn] * MASK_INS).sum()
            deleted = (profile[fn] * MASK_DEL).sum()
            substed = (profile[fn] * MASK_SUBST).sum()
            total = profile[fn].sum()

            subst_freq.append(substed / total * 100)
            ins_freq.append(inserted / total * 100)
            del_freq.append(deleted / total * 100)

        plt.plot(xpositions, subst_freq, label='subst', c=iwork_colors.yellow, linewidth=1.2)
        plt.plot(xpositions, ins_freq, label='ins', c=iwork_colors.red, linewidth=1.2)
        plt.plot(xpositions, del_freq, label='del', c=iwork_colors.blue, linewidth=1.2)

        ax = plt.gca()
        plt.setp(ax.get_xticklabels(), visible=False)
        plt.xlim(0, fieldsize-1)
        plt.xlabel('Position in tag')
        plt.ylabel('Frequency (percent)')
        plt.title(sample)

        box = ax.get_position()
        ax.set_position([box.x0 + 0.1, box.y0 + box.height * 0.2,
                         box.width * 0.9, box.height * 0.77])
        ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), ncol=3, columnspacing=0.5,
                  frameon=False, handletextpad=0.5)

        adjust_numbers_style(ax, xgrid=True)

        for format in PLOT_FORMATS:
            plt.savefig(options['plots'].format(format=format, type='total', sample=sample))

        plt.cla()
        plt.clf()
def plot_error_profiles_pertype(options, data, fieldsize, plottype):
    xpositions = np.arange(fieldsize)

    freqmask = {'del': MASK_DEL, 'subst': MASK_SUBST}[plottype]

    for sample, _ in options['profiles']:
        profile = data[sample][COLFORSTATS]

        fig = plt.figure(figsize=(3.5, 2.8))

        freqs = [[] for i in range(len(BASES) - 1)]

        for fn in range(fieldsize):
            profmasked = profile[fn] * freqmask

            for basei, freqtbl in enumerate(freqs):
                basereads = profile[fn, basei].sum()
                freqtbl.append(profmasked[basei].sum() / basereads)

        for base, freqtbl, color in zip(BASES, freqs, iwork_colors):
            plt.plot(xpositions, freqtbl, label=base, c=color, linewidth=1.2)

        ax = plt.gca()
        plt.setp(ax.get_xticklabels(), visible=False)
        plt.xlim(0, fieldsize-1)
        plt.xlabel('Position in tag')
        plt.ylabel('Frequency (percent)')
        plt.title(sample)

        box = ax.get_position()
        ax.set_position([box.x0 + 0.1, box.y0 + box.height * 0.2,
                         box.width * 0.9, box.height * 0.77])
        ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), ncol=4, columnspacing=0.5,
                  frameon=False, handletextpad=0.5)

        adjust_numbers_style(ax, xgrid=True)

        for format in PLOT_FORMATS:
            plt.savefig(options['plots'].format(format=format, type=plottype, sample=sample))

        plt.cla()
        plt.clf()
def write_plot(result, pdf=None, png=None):
    fig = plt.figure(figsize=(3.8, 2))

    plt.hist([r['z'] for r in result], bins=20, facecolor='#444444', rwidth=0.8)

    plt.xlabel('Enrichment (z)')
    plt.ylabel('Number of seq.')
    plt.axvline(1.644854, color='red') # top 0.05 line

    adjust_numbers_style(plt.gca())

    plt.tight_layout()

    if pdf is not None:
        plt.savefig(pdf, format='pdf')

    if png is not None:
        plt.savefig(png, format='png')

    plt.clf()
    plt.cla()
def plot_to_file(options, stats, outputs):
    figwidth = 2 + len(options['samples']) * len(options['plot_classes']) / 4

    fig = plt.figure(figsize=(figwidth, 3))

    ax = fig.add_subplot(1, 1, 1)

    class_names = options['plot_classes']
    xstep = 0.8 / len(options['samples'])
    xwidth = xstep * 0.8
    xcenterbase = np.arange(0, len(options['plot_classes']), 1)
    xleftbase = xcenterbase - 0.4

    for sampleno, sample in enumerate(options['samples']):
        name = sample['name']
        xlefts = xleftbase + xstep * sampleno
        data = [stats[cls][name] for cls in options['plot_classes']]
        plt.bar(xlefts, data, width=xwidth, facecolor=iwork_colors[sampleno],
                label=name, edgecolor='#666666', zorder=6)

    plt.xlim(-0.5, len(options['plot_classes']) - 0.5)
    plt.xticks(xcenterbase)
    ax.set_xticklabels(class_names)
    plt.setp(ax.get_xticklines(), visible=False)

    plt.legend(loc='best')
    plt.xlabel('Class')
    plt.ylabel('% Reads')

    if options['branding']:
        plt.annotate(options['branding'], (3, 3), fontsize=8, color='#888888',
            textcoords='figure points', horizontalalignment='left',
            verticalalignment='bottom')

    adjust_numbers_style(ax)
    plt.tight_layout()

    for output in outputs:
        plt.savefig(output)