Snakefile 28 KB
Newer Older
1
2
from tools import *
from raw_reads import raw_reads
3
workdir: config['params']['results_dir']
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
import re
import snakemake.utils
import csv

#############
# Wildcards #
#############

STEPS = config["steps"]
PREPARE_REPORT_OUTPUTS = config["prepare_report_outputs"]
PREPARE_REPORT_SCRIPTS = config["prepare_report_scripts"]
OUTPUTS = config["outputs"]
PARAMS_INFO = config["params_info"]
mmassaviol's avatar
mmassaviol committed
18
MULTIQC = config["multiqc"]
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
config = config["params"]

##########
# Inputs #
##########

# raw_inputs function call
raw_reads = raw_reads(config['results_dir'], config['sample_dir'], config['SeOrPe'])
config.update(raw_reads)
SAMPLES = raw_reads['samples']

def get_individus():
    if (config["demultiplexing"] == "null"):
        return SAMPLES
    else:
mmassaviol's avatar
mmassaviol committed
34
        with open(config["demultiplexing__demultiplexing_astrid_cruaud_barcodes"], mode="r") as infile:
35
36
37
38
39
40
41
            reader = csv.reader(infile,delimiter='\t')
            next(reader, None) # remove header (first line)
            return [row[0] for row in reader]

INDIVIDUS = get_individus()

# Tools inputs functions
mmassaviol's avatar
mmassaviol committed
42
def quality_control__fastqc_PE_inputs():
43
44
45
46
47
	inputs = dict()
	inputs["read"] = raw_reads["read"]
	inputs["read2"] = raw_reads["read2"]
	return inputs

mmassaviol's avatar
mmassaviol committed
48
def quality_control__fastqc_SE_inputs():
49
50
51
52
	inputs = dict()
	inputs["read"] = raw_reads["read"]
	return inputs

mmassaviol's avatar
mmassaviol committed
53
def qcfilter_adaptertrim__trimmomatic_PE_inputs():
54
55
56
57
58
	inputs = dict()
	inputs["read"] = raw_reads["read"]
	inputs["read2"] = raw_reads["read2"]
	return inputs

mmassaviol's avatar
mmassaviol committed
59
def qcfilter_adaptertrim__trimmomatic_SE_inputs():
60
61
62
63
	inputs = dict()
	inputs["read"] = raw_reads["read"]
	return inputs

mmassaviol's avatar
mmassaviol committed
64
def merge_overlapps__flash_inputs():
65
	inputs = dict()
mmassaviol's avatar
mmassaviol committed
66
67
	inputs["read"] = rules.qcfilter_adaptertrim__trimmomatic_PE.output.readFP
	inputs["read2"] = rules.qcfilter_adaptertrim__trimmomatic_PE.output.readRP
68
69
	return inputs

mmassaviol's avatar
mmassaviol committed
70
def demultiplexing__demultiplexing_astrid_cruaud_inputs():
71
	inputs = dict()
mmassaviol's avatar
mmassaviol committed
72
	inputs["extended_frags"] = expand(rules.merge_overlapps__flash.output.extendedFrags, sample=SAMPLES)
73
74
	return inputs

mmassaviol's avatar
mmassaviol committed
75
def assembling__velvet_inputs():
76
	inputs = dict()
mmassaviol's avatar
mmassaviol committed
77
	inputs["merged_reads"] = config["results_dir"]+"/"+config["demultiplexing__demultiplexing_astrid_cruaud_output_dir"]+"/{sample}_fq"
78
79
	return inputs

mmassaviol's avatar
mmassaviol committed
80
def contigmaptouce__lastz_inputs():
81
	inputs = dict()
mmassaviol's avatar
mmassaviol committed
82
	inputs["contigs"] = rules.assembling__velvet.output.contigs
83
84
	return inputs

mmassaviol's avatar
mmassaviol committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def mapping_check_bf__samtools_stats_inputs():
	inputs = dict()
	inputs["bam"] = rules.contigmaptouce__lastz.output.align_on_ref
	return inputs

def mapping_check_af__samtools_stats_inputs():
	inputs = dict()
	inputs["bam"] = rules.contigmaptouce__lastz.output.filtered
	return inputs

def convert_to_phylip__sam_to_phylip_inputs():
	inputs = dict()
	inputs["sams"] = expand(rules.contigmaptouce__lastz.output.filtered, sample=INDIVIDUS)
	return inputs
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

def prepare_report_inputs():
    inputs = list()
    for step in STEPS:
        inputs.extend(step_outputs(step["name"]))
    return inputs

