#processor.py
#version 2013.06.12

def processMutations(first_file, second_file , input_directory, output_file, output_directory, lines_skip):
	import methodslist as ml
	import xlwt
	import os
	
	# Set directory
	old_dir = os.getcwd()
	os.chdir(output_directory)
	
	print '\nStarting at %r' % input_directory

	#Import text to tables
	table1 = ml.importTable(first_file, input_directory, lines_skip)
	table2 = ml.importTable(second_file, input_directory, lines_skip)

	#Save rows with detected mutations
	keeps1 = ml.saveByValue(table1, 'judgement', 'KEEP')
	keeps2 = ml.saveByValue(table2, 'judgement', 'KEEP')

	#Find mutated rows with that are matched and unmatched
	total_comparison = ml.compareTables(keeps1, keeps2)
	'''
	0 is same_first_table
	1 is same_second_table
	2 is diff_first_table
	3 is diff_second_table

	'''
	matched_keeps1 = total_comparison[0]
	matched_keeps2 = total_comparison[1]
	unmatched_keeps1 = total_comparison[2]
	unmatched_keeps2 = total_comparison[3]
	
	#Save rows with no detected mutations
	rejects1 = ml.saveByValue(table1, 'judgement', 'REJECT')
	rejects2 = ml.saveByValue(table2, 'judgement', 'REJECT')

	#Compare unmatched KEEP rows of one table to REJECT rows of other table
	total_compare_reject2_unmatched1 = ml.compareTables(unmatched_keeps1, rejects2)
	matched_reject2_unmatched1 = total_compare_reject2_unmatched1[1] #1 is the second table
	total_compare_reject1_unmatched2 = ml.compareTables(unmatched_keeps2, rejects1)
	matched_reject1_unmatched2 = total_compare_reject1_unmatched2[1]

	#Select only certain categories
	keeps1 = ml.saveByCategory(keeps1)
	keeps2 = ml.saveByCategory(keeps2)
	matched_reject2_unmatched1 = ml.saveByCategory(matched_reject2_unmatched1)
	matched_reject1_unmatched2 = ml.saveByCategory(matched_reject1_unmatched2)
	
	#Calculate concordance between NC TC and NF TF
	rejects1_unmatched_num = len(unmatched_keeps1)-1.0
	rejects2_unmatched_num = len(unmatched_keeps2)-1.0
	keeps_num = len(matched_keeps1)-1.0
	total_num = rejects1_unmatched_num + rejects2_unmatched_num + keeps_num
	rejects1_unmatched_percent = rejects1_unmatched_num/total_num * 100 
	rejects2_unmatched_percent = rejects2_unmatched_num/total_num * 100
	keeps_percent = keeps_num/total_num * 100
	
	#Write tables to text files
	book = xlwt.Workbook()