annotate STACKS_procrad.py @ 3:0e0ff9e9c761 default tip

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