def prepare_report_scripts():
    scripts = list()
    for step in STEPS:
        tool = config[step["name"]]
        prefix = tool+".prepare.report."
        if type(PREPARE_REPORT_SCRIPTS) == type(""):
            if prefix in PREPARE_REPORT_SCRIPTS:
                scripts.append("/workflow/scripts/"+PREPARE_REPORT_SCRIPTS)
        else :        
            script = [s for s in PREPARE_REPORT_SCRIPTS if prefix in s]
            if (len(script)==1):
                scripts.append("/workflow/scripts/"+script[0])
    return scripts

def prepare_report_outputs():
    outputs = list()
    outputs.append(config["results_dir"] + "/outputs_mqc.csv")
    for step in STEPS:
        tool = config[step["name"]]
        if (tool in PREPARE_REPORT_OUTPUTS.keys()):
            if type(PREPARE_REPORT_OUTPUTS[tool]) == type(""):
                outputs.append(config["results_dir"]+"/"+tool+"/"+PREPARE_REPORT_OUTPUTS[tool])
            else:
                for output in PREPARE_REPORT_OUTPUTS[tool]:
                    outputs.append(config["results_dir"]+"/"+tool+"/"+output)
    return outputs

def multiqc_inputs():
    # Need prepare_report inputs and outputs in case prepare_reports has no outputs
    return prepare_report_outputs()

###########
# Outputs #
###########

def step_outputs(step):
	outputs = list()
	if (step == "quality_control" and config['SeOrPe'] == 'PE' ):
mmassaviol's avatar
mmassaviol committed
144
		outputs = expand(rules.quality_control__fastqc_PE.output, sample=SAMPLES)
145
146
		
	if (step == "qcfilter_adaptertrim" and config['SeOrPe'] == 'PE' ):
mmassaviol's avatar
mmassaviol committed
147
		outputs = expand(rules.qcfilter_adaptertrim__trimmomatic_PE.output, sample=SAMPLES)
148
149
		
	if (step == "merge_overlapps"  ):
mmassaviol's avatar
mmassaviol committed
150
		outputs = expand(rules.merge_overlapps__flash.output, sample=SAMPLES)
151
152
		
	if (step == "demultiplexing"  ):
mmassaviol's avatar
mmassaviol committed
153
		outputs = rules.demultiplexing__demultiplexing_astrid_cruaud.output
154
155
		
	if (step == "assembling"  ):
mmassaviol's avatar
mmassaviol committed
156
		outputs = expand(rules.assembling__velvet.output, sample=INDIVIDUS)
157
158
		
	if (step == "contigmaptouce"  ):
mmassaviol's avatar
mmassaviol committed
159
160
161
162
163
164
165
166
167
168
		outputs = expand(rules.contigmaptouce__lastz.output, sample=INDIVIDUS)

	if (step == "mapping_check_bf"  ):
		outputs = expand(rules.mapping_check_bf__samtools_stats.output, sample=INDIVIDUS)

	if (step == "mapping_check_af"  ):
		outputs = expand(rules.mapping_check_af__samtools_stats.output, sample=INDIVIDUS)

	if (step == "convert_to_phylip"  ):
		outputs = rules.convert_to_phylip__sam_to_phylip.output
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
		
	if (step == "all"):
		outputs = list(rules.multiqc.output)

	return outputs

# get outputs for each choosen tools
def workflow_outputs(step):
	outputs = list()
	outputs.extend(step_outputs(step))
	return outputs

#########
# Rules #
#########

mmassaviol's avatar
mmassaviol committed
185
186

rule quality_control__fastqc_SE:
187
    input:
mmassaviol's avatar
mmassaviol committed
188
        **quality_control__fastqc_SE_inputs()
189
    output:
mmassaviol's avatar
mmassaviol committed
190
191
192
        html = config["results_dir"]+'/'+config["quality_control__fastqc_SE_output_dir"]+'/{sample}'+config["sample_suffix"].split(".")[0]+'_fastqc.html',
        zip = config["results_dir"]+'/'+config["quality_control__fastqc_SE_output_dir"]+'/{sample}'+config["sample_suffix"].split(".")[0]+'_fastqc.zip',
    log: config["results_dir"]+'/logs/' + config["quality_control__fastqc_SE_output_dir"] + '/{sample}_fastqc_log.txt'
193
194
    threads: 1
    params:
mmassaviol's avatar
mmassaviol committed
195
196
        command = config["quality_control__fastqc_SE_command"],
        output_dir = config["results_dir"]+'/'+config["quality_control__fastqc_SE_output_dir"]
197
198
199
200
201
202
203
204
    shell:
        "{params.command} "
        "{input.read} "
        "-t {threads} "
        "-o {params.output_dir} "
        "--quiet "
        "|& tee {log}"

mmassaviol's avatar
mmassaviol committed
205
rule quality_control__fastqc_PE:
206
    input:
mmassaviol's avatar
mmassaviol committed
207
        **quality_control__fastqc_PE_inputs()
208
    output:
