-
Notifications
You must be signed in to change notification settings - Fork 14
/
pdx_exomeseq.py
281 lines (229 loc) · 10.2 KB
/
pdx_exomeseq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
Gregory Way 2017
pdx_exomeseq
Main function that aggregates all samples and determines which command to run
depending on command line arguments. Then submits these commands as scheduled
jobs to the Dartmouth Discovery cluster.
Usage:
Invoke via command line with specific arguments including:
{'fastqc', 'multiqc', 'trimgalore', 'bwa', 'samtools', 'disambiguate',
'variant'}
Some commands may also have subcommands. E.g. with samtools, specify one of the
following subcommands:
{'sort_name', 'fixmate', 'sort_position', 'rmdup', 'index_bam',
'index_bam_gatk'}
E.g.
python pdx_exomeseq.py samtools
--sub_command 'sort_name' \
--genome 'hg' \
--input_directory 'processed/sam' \
--output_directory 'processed/bam' \
--walltime '06:00:00' \
--nodes 2 \
--cores 12 \
--humanonly
The specific pipeline using this script is given in `wes_pipeline.sh`
"""
import os
import yaml
import util.arguments as arguments
# Load command arguments
args = arguments.get_args()
command = args.which
genome = args.genome
input_dir = args.input_directory
output_dir = args.output_directory
config = args.config_yaml
walltime = args.walltime
nodes = str(args.nodes)
cores = str(args.cores)
# Load configuration
with open(config, 'r') as stream:
config = yaml.load(stream)
# Load constants
python = config['python']
java = config['java']
rscript = config['r']
conda_env = config['condaenv']
if genome == 'hg':
genome_ref = config['hgreference']
elif genome == 'mm':
genome_ref = config['mmreference']
combined_ref = config['combinedref']
dbsnp = config['dbsnp']
exonbed = config['exonbed']
base_dir = config['directory']
fastqc = config['fastqc']
multiqc = config['multiqc']
trimgalore = config['trimgalore']
cutadapt = config['cutadapt']
bwa = config['bwa']
samtools = config['samtools']
picard = config['picard']
gatk = config['gatk']
disambiguate = config['disambiguate']
mosdepth = config['mosdepth']
# Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)
############################
# Generate the commands
############################
submit_commands = {}
# General purpose module load of pdx-exome seq conda env
conda_build = ['m', 'load', 'python/3-Anaconda', '&&',
'source', 'activate', conda_env, '&&']
java_load = ['m', 'load', 'java/1.8', '&&']
# Obtain samples to process
all_samples = args.func(args)
# FastQC
if command == 'fastqc':
for sample_id in all_samples:
fastqc_com = conda_build + [fastqc, sample_id, '-o', output_dir]
submit_commands[sample_id] = fastqc_com
# MultiQC
if command == 'multiqc':
html_file = args.html_file
multiqc_com = conda_build + [multiqc, input_dir, '--force', '--filename',
html_file]
submit_commands['multiqc'] = multiqc_com
# TrimGalore
if command == 'trimgalore':
fastqc_results_dir = args.fastqc_results_dir
for sample_1, sample_2 in all_samples:
# The Second output specifies which directory to perform fastqc on
trimgalore_com = [trimgalore,
'--paired', sample_1, sample_2,
'--output_dir', output_dir,
'--fastqc_args',
'"--outdir {}"'.format(fastqc_results_dir)]
trimgalore_com = conda_build + trimgalore_com
submit_commands[sample_1] = trimgalore_com
# BWA mem
if command == 'bwa':
for sample_1, sample_2 in all_samples:
sample_name = sample_1.replace('_R1_', '_').replace('_val_1', '')
sample_base = os.path.join(base_dir, output_dir, sample_name)
# Output file suffixes
sample_sam = '{}.sam'.format(sample_base)
bwa_mem_com = [bwa, 'mem', '-t', '8', genome_ref,
os.path.join('processed', 'trimmed', sample_1),
os.path.join('processed', 'trimmed', sample_2), '>',
sample_sam]
bwa_mem_com = conda_build + bwa_mem_com
submit_commands[sample_sam] = bwa_mem_com
# samtools sort to bam
if command == 'samtools':
sub_command = args.sub_command
for sample_id in all_samples:
sample_name = sample_id.replace('_R1_', '_').replace('_val_1', '')
sample_base = os.path.join(base_dir, output_dir, sample_name)
sample_sort_bam = '{}_sorted'.format(sample_base)
sample_sorted_fixmate_bam = '{}_sorted_fixmate.bam'.format(sample_base)
sample_sorted_position_bam = '{}_positionsort.bam'.format(sample_base)
sample_markdup_bam = '{}_rmdup.bam'.format(sample_base)
sample_markdup_bai = '{}.bai'.format(sample_base)
sample_bamindex_gatk = '{}.bai'.format(sample_base)
if sub_command == 'sort_name':
# `-n` sorts by name, which is required for fixmate
if genome == 'hg':
input_samp = os.path.join('processed', 'sam', sample_id)
elif genome == 'mm':
input_samp = os.path.join('processed', 'sam_mouse', sample_id)
samtools_com = [samtools, 'view', '-bS', input_samp, '|',
samtools, 'sort', '-n', '-',
sample_sort_bam]
elif sub_command == 'fixmate':
samtools_com = [samtools, 'fixmate',
os.path.join(input_dir, sample_id),
sample_sorted_fixmate_bam]
elif sub_command == 'sort_position':
samtools_com = [samtools, 'sort',
os.path.join(input_dir, sample_id),
sample_sorted_position_bam]
elif sub_command == 'rmdup':
samtools_com = [samtools, 'rmdup',
os.path.join(input_dir, sample_id),
sample_markdup_bam]
elif sub_command == 'index_bam':
sample_file = os.path.join(input_dir, sample_id)
samtools_com = [samtools, 'index', sample_file, sample_markdup_bai]
elif sub_command == 'index_bam_gatk':
sample_file = os.path.join(input_dir, sample_id)
sample_output_file = os.path.join(output_dir, '{}.bai'.format(sample_id))
samtools_com = [samtools, 'index', sample_file, sample_output_file]
elif sub_command == 'flagstat':
sample_file = os.path.join(input_dir, sample_id)
sample_output_file = os.path.join(output_dir, '{}.flagstat.txt'.format(sample_id))
samtools_com = [samtools, 'flagstat', sample_file, '>', sample_output_file]
elif sub_command == 'merge':
sample_file = os.path.join(input_dir, sample_id)
if 'L001' in sample_id and '.bai' not in sample_id:
output_bam = '{}.merged.bam'.format(sample_id.split('_')[0])
output_bam = os.path.join(output_dir, output_bam)
replicate_2 = sample_file.replace('L001', 'L002')
replicate_3 = sample_file.replace('L001', 'L003')
replicate_4 = sample_file.replace('L001', 'L004')
samtools_com = [samtools, 'merge', '-r', output_bam, sample_file,
replicate_2, replicate_3, replicate_4]
else:
continue
samtools_com = conda_build + samtools_com
submit_commands[sample_id] = samtools_com
# Remove mouse reads using ngs_disambiguate
if command == 'disambiguate':
human_dir = args.human_dir
mouse_dir = args.mouse_dir
for sample_id in all_samples:
disambiguate_com = [disambiguate, '--prefix', sample_id,
'--output-dir', output_dir, '--aligner', 'bwa',
'--', human_dir, mouse_dir]
disambiguate_com = conda_build + disambiguate_com
submit_commands[sample_id] = disambiguate_com
# call variants using GATK MuTect2
if command == 'variant':
sub_command = args.sub_command
num_threads = args.threads
conf = args.min_confidence
quality = args.min_base_quality_score
for sample_id in all_samples:
sample_name = sample_id.replace('_R1_', '_').replace('_val_1', '')
sample_base = os.path.join(base_dir, output_dir, sample_name)
sample_addreadgroup = '{}.rg.bam'.format(sample_base)
if sub_command == 'mutect2':
tumor_id = os.path.join(input_dir, sample_id)
output_file = os.path.join(output_dir, '{}.GATK.vcf'.format(sample_id))
variant_com = [gatk, '-T', 'MuTect2',
'--num_cpu_threads_per_data_thread', num_threads,
'--standard_min_confidence_threshold_for_calling',
conf,
'--min_base_quality_score', quality,
'-I:tumor', tumor_id, '-o', output_file,
'-R', genome_ref]
elif sub_command == 'add_read_groups':
tumor_id = os.path.join(input_dir, sample_id)
output_file = os.path.join(output_dir, '{}.rg.bam'.format(sample_id))
variant_com = [picard, 'AddOrReplaceReadGroups',
'I={}'.format(tumor_id),
'O={}'.format(output_file),
'RGID={}'.format(sample_id),
'RGLB=bwa-mem',
'RGPL=illumina',
'RGSM={}'.format(sample_id),
'RGPU={}'.format(sample_id),
'CREATE_INDEX=true',
'VALIDATION_STRINGENCY=SILENT']
variant_com = conda_build + java_load + variant_com
submit_commands[sample_id] = variant_com
if command == 'mosdepth':
for sample_id in all_samples:
tumor_id = os.path.join(input_dir, sample_id)
output_prefix = os.path.join(output_dir, sample_id)
mosdepth_com = [mosdepth, '--by', exonbed, output_prefix, tumor_id]
submit_commands[sample_id] = conda_build + java_load + mosdepth_com
if __name__ == '__main__':
# Submit jobs to cluster
for sample_id, com in submit_commands.items():
schedule_id = '{}_{}'.format(sample_id, command)
arguments.schedule_job(command=com, name=schedule_id, python=python,
nodes=nodes, cores=cores, walltime=walltime)