annotate dpmix.py @ 5:8a1147101f85

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