mmassaviol's avatar
mmassaviol committed
209
210
211
212
213
        html1 = config["results_dir"]+'/'+config["quality_control__fastqc_PE_output_dir"]+'/{sample}_R1'+config["sample_suffix"].split(".")[0]+'_fastqc.html',
        zip1 = config["results_dir"]+'/'+config["quality_control__fastqc_PE_output_dir"]+'/{sample}_R1'+config["sample_suffix"].split(".")[0]+'_fastqc.zip',
        html2 = config["results_dir"]+'/'+config["quality_control__fastqc_PE_output_dir"]+'/{sample}_R2'+config["sample_suffix"].split(".")[0]+'_fastqc.html',
        zip2 = config["results_dir"]+'/'+config["quality_control__fastqc_PE_output_dir"]+'/{sample}_R2'+config["sample_suffix"].split(".")[0]+'_fastqc.zip',
    log: config["results_dir"]+'/logs/' + config["quality_control__fastqc_PE_output_dir"] + '/{sample}_fastqc_log.txt'
214
215
    threads: 2
    params:
mmassaviol's avatar
mmassaviol committed
216
217
        command = config["quality_control__fastqc_PE_command"],
        output_dir = config["results_dir"]+'/'+config["quality_control__fastqc_PE_output_dir"]
218
219
220
221
222
223
224
225
226
    shell:
        "{params.command} "
        "{input.read} "
        "{input.read2} "
        "-t {threads} "
        "-o {params.output_dir} "
        "--quiet "
        "|& tee {log}"

mmassaviol's avatar
mmassaviol committed
227
ruleorder:  quality_control__fastqc_PE > quality_control__fastqc_SE
228

mmassaviol's avatar
mmassaviol committed
229
rule qcfilter_adaptertrim__trimmomatic_PE:
230
    input:
mmassaviol's avatar
mmassaviol committed
231
        **qcfilter_adaptertrim__trimmomatic_PE_inputs()
232
    output:
mmassaviol's avatar
mmassaviol committed
233
234
235
236
        readFP = config["results_dir"]+"/"+config["qcfilter_adaptertrim__trimmomatic_PE_output_dir"]+"/{sample}_forward_paired.fq.gz",
        readFU = config["results_dir"]+"/"+config["qcfilter_adaptertrim__trimmomatic_PE_output_dir"]+"/{sample}_forward_unpaired.fq.gz",
        readRP = config["results_dir"]+"/"+config["qcfilter_adaptertrim__trimmomatic_PE_output_dir"]+"/{sample}_reverse_paired.fq.gz",
        readRU = config["results_dir"]+"/"+config["qcfilter_adaptertrim__trimmomatic_PE_output_dir"]+"/{sample}_reverse_unpaired.fq.gz",
237
    log:
mmassaviol's avatar
mmassaviol committed
238
        config["results_dir"]+'/logs/' + config["qcfilter_adaptertrim__trimmomatic_PE_output_dir"] + '/{sample}_trimmomatic_log.txt'
239
    params:
mmassaviol's avatar
mmassaviol committed
240
241
242
243
        command = config["qcfilter_adaptertrim__trimmomatic_PE_command"],
        qc_score = config["qcfilter_adaptertrim__trimmomatic_qc_score"],
        ILLUMINACLIP = "ILLUMINACLIP:" + config["qcfilter_adaptertrim__trimmomatic_fastaWithAdapters"] + ":" + config["qcfilter_adaptertrim__trimmomatic_illuminaclip"] if (config["qcfilter_adaptertrim__trimmomatic_fastaWithAdapters"] != "") else "",
        otherparams = config["qcfilter_adaptertrim__trimmomatic_otherparams"]
244
    threads:
245
        config["qcfilter_adaptertrim__trimmomatic_threads"]
246
247
248
249
250
251
252
253
254
255
256
257
258
259
    shell:
        "{params.command} "
        "{params.qc_score} "
        "-threads {threads} "
        "{input.read} "
        "{input.read2} "
        "{output.readFP} "
        "{output.readFU} "
        "{output.readRP} "
        "{output.readRU} "
        "{params.ILLUMINACLIP} "
        "{params.otherparams} "
        "|& tee {log}"

mmassaviol's avatar
mmassaviol committed
260
rule qcfilter_adaptertrim__trimmomatic_SE:
261
    input:
mmassaviol's avatar
mmassaviol committed
262
        **qcfilter_adaptertrim__trimmomatic_SE_inputs()
263
    output:
mmassaviol's avatar
mmassaviol committed
264
        read = config["results_dir"]+"/"+config["qcfilter_adaptertrim__trimmomatic_SE_output_dir"]+"/{sample}_trimmed.fq.gz",
265
    log:
mmassaviol's avatar
mmassaviol committed
266
        config["results_dir"]+'/logs/' + config["qcfilter_adaptertrim__trimmomatic_PE_output_dir"] + '/{sample}_trimmomatic_log.txt'
