annotate dpmix.py @ 10:9b92372de9f6

rename snp to gsSnp, sap to gdSap
author Richard Burhans <burhans@bx.psu.edu>
date Tue, 04 Sep 2012 12:49:53 -0400
parents 22fe0154fa54
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
1 #!/usr/bin/env python
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
2
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
3 import errno
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
4 import sys
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
5 import os
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
6 import subprocess
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
7 from Population import Population
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
8 import gd_composite
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
9 from dpmix_plot import make_dpmix_plot
9
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
10 from LocationFile import LocationFile
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
11
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
12 ################################################################################
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
13
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
14 def mkdir_p(path):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
15 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
16 os.makedirs(path)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
17 except OSError, e:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
18 if e.errno <> errno.EEXIST:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
19 raise
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
20
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
21 def run_program(prog, args, stdout_file=None, space_to_tab=False):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
22 #print "args: ", ' '.join(args)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
23 p = subprocess.Popen(args, bufsize=-1, executable=prog, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
24 (stdoutdata, stderrdata) = p.communicate()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
25 rc = p.returncode
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
26
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
27 if stdout_file is not None:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
28 with open(stdout_file, 'w') as ofh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
29 lines = stdoutdata.split('\n')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
30 for line in lines:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
31 line = line.strip()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
32 if line:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
33 if space_to_tab:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
34 line = line.replace(' ', '\t')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
35 print >> ofh, line
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
36
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
37 if rc != 0:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
38 print >> sys.stderr, "FAILED: rc={0}: {1}".format(rc, ' '.join(args))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
39 print >> sys.stderr, stderrdata
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
40 sys.exit(1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
41
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
42 ################################################################################
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
43
9
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
44 if len(sys.argv) < 15:
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
45 print "usage"
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
46 sys.exit(1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
47
9
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
48 input, data_source, switch_penalty, ap1_input, ap2_input, p_input, output, output2, output2_dir, dbkey, ref_column, galaxy_data_index_dir, heterochromatin_loc_file = sys.argv[1:14]
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
49 individual_metadata = sys.argv[14:]
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
50
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
51 chrom = 'all'
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
52 add_logs = '0'
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
53
9
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
54 loc_path = os.path.join(galaxy_data_index_dir, heterochromatin_loc_file)
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
55 location_file = LocationFile(loc_path)
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
56 heterochrom_path = location_file.get_values_if_exists(dbkey)
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
57 if heterochrom_path is None:
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
58 heterochrom_path = '/dev/null'
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
59
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
60 population_list = []
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
61
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
62 p_total = Population()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
63 p_total.from_tag_list(individual_metadata)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
64
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
65 ap1 = Population(name='Ancestral population 1')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
66 ap1.from_population_file(ap1_input)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
67 population_list.append(ap1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
68 if not p_total.is_superset(ap1):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
69 print >> sys.stderr, 'There is an individual in ancestral population 1 that is not in the SNP table'
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
70 sys.exit(1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
71
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
72 ap2 = Population(name='Ancestral population 2')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
73 ap2.from_population_file(ap2_input)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
74 population_list.append(ap2)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
75 if not p_total.is_superset(ap2):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
76 print >> sys.stderr, 'There is an individual in ancestral population 2 that is not in the SNP table'
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
77 sys.exit(1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
78
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
79 p = Population(name='Potentially admixed')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
80 p.from_population_file(p_input)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
81 population_list.append(p)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
82 if not p_total.is_superset(p):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
83 print >> sys.stderr, 'There is an individual in the population that is not in the SNP table'
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
84 sys.exit(1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
85
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
86 mkdir_p(output2_dir)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
87
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
88 ################################################################################
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
89 # Create tabular file
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
90 ################################################################################
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
91
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
92 misc_file = os.path.join(output2_dir, 'misc.txt')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
93
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
94 prog = 'dpmix'
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
95 args = [ prog ]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
96 args.append(input)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
97 args.append(ref_column)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
98 args.append(chrom)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
99 args.append(data_source)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
100 args.append(add_logs)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
101 args.append(switch_penalty)
9
22fe0154fa54 added support for heterochromatic regions
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
102 args.append(heterochrom_path)
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
103 args.append(misc_file)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
104
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
105 columns = ap1.column_list()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
106 for column in columns:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
107 args.append('{0}:1:{1}'.format(column, ap1.individual_with_column(column).name))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
108
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
109 columns = ap2.column_list()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
110 for column in columns:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
111 args.append('{0}:2:{1}'.format(column, ap2.individual_with_column(column).name))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
112
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
113 columns = p.column_list()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
114 for column in columns:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
115 args.append('{0}:0:{1}'.format(column, p.individual_with_column(column).name))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
116
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
117 run_program(None, args, stdout_file=output, space_to_tab=True)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
118
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
119 ################################################################################
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
120 # Create pdf file
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
121 ################################################################################
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
122
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
123 pdf_file = os.path.join(output2_dir, 'dpmix.pdf')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
124 make_dpmix_plot(dbkey, output, pdf_file, galaxy_data_index_dir)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
125
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
126 ################################################################################
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
127 # Create html
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
128 ################################################################################
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
129
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
130 info_page = gd_composite.InfoPage()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
131 info_page.set_title('dpmix Galaxy Composite Dataset')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
132
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
133 display_file = gd_composite.DisplayFile()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
134 display_value = gd_composite.DisplayValue()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
135
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
136 out_pdf = gd_composite.Parameter(name='dpmix.pdf', value='dpmix.pdf', display_type=display_file)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
137 out_misc = gd_composite.Parameter(name='misc.txt', value='misc.txt', display_type=display_file)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
138
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
139 info_page.add_output_parameter(out_pdf)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
140 info_page.add_output_parameter(out_misc)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
141
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
142 if data_source == '0':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
143 data_source_value = 'sequence coverage'
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
144 elif data_source == '1':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
145 data_source_value = 'estimated genotype'
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
146
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
147 in_data_source = gd_composite.Parameter(description='Data source', value=data_source_value, display_type=display_value)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
148 in_switch_penalty = gd_composite.Parameter(description='Switch penalty', value=switch_penalty, display_type=display_value)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
149
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
150 info_page.add_input_parameter(in_data_source)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
151 info_page.add_input_parameter(in_switch_penalty)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
152
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
153 misc_populations = gd_composite.Parameter(name='Populations', value=population_list, display_type=gd_composite.DisplayPopulationList())
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
154
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
155 info_page.add_misc(misc_populations)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
156
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
157 with open(output2, 'w') as ofh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
158 print >> ofh, info_page.render()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
159
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
160 sys.exit(0)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
161
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
162