#!/usr/bin/env python

import time
from DurandDB import blastq
from matplotlib import pyplot


def plot_histogram(br_id=None, nc_id=None, stype=None, color='blue',
                   xlimit=None, ylimit=None, fprefix=None,
                   title=None, ylog=False,
                   set_id_filter=None, symmetric=False):

    histarr = bq.fetch_score_histogram(br_id=br_id, nc_id=nc_id, stype=stype,
                                       set_id_filter=set_id_filter,
                                       symmetric = symmetric)

    pyplot.ioff() # turn off figure updates
    pyplot.rc('figure', figsize=(8,5))

    fig = pyplot.figure()        
    ax = fig.add_subplot(111)
    #if xlog is not None:
    #    xlog = (stype == 'e_value')
    if stype == 'e_value': ax.set_xscale('log')
    if ylog: ax.set_yscale('log')

    fig.suptitle("%s\nScore Histogram: (%s, %s, %s)" % (title, stype, br_id, nc_id))

    # left positions
    left = [ a[0] for a in histarr ]
    width = [ a[1] - a[0] for a in histarr ]
    height = [ a[2] for a in histarr ]

    pyplot.bar( left, height, width, color=color, edgecolor=color)

    #pyplot.axis('tight')
    #if xlimit is not None: ax.set_xlim( *xlimit)
    #if ylimit is not None: ax.set_ylim( *ylimit)
    #else:
    #    pyplot.ylim( (min(height),max(height)) )

    # for whatever reason, bar() doesn't correctly handle the log scale range
    #if ylog and ylimit is not None:
    #    pyplot.ylim((0.5, max(height)))

    if stype == 'e_value':
        ax.invert_xaxis()

    ax.grid(color='gray', alpha=0.5)

    if stype=='e_value':
        xlabel='-log(e_value / dbsize + 1E-200)'
    else:
        xlabel=stype

    ax.set_xlabel(xlabel)
    ax.set_ylabel('Count')

    fig.subplots_adjust(bottom=0.1, left=0.1, top=0.90, right=0.98)

    if fprefix is not None:
        fname = fprefix + '_%s_%s_%s_%s.png' % (stype, br_id, nc_id, set_id_filter)
        fig.savefig( 'figures/%s' % fname)
        pyplot.close()
    else:
        fig.show()

    return (left, height, width)


