#!/usr/bin/env python

from pyPheWAS.pyPhewasCorev2 import *
import sys, os
import datetime
import pandas as pd

optargs = {
	'--phenotype': 'phenotype_file',
	'--group':'genotype_file',
	'--path':'path',
	'--phenotypeout': 'final_pfile',
	'--eventcolumn':'ec',
	'--precision':'precision',
	'--type':'type',
	'--phenotype_column':'pcol'
}

args = sys.argv[1:]

# Define any default arguments
kwargs = {'path':'.','ec':'Event_date', 'precision':2,'type':'ICD','pcol':'icd9'}

kwargs = process_args(kwargs, optargs, *args)

# Change path to absolute path
kwargs['path'] = os.path.join(os.path.abspath(kwargs['path']),'')

print(kwargs)

# Assert that valid files were given
assert kwargs['phenotype_file'].endswith('.csv'), "%s is not a valid phenotype file, must be a .csv file" % (kwargs['phenotypes'])
assert kwargs['genotype_file'].endswith('.csv'), "%s is not a valid group file, must be a .csv file" % (kwargs['groups'])

# Assert that the output file is valid
assert kwargs['final_pfile'].endswith('.csv'), "%s is not a valid output file, must be a .csv file" % (kwargs['phenout'])

# Print Arguments
display_kwargs(kwargs)

# Fill paths
kwargs['phenotype_file'] = os.sep.join([kwargs['path'], kwargs['phenotype_file']])
kwargs['genotype_file'] = os.sep.join([kwargs['path'], kwargs['genotype_file']])

kwargs['final_pfile'] = os.sep.join([kwargs['path'], kwargs['final_pfile']])

# Make precision an integer
kwargs['precision'] = int(kwargs['precision'])

# Make all arguments local variables
locals().update(kwargs)

group = pd.read_csv(genotype_file)
phen = pd.read_csv(phenotype_file)
group['nDOB'] = pd.to_datetime(group['DOB'])
phen['nEvent_date'] = pd.to_datetime(phen[ec],infer_datetime_format=True)
df = pd.merge(group, phen, on='id')

century = datetime.timedelta(365.2425) * 100
df.loc[df['nEvent_date'] < df['nDOB'], 'nDOB'] = df[df['nEvent_date'] < df['nDOB']]['nDOB'] - century

df['AgeAt'+type] = (df['nEvent_date'] - df['nDOB']).astype('timedelta64[D]')/365.2425
df['AgeAt'+type] = df['AgeAt'+type].round(2)
df[['id', pcol, ec, 'AgeAt'+type]].to_csv(final_pfile, index=False)