#!/usr/bin/env python

import argparse
import pandas as pd
from pyphe import growthcurves
from pyphe.analysis import check_mkdir
from os import path

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Welcome to pyphe-growthcurves, part of the pyphe toolbox. Written by stephan.kamrad@crick.ac.uk and maintained at https://github.com/Bahler-Lab/pyphe.')
    
    parser.add_argument('--input', type=str, required=True, help='Path to the growth curve file to analyse. This file contains one growth curve per column. The first column must be the timepoints and there must be a header row with unique identifiers for each curve.')
    
    parser.add_argument('--fitrange', type=int, default=4, help='Number of timepoint over which to fit linear regression. Defaults to 4. Please adjust this to the density of your timepoints and use higher values for more noisy data.')
    parser.add_argument('--lag-method', type=str, choices=['abs', 'rel'], default='rel', help='Method to use for determining lag. "abs" will measure time until the defined biomass threshold is crossed. "rel" will fist determine the inital biomass and measure the time until the biomass has passed this value times the threshold. Defaults to "rel".')
    parser.add_argument('--lag-threshold', type=float, default=2.0, help='Threshold to use for determining lag. With method "abs", this will measure time until the defined biomass threshold is crossed. With "rel" will fist determine the inital biomass and measure the time until the biomass has passed this value times the threshold. Defaults to 2.0, so with method "rel", this will measure the time taken for the first doubling.')
    parser.add_argument('--t0-fitrange', type=int, default=3, help='Specify the number of timepoint to use at the beginning of the growth curve to determine the initial biomass by averaging them. Defaults to 3.')
    parser.add_argument('--plots', default=False, action='store_true', help='Set this option (no argument required) to produce a plot of all growthcurves as pdf.')
    parser.add_argument('--plot-ylim', type=float, help='Specify the upper limit of the y-axis of growth curve plots. Useful if you want curves to be directly comparable. If not set, the axis of each curve is scaled to the data.')
    parser.add_argument('--out', type=str, default='.', help='Folder to save result files in. Result files have the same name as the input file with _results.csv appended.')
    
    args = parser.parse_args()
    
    #Import the data and perform some basic checks
    gdata = pd.read_csv(args.input, index_col=0)
    try: 
        gdata.index = gdata.index.map(float)
    except Exception as eo:
        print('The first column must contain the timepoint and these must only have numeric values (no units or other string).')
    assert all(gdata.index[i] <= gdata.index[i+1] for i in range(len(gdata.index)-1)), 'Timepoints must be in ascending order.'
    
    outdir = args.out.strip('/').strip('\\')
    in_baseStr = '.'.join(path.split(args.input)[1].split('.')[:-1])
    
    check_mkdir(outdir)
    result = growthcurves.analyse_growthcurve(gdata, args.fitrange, args.t0_fitrange, args.lag_method, args.lag_threshold, args.plots, args.plot_ylim, outdir, in_baseStr)
      
    result.to_csv(outdir + '/' + in_baseStr + '_results.csv')
    
    print('Analysis done: %s'%args.input)

    