#!/usr/bin/env python

import sys, os, time, math
from JJnetstat import stataggregate
from JJnetstat import plothelper as nxplot
    
if __name__ == "__main__":
    date = time.strftime('%Y%m%d')

    #
    # set_id |           name            |                                                                  description                                                                
    # 1 | mouse_human_all           | set_id 79, all annotated family members from NC paper
    # 2 | mouse_human_all-kin       | set_id 79, all annotated family members-KIN from NC paper
    # 106 | ppod_all_may09            | PPOD complete (5 May 2009) dataset.
    # 109 | ppod_human_mouse          | PPOD (5 May 2009) dataset, Mouse and Human genes only
    # 110 | ppod_human_mouse_families | PPOD (5 May 2009 dataset, Mouse and Human annotated family members only
    # 111 | ppod_scerevisiae          | PPOD (5 May 2009) dataset, Yeast genes only
    # 112 | ppod_human                | PPOD (5 May 2009) dataset, Human genes only

    # NC 808 is a correct, including LOG, run of the 48 genomes
    #  nc_id | br_id |            date            | e_thresh | nc_thresh |       smin       | smin_factor | use_symmetric | score_type | self_hits | bit_thresh | blast_hit_limit 
    # 808 |   102 | 2011-07-21 17:57:30.690078 |       -1 |      0.05 | 31.7911685531896 |        0.95 | t             | bit_score  |         1 |            |

    #  br_id | set_id |            date            | num_sequences | num_residues |                                                                                      params         
    # 102 |    106 | 2010-01-08 14:50:37.378723 |        603991 |    267551576 | {'descriptions':0, 'alignments':num_sequences, 'expectation':num_sequences*10, 'search_length':num_r ...

    #(set_id, name) = (112, 'ppod_human')
    (set_id, name) = (109, 'ppod_human_mouse')
    
    #(stype, run_id, set_id_filter, symmetric, 'descr')
    networks = [ ('bit_score', 102, set_id, True, name),
                 ('nc_score', 808, set_id, True, name),
                 #('nc_score', 698, 2, 'hm_all-kin'),
                 #('bit_score', 75, 2, 'hm_all-kin'),
                 ]
    thresholds = { 'nc_score': (0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35,
                                0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7,
                                0.75,0.8, 0.85, 
                                0.9, 0.95, 0.99
                                ),
                   'bit_score': (29, 35, 40, 50, 60, 70, 80,
                                 90, 100, 125, 150, 175, 200, 225, 250,
                                 275, 300, 325, 350, 375, 400, 425, 450, 475,
                                 500, 600, 700, 800, 900, 1000
                                 )
                   }

    # Calculate statistics for each network
    net_set = stataggregate.stat_set_debug( networks, thresholds)
    #net_set = stataggregate.stat_set_pp( networks, thresholds)
    net_set.calculate_statistics( omit_spl=True, cacheq=True, verbose=False)

        
    g_plot = nxplot.nxplot( net_set.net_stat)
    ftype = 'pdf'

    equiv_density_trans = g_plot.build_equiv_transform( 'graph_density',
                                                        equiv_runs=[('bit_score', 102, set_id, True),
                                                                    ('nc_score', 808, set_id, True)])

    runs = net_set.net_stat.get_runs( stype_run_id=('bit_score', 102, set_id, True))
    runs += net_set.net_stat.get_runs( stype_run_id=('nc_score', 808, set_id, True))
    
    # Draw plots
    for stat_name in ('ccomp_num', 'graph_mean_cc', 'ccomp_avg_density_w',
                      'graph_density',
                      'ccomp_num_single',
                      'ccomp_avg_size', 
                      # 'ccomp_avg_spl'
                      ):

        if stat_name=='ccomp_num': legend_position='bottom'
        else: legend_position='bottomright'

        g_plot.plot_statistic( stat_name, ftype=ftype, fprefix='figures/%s_%s' % (date, name),
                               base_stype='nc_score',
                               top_transform=equiv_density_trans, absolute=True,
                               runs=runs, draw_title=False,
                               #legend_position=legend_position, 
                               #lwd=2,
                               ylab=False,
                               legend_position='topright',
                               draw_legend= stat_name=='ccomp_avg_size',
                               ylog= stat_name=='ccomp_avg_size',
                               colarr = ['orange', 'blue']
                               )