if __name__ == "__main__":
    bq = blastq.blastq(cacheq=True)

    date = time.strftime('%Y%m%d')

    

    #plot_histogram( br_id=75, nc_id=698, stype='nc_score', color='blue',
    #                fprefix=date, title='Human_Mouse', xlimit=(-0.01,1.01))

    #plot_histogram( br_id=75, nc_id=698, stype='e_value', color='green',
    #                fprefix=date, title='Human_Mouse', ylog=True)

    #plot_histogram( br_id=87, nc_id=731, stype='nc_score', color='blue',
    #                fprefix=date, title='S_cer', xlimit=(-0.01,1.01))
    
    #plot_histogram( br_id=87, nc_id=731, stype='e_value', color='green',
    #                fprefix=date, title='S_cer', ylog=True)

    #plot_histogram( br_id=88, nc_id=732, stype='nc_score', color='blue',
    #                fprefix=date, title='Pre-post', xlimit=(-0.01,1.02))
    #plot_histogram( br_id=88, nc_id=732, stype='e_value', color='green',
    #                fprefix=date, title='Pre-post', ylog = True)

    #plot_histogram( br_id=89, nc_id=733, stype='nc_score', color='blue',
    #                fprefix=date, title='Post-post', xlimit=(-0.01,1.05))
    #plot_histogram( br_id=89, nc_id=733, stype='e_value', color='green',
    #                fprefix=date, title='Post-post', ylog=True)


    #plot_histogram( br_id=90, nc_id=734, stype='nc_score', color='blue',
    #                fprefix=date, title='Four_Species', xlimit=(-0.01,1.05))
    #plot_histogram( br_id=90, nc_id=734, stype='e_value', color='green',
    #                fprefix=date, title='Four_Species', ylog=True)

    #plot_histogram( br_id=91, nc_id=735, stype='nc_score', color='blue',
    #                fprefix=date, title='Nine_Species', xlimit=(-0.01,1.05))
    #plot_histogram( br_id=91, nc_id=735, stype='e_value', color='green',
    #                fprefix=date, title='Nine_Species', ylog=True)


    #for (set_id_filter, title) in ( (None, 'All'), (97, 'Human'), (98, 'Mouse'),
    #                                (99, 'Scer'), (100, 'Dmel'), (101, 'Arabidopsis')):
    #    plot_histogram( br_id=95, nc_id=736, stype='nc_score', color='blue',
    #                    fprefix=date, title='PPOD: ' + title, xlimit=(-0.01,1.05),
    #                    set_id_filter = set_id_filter)
    #    plot_histogram( br_id=95, nc_id=736, stype='e_value', color='green',
    #                    fprefix=date, title='PPOD: ' + title, ylog=True,
    #                    set_id_filter = set_id_filter)

    #plot_histogram( nc_id=746, stype='nc_score', color='blue',
    #                fprefix=date, title='Cluster->all', ylog=True)

    #plot_histogram( nc_id=750, stype='nc_score', color='blue',
    #                fprefix=date, title='Cluster->Cluster', ylog=True)

    #plot_histogram( nc_id=777, stype='nc_score', color='blue',
    #                fprefix=date, title='NC.  Non symmetric, compositional=2, 12 species (nc_id 777)', ylog=False)

    #plot_histogram( nc_id=779, stype='nc_score', color='blue',
    #                fprefix=date, title='NC.  Symmetric, compositional=2, 12 species (nc_id 779)', ylog=False)

    #plot_histogram( nc_id=780, stype='nc_score', color='blue',
    #                fprefix=date, title='NC.  Non symmetric, compositional=f, 12 species (nc_id 780)', ylog=False)

    #plot_histogram( nc_id=781, stype='nc_score', color='blue',
    #                fprefix=date, title='NC.  Symmetric, 12 species (nc_id 781)', ylog=True)

    #plot_histogram( br_id=105, stype='e_value', color='green',
    #                fprefix=date, title='BLAST.  Symmetric, 12 species (br_id 105)',
    #                symmetric = True, ylog=True)

    #plot_histogram( nc_id=808, stype='nc_score', color='blue',
    #                fprefix=date, title='NC.  Symmetric, 48 species (nc_id 808)', ylog=True)

    #plot_histogram( nc_id=816, stype='nc_score', color='blue',
    #                fprefix=date, title='NC.  Symmetric, 48 species (nc_id 816). bit_score>=100', ylog=True)

    #plot_histogram( nc_id=817, stype='nc_score', color='blue',
    #                fprefix=date, title='NC.  Symmetric, 48 species (nc_id 817). bit_score>=60', ylog=True)

    #plot_histogram( br_id=102, stype='bit_score', color='green',
    #                fprefix=date, title='BLAST.  Symmetric, 48 species (br_id 102)',
    #                symmetric = True, ylog=True)

    #plot_histogram( br_id=102, stype='e_value', color='green',
    #                fprefix=date, title='BLAST.  Not Symmetric, 48 species (br_id 102)',
    #                symmetric = False, ylog=True)

    #plot_histogram( br_id=90, stype='bit_score', color='green',
    #                set_id_filter=91,
    #                fprefix=date, title='BLAST.  Not Symmetric, (br_id 90, set 91)',
    #                symmetric = False,
    #                ylog=True)

    #plot_histogram( nc_id=731, stype='nc_score', color='blue',
    #                set_id_filter=91,
    #                fprefix=date, title='nc_id 731, br_id 89, set 91)',
    #                symmetric = False,
    #                ylog=False)

    plot_histogram( br_id=102, stype='bit_score', color='green',
                    fprefix=date, title='BLAST.  Symmetric, 48 genomes. (br_id 102)',
                    symmetric = True,
                    ylog=True)

    plot_histogram( br_id=102, stype='bit_score', color='green',
                    set_id_filter=109,
                    fprefix=date, title='BLAST.  Symmetric, Human and Mouse (br_id 102, set 109)',
                    symmetric = True,
                    ylog=True)

    plot_histogram( nc_id=808, stype='nc_score', color='blue',
                    fprefix=date, title='NC. 48 genomes. (nc_id 808)',
                    ylog=False)

    plot_histogram( nc_id=808, stype='nc_score', color='blue',
                    set_id_filter=109,
                    fprefix=date, title='NC. Human and Mouse (nc_id 808, set 109)',
                    ylog=False)
