-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile-irap
293 lines (227 loc) · 9.95 KB
/
Snakefile-irap
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
282
283
284
285
286
287
288
289
290
291
292
293
import os
import csv
from tabulate import tabulate
from snakemake.utils import min_version
min_version("7.25.3")
include: "rules/common.smk"
metadata_df = read_metadata(config["metadata_path"], config["dataset_id"] )
SAMPLES = get_unique_ena_runs(metadata_df)
FIRST_SAMPLE = str(SAMPLES[0])
print(f"Samples to be analysed: {SAMPLES}")
wildcard_constraints:
sample="ERR\d+"
rule all:
input: "out/workflow-irap.done"
rule validating_fastq_pairs:
input:
fq1 = "out/{sample}/{sample}_1.fastq.gz",
fq2 = "out/{sample}/{sample}_2.fastq.gz"
output:
"out/{sample}/{sample}_validating_fastq_pairs.done"
log:
"logs/{sample}_validating_fastq_pairs.log"
conda:
"envs/fastq_utils.yml"
resources:
mem_mb=get_mem_mb
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
exec &> "{log}"
fastq_info {input.fq1} {input.fq2}
if [ $? -ne 0 ]; then
echo "ERROR: Failed fastq validation for {input.fq1} and {input.fq2}"
exit 1
else
echo "validation successful for {input.fq1} and {input.fq2}"
fi
touch {output}
"""
rule run_irap_stage0:
"""
This ensures Irap stage0 is run only once, for the first sample
"""
input:
fq1 = f"out/{FIRST_SAMPLE}/{FIRST_SAMPLE}_1.fastq.gz",
fq2 = f"out/{FIRST_SAMPLE}/{FIRST_SAMPLE}_2.fastq.gz",
check = f"out/{FIRST_SAMPLE}/{FIRST_SAMPLE}_validating_fastq_pairs.done"
output:
completed = f"out/stage0_{FIRST_SAMPLE}.txt"
conda: "envs/isl.yaml"
log: f"logs/irap_stage0_{FIRST_SAMPLE}.log"
params:
private_script=config["private_script"],
conf=config["irap_config"],
root_dir=config["atlas_ca_root"],
strand="both",
irapMem=16000000000,
irapDataOption="",
filename= f"{FIRST_SAMPLE}",
read_type = config["read_type"]
resources:
mem_mb=16000
threads: 16
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
exec &> "{log}"
source {params.private_script}/ega_bulk_env.sh
source {params.private_script}/ega_bulk_init.sh
source {params.root_dir}/isl/lib/generic_routines.sh
source {params.root_dir}/isl/lib/process_routines.sh
source {params.root_dir}/isl/lib/irap.sh
cp {params.private_script}/ega_bulk_env.sh $IRAP_SINGLE_LIB
library={params.filename}
echo "library: $library"
workingDir=$ISL_WORKING_DIR
source {params.root_dir}/scripts/aux.sh
which get_local_relative_library_path
which get_library_path
localFastqPath=$(get_local_relative_library_path $library )
echo "workingDir: $workingDir"
echo "localFastqPath: $localFastqPath"
mkdir -p $(dirname $workingDir/$localFastqPath)
pushd $workingDir > /dev/null
if [[ {params.read_type} == "se" ]]; then
# fastq is SE
cp {params.root_dir}/{input.fq1} $workingDir/${{localFastqPath}}.fastq
java -jar {params.root_dir}/scripts/validatefastq-assembly-0.1.1.jar --fastq1 $workingDir/${{localFastqPath}}.fastq
echo "Calling irap_single_lib...SE mode"
cmd="irap_single_lib -0 -A -f -o irap_single_lib -1 $workingDir/${{localFastqPath}}.fastq -c {params.conf} -s {params.strand} -m {params.irapMem} -t {threads} -C {params.irapDataOption}"
echo "stage0 will run now:"
eval $cmd
echo "stage0 finished"
else
# fastq is PE
echo "Calling irap_single_lib...PE mode"
cmd="irap_single_lib -0 -A -f -o irap_single_lib -1 {params.root_dir}/{input.fq1} -2 {params.root_dir}/{input.fq2} -c {params.conf} -s {params.strand} -m {params.irapMem} -t {threads} -C {params.irapDataOption}"
echo "stage0 will run now:"
eval $cmd
echo "stage0 finished"
fi
popd
touch {output.completed}
"""
rule run_irap:
input:
fq1 = "out/{sample}/{sample}_1.fastq.gz",
fq2 = "out/{sample}/{sample}_2.fastq.gz",
check = "out/{sample}/{sample}_validating_fastq_pairs.done",
stage0_completed=rules.run_irap_stage0.output.completed
output:
completed = "out/{sample}/{sample}_irap_completed.done"
conda: "envs/isl.yaml"
log: "logs/{sample}_irap.log"
params:
private_script=config["private_script"],
conf=config["irap_config"],
root_dir=config["atlas_ca_root"],
strand="both",
irapDataOption="",
filename="{sample}",
first_sample=f"{FIRST_SAMPLE}",
read_type = config["read_type"]
resources:
mem_mb=get_mem_mb
threads: 24
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
exec &> "{log}"
irapMem=$(("{resources.mem_mb}000000"))
source {params.private_script}/ega_bulk_env.sh
source {params.root_dir}/isl/lib/generic_routines.sh
source {params.root_dir}/isl/lib/process_routines.sh
source {params.root_dir}/isl/lib/irap.sh
library={params.filename}
echo "library: $library"
workingDir=$ISL_WORKING_DIR
source {params.root_dir}/scripts/aux.sh
which get_local_relative_library_path
which get_library_path
localFastqPath=$(get_local_relative_library_path $library )
echo "workingDir: $workingDir"
echo "localFastqPath: $localFastqPath"
echo "sample: {wildcards.sample}"
echo "first sample: {params.first_sample}"
if [[ "{wildcards.sample}" != "{params.first_sample}" ]]; then
mkdir -p $(dirname $workingDir/$localFastqPath)
fi
pushd $workingDir > /dev/null
export THREADS={threads} # for irap_single_lib
if [[ {params.read_type} == "se" ]]; then
# fastq is SE
cp {params.root_dir}/{input.fq1} $workingDir/${{localFastqPath}}.fastq
java -jar {params.root_dir}/scripts/validatefastq-assembly-0.1.1.jar --fastq1 $workingDir/${{localFastqPath}}.fastq
echo "Calling irap_single_lib...SE mode"
cmd="irap_single_lib -A -f -o irap_single_lib -1 $workingDir/${{localFastqPath}}.fastq -c {params.conf} -s {params.strand} -m $irapMem -t {threads} -C {params.irapDataOption}"
echo "SE IRAP will run now:"
eval $cmd
echo "irap_single_lib SE finished for {wildcards.sample}"
else
# fastq is PE
echo "Calling irap_single_lib..."
cmd="irap_single_lib -A -f -o irap_single_lib -1 {params.root_dir}/{input.fq1} -2 {params.root_dir}/{input.fq2} -c {params.conf} -s {params.strand} -m $irapMem -t {threads} -C {params.irapDataOption}"
echo "PE IRAP will run now:"
eval $cmd
echo "irap_single_lib PE finished for {wildcards.sample}"
fi
popd
# the files to collected by aggregation from irap are in $IRAP_SINGLE_LIB/out
touch {output.completed}
"""
rule prepare_aggregation:
"""
Prepare aggregation of irap outputs, which are at
$ISL_WORKING_DIR/irap_single_lib/{root_dir}/out/{sample}
and copy essential aggregation files into:
$IRAP_SINGLE_LIB/out/{sample[0:5]}/{sample}
"""
input: "out/{sample}/{sample}_irap_completed.done"
output: "out/{sample}/{sample}_prepare_aggregation.done"
log: "logs/{sample}_prepare_agreggation.log"
params:
private_script=config["private_script"],
root_dir=config["atlas_ca_root"]
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
exec &> "{log}"
source {params.private_script}/ega_bulk_env.sh
prefix_sample=$(echo {wildcards.sample} | cut -c1-6)
# move irap outputs to $ISL_WORKING_DIR/irap_single_lib/out/sample[0:5]/sample
destination_dir=$ISL_RESULTS_DIR/$prefix_sample/{wildcards.sample}
mkdir -p $destination_dir
cp $ISL_WORKING_DIR/irap_single_lib{params.root_dir}/out/{wildcards.sample}/{wildcards.sample}*kallisto* $destination_dir/
cp $ISL_WORKING_DIR/irap_single_lib{params.root_dir}/out/{wildcards.sample}/{wildcards.sample}*htseq2* $destination_dir/
cp $ISL_WORKING_DIR/irap_single_lib{params.root_dir}/out/{wildcards.sample}/{wildcards.sample}.versions.tsv $destination_dir/irap.versions.tsv
cp -r $ISL_WORKING_DIR/irap_single_lib{params.root_dir}/out/{wildcards.sample}/logs $destination_dir/
cp -r $ISL_WORKING_DIR/irap_single_lib{params.root_dir}/out/{wildcards.sample}/qc $destination_dir/
# file checks
if ls "$destination_dir"/*kallisto* 1> /dev/null 2>&1; then
echo "Files matching the pattern '*kallisto*' exist in the folder $destination_dir"
else
echo "No files matching the pattern '*kallisto*' exist in the folder $destination_dir"
exit 1
fi
if ls "$destination_dir"/*htseq2* 1> /dev/null 2>&1; then
echo "Files matching the pattern '*htseq2*' exist in the folder $destination_dir"
else
echo "No files matching the pattern '*htseq2*' exist in the folder $destination_dir"
exit 1
fi
echo "tophat2 align summary:"
cat $ISL_WORKING_DIR/processing_data/h/homo_sapiens/irap_qc/tophat2/*/{wildcards.sample}/{wildcards.sample}/align_summary.txt
echo "Controlled-access analysis for EGA dataset completed for {wildcards.sample}. Ready for aggregation"
touch {output}
"""
rule final_workflow_check:
input:
expand(["out/{sample}/{sample}_prepare_aggregation.done"], sample=SAMPLES)
output:
"out/workflow-irap.done"
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
touch {output}
"""