Mercurial > repos > greg > phylogenomics_analysis
comparison phylogenomics_analysis.py @ 64:a0c347192b08 draft
Uploaded
| author | greg |
|---|---|
| date | Tue, 28 Feb 2017 09:39:54 -0500 |
| parents | |
| children | 5f76ffe55d0f |
comparison
equal
deleted
inserted
replaced
| 63:c2d2ca6cf94b | 64:a0c347192b08 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 import argparse | |
| 3 import os | |
| 4 import shutil | |
| 5 import subprocess | |
| 6 import sys | |
| 7 import tempfile | |
| 8 | |
| 9 BUFF_SIZE = 1048576 | |
| 10 OUTPUT_DIR = 'phylogenomicsAnalysis_dir' | |
| 11 | |
| 12 parser = argparse.ArgumentParser() | |
| 13 | |
| 14 parser.add_argument('--orthogroup_faa', dest='orthogroup_faa', help="Input dataset files_path") | |
| 15 parser.add_argument('--scaffold', dest='scaffold', default='mode', help='Orthogroups or gene families proteins scaffold') | |
| 16 parser.add_argument('--method', dest='method', help='Protein clustering method') | |
| 17 parser.add_argument('--config_dir', dest='config_dir', help='Directory containing default configuration files') | |
| 18 parser.add_argument('--num_threads', dest='num_threads', type=int, help='Number of threads to use for execution') | |
| 19 parser.add_argument('--orthogroup_fna', dest='orthogroup_fna', default=None, help="Flag for coding sequences associated with orthogroups") | |
| 20 parser.add_argument('--sequence_type', dest='sequence_type', default=None, help="Sequence type used in the phylogenetic inference") | |
| 21 parser.add_argument('--alignments_method', dest='alignments_method', default=None, help='Multiple sequence alignments method') | |
| 22 parser.add_argument('--pasta_script_path', dest='pasta_script_path', default=None, help='Path to script for executing pasta') | |
| 23 parser.add_argument('--pasta_iter_limit', dest='pasta_iter_limit', type=int, default=None, help='"Maximum number of iteration that the PASTA algorithm will execute') | |
| 24 parser.add_argument('--tree_inference', dest='single_copy_custom', default=None, help='Phylogenetic trees inference method') | |
| 25 parser.add_argument('--rooting_order', dest='rooting_order', default=None, help='Rooting order configuration for rooting trees') | |
| 26 parser.add_argument('--bootstrap_replicates', dest='bootstrap_replicates', type=int, default=None, help='Number of replicates for rapid bootstrap analysis') | |
| 27 parser.add_argument('--max_orthogroup_size', dest='max_orthogroup_size', type=int, default=None, help='Maximum number of sequences in orthogroup alignments') | |
| 28 parser.add_argument('--min_orthogroup_size', dest='min_orthogroup_size', type=int, default=None, help='Minimum number of sequences in orthogroup alignments') | |
| 29 parser.add_argument('--remove_sequences', dest='remove_sequences', default=None, type=float, help='Remove sequences with gaps of') | |
| 30 parser.add_argument('--trim_type', dest='trim_type', default=None, help='Process used for gap trimming') | |
| 31 parser.add_argument('--gap_trimming', dest='gap_trimming', default=None, type=float, help='Remove sites in alignments with gaps of') | |
| 32 parser.add_argument('--output_ptortho', dest='output_ptortho', default=None, help='Output for orthogroups') | |
| 33 parser.add_argument('--output_ptortho_dir', dest='output_ptortho_dir', default=None, help='output_ptortho.files_path') | |
| 34 parser.add_argument('--output_ptorthocs', dest='output_ptorthocs', default=None, help='Output for orthogroups with corresponding coding sequences') | |
| 35 parser.add_argument('--output_ptorthocs_dir', dest='output_ptorthocs_dir', default=None, help='output_ptorthocs.files_path') | |
| 36 parser.add_argument('--output_aln', dest='output_aln', default=None, help='Output for orthogroups alignments') | |
| 37 parser.add_argument('--output_aln_dir', dest='output_aln_dir', default=None, help='output_aln.files_path') | |
| 38 parser.add_argument('--output_tree', dest='output_tree', default=None, help='Output for phylogenetic trees') | |
| 39 parser.add_argument('--output_tree_dir', dest='output_tree_dir', default=None, help='output_tree.files_path') | |
| 40 | |
| 41 args = parser.parse_args() | |
| 42 | |
| 43 | |
| 44 def get_stderr_exception(tmp_err, tmp_stderr, tmp_out, tmp_stdout, include_stdout=False): | |
| 45 tmp_stderr.close() | |
| 46 # Get stderr, allowing for case where it's very large. | |
| 47 tmp_stderr = open(tmp_err, 'rb') | |
| 48 stderr_str = '' | |
| 49 buffsize = BUFF_SIZE | |
| 50 try: | |
| 51 while True: | |
| 52 stderr_str += tmp_stderr.read(buffsize) | |
| 53 if not stderr_str or len(stderr_str) % buffsize != 0: | |
| 54 break | |
| 55 except OverflowError: | |
| 56 pass | |
| 57 tmp_stderr.close() | |
| 58 if include_stdout: | |
| 59 tmp_stdout = open(tmp_out, 'rb') | |
| 60 stdout_str = '' | |
| 61 buffsize = BUFF_SIZE | |
| 62 try: | |
| 63 while True: | |
| 64 stdout_str += tmp_stdout.read(buffsize) | |
| 65 if not stdout_str or len(stdout_str) % buffsize != 0: | |
| 66 break | |
| 67 except OverflowError: | |
| 68 pass | |
| 69 tmp_stdout.close() | |
| 70 if include_stdout: | |
| 71 return 'STDOUT\n%s\n\nSTDERR\n%s\n' % (stdout_str, stderr_str) | |
| 72 return stderr_str | |
| 73 | |
| 74 | |
| 75 def move_directory_files(source_dir, destination_dir): | |
| 76 source_directory = os.path.abspath(source_dir) | |
| 77 destination_directory = os.path.abspath(destination_dir) | |
| 78 if not os.path.isdir(destination_directory): | |
| 79 os.makedirs(destination_directory) | |
| 80 for dir_entry in os.listdir(source_directory): | |
| 81 source_entry = os.path.join(source_directory, dir_entry) | |
| 82 shutil.move(source_entry, destination_directory) | |
| 83 | |
| 84 | |
| 85 def stop_err(msg): | |
| 86 sys.stderr.write(msg) | |
| 87 sys.exit(1) | |
| 88 | |
| 89 | |
| 90 def write_html_output(output, title, dir): | |
| 91 with open(output, 'w') as fh: | |
| 92 fh.write('<html><head><h3>%s</h3></head>\n' % title) | |
| 93 fh.write('<body><p/><table cellpadding="2">\n') | |
| 94 fh.write('<tr><th>Size</th><th>Name</th></tr>\n') | |
| 95 for index, fname in enumerate(sorted(os.listdir(dir))): | |
| 96 if index % 2 == 0: | |
| 97 bgcolor = '#D8D8D8' | |
| 98 else: | |
| 99 bgcolor = '#FFFFFF' | |
| 100 try: | |
| 101 size = str(os.path.getsize(os.path.join(dir, fname))) | |
| 102 except: | |
| 103 size = 'unknown' | |
| 104 link = '<a href="%s" type="text/plain">%s</a>\n' % (fname, fname) | |
| 105 fh.write('<tr bgcolor="%s"><td>%s</td><td>%s</td></tr>\n' % (bgcolor, size, link)) | |
| 106 fh.write('</table></body></html>\n') | |
| 107 | |
| 108 | |
| 109 # Define command response buffers. | |
| 110 tmp_out = tempfile.NamedTemporaryFile().name | |
| 111 tmp_stdout = open(tmp_out, 'wb') | |
| 112 tmp_err = tempfile.NamedTemporaryFile().name | |
| 113 tmp_stderr = open(tmp_err, 'wb') | |
| 114 # Build the command line. | |
| 115 cmd = 'PhylogenomicsAnalysis' | |
| 116 cmd += ' --orthogroup_faa %s' % args.orthogroup_faa | |
| 117 cmd += ' --scaffold %s' % args.scaffold | |
| 118 cmd += ' --method %s' % args.method | |
| 119 cmd += ' --config_dir %s' % args.config_dir | |
| 120 cmd += ' --num_threads %d' % args.num_threads | |
| 121 | |
| 122 if args.orthogroup_fna is not None: | |
| 123 cmd += ' --orthogroup_fna' | |
| 124 if args.sequence_type is not None: | |
| 125 cmd += ' --sequence_type %s' % args.sequence_type | |
| 126 if args.alignments_method is not None: | |
| 127 if args.alignments_method == 'create_alignments': | |
| 128 cmd += ' --create_alignments' | |
| 129 elif args.alignments_method == 'add_alignments': | |
| 130 cmd += ' --add_alignments' | |
| 131 elif args.alignments_method == 'pasta_alignments': | |
| 132 cmd += ' --pasta_alignments' | |
| 133 cmd += ' --pasta_script_path %s' % args.pasta_script_path | |
| 134 cmd += ' --pasta_iter_limit %d' % args.pasta_iter_limit | |
| 135 if args.tree_inference is not None: | |
| 136 cmd += ' --tree_inference %s' % args.tree_inference | |
| 137 if args.tree_inference == 'raxml': | |
| 138 if args.rooting_order is not None: | |
| 139 cmd += ' --rooting_order %s' % args.rooting_order | |
| 140 cmd += ' --bootstrap_replicates %d' % args.bootstrap_replicates | |
| 141 if args.max_orthogroup_size is not None: | |
| 142 cmd += ' --max_orthogroup_size %d' % args.max_orthogroup_size | |
| 143 if args.min_orthogroup_size is not None: | |
| 144 cmd += ' --min_orthogroup_size %d' % args.min_orthogroup_size | |
| 145 if args.remove_sequences is not None: | |
| 146 cmd += ' --remove_sequences %4f' % args.remove_sequences | |
| 147 if args.trim_type is not None: | |
| 148 if args.trim_type == 'automated_trimming': | |
| 149 cmd += ' --automated_trimming' | |
| 150 else: | |
| 151 cmd += ' --gap_trimming %4f' % args.gap_trimming | |
| 152 # Run the command. | |
| 153 proc = subprocess.Popen(args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True) | |
| 154 rc = proc.wait() | |
| 155 # Handle execution errors. | |
| 156 if rc != 0: | |
| 157 error_message = get_stderr_exception(tmp_err, tmp_stderr, tmp_out, tmp_stdout) | |
| 158 stop_err( error_message ) | |
| 159 # Handle phylogenies for orthogroups outputs. | |
| 160 if args.orthogroup_fna is not None: | |
| 161 out_file = args.output_ptorthocs | |
| 162 orthogroups_dest_dir = args.output_ptorthocs_dir | |
| 163 title = 'Phylogenies files for orthogroups and corresponding coding sequences' | |
| 164 else: | |
| 165 out_file = args.output_ptortho | |
| 166 orthogroups_dest_dir = args.output_ptortho_dir | |
| 167 title = 'Phylogenies files for orthogroups' | |
| 168 orthogroups_src_dir = os.path.join(OUTPUT_DIR, 'orthogroups_fasta') | |
| 169 move_directory_files(orthogroups_src_dir, orthogroups_dest_dir) | |
| 170 write_html_output(out_file, title, orthogroups_dest_dir) | |
| 171 # Handle multiple sequences alignments for orthogroups outputs. | |
| 172 if args.output_aln is not None: | |
| 173 alignments_src_dir = os.path.join(OUTPUT_DIR, 'orthogroups_aln') | |
| 174 alignments_dest_dir = args.output_aln_dir | |
| 175 title = 'Multiple sequence alignments files for orthogroups' | |
| 176 move_directory_files(alignments_src_dir, alignments_dest_dir) | |
| 177 write_html_output(args.output_aln, title, alignments_dest_dir) | |
| 178 # Handle phylogenies for orthogroups outputs. | |
| 179 if args.output_tree is not None: | |
| 180 trees_src_dir = os.path.join(OUTPUT_DIR, 'orthogroups_tree') | |
| 181 trees_dest_dir = args.output_tree_dir | |
| 182 title = 'Phylogenetic tree files for orthogroups' | |
| 183 move_directory_files(trees_src_dir, trees_dest_dir) | |
| 184 write_html_output(args.output_tree, title, trees_dest_dir) |
