#!/usr/bin/env python

from pyPheWAS.pyPhewasCore import *
import sys, os

optargs = {
	'--path':'path',
	'--feature_matrix':'feature_matrix',
	'--covariates':'covariates',
	'--reg_type':'reg_type',
	'--group':'groupfile',
	'--outfile':'outfile'
}

args = sys.argv[1:]

# Define 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']),'')

# 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'])
str_reg_type = kwargs['reg_type']
kwargs['reg_type'] = regression_map[kwargs['reg_type']]

# Assert that valid files were given
assert kwargs['feature_matrix'].endswith('.csv'), "%s is not a valid phenotype file, must be a .csv file" % (kwargs['feature_matrix'])
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'] = "regressions_" + 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)

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

print("Retrieving feature matrix...")
fm = np.loadtxt(path + feature_matrix, delimiter=',')

print("Running PheWAS regressions...")
regressions = run_phewas(fm, genotypes,covariates, reg_type)

print("Saving regression data to %s" % (path + outfile))
header = ','.join(['str_reg_type', str_reg_type, 'group', groupfile]) + '\n'
f = open(path + outfile, 'w')
f.write(header)
regressions.to_csv(f)
f.close()