Mercurial > repos > rico > quote_test
comparison population_structure.py @ 0:939c20789501 default tip
Uploaded
| author | rico |
|---|---|
| date | Fri, 06 Apr 2012 10:51:28 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:939c20789501 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 import errno | |
| 4 import os | |
| 5 import shutil | |
| 6 import subprocess | |
| 7 import sys | |
| 8 from BeautifulSoup import BeautifulSoup | |
| 9 import gd_composite | |
| 10 | |
| 11 ################################################################################ | |
| 12 | |
| 13 def run_admixture(ped_file, populations): | |
| 14 prog = 'admixture' | |
| 15 | |
| 16 args = [] | |
| 17 args.append(prog) | |
| 18 args.append(input_ped_file) | |
| 19 args.append(populations) | |
| 20 | |
| 21 #print "args:", ' '.join(args) | |
| 22 ofh = open('/dev/null', 'w') | |
| 23 p = subprocess.Popen(args, bufsize=-1, stdin=None, stdout=ofh, stderr=sys.stderr) | |
| 24 rc = p.wait() | |
| 25 ofh.close() | |
| 26 | |
| 27 def run_r(input_file, output_file, populations): | |
| 28 prog = 'R' | |
| 29 | |
| 30 args = [] | |
| 31 args.append(prog) | |
| 32 args.append('--vanilla') | |
| 33 args.append('--quiet') | |
| 34 args.append('--args') | |
| 35 args.append(input_file) | |
| 36 args.append(output_file) | |
| 37 args.append(populations) | |
| 38 | |
| 39 _realpath = os.path.realpath(__file__) | |
| 40 _script_dir = os.path.dirname(_realpath) | |
| 41 r_script_file = os.path.join(_script_dir, 'population_structure.r') | |
| 42 | |
| 43 ifh = open(r_script_file) | |
| 44 ofh = open('/dev/null', 'w') | |
| 45 p = subprocess.Popen(args, bufsize=-1, stdin=ifh, stdout=ofh, stderr=None) | |
| 46 rc = p.wait() | |
| 47 ifh.close() | |
| 48 ofh.close() | |
| 49 | |
| 50 def mkdir_p(path): | |
| 51 try: | |
| 52 os.makedirs(path) | |
| 53 except OSError, e: | |
| 54 if e.errno <> errno.EEXIST: | |
| 55 raise | |
| 56 | |
| 57 def get_populations(input): | |
| 58 pops = [] | |
| 59 pop_names = {} | |
| 60 | |
| 61 with open(input) as fh: | |
| 62 soup = BeautifulSoup(fh) | |
| 63 misc = soup.find('div', {'id': 'gd_misc'}) | |
| 64 | |
| 65 return 'Populations\n{0}'.format(misc('ul')[0]) | |
| 66 | |
| 67 ################################################################################ | |
| 68 | |
| 69 if len(sys.argv) != 6: | |
| 70 print >> sys.stderr, "Usage" | |
| 71 sys.exit(1) | |
| 72 | |
| 73 input_html_file, input_ped_file, output_file, extra_files_path, populations = sys.argv[1:6] | |
| 74 populations_html = get_populations(input_html_file) | |
| 75 | |
| 76 run_admixture(input_ped_file, populations) | |
| 77 | |
| 78 ped_base = os.path.basename(input_ped_file) | |
| 79 if ped_base.endswith('.ped'): | |
| 80 ped_base = ped_base[:-4] | |
| 81 | |
| 82 p_file = '%s.%s.P' % (ped_base, populations) | |
| 83 q_file = '%s.%s.Q' % (ped_base, populations) | |
| 84 | |
| 85 mkdir_p(extra_files_path) | |
| 86 numeric_output_file = os.path.join(extra_files_path, 'numeric.txt') | |
| 87 shutil.copy2(q_file, numeric_output_file) | |
| 88 os.remove(p_file) | |
| 89 os.remove(q_file) | |
| 90 | |
| 91 graphical_output_file = os.path.join(extra_files_path, 'graphical.pdf') | |
| 92 run_r(numeric_output_file, graphical_output_file, populations) | |
| 93 | |
| 94 ################################################################################ | |
| 95 | |
| 96 info_page = gd_composite.InfoPage() | |
| 97 info_page.set_title('Population structure Galaxy Composite Dataset') | |
| 98 | |
| 99 display_file = gd_composite.DisplayFile() | |
| 100 display_value = gd_composite.DisplayValue() | |
| 101 | |
| 102 out_pdf = gd_composite.Parameter(name='graphical.pdf', value='graphical.pdf', display_type=display_file) | |
| 103 out_txt = gd_composite.Parameter(name='numeric.txt', value='numeric.txt', display_type=display_file) | |
| 104 | |
| 105 info_page.add_output_parameter(out_pdf) | |
| 106 info_page.add_output_parameter(out_txt) | |
| 107 | |
| 108 in_pops = gd_composite.Parameter(description='Number of populations', value=populations, display_type=display_value) | |
| 109 | |
| 110 info_page.add_input_parameter(in_pops) | |
| 111 | |
| 112 misc_pops = gd_composite.Parameter(description=populations_html, display_type=display_value) | |
| 113 | |
| 114 info_page.add_misc(misc_pops) | |
| 115 | |
| 116 | |
| 117 with open (output_file, 'w') as ofh: | |
| 118 print >> ofh, info_page.render() | |
| 119 | |
| 120 | |
| 121 sys.exit(0) |
