annotate prepare_population_structure.py @ 0:32f8fbdd754c default tip

Uploaded
author rico
date Fri, 06 Apr 2012 13:59:29 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
32f8fbdd754c Uploaded
rico
parents:
diff changeset
1 #!/usr/bin/env python
32f8fbdd754c Uploaded
rico
parents:
diff changeset
2
32f8fbdd754c Uploaded
rico
parents:
diff changeset
3 import errno
32f8fbdd754c Uploaded
rico
parents:
diff changeset
4 import os
32f8fbdd754c Uploaded
rico
parents:
diff changeset
5 import shutil
32f8fbdd754c Uploaded
rico
parents:
diff changeset
6 import subprocess
32f8fbdd754c Uploaded
rico
parents:
diff changeset
7 import sys
32f8fbdd754c Uploaded
rico
parents:
diff changeset
8 from Population import Population
32f8fbdd754c Uploaded
rico
parents:
diff changeset
9 import gd_composite
32f8fbdd754c Uploaded
rico
parents:
diff changeset
10
32f8fbdd754c Uploaded
rico
parents:
diff changeset
11 ################################################################################
32f8fbdd754c Uploaded
rico
parents:
diff changeset
12
32f8fbdd754c Uploaded
rico
parents:
diff changeset
13 def do_import(filename, files_path, min_reads, min_qual, min_spacing, tags, using_info, population_list):
32f8fbdd754c Uploaded
rico
parents:
diff changeset
14 info_page = gd_composite.InfoPage()
32f8fbdd754c Uploaded
rico
parents:
diff changeset
15 info_page.set_title('Prepare to look for population structure Galaxy Composite Dataset')
32f8fbdd754c Uploaded
rico
parents:
diff changeset
16
32f8fbdd754c Uploaded
rico
parents:
diff changeset
17 display_file = gd_composite.DisplayFile()
32f8fbdd754c Uploaded
rico
parents:
diff changeset
18 display_value = gd_composite.DisplayValue()
32f8fbdd754c Uploaded
rico
parents:
diff changeset
19
32f8fbdd754c Uploaded
rico
parents:
diff changeset
20 out_ped = gd_composite.Parameter(name='admix.ped', value='admix.ped', display_type=display_file)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
21 out_map = gd_composite.Parameter(name='admix.map', value='admix.map', display_type=display_file)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
22 out_use = gd_composite.Parameter(description=using_info, display_type=display_value)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
23
32f8fbdd754c Uploaded
rico
parents:
diff changeset
24 info_page.add_output_parameter(out_ped)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
25 info_page.add_output_parameter(out_map)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
26 info_page.add_output_parameter(out_use)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
27
32f8fbdd754c Uploaded
rico
parents:
diff changeset
28 in_min_reads = gd_composite.Parameter(description='Minimum reads covering a SNP, per individual', value=min_reads, display_type=display_value)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
29 in_min_qual = gd_composite.Parameter(description='Minimum quality value, per individual', value=min_qual, display_type=display_value)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
30 in_min_spacing = gd_composite.Parameter(description='Minimum spacing between SNPs on the same scaffold', value=min_spacing, display_type=display_value)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
31
32f8fbdd754c Uploaded
rico
parents:
diff changeset
32 info_page.add_input_parameter(in_min_reads)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
33 info_page.add_input_parameter(in_min_qual)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
34 info_page.add_input_parameter(in_min_spacing)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
35
32f8fbdd754c Uploaded
rico
parents:
diff changeset
36 misc_populations = gd_composite.Parameter(name='Populations', value=population_list, display_type=gd_composite.DisplayPopulationList())
32f8fbdd754c Uploaded
rico
parents:
diff changeset
37 info_page.add_misc(misc_populations)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
38
32f8fbdd754c Uploaded
rico
parents:
diff changeset
39 with open(filename, 'w') as ofh:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
40 print >> ofh, info_page.render()
32f8fbdd754c Uploaded
rico
parents:
diff changeset
41
32f8fbdd754c Uploaded
rico
parents:
diff changeset
42 def mkdir_p(path):
32f8fbdd754c Uploaded
rico
parents:
diff changeset
43 try:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
44 os.makedirs(path)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
45 except OSError, e:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
46 if e.errno <> errno.EEXIST:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
47 raise
32f8fbdd754c Uploaded
rico
parents:
diff changeset
48
32f8fbdd754c Uploaded
rico
parents:
diff changeset
49 def die(message, exit=True):
32f8fbdd754c Uploaded
rico
parents:
diff changeset
50 print >> sys.stderr, message
32f8fbdd754c Uploaded
rico
parents:
diff changeset
51 if exit:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
52 sys.exit(1)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
53
32f8fbdd754c Uploaded
rico
parents:
diff changeset
54 ################################################################################
32f8fbdd754c Uploaded
rico
parents:
diff changeset
55
32f8fbdd754c Uploaded
rico
parents:
diff changeset
56 if len(sys.argv) < 9:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
57 die("Usage")
32f8fbdd754c Uploaded
rico
parents:
diff changeset
58
32f8fbdd754c Uploaded
rico
parents:
diff changeset
59 # parse command line
32f8fbdd754c Uploaded
rico
parents:
diff changeset
60 input_snp_filename, min_reads, min_qual, min_spacing, output_filename, output_files_path = sys.argv[1:7]
32f8fbdd754c Uploaded
rico
parents:
diff changeset
61 args = sys.argv[7:]
32f8fbdd754c Uploaded
rico
parents:
diff changeset
62
32f8fbdd754c Uploaded
rico
parents:
diff changeset
63 individual_metadata = []
32f8fbdd754c Uploaded
rico
parents:
diff changeset
64 population_files = []
32f8fbdd754c Uploaded
rico
parents:
diff changeset
65 population_names = []
32f8fbdd754c Uploaded
rico
parents:
diff changeset
66 all_individuals = False
32f8fbdd754c Uploaded
rico
parents:
diff changeset
67
32f8fbdd754c Uploaded
rico
parents:
diff changeset
68 for arg in args:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
69 if arg == 'all_individuals':
32f8fbdd754c Uploaded
rico
parents:
diff changeset
70 all_individuals = True
32f8fbdd754c Uploaded
rico
parents:
diff changeset
71 elif len(arg) > 11:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
72 tag = arg[:11]
32f8fbdd754c Uploaded
rico
parents:
diff changeset
73 value = arg[11:]
32f8fbdd754c Uploaded
rico
parents:
diff changeset
74 if tag == 'individual:':
32f8fbdd754c Uploaded
rico
parents:
diff changeset
75 individual_metadata.append(value)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
76 elif tag == 'population:':
32f8fbdd754c Uploaded
rico
parents:
diff changeset
77 filename, name = value.split(':', 1)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
78 population_files.append(filename)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
79 population_names.append(name)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
80
32f8fbdd754c Uploaded
rico
parents:
diff changeset
81 p_total = Population()
32f8fbdd754c Uploaded
rico
parents:
diff changeset
82 p_total.from_tag_list(individual_metadata)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
83
32f8fbdd754c Uploaded
rico
parents:
diff changeset
84 individual_population = {}
32f8fbdd754c Uploaded
rico
parents:
diff changeset
85
32f8fbdd754c Uploaded
rico
parents:
diff changeset
86 population_list = []
32f8fbdd754c Uploaded
rico
parents:
diff changeset
87
32f8fbdd754c Uploaded
rico
parents:
diff changeset
88 if all_individuals:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
89 p1 = p_total
32f8fbdd754c Uploaded
rico
parents:
diff changeset
90 p1.name = 'All Individuals'
32f8fbdd754c Uploaded
rico
parents:
diff changeset
91 population_list.append(p1)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
92 else:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
93 p1 = Population()
32f8fbdd754c Uploaded
rico
parents:
diff changeset
94 for idx in range(len(population_files)):
32f8fbdd754c Uploaded
rico
parents:
diff changeset
95 population_file = population_files[idx]
32f8fbdd754c Uploaded
rico
parents:
diff changeset
96 population_name = population_names[idx]
32f8fbdd754c Uploaded
rico
parents:
diff changeset
97 this_pop = Population(population_name)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
98 this_pop.from_population_file(population_file)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
99 population_list.append(this_pop)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
100 p1.from_population_file(population_file)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
101 tags = p1.tag_list()
32f8fbdd754c Uploaded
rico
parents:
diff changeset
102 for tag in tags:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
103 if tag not in individual_population:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
104 individual_population[tag] = population_name
32f8fbdd754c Uploaded
rico
parents:
diff changeset
105
32f8fbdd754c Uploaded
rico
parents:
diff changeset
106 if not p_total.is_superset(p1):
32f8fbdd754c Uploaded
rico
parents:
diff changeset
107 print >> sys.stderr, 'There is an individual in the population that is not in the SNP table'
32f8fbdd754c Uploaded
rico
parents:
diff changeset
108 sys.exit(1)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
109
32f8fbdd754c Uploaded
rico
parents:
diff changeset
110 # run tool
32f8fbdd754c Uploaded
rico
parents:
diff changeset
111 prog = 'admix_prep'
32f8fbdd754c Uploaded
rico
parents:
diff changeset
112
32f8fbdd754c Uploaded
rico
parents:
diff changeset
113 args = []
32f8fbdd754c Uploaded
rico
parents:
diff changeset
114 args.append(prog)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
115 args.append(input_snp_filename)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
116 args.append(min_reads)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
117 args.append(min_qual)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
118 args.append(min_spacing)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
119
32f8fbdd754c Uploaded
rico
parents:
diff changeset
120 tags = p1.tag_list()
32f8fbdd754c Uploaded
rico
parents:
diff changeset
121 for tag in tags:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
122 args.append(tag)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
123
32f8fbdd754c Uploaded
rico
parents:
diff changeset
124 #print "args:", ' '.join(args)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
125 p = subprocess.Popen(args, bufsize=-1, stdin=None, stdout=subprocess.PIPE, stderr=sys.stderr)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
126 (stdoutdata, stderrdata) = p.communicate()
32f8fbdd754c Uploaded
rico
parents:
diff changeset
127 rc = p.returncode
32f8fbdd754c Uploaded
rico
parents:
diff changeset
128
32f8fbdd754c Uploaded
rico
parents:
diff changeset
129 if rc != 0:
32f8fbdd754c Uploaded
rico
parents:
diff changeset
130 die('admix_prep failed: rc={0}'.format(rc))
32f8fbdd754c Uploaded
rico
parents:
diff changeset
131
32f8fbdd754c Uploaded
rico
parents:
diff changeset
132 using_info = stdoutdata.rstrip('\r\n')
32f8fbdd754c Uploaded
rico
parents:
diff changeset
133 mkdir_p(output_files_path)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
134 output_ped_filename = os.path.join(output_files_path, 'admix.ped')
32f8fbdd754c Uploaded
rico
parents:
diff changeset
135 output_map_filename = os.path.join(output_files_path, 'admix.map')
32f8fbdd754c Uploaded
rico
parents:
diff changeset
136 shutil.copy2('admix.ped', output_ped_filename)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
137 shutil.copy2('admix.map', output_map_filename)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
138 do_import(output_filename, output_files_path, min_reads, min_qual, min_spacing, tags, using_info, population_list)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
139
32f8fbdd754c Uploaded
rico
parents:
diff changeset
140 os.unlink('admix.ped')
32f8fbdd754c Uploaded
rico
parents:
diff changeset
141 os.unlink('admix.map')
32f8fbdd754c Uploaded
rico
parents:
diff changeset
142
32f8fbdd754c Uploaded
rico
parents:
diff changeset
143 sys.exit(0)
32f8fbdd754c Uploaded
rico
parents:
diff changeset
144