267
    params:
mmassaviol's avatar
mmassaviol committed
268
269
270
271
        command = config["qcfilter_adaptertrim__trimmomatic_SE_command"],
        qc_score = config["qcfilter_adaptertrim__trimmomatic_qc_score"],
        ILLUMINACLIP = "ILLUMINACLIP:" + config["qcfilter_adaptertrim__trimmomatic_fastaWithAdapters"] + ":" + config["qcfilter_adaptertrim__trimmomatic_illuminaclip"] if (config["qcfilter_adaptertrim__trimmomatic_fastaWithAdapters"] != "") else "",
        otherparams = config["qcfilter_adaptertrim__trimmomatic_otherparams"]
272
    threads:
273
        config["qcfilter_adaptertrim__trimmomatic_threads"]
274
275
276
277
278
279
280
281
282
283
    shell:
        "{params.command} "
        "{params.qc_score} "
        "-threads {threads} "
        "{input} "
        "{output} "
        "{params.ILLUMINACLIP} "
        "{params.otherparams} "
        "|& tee {log}"

mmassaviol's avatar
mmassaviol committed
284
ruleorder:  qcfilter_adaptertrim__trimmomatic_PE > qcfilter_adaptertrim__trimmomatic_SE
285
286


mmassaviol's avatar
mmassaviol committed
287
rule merge_overlapps__flash:
288
    input:
mmassaviol's avatar
mmassaviol committed
289
        **merge_overlapps__flash_inputs()
290
    output:
mmassaviol's avatar
mmassaviol committed
291
292
293
294
295
        extendedFrags = config["results_dir"] + "/" +  config["merge_overlapps__flash_output_dir"] + "/{sample}.extendedFrags.fastq",
        notCombined_1 = config["results_dir"] + "/" +  config["merge_overlapps__flash_output_dir"] + "/{sample}.notCombined_1.fastq",
        notCombined_2 = config["results_dir"] + "/" +  config["merge_overlapps__flash_output_dir"] + "/{sample}.notCombined_2.fastq",
        hist = config["results_dir"] + "/" +  config["merge_overlapps__flash_output_dir"] + "/{sample}.flash.hist",
        histogram = config["results_dir"] + "/" +  config["merge_overlapps__flash_output_dir"] + "/{sample}.histogram",
296
    params:
mmassaviol's avatar
mmassaviol committed
297
298
299
300
301
302
        output_dir = config["results_dir"] + "/" + config["merge_overlapps__flash_output_dir"],
        command = config["merge_overlapps__flash_command"],
        min_overlap = config["merge_overlapps__flash_min_overlap"],
        max_overlap = config["merge_overlapps__flash_max_overlap"],
        max_mismatch_density = config["merge_overlapps__flash_max_mismatch_density"],
        tmp_hist = config["results_dir"] + "/" +  config["merge_overlapps__flash_output_dir"] + "/{sample}.hist", # output from flash to be renamed for multiqc detection
303
    log:
mmassaviol's avatar
mmassaviol committed
304
        config["results_dir"]+"/logs/" + config["merge_overlapps__flash_output_dir"] + "/{sample}_flash_log.txt"
305
    threads: config["merge_overlapps__flash_threads"]
306
307
308
309
310
311
312
313
314
315
    shell:
        "{params.command} "
        "-x {params.max_mismatch_density} "
        "-m {params.min_overlap} "
        "-M {params.max_overlap} "
        "-d {params.output_dir} "
        "-o {wildcards.sample} "
        "-t {threads} "
        "{input.read} "
        "{input.read2} "
316
317
        "|& tee {log}; "
        "mv {params.tmp_hist} {output.hist}" # with flash in hist name it is detected by multiqc
318
319
320



mmassaviol's avatar
mmassaviol committed
321
rule demultiplexing__demultiplexing_astrid_cruaud:
322
    input:
mmassaviol's avatar
mmassaviol committed
323
        **demultiplexing__demultiplexing_astrid_cruaud_inputs()
324
    output:
mmassaviol's avatar
mmassaviol committed
325
326
        demultiplexed = expand(config["results_dir"] + "/" + config["demultiplexing__demultiplexing_astrid_cruaud_output_dir"] + "/{sample}_fq", sample=INDIVIDUS),
        stats = config["results_dir"] + "/" + config["demultiplexing__demultiplexing_astrid_cruaud_output_dir"] + "/stat_demultiplexing_mqc.csv",
327
    params:
mmassaviol's avatar
mmassaviol committed
328
329
        output_dir = config["results_dir"] + "/" + config["demultiplexing__demultiplexing_astrid_cruaud_output_dir"],
        barcodes = config["demultiplexing__demultiplexing_astrid_cruaud_barcodes"]
330
    log:
