Convert from mach to PEDMAP user Kantale

From PyPedia
Jump to: navigation, search

Contents

[edit] Documentation

Converts from mach to plink's PED and MAP format.

[edit] Parameters

<inputs>
</inputs>


[edit] Return

[edit] See also

[edit] Code

def Convert_from_mach_to_PEDMAP_user_Kantale(
	input_hap_filename = None,
	input_map_filename = None,
	output_ped_filename = None,
	output_map_filename = None,
):
	print "Converting haps.."
	input_hap_file = open(input_hap_filename, "U")
	output_ped_file = open(output_ped_filename, "w")
	hap_count = 0
	first = True
	first_hap = None
	while True:
		line_1 = input_hap_file.readline()
		if not line_1: break

		line_2 = input_hap_file.readline()
		
		hap_count += 1

		if hap_count % 1 == 0:
			print "Samples converted:", hap_count, Print_now_user_Kantale()

		ls_1 = line_1.replace("\n", "").split()
		indiv_1 = ls_1[0]
		hap_name_1 = ls_1[1]
		haps_1 = list(ls_1[2])
		haps_length_1 = len(haps_1)
		
		ls_2 = line_2.replace("\n", "").split()
		indiv_2 = ls_2[0]
		hap_name_2 = ls_2[1]
		haps_2 = list(ls_2[2])
		haps_length_2 = len(haps_2)
		
		if indiv_1 != indiv_2:
			raise Exception("Hap file does not contain consecutive haplotypes of the same individuals")
		
		if haps_length_1 != haps_length_2:
			raise Exception("Invalid length of haplotypes for common individuals")
		
		output_ped_file.write(str.join(" ", [indiv_1, indiv_2, "0", "0", "0", "1"]))
		for i in range(haps_length_1):
			output_ped_file.write(" " + haps_1[i])
			output_ped_file.write(" " + haps_2[i])
		output_ped_file.write("\n")

#		Too slow
#		to_print = [indiv_1, indiv_2, "0", "0", "0", "1"] + reduce(lambda x,y: x+y, [[x,y] for x, y in zip(haps_1, haps_2)])
#		output_ped_file.write(str.join(" ", to_print) + "\n")

	output_ped_file.close()

	print "Converting maps.."
	output_map_file = open(output_map_filename, "w")
	for line in open(input_map_filename):
		ls = line.replace("\n", "").split()
		output_map_file.write(str.join(" ", [ls[0], ls[1], "0", ls[2]]) + "\n")
	output_map_file.close()
	print "..Done"

[edit] Unit Tests

def uni1():
	return True

[edit] Development Code

def Convert_from_mach_to_PEDMAP_user_Kantale():
	pass

[edit] Permissions

[edit] Documentation Permissions

Kantale

[edit] Code Permissions

Kantale

[edit] Unit Tests Permissions

Kantale

[edit] Permissions Permissions

Kantale

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox