#!/usr/bin/env python

from pyPheWAS.pyPhewasCorev2 import *
import sys, os

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

args = sys.argv[1:]

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

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

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

if kwargs['response'] == None:
	kwargs['response'] = ""
# Print Arguments
display_kwargs(kwargs)

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

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

a = np.loadtxt(path + 'agg_measures_' + feature_matrix, delimiter=',')
b = np.loadtxt(path + 'icd_age_' + feature_matrix, delimiter=',')
c = np.loadtxt(path + 'phewas_cov_' + feature_matrix, delimiter=',')
fm = np.array([a,b,c])

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

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