mmassaviol's avatar
mmassaviol committed
331
        config["results_dir"]+"/logs/" + config["demultiplexing__demultiplexing_astrid_cruaud_output_dir"] + "/demultiplexing_astrid_cruaud_log.txt"
332
    threads: config["demultiplexing__demultiplexing_astrid_cruaud_threads"]
333
334
335
336
337
338
    shell:
        "cd {params.output_dir} && "
        #"touch {output.demultiplexed} && "
        # tail to remove header (sample barcode_P1 barcode_P2 barcode_P2_rev term)
        "tail -n +2 {params.barcodes} | parallel --jobs {threads} --colsep '\\t' \"grep -B 1 --no-group-separator -i '^{{2}}.*{{4}}$' {input.extended_frags} > {{1}}'_fq'\" |& tee {log} ; " # test if --no-group-separator works with the next parts of the workflow
        # nb reads/sample :
mmassaviol's avatar
mmassaviol committed
339
        "echo -e \"# description: 'Number of reads for each individu after demultiplexing'\\n# section_name: 'Demultiplexing stats'\\n# plot_type: 'table'\\n# pconfig:\\n#  format: '{{:,.0f}}'\\nIndividu\\tNumber of reads\" > {output.stats}; "
340
        "for i in $(ls *_fq); do echo -e $i'\\t'$(grep -ic '@' $i); done >> {output.stats} 2>> {log}; " # count with grep @ because no risk of @ in quality sequence as it is no more present in the file
341
342
343
344
        # remove barcodes from beginning / end of reads
        "tail -n +2 {params.barcodes} | parallel --jobs {threads} --colsep '\\t' \"sed -i 's/^{{2}}//g;s/{{4}}$//g' {{1}}'_fq'\" |& tee -a {log}"


mmassaviol's avatar
mmassaviol committed
345
rule assembling__velvet:
346
	input:
mmassaviol's avatar
mmassaviol committed
347
		**assembling__velvet_inputs(),
348
	output: 
mmassaviol's avatar
mmassaviol committed
349
350
351
		contigs = config["results_dir"] + "/" + config["assembling__velvet_output_dir"] + "/{sample}/contigs.fa",
		stats = config["results_dir"] + "/" + config["assembling__velvet_output_dir"] + "/{sample}/stats.txt",
		LastGraph = config["results_dir"] + "/" + config["assembling__velvet_output_dir"] + "/{sample}/LastGraph",
352
	params: 
mmassaviol's avatar
mmassaviol committed
353
354
355
356
		output_dir = config["results_dir"] + "/" + config["assembling__velvet_output_dir"]+ "/{sample}",
		command = config["assembling__velvet_command"],
		min_contig_lgth = config["assembling__velvet_min_contig_lgth"],
		hash_length = config["assembling__velvet_hash_length"],
357
	log: 
mmassaviol's avatar
mmassaviol committed
358
		config["results_dir"] + "/logs/" + config["assembling__velvet_output_dir"] + "/{sample}_velvet_log.txt"
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
	shell: 
		# VELVETH
		"{params.command}h "
		"{params.output_dir} "
		"{params.hash_length} "
		"-fastq "
		"{input.merged_reads} "
		"|& tee {log}; "
		# VELVETG
		"{params.command}g "
		"{params.output_dir} "
		"-min_contig_lgth {params.min_contig_lgth} "
		"|& tee -a {log}"


mmassaviol's avatar
mmassaviol committed
374
rule contigmaptouce__lastz:
375
	input:
mmassaviol's avatar
mmassaviol committed
376
377
		**contigmaptouce__lastz_inputs(),
		reference_fasta = config["contigmaptouce__lastz_reference_fasta"],
378
	output: 
mmassaviol's avatar
mmassaviol committed
379
380
381
		align_on_ref = config["results_dir"] + "/" + config["contigmaptouce__lastz_output_dir"] + "/{sample}.vs_probes_default.sam",
		filtered = config["results_dir"] + "/" + config["contigmaptouce__lastz_output_dir"] + "/filtered/{sample}.vs_probes_default.filtered.sam",
		stats = config["results_dir"] + "/" + config["contigmaptouce__lastz_output_dir"] + "/stats/{sample}_lastz_sam_stats.tsv",
382
	params: 
mmassaviol's avatar
mmassaviol committed
383
384
		output_dir = config["results_dir"] + "/" + config["contigmaptouce__lastz_output_dir"]+ "/",
		command = config["contigmaptouce__lastz_command"]
385
	log: 
mmassaviol's avatar
mmassaviol committed
386
		config["results_dir"] + "/logs/" + config["contigmaptouce__lastz_output_dir"] + "/{sample}_lastz_log.txt"
