#!/usr/bin/env python3

import argparse
import pandas as pd

version='1.1.0'

parser = argparse.ArgumentParser(description="%s v%s: Bind the columns from different log outputs of findOligo, testOligo, testThorough, rateAccess and/or any other tab delimited table with corresponding identifiers." % ('%(prog)s', version), add_help=False)

# Add the arguments to the parser
requirArgs = parser.add_argument_group('Required arguments')
finputArgs = parser.add_argument_group('Optional arguments related to the input')
outputArgs = parser.add_argument_group('Optional arguments related to the output')
optionArgs = parser.add_argument_group('Other optional arguments')

requirArgs.add_argument("-f", "--files", dest="files_in", required=True, nargs="+",
						help="The files to be bound in the given order. Note that one column of each file should contain the exact same names to extract the information for the same row, but not necessarily in the same order. Columns with the same name will be automatically combined and not duplicated.")

requirArgs.add_argument("-o", "--output", dest="file_out", required=False,
						help="The name of the output file.")

finputArgs.add_argument("-i", "--identifier", dest="identifier", required=False, default="identifier",
						help="The column name to identify similar rows. Default=%(default)s.")

outputArgs.add_argument("-r", "--remove", dest="remove", required=False, action="store_true",
						  help="If selected, will delete the input files before exiting.")

outputArgs.add_argument("-d", "--drop", dest="drop", required=False, action="store_true",
						  help="If selected, will remove empty rows.")

optionArgs.add_argument("-v", "--verbose", dest="verbose", required=False, action="store_false",
						  help="If selected, will not print information to the console.")

optionArgs.add_argument("-h", "--help", action="help",
						  help="Show this help message and exit.")

optionArgs.add_argument("-V", "--version", action='version',
						  version='oligoN-design %s v%s' % ('%(prog)s', version),
						  help='Show the version number and exit.')

args = parser.parse_args()

# Reading input files ______________________________________________________________________________
if args.verbose:
	print("  Reading files:", *args.files_in)

out = pd.DataFrame()
first = True
for filei in args.files_in:
	f = pd.read_csv(filei, sep="\t")
	if first:
		out = f
		first = False
	else:
		out = pd.merge(out, f, how="left", on=args.identifier, suffixes=('', '_new'))
		out.drop(out.filter(regex='_new$').columns.tolist(), axis=1, inplace=True)

if args.drop:
	out = out.dropna()

# Exporting final dataframe ________________________________________________________________________
if args.verbose:
	print("  Exporting to:", args.file_out)

out.to_csv(args.file_out, sep="\t", index=False)

if args.remove:
	if args.verbose:
		print("  Deleting input files")
	import os
	for filei in args.files_in:
		os.remove(filei)

if args.verbose:
	print("Done")
