comparison nucleotide_diversity_pi.py @ 22:95a05c1ef5d5

update to devshed revision aaece207bd01
author Richard Burhans <burhans@bx.psu.edu>
date Mon, 11 Mar 2013 11:28:06 -0400
parents
children 8997f2ca8c7a
comparison
equal deleted inserted replaced
21:d6b961721037 22:95a05c1ef5d5
1 #!/usr/bin/env python
2
3 import sys
4 import subprocess
5 from Population import Population
6
7 ################################################################################
8
9 def run_program(prog, args, stdout_file=None, space_to_tab=False):
10 #print "args: ", ' '.join(args)
11 p = subprocess.Popen(args, bufsize=-1, executable=prog, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
12 (stdoutdata, stderrdata) = p.communicate()
13 rc = p.returncode
14
15 if stdout_file is not None:
16 with open(stdout_file, 'w') as ofh:
17 lines = stdoutdata.split('\n')
18 for line in lines:
19 line = line.strip()
20 if line:
21 if space_to_tab:
22 line = line.replace(' ', '\t')
23 print >> ofh, line
24
25 if rc != 0:
26 print >> sys.stderr, "FAILED: rc={0}: {1}".format(rc, ' '.join(args))
27 print >> sys.stderr, stderrdata
28 sys.exit(1)
29
30 ################################################################################
31
32 if len(sys.argv) < 8:
33 print >> sys.stderr, "Usage"
34 sys.exit(1)
35
36 gd_saps_file, gd_snps_file, covered_intervals_file, gd_indivs_file, output_file = sys.argv[1:6]
37 individual_metadata = sys.argv[6:]
38
39 p_total = Population()
40 p_total.from_tag_list(individual_metadata)
41
42 p1 = Population()
43 p1.from_population_file(gd_indivs_file)
44 if not p_total.is_superset(p1):
45 print >> sys.stderr, 'There is an individual in the population individuals that is not in the SNP table'
46 sys.exit(1)
47
48 ################################################################################
49
50 prog = 'get_pi'
51
52 args = [ prog ]
53 args.append(gd_saps_file)
54 args.append(gd_snps_file)
55 args.append(covered_intervals_file)
56
57 columns = p1.column_list()
58 for column in columns:
59 args.append('{0}'.format(column))
60
61 run_program(None, args, stdout_file=output_file)
62
63 sys.exit(0)
64