#!/usr/bin/env python

"""[License: GNU General Public License v3 (GPLv3)]
 
 This file is part of FuMa.
 
 FuMa is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 FuMa is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program. If not, see <http://www.gnu.org/licenses/>.

 Documentation as defined by:
 <http://epydoc.sourceforge.net/manual-fields.html#fields-synonyms>
"""

import sys,argparse

def convert(filename_in,fh_out,true_value,false_value):
	header = False
	
	# Offset for left-genes, right-genes and large gene spanning
	column_offset = 3
	
	with open(filename_in,"r") as fh:
		for line in fh:
			line = line
			params = line.strip("\n").split("\t")
			
			if(header == False):
				header = len(params)-column_offset
				fh_out.write(line)
			else:
				fh_out.write("\t".join(params[0:column_offset]))
				for i in range(column_offset,header+column_offset):
					if(len(params[i]) > 0):
						fh_out.write("\t"+true_value)
					else:
						fh_out.write("\t"+false_value)
				fh_out.write("\n")


if __name__ == "__main__":
	parser = argparse.ArgumentParser()
	
	parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,epilog="For more info please visit:\n<https://github.com/yhoogstrate/fuma>")
	
	parser.add_argument("-t","--true_value",default="TRUE")
	parser.add_argument("-f","--false_value",default="FALSE")
	parser.add_argument("-o","--output",help="output filename; '-' for stdout",default="-")
	
	parser.add_argument("input_file",help="FuMa List output",nargs=1)
	
	args = parser.parse_args()
	
	convert(args.input_file[0],(sys.stdout if args.output == "-" else open(args.output,"w")),args.true_value,args.false_value)
