#!/usr/bin/env python

from pyPheWAS.pyPhewasCore import *
import sys, os

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

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

args = sys.argv[1:]

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

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'])

# 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)

print("Saving feature matrix to %s" % (path + outfile))
np.savetxt(path + outfile, fm, comments='', delimiter=',', fmt='%1u')