387
388
389
390
391
392
393
394
	shell: 
		"{params.command} "
		"{input.reference_fasta}[multiple,unmask] "
		"{input.contigs}[unmask] "
		"--gapped "
		"--format=softsam "
		"--ambiguous=iupac "
		"--output='{output.align_on_ref}' "
395
		"|& tee {log}; "
396

397
398
		# stats
        "NB_ENTRIES=$(samtools view {output.align_on_ref} | wc -l); "
399

400
        "if [ $NB_ENTRIES -gt 0 ]; then "
401

402
403
404
405
406
407
408
409
        "NB_LOCUS=$(samtools view {output.align_on_ref} | cut -f 3 | sort -u | wc -l); "
        "LIST_MULTI_LOCUS_MAPPED=$(samtools view {output.align_on_ref} | cut -f 1,3 | uniq | awk 'BEGIN {{ FS=OFS=SUBSEP=\"\\t\"; RS=\"\\n\"}}{{arr[$2]+=1 }}END {{for (i in arr) if (arr[i] > 1) print i}}' -); "
        "NB_MULTI_LOCUS_MAPPED=$(echo $LIST_MULTI_LOCUS_MAPPED | wc -w); "
        "LIST_MULTI_CLUSTER_MAPPED=$(samtools view {output.align_on_ref} | cut -f 1,3 | uniq | awk 'BEGIN {{ FS=OFS=SUBSEP=\"\\t\"; RS=\"\\n\"}}{{arr[$1]+=1 }}END {{for (i in arr) if (arr[i] > 1) print i}}' -); "
        "NB_MULTI_CLUSTER_MAPPED=$(echo $LIST_MULTI_CLUSTER_MAPPED | wc -w); "
        "printf '%s\\n' \"${{LIST_MULTI_CLUSTER_MAPPED[@]}}\" > /tmp/{wildcards.sample}_lastz.tmp; "
        "printf '%s\\n' \"${{LIST_MULTI_LOCUS_MAPPED[@]}}\" >> /tmp/{wildcards.sample}_lastz.tmp; "
        "sed -i '/^$/d' /tmp/{wildcards.sample}_lastz.tmp; "
410

411
412
413
414
415
416
417
418
419
420
421
422
423
        # filter locus and clusters multimapped
        "samtools view -h {output.align_on_ref} | grep -v -f /tmp/{wildcards.sample}_lastz.tmp - > {output.filtered}; "

        "else "
        "NB_LOCUS=0;"
        "NB_MULTI_LOCUS_MAPPED=0;"
        "NB_MULTI_CLUSTER_MAPPED=0;"
        "touch {output.filtered}; "
    	"fi;\n"

        "NB_ENTRIES_AFTER_FILTER=$(samtools view {output.filtered} | wc -l); "
		
        "echo -e NB_ENTRIES'\\t'NB_LOCUS'\\t'NB_MULTI_LOCUS_MAPPED'\\t'NB_MULTI_CLUSTER_MAPPED'\\t'NB_ENTRIES_AFTER_FILTER'\\n'$NB_ENTRIES'\\t'$NB_LOCUS'\\t'$NB_MULTI_LOCUS_MAPPED'\\t'$NB_MULTI_CLUSTER_MAPPED'\\t'$NB_ENTRIES_AFTER_FILTER > {output.stats}; "
424

mmassaviol's avatar
mmassaviol committed
425
426
427
428
429
430
431
432
rule mapping_check_bf__samtools_stats:
    input:
        **mapping_check_bf__samtools_stats_inputs()
    output:
        stats = config["results_dir"]+'/' + config["mapping_check_bf__samtools_stats_output_dir"] + '/{sample}.txt',
    log:
        config["results_dir"]+'/logs/' + config["mapping_check_bf__samtools_stats_output_dir"] + '/{sample}_samtools_stats_log.txt'
    threads:
433
        config["mapping_check_bf__samtools_stats_threads"]
mmassaviol's avatar
mmassaviol committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
    params:
        command = config["mapping_check_bf__samtools_stats_command"],
        output_dir = config["results_dir"]+'/'+config["mapping_check_bf__samtools_stats_output_dir"]
    shell:
        "{params.command} {input.bam} -@ {threads} 2> {log} > {output.stats}"

rule mapping_check_af__samtools_stats:
    input:
        **mapping_check_af__samtools_stats_inputs()
    output:
        stats = config["results_dir"]+'/' + config["mapping_check_af__samtools_stats_output_dir"] + '/{sample}.txt',
    log:
        config["results_dir"]+'/logs/' + config["mapping_check_af__samtools_stats_output_dir"] + '/{sample}_samtools_stats_log.txt'
    threads:
448
        config["mapping_check_af__samtools_stats_threads"]
mmassaviol's avatar
mmassaviol committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
    params:
        command = config["mapping_check_af__samtools_stats_command"],
        output_dir = config["results_dir"]+'/'+config["mapping_check_af__samtools_stats_output_dir"]
    shell:
        "{params.command} {input.bam} -@ {threads} 2> {log} > {output.stats}"

