#!/usr/bin/env python

from pyPheWAS.pyPhewasCorev2 import *
import sys, os

optargs = {
	'--phenotype': 'phenotypefile',
	'--group':'groupfile',
	'--path':'path',
	'--outfile': 'outfile',
	'--reg_type':'reg_type',
	'--phewas_cov':'phewas_cov'
}

"""
Retrieve and validate all arguments.
"""

args = sys.argv[1:]

# Define any default arguments
kwargs = {'outfile' : None, 'path':'.','phewas_cov':''}

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 a valid regression type was used
assert kwargs['reg_type'] in regression_map.keys(), "%s is not a valid regression type" % (kwargs['reg_type'])
kwargs['reg_type'] = regression_map[kwargs['reg_type']]

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

# Assign the output file if none was assigned
if kwargs['outfile'] == None:
	kwargs['outfile'] = "feature_matrix_" + kwargs['groupfile']

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

# Check phewas_cov
if kwargs['phewas_cov']:
	kwargs['phewas_cov'] = float(kwargs['phewas_cov'])

# Print Arguments
display_kwargs(kwargs)

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

# Perform the Phewas code lookup

print("Retrieving phenotype data...")
phenotypes = get_input(path, phenotypefile, reg_type)

print("Retrieving group data...")
genotypes = get_group_file(path, groupfile)

print("Generating feature matrix...")
fm = generate_feature_matrix(genotypes, phenotypes, reg_type, phewas_cov)

print("Saving feature matrix to %s" % (path + outfile))

np.savetxt(path + 'agg_measures_' + outfile, fm[0],delimiter=',')
np.savetxt(path + 'icd_age_' + outfile, fm[1],delimiter=',')
np.savetxt(path + 'phewas_cov_' + outfile, fm[2],delimiter=',')
