def processGeneCalls(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)

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

	'''
	matched1 = total_comparison[0]
	matched2 = total_comparison[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 and write condordance
	nctc_precent = (len(matched1) - 1.0)/(len(table1) - 1.0)
	nftf_precent = (len(matched2) - 1.0)/(len(table2) - 1.0)
	'''
	print('NC TC Concordance: %1.4f' % nctc_precent)
	print('NF TF Concordance: %1.4f' % nftf_precent)
	'''
	summary_table = [['Concordance', '', '', ''], ['', 'FFPE', 'Both', 'Cryo']]
	summary_table.append(['', matched2, table2, nftf_precent])
	ml.outputTable(book, summary_table, 'Concordance Summary', output_directory)
	
	#Write tables to text files
	book = xlwt.Workbook()
	ml.outputTable(book, table1, 'NC TC Input', output_directory)
	ml.outputTable(book, table2, 'NF TF Input', output_directory)
	ml.outputTable(book, matched1, 'NC TC Matches', output_directory)
	ml.outputTable(book, matched2, 'NF TF Matches', output_directory)
	book.save(output_file)
	os.chdir(old_dir)

	print 'Finished'
def processGeneCalls(first_file, second_file, input_directory, output_file,
                     output_directory, lines_skip):
    from xlsxwriter.workbook import Workbook
    import methodslist as ml
    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)

    # Find mutated rows with that are matched and unmatched
    total_comparison = ml.compareTables(table1, table2)
    matched1 = total_comparison[0]
    matched2 = total_comparison[1]
    unmatched1 = total_comparison[2]
    unmatched2 = total_comparison[3]
    '''
	#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 condordance
    condordance_table = ml.calcCondordance(unmatched1, unmatched2, matched1)

    # Write tables to text files
    book = Workbook(output_file)

    ml.outputTable(book, condordance_table, 'Concordance Summary',
                   output_directory)
    ml.outputTable(book, table1, 'NC TC Input', output_directory)
    ml.outputTable(book, table2, 'NF TF Input', output_directory)
    ml.outputTable(book, matched1, 'NC TC Matches', output_directory)
    ml.outputTable(book, matched2, 'NF TF Matches', output_directory)
    book.close()
    os.chdir(old_dir)

    print('Finished')
def processMutations(first_file, second_file , input_directory, output_file, output_directory, lines_skip):
	from xlsxwriter.workbook import Workbook
	import methodslist as ml
	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)
	'''
	Comparision array:
	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 column
	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
	concordance_table = ml.calcCondordance(unmatched_keeps1, unmatched_keeps2, matched_keeps1)
	
	# Write tables to text files
	book = Workbook(output_file)
	
	ml.outputTable(book, concordance_table, 'Concordance Summary', output_directory)
	ml.outputTable(book, keeps1, 'NC TC KEEP', output_directory)
	ml.outputTable(book, keeps2, 'NF TF KEEP', output_directory)
	ml.outputTable(book, matched_reject2_unmatched1, 'NF TF Matched REJECTS', output_directory)
	ml.outputTable(book, matched_reject1_unmatched2, 'NC TC Matched REJECTS', output_directory)
	
	book.close()
	os.chdir(old_dir)
	print('Finished')
#Debug
	ml.outputTable('rejects_NC_TC.txt', rejects1)
	ml.outputTable('rejects_NF_TF.txt', rejects2)

	ml.outputTable('unmatched_NC_TC.txt', unmatched_keeps1)
	ml.outputTable('unmatched_NF_TF.txt', unmatched_keeps2)
	
'''
#Select only certain categories
keeps1 = ml.saveByCategory(keeps1)
keeps2 = ml.saveByCategory(keeps2)
matched_reject1_unmatched2 = ml.saveByCategory(matched_reject1_unmatched2)
matched_reject2_unmatched1 = ml.saveByCategory(matched_reject2_unmatched1)

#Write tables to text files
'''
ml.outputTable(keeps1, 'NC TC KEEP', 'output_keeps.NC_TC.xls')
ml.outputTable(keeps2, 'NF TF KEEP', 'output_keeps.NF_TF.xls')
ml.outputTable(matched_reject1_unmatched2, 'NC TC Matched REJECTS', 'output_reject of unmatched.NC_TC.xls')
ml.outputTable(matched_reject2_unmatched1, 'NF TF Matched REJECTS','output_reject of unmatched.NF_TF.xls')
'''

book = xlwt.Workbook()
sheet1 = book.add_sheet('NC TC KEEP')
sheet2 = book.add_sheet('NF TF KEEP')
sheet3 = book.add_sheet('NC TC Matched REJECTS')
sheet4 = book.add_sheet('NF TF Matched REJECTS')
ml.outputTable(book, keeps1, sheet1, 'output_Pt3A.xls')
ml.outputTable(book, keeps2, sheet2, 'output_Pt3A.xls')
ml.outputTable(book, matched_reject1_unmatched2, sheet3, 'output_Pt3A.xls')
ml.outputTable(book, matched_reject2_unmatched1, sheet4, 'output_Pt3A.xls')
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()
	
	#Output concordance
	summary_table = [['Concordance', '', '', ''], ['', 'FFPE', 'Both', 'Cryo']]
	summary_table.append(['', rejects2_unmatched_num, keeps_num, rejects1_unmatched_num])
	summary_table.append(['', rejects2_unmatched_percent, keeps_percent, rejects1_unmatched_percent])
	
	#Output tables
	ml.outputTable(book, summary_table, 'Concordance Summary', output_directory)
	ml.outputTable(book, keeps1, 'NC TC KEEP', output_directory)
	ml.outputTable(book, keeps2, 'NF TF KEEP', output_directory)
	ml.outputTable(book, matched_reject2_unmatched1, 'NF TF Matched REJECTS', output_directory)
	ml.outputTable(book, matched_reject1_unmatched2, 'NC TC Matched REJECTS', output_directory)
	
	book.save(output_file)
	os.chdir(old_dir)
	print 'Finished'
rejects1 = ml.saveRejects(table1)
rejects2 = ml.saveRejects(table2)

#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]
'''
#Debug
	ml.outputTable('rejects_NC_TC.txt', rejects1)
	ml.outputTable('rejects_NF_TF.txt', rejects2)

	ml.outputTable('unmatched_NC_TC.txt', unmatched_keeps1)
	ml.outputTable('unmatched_NF_TF.txt', unmatched_keeps2)
	
'''
#Select only certain categories
keeps1 = ml.selectCategories(keeps1)
keeps2 = ml.selectCategories(keeps2)
matched_reject1_unmatched2 = ml.selectCategories(matched_reject1_unmatched2)
matched_reject2_unmatched1 = ml.selectCategories(matched_reject2_unmatched1)

#Write tables to text files
ml.outputTable('output_keeps.NC_TC.txt', keeps1)
ml.outputTable('output_keeps.NF_TF.txt', keeps2)
ml.outputTable('output_reject of unmatched.NC_TC.txt', matched_reject1_unmatched2)
ml.outputTable('output_reject of unmatched.NF_TF.txt', matched_reject2_unmatched1)

print 'Finished'