rule convert_to_phylip__sam_to_phylip:
	input:
		**convert_to_phylip__sam_to_phylip_inputs(),
	output: 
		phylips = directory(config["results_dir"] + "/" + config["convert_to_phylip__sam_to_phylip_output_dir"] + "/"),
	params: 
		output_dir = config["results_dir"] + "/" + config["convert_to_phylip__sam_to_phylip_output_dir"]+ "/",
		input_dir = lambda w, input: os.path.dirname(input.sams[0])
	log: 
		config["results_dir"] + "/logs/" + config["convert_to_phylip__sam_to_phylip_output_dir"] + "/sam_to_phylip_log.txt"
	threads: 1
	shell: 
		"for file in $(ls {params.input_dir}/*.sam);\n"
		"do\n"
		"	NB_ENTRIES=$(samtools view $file | wc -l);\n"
	        "	if [ $NB_ENTRIES -gt 0 ]; then\n"
		"		sample=$(basename $file | cut -f 1 -d '.');\n"
				# Create sam header for each locus if not done yet (needed for samtools fasta)
		"		grep \"^@SQ\" $file | awk '{{split($2,a,\":\"); if(system(\"test -f {params.output_dir}/\"a[2]\".sam\")!=0) {{print $0 > \"{params.output_dir}/\"a[2]\".sam\"}} close(\"{params.output_dir}/\"a[2])\".sam\"}}';\n"
				# Group entries by locus
		"		samtools view $file | awk -v sample=\"$sample\" 'BEGIN {{OFS=\"\t\"}} {{$1=sample; print $0 >> \"{params.output_dir}/\"$3\".sam\"}} close(\"{params.output_dir}/\"$3\".sam\")';\n"
		"	fi;\n"
		"done;\n"

		"echo 'Group by reference done';\n"

		# sam to fasta
		"for file in $(ls {params.output_dir}/*.sam);\n"
		"do\n"
		"	NB_ENTRIES=$(samtools view $file | wc -l);\n"
	        "	if [ $NB_ENTRIES -gt 0 ]; then\n"
		"		outname=$(echo $file | cut -f 1 -d '.');\n"
		"		samtools fasta $file > ${{outname}}.fasta;\n"
		"	fi;\n"
		"done;"

		"echo 'Turn sam into fasta done';\n"

		# fasta to phylip
		"for file in $(ls {params.output_dir}/*.fasta);\n"
		"do\n"
		"	outname=$(echo $file | cut -f 1 -d '.');\n"
		"	seqret -sequence $file -osf phylip3 -outseq ${{outname}}.phylip;\n"
		"done;"

		"echo 'Turn fasta into phylip done';\n"

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525


import collections

rule prepare_report:
    input:
        *prepare_report_inputs(),
    output:
        *prepare_report_outputs(),
        config_multiqc = config["results_dir"] + "/config_multiqc.yaml",
        params_tab = config["results_dir"] + "/params_tab_mqc.csv",
        versions_tab = config["results_dir"] + "/Tools_version_mqc.csv",
        citations_tab = config["results_dir"] + "/Citations_mqc.csv"
    params:
        params_file = workflow.overwrite_configfiles,
        results_dir = config["results_dir"]
    log:
        config["results_dir"]+"/logs/prepare_report_log.txt"
    run:
        # Specific scripts for each tool
        for script in prepare_report_scripts():
            if (os.path.splitext(script)[1] in [".R",".r"]):
                shell("Rscript "+script+" {params.params_file} |& tee {log}")
            elif (os.path.splitext(script)[1] in [".py"]):
526
                shell("python3 "+script+" {params.params_file} |& tee {log}")
527
528
529
530
531
532
533
534
535
536
537
538
539
540
            elif (os.path.splitext(script)[1] in [".sh"]):
                shell("/bin/bash "+script+" {params.params_file} |& tee {log}")
        # Outputs files for Multiqc report
        outfile = config["results_dir"] + "/outputs_mqc.csv"
        head = """
# description: 'This is the list of the files generated by each step of the workflow'
# section_name: 'Workflow outputs'
"""
        with open(outfile,"w") as out:
            out.write(head)
            out.write("step\ttool\tfile\tdescription\n")#\tname
            for step in STEPS:
                tool = config[step["name"]]
                i=1
mmassaviol's avatar
mmassaviol committed
541
                for command in OUTPUTS[step["name"] + "__" + tool]:
542
                    if ("SeOrPe" not in config.keys() or (config["SeOrPe"] == "SE" and not("_PE" in command)) or (config["SeOrPe"] == "PE" and not("_SE" in command))):
