annotate STACKS_procrad.py @ 0:d6ba40f6c824

first commit
author cmonjeau
date Mon, 24 Aug 2015 09:29:12 +0000
parents
children c9e10e0d6c10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
1 #!/usr/bin/python
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
2 # -*- coding: utf-8 -*-
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
3
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
4 import sys
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
5 import os
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
6 import re
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
7 import tempfile
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
8 import subprocess
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
9 import glob
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
10 import shutil
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
11 import argparse
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
12 from os.path import basename
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
13 import zipfile
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
14 import tarfile
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
15 import gzip
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
16 from stacks import *
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
17
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
18
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
19 def __main__():
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
20
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
21 # arguments recuperation
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
22
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
23 parser = argparse.ArgumentParser()
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
24 parser.add_argument('--input_type')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
25 parser.add_argument('--input_enzyme')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
26 parser.add_argument('--input_single')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
27 parser.add_argument('--input_paired1')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
28 parser.add_argument('--input_paired2')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
29 parser.add_argument('--inputype')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
30 parser.add_argument('--sample_name')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
31 parser.add_argument('--barcode')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
32 parser.add_argument('--output_choice')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
33 parser.add_argument('--output_archive')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
34 parser.add_argument('--enzyme1')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
35 parser.add_argument('--enzyme2')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
36 parser.add_argument('--outype')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
37 parser.add_argument('--qualitenc')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
38 parser.add_argument('-D', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
39 parser.add_argument('-t')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
40 parser.add_argument('-q', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
41 parser.add_argument('--activate_advanced_options')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
42 parser.add_argument('-r', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
43 parser.add_argument('-w', default='0.15')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
44 parser.add_argument('-s', default='10')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
45 parser.add_argument('-c', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
46 parser.add_argument('--inline_null', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
47 parser.add_argument('--index_null', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
48 parser.add_argument('--inline_inline', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
49 parser.add_argument('--index_index', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
50 parser.add_argument('--inline_index', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
51 parser.add_argument('--index_inline', action='store_true')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
52 parser.add_argument('--logfile')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
53 options = parser.parse_args()
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
54
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
55 # create the working dir
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
56 os.mkdir('inputs')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
57 os.mkdir('job_outputs')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
58 os.mkdir('galaxy_outputs')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
59
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
60 cmd_line = []
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
61 cmd_line.append('process_radtags')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
62 cmd_line.extend(['-p', 'inputs'])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
63 cmd_line.extend(['-i', options.inputype])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
64 cmd_line.extend(['-b', options.barcode])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
65
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
66 # parse config files and create symlink into the temp dir
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
67
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
68 if options.input_type == 'single':
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
69
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
70 # load the config file
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
71 input_single = options.input_single
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
72
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
73 # parse the input_file to extract filenames and filepaths
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
74 tab_files = galaxy_config_to_tabfiles(input_single)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
75
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
76 # create symlink into the temp dir
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
77 create_symlinks_from_tabfiles(tab_files, 'inputs')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
78 else:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
79
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
80 # load config files
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
81 input_paired1 = options.input_paired1
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
82 input_paired2 = options.input_paired2
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
83
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
84 # parse the input_file to extract filenames and filepaths
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
85
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
86 tab_files_paired1 = galaxy_config_to_tabfiles(input_paired1)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
87 tab_files_paired2 = galaxy_config_to_tabfiles(input_paired2)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
88
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
89 # create symlinks into the temp dir
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
90
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
91 create_symlinks_from_tabfiles(tab_files_paired1, 'inputs')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
92 create_symlinks_from_tabfiles(tab_files_paired2, 'inputs')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
93
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
94 cmd_line.append('-P')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
95
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
96 # test nb enzyme
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
97 if options.input_enzyme == '1':
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
98 cmd_line.extend(['-e', options.enzyme1])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
99
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
100 if options.input_enzyme == '2':
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
101 cmd_line.extend(['---renz_1', options.enzyme1, '--renz_2', options.enzyme2])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
102
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
103 # quality
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
104 cmd_line.extend(['-E', options.qualitenc])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
105
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
106 # outputs
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
107 cmd_line.extend(['-o', 'job_outputs/'])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
108 cmd_line.extend(['-y', options.outype])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
109
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
110 # test capture discards
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
111 if options.D:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
112 cmd_line.append('-D')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
113
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
114 # optional options
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
115 if options.activate_advanced_options == "true":
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
116
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
117 if options.q:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
118 cmd_line.append('-q')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
119 if options.r:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
120 cmd_line.append('-r')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
121
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
122 cmd_line.extend(['-w', options.w, '-s', options.s])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
123
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
124 if options.c:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
125 cmd_line.append('-c')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
126 if options.t != '-1':
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
127 cmd_line.extend(['-t', options.t])
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
128 if options.inline_null:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
129 cmd_line.append('--inline_null')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
130 if options.index_null:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
131 cmd_line.append('--index_null')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
132 if options.inline_inline:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
133 cmd_line.append('--inline_inline')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
134 if options.index_index:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
135 cmd_line.append('--index_index')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
136 if options.inline_index:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
137 cmd_line.append('--inline_index')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
138 if options.index_inline:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
139 cmd_line.append('--index_inline')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
140
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
141 print '[CMD_LINE] : ' + ' '.join(cmd_line)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
142
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
143 p = subprocess.call(cmd_line)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
144
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
145 # postprocesses
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
146
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
147 try:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
148 shutil.move('job_outputs/process_radtags.log', options.logfile)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
149 except:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
150 sys.stderr.write('Error in process_radtags execution; Please read the additional output (stdout)\n')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
151 sys.exit(1)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
152
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
153 if options.discard_file:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
154 discards_file_name = glob.glob('job_outputs/*.discards')[0]
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
155 shutil.move(discards_file_name, options.discard_file)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
156
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
157 # manage outputs names
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
158
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
159 change_outputs_procrad_name(os.getcwd() + '/job_outputs', options.sample_name)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
160
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
161 # generate additional output archive file
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
162
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
163 if options.output_choice != '1':
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
164 generate_additional_archive_file(os.getcwd() + '/job_outputs', options.output_archive)
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
165
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
166 # if user has not choose the only zip archive
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
167
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
168 if options.output_choice != '3':
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
169 list_files = glob.glob('job_outputs/*')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
170 for i in list_files:
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
171 shutil.move(i, 'galaxy_outputs')
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
172
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
173
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
174 if __name__ == '__main__':
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
175 __main__()
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
176
d6ba40f6c824 first commit
cmonjeau
parents:
diff changeset
177