96
|
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 = 'geneFamilyClassification_dir'
|
|
11
|
|
12 parser = argparse.ArgumentParser()
|
|
13 parser.add_argument('--input', dest='input', help="Input dataset")
|
|
14 parser.add_argument('--scaffold', dest='scaffold', default='mode', help='Orthogroups or gene families proteins scaffold')
|
|
15 parser.add_argument('--method', dest='method', help='Protein clustering method')
|
|
16 parser.add_argument('--classifier', dest='classifier', help='Protein classification 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('--super_orthogroups', dest='super_orthogroups', default=None, help='Super orthogroups clustering specification')
|
|
20 parser.add_argument('--single_copy_custom', dest='single_copy_custom', default=None, help='Custom single copy orthogroup configuration')
|
|
21 parser.add_argument('--single_copy_taxa', dest='single_copy_taxa', type=int, default=None, help='"Minimum single copy taxa required in orthogroup')
|
|
22 parser.add_argument('--taxa_present', dest='taxa_present', type=int, default=None, help='Minimum taxa required in single copy orthogroup')
|
|
23 parser.add_argument('--orthogroup_fasta', dest='orthogroup_fasta', default=None, help='Flag to create orthogroup sequences')
|
|
24 parser.add_argument('--coding_sequences', dest='coding_sequences', default=None, help='Flag to create orthogroup coding sequences')
|
|
25 parser.add_argument('--save_hmmscan_log', dest='save_hmmscan_log', default=None, help='Flag to save the hmmscan log')
|
|
26 parser.add_argument('--hmmscan_log', dest='hmmscan_log', default=None, help='hmmscan log file')
|
|
27 parser.add_argument('--output_ptortho', dest='output_ptortho', default=None, help='Output for orthogroups')
|
101
|
28 parser.add_argument('--output_ptortho_dir', dest='output_ptortho_dir', default=None, help='output_ptortho.files_path')
|
96
|
29 parser.add_argument('--output_ptorthocs', dest='output_ptorthocs', default=None, help='Output for orthogroups with corresponding coding sequences')
|
101
|
30 parser.add_argument('--output_ptorthocs_dir', dest='output_ptorthocs_dir', default=None, help='output_ptorthocs.files_path')
|
96
|
31 parser.add_argument('--output_ptsco', dest='output_ptsco', default=None, help='Output for single copy orthogroups')
|
101
|
32 parser.add_argument('--output_ptsco_dir', dest='output_ptsco_dir', default=None, help='output_ptsco.files_path')
|
96
|
33
|
|
34 args = parser.parse_args()
|
|
35
|
|
36
|
|
37 def get_stderr_exception(tmp_err, tmp_stderr, tmp_out, tmp_stdout, include_stdout=False):
|
|
38 tmp_stderr.close()
|
|
39 """
|
|
40 Return a stderr string of reasonable size.
|
|
41 """
|
|
42 # Get stderr, allowing for case where it's very large.
|
|
43 tmp_stderr = open(tmp_err, 'rb')
|
|
44 stderr_str = ''
|
|
45 buffsize = BUFF_SIZE
|
|
46 try:
|
|
47 while True:
|
|
48 stderr_str += tmp_stderr.read(buffsize)
|
|
49 if not stderr_str or len(stderr_str) % buffsize != 0:
|
|
50 break
|
|
51 except OverflowError:
|
|
52 pass
|
|
53 tmp_stderr.close()
|
|
54 if include_stdout:
|
|
55 tmp_stdout = open(tmp_out, 'rb')
|
|
56 stdout_str = ''
|
|
57 buffsize = BUFF_SIZE
|
|
58 try:
|
|
59 while True:
|
|
60 stdout_str += tmp_stdout.read(buffsize)
|
|
61 if not stdout_str or len(stdout_str) % buffsize != 0:
|
|
62 break
|
|
63 except OverflowError:
|
|
64 pass
|
|
65 tmp_stdout.close()
|
|
66 if include_stdout:
|
|
67 return 'STDOUT\n%s\n\nSTDERR\n%s\n' % (stdout_str, stderr_str)
|
|
68 return stderr_str
|
|
69
|
|
70
|
|
71 def move_directory_files(source_dir, destination_dir):
|
|
72 source_directory = os.path.abspath(source_dir)
|
|
73 destination_directory = os.path.abspath(destination_dir)
|
|
74 if not os.path.isdir(destination_directory):
|
|
75 os.makedirs(destination_directory)
|
|
76 for dir_entry in os.listdir(source_directory):
|
|
77 source_entry = os.path.join(source_directory, dir_entry)
|
|
78 shutil.move(source_entry, destination_directory)
|
|
79
|
|
80
|
|
81 def stop_err(msg):
|
|
82 sys.stderr.write(msg)
|
|
83 sys.exit(1)
|
|
84
|
|
85
|
|
86 def write_html_output(output, title, dir):
|
107
|
87 with open(output, 'w') as fh:
|
109
|
88 fh.write('<html><head><title>%s</title></head>\n' % title)
|
|
89 fh.write('<body><p/><table>\n')
|
|
90 fh.write('<tr><th>Size</th><th>Name</th></tr>\n')
|
106
|
91 for fname in os.listdir(dir):
|
109
|
92 size = str(os.path.getsixe(fname))
|
|
93 link = '<a href="%s" type="text/plain">%s</a>\n' % (fname, fname)
|
|
94 fh.write('<tr><td>%s</td><td>%s</td></tr>\n' % (size, link))
|
|
95 fh.write('</table></body></html>\n')
|
96
|
96
|
|
97
|
|
98 # Define command response buffers.
|
|
99 tmp_out = tempfile.NamedTemporaryFile().name
|
|
100 tmp_stdout = open(tmp_out, 'wb')
|
|
101 tmp_err = tempfile.NamedTemporaryFile().name
|
|
102 tmp_stderr = open(tmp_err, 'wb')
|
|
103 # Build the command line.
|
|
104 cmd = 'GeneFamilyClassifier'
|
|
105 cmd += ' --proteins %s' % args.input
|
|
106 cmd += ' --scaffold %s' % args.scaffold
|
|
107 cmd += ' --method %s' % args.method
|
|
108 cmd += ' --classifier %s' % args.classifier
|
|
109 cmd += ' --config_dir %s' % args.config_dir
|
|
110 cmd += ' --num_threads %d' % args.num_threads
|
|
111 if args.super_orthogroups is not None:
|
|
112 cmd += ' --super_orthogroups %s' % args.super_orthogroups
|
|
113 if args.single_copy_custom is not None:
|
|
114 cmd += ' --single_copy_custom %s' % args.single_copy_custom
|
|
115 if args.single_copy_taxa is not None:
|
|
116 cmd += ' --single_copy_taxa %d' % args.single_copy_taxa
|
|
117 if args.taxa_present is not None:
|
|
118 cmd += ' --taxa_present %d' % args.taxa_present
|
|
119 if args.orthogroup_fasta is None:
|
|
120 create_ortho_sequences = False
|
|
121 else:
|
|
122 create_ortho_sequences = True
|
|
123 cmd += ' --orthogroup_fasta'
|
|
124 if args.coding_sequences is None:
|
|
125 create_corresponding_coding_sequences = False
|
|
126 else:
|
|
127 create_corresponding_coding_sequences = True
|
|
128 cmd += ' --coding_sequences %s' % args.coding_sequences
|
|
129 # Run the command.
|
|
130 proc = subprocess.Popen(args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True)
|
|
131 rc = proc.wait()
|
|
132 # Handle execution errors.
|
|
133 if rc != 0:
|
|
134 error_message = get_stderr_exception(tmp_err, tmp_stderr, tmp_out, tmp_stdout)
|
|
135 stop_err( error_message )
|
|
136 # Handle hmmscan.log output.
|
|
137 if args.classifier in ['hmmscan', 'both']:
|
|
138 src_hmmscan_log = os.path.join(OUTPUT_DIR, 'hmmscan.log')
|
|
139 if os.path.exists(src_hmmscan_log):
|
|
140 if args.save_hmmscan_log is None:
|
|
141 os.remove(args.hmmscan_log)
|
|
142 else:
|
|
143 shutil.move(src_hmmscan_log, args.hmmscan_log)
|
|
144 # Handle orthogroups outputs.
|
|
145 if create_ortho_sequences:
|
|
146 if create_corresponding_coding_sequences:
|
|
147 out_file = args.output_ptorthocs
|
101
|
148 orthogroups_fasta_dest_dir = args.output_ptorthocs_dir
|
109
|
149 title = 'Orthogroups and corresponding coding sequences files'
|
96
|
150 else:
|
|
151 out_file = args.output_ptortho
|
101
|
152 orthogroups_fasta_dest_dir = args.output_ptortho_dir
|
109
|
153 title = 'Orthogroups files'
|
96
|
154 orthogroups_fasta_src_dir = os.path.join(OUTPUT_DIR, 'orthogroups_fasta')
|
103
|
155 move_directory_files(orthogroups_fasta_src_dir, orthogroups_fasta_dest_dir)
|
96
|
156 write_html_output(out_file, title, orthogroups_fasta_dest_dir)
|
|
157 # Handle single copy orthogroup outputs.
|
|
158 if args.output_ptsco is not None:
|
|
159 single_copy_fasta_src_dir = os.path.join(OUTPUT_DIR, 'single_copy_fasta')
|
101
|
160 single_copy_fasta_dest_dir = args.output_ptsco_dir
|
109
|
161 title = 'Single copy orthogroups files'
|
96
|
162 move_directory_files(single_copy_fasta_src_dir, single_copy_fasta_dest_dir)
|
|
163 write_html_output(args.output_ptsco, title, single_copy_fasta_dest_dir)
|