mmassaviol's avatar
mmassaviol committed
543
                        outputs = OUTPUTS[step["name"] + "__" + tool][command]
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
                        for files in outputs:
                            name = files["file"] if 'file' in files.keys() else files["directory"]
                            path = config[command+"_output_dir"] + "/" + name #config["results_dir"] +"/"+
                            out.write(str(i)+"-"+step["title"]+"\t"+tool+"\t"+path+"\t"+files["description"]+"\n")#"\t"+files["name"]+
                            i+=1
        
        # Params list for Multiqc report
        params_list = "params_name\tdescription\tvalue\n"
        head = """# description: 'This is the list of the parameters for each rule'
# section_name: 'Workflow parameters'
"""
        for step in STEPS:
            tool = config[step["name"]]
            for key, value in config.items():
                if (tool in key and tool != "null" and "_command" not in key) or (key in ["results_dir","sample_dir","sample_suffix","SeOrPe"]) and ("SeOrPe" not in config.keys() or (config["SeOrPe"] == "SE" and not("_PE" in key)) or (config["SeOrPe"] == "PE" and not("_SE" in key))):
                    if (key in PARAMS_INFO.keys() and "label" in PARAMS_INFO[key].keys()):
                        description = PARAMS_INFO[key]["label"]
                    else:
                        description = ''
                    params_list += key + "\t'" + description + "'\t'" + str(value) + "'\n"

        with open(output.params_tab,"w") as out:
            out.write(head)
            out.write(params_list)

        # Tools version
        with open(output.versions_tab,"w") as out:
            versions = read_yaml("/workflow/versions.yaml")
            head = """# description: 'This is the list of the tools used and their version'
# section_name: 'Tools version'
"""
            out.write(head)
            out.write("Tool\tVersion"+"\n")
            for tool, version in versions["base_tools"].items():
                out.write(tool+"\t"+str(version)+"\n")
            for step in STEPS:
                tool = config[step["name"]]
                if (tool in versions.keys()):
                    out.write(tool+"\t"+str(versions[tool])+"\n")

        # Citations
        with open(output.citations_tab,"w") as out:
            citations = read_yaml("/workflow/citations.yaml")
            head = """# description: 'This is the list of the citations of used tools'
# section_name: 'Citations'
"""
            out.write(head)
            out.write("Tool\tCitation\n")
            for tool, citation in citations["base_tools"].items():
                out.write(tool+"\t"+" ; ".join(citation)+"\n")
            final_citations = collections.OrderedDict()
            for step in STEPS:
                tool = config[step["name"]]
                if (tool in citations.keys()):
                    final_citations.update(citations[tool])
            for tool, citation in final_citations.items():
                out.write(tool+"\t"+" ; ".join(citation)+"\n")

        # Config for Multiqc report
        shell("python3 /workflow/generate_multiqc_config.py {params.params_file} {output.config_multiqc}")

rule multiqc:
    input:
        multiqc_inputs(),
        config_multiqc = config["results_dir"] + "/config_multiqc.yaml"
    output:
        multiqc_dir = directory(config["results_dir"]+"/multiqc_data"),
        multiqc_report = config["results_dir"]+"/multiqc_report.html"
    params:
        output_dir = config["results_dir"]
    log:
        config["results_dir"]+'/logs/multiqc/multiqc_log.txt'
mmassaviol's avatar
mmassaviol committed
616
617
618
619
620
621
622
623
624
625
626
627
    run:
        modules_to_run = "-m custom_content "
        for module in MULTIQC.values():
            if (module != "custom"):
                modules_to_run += "-m " + module + " "
        shell(
            "multiqc --config {input.config_multiqc} " +
            "-o {params.output_dir} " +
            "-f {params.output_dir} " +
            modules_to_run +
            "|& tee {log}"
        )
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653

# Final Snakemake rule waiting for outputs of the final step choosen by user (default all steps)
rule all:
    input:
        workflow_outputs("all")
    output:
        Snakefile = config["results_dir"]+"/workflow/Snakefile",
        get_samples = config["results_dir"]+"/workflow/get_samples.py",
        scripts = directory(config["results_dir"]+"/workflow/scripts"),
        params = config["results_dir"]+"/workflow/params.yml"
    params:
        params_file = workflow.overwrite_configfiles,
    shell:
        "cp /workflow/Snakefile {output.Snakefile} && "
        "cp /workflow/get_samples.py {output.get_samples} && "
        "cp -r /workflow/scripts {output.scripts} && "
        "cp {params.params_file} {output.params}"

onsuccess:
    print("Workflow finished with SUCCESS")
    shell("touch "+config["results_dir"]+"/logs/workflow_end.ok")

onerror:
    print("An ERROR occurred")
    shell("cat {log} > "+config["results_dir"]+"/logs/workflow_end.error")
    #shell("mail -s "an error occurred" youremail@provider.com < {log}")