#!/usr/bin/env python
import argparse
from pyphe.interpret import interpret 

if __name__ == '__main__':
    ###Set up parsing of command line arguments with argparse###
    parser = argparse.ArgumentParser(description='Welcome to pyphe-interpret, part of the pyphe toolbox. Written by stephan.kamrad@crick.ac.uk and maintained at https://github.com/Bahler-Lab/pyphe')
  
   
    parser.add_argument('--ld', type=str, required=True, help="Path to the Data Report Table produced by pyphe-analyse.")
    parser.add_argument('--out', type=str, default='pyphe-quantify-report', help='Specifies the path where to save the output data result. By default, a table with all replicates will be saved as pyphe-quantify-report_reps.csv and the statistic table will be saved as pyphe-quantify-report_summaryStats.csv in the current working directory. Existing files will be overwritten.')
    parser.add_argument('--condition_column', type=str, required=True, help='Name of the condition column in the data report. Data will be grouped by the strain_column and differences between all unique conditions in the condition_column and the control_condition will be tested for.')
    parser.add_argument('--strain_column', type=str, required=True, help='Name of the strain column in the data report. Data will be grouped by the strain_column and differences between all unique conditions in the condition_column and the control_condition will be tested for.')
    parser.add_argument('--values_column', type=str, default='Colony_size_corr_checked', help='Name of the column in the data report to use as fitness values. Defaults to "Colony_size_corr_checked".')
    parser.add_argument('--control_condition', type=str, required=True, help='Name of the control condition. Effect sizes and and p-values will conpare each condition agaisnt the control condition.')
    parser.add_argument('--ld_encoding', default='utf-8', type=str, help='Encoding of the data report table to be passed to pandas.read_csv().')
    parser.add_argument('--filter_circularity', type=float, default=None, help='Exclude colonies from the analysis with a circularity below the one specified. A circularity of 1 corresponds to a perfect circle. We recommend a threshold around 0.85.')
    parser.add_argument('--set_missing_na', action='store_true', default=False, help='Set 0-sized colonies to NA. This is recommended if you expect no missing colonies in your data, which means these are probably due to pinning errors.')

    args = parser.parse_args()

    #Run analysis
    print('Interpretation is starting, with following parameters:')
    for k, v in vars(args).items():
        print('%s: %s'%(k, str(v)))

    #Load ld
    ld = pd.read_csv(args.ld, index_col=0, ld_encoding=args.ld_encoding)

    interpret(ld, args.condition_column, args.strain_column, args.values_column, args.control_condition, args.out,circularity=args.circularity, set_missing_na=args.set_missing_na)
