Snakefile.smk 11.7 KB
Newer Older
jacqueslagnel's avatar
jacqueslagnel committed
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
294
295
296
297
298
299
300
301
302
# WGS Snakemake pipeline for Paired-end Illumina sequencing using a Reference genome
# Pipeline features:
# 1) read preprocessing,
# 2) reads QC,
# 3) mapping with bwa,
# 4) mapping QC
# 5) SNPs calling using freebayes
#
# Snakemake features: fastq from csv file, config, modules, SLURM


import pandas as pd
from snakemake.utils import min_version
# snakemake built-in report function requires min version 5.1
min_version("5.1.0")

#read the sample file using pandas lib (sample names+ fastq names) and crezate index using the sample name
samples = pd.read_csv(config["samplesfile"], sep='\t', dtype=str, comment='#').set_index(["SampleName"], drop=False)

rule final_outs:
    input:
        "{outdir}/multiqc/multiqc_report_fastqc.html".format(outdir=config["outdir"]),
        "{outdir}/multiqc/multiqc_report_bam.html".format(outdir=config["outdir"]),
        "{outdir}/gene_count_dicoexpress.csv".format(outdir=config["outdir"])

#function return only the fastq1 file and adds the fastq path
def get_fastq1(wildcards):
    return config["fq_dir"]+"/"+samples.loc[(wildcards.sample), ["fq1"]].dropna()

#function return only the fastq2 file and adds the fastq path
def get_fastq2(wildcards):
    return config["fq_dir"]+"/"+samples.loc[(wildcards.sample), ["fq2"]].dropna()

#module 4 fastp for Paired-end read
#use with utils.smk
# 2alternatives preprocessing
rule fastp_pe:
    input:
        R1  = get_fastq1,
        R2  = get_fastq2
        #R1 = lambda wildcards: "../data/"+samples.fq1[wildcards.sample],
        #R2 = lambda wildcards: "../data/"+samples.fq2[wildcards.sample]
        #R1  = lambda wildcards: config["fq_dir"]+"/"+samples.loc[(wildcards.sample), ["fq1"]].dropna(),
        #R2  = lambda wildcards: config["fq_dir"]+"/"+samples.loc[(wildcards.sample), ["fq2"]].dropna()
    output:
        R1 = "{outdir}/fastp/{{sample}}_1_trim.fastq.gz".format(outdir=config["outdir"]),
        R2 = "{outdir}/fastp/{{sample}}_2_trim.fastq.gz".format(outdir=config["outdir"])
    message: "Running fastp on files {input.R1} and {input.R2} \n"
    params:
        fqdir      = config["fq_dir"],
        outdir     = config["outdir"],
        modules    = config["MODULES"],
        fastp_bin  = config["fastp_bin"],
        bind       = config["BIND"],
        json       = config["outdir"]+"/fastp/{sample}_trim.json",
        html       = config["outdir"]+"/fastp/{sample}_trim.html"
    shell:
       """
        singularity exec {params.bind} {params.fastp_bin} fastp \
        -i {input.R1} \
        -I {input.R2} \
        -o {output.R1} \
        -O {output.R2} \
        -h {params.html} \
        -j {params.json} \
        --max_len1 350 \
        --correction \
        --cut_mean_quality 20 \
        --cut_window_size 4 \
        --low_complexity_filter \
        --complexity_threshold 30 \
        -w 4
        rm -f {params.html} {params.json}
        exit 0

        singularity exec {params.bind} {params.fastp_bin} fastp \
        -i {input.R1} \
        -I {input.R2} \
        -o {output.R1} \
        -O {output.R2} \
        --json={params.json} \
        --html={params.html} \
        --trim_tail1=1 \
        --cut_front \
        --cut_tail \
        --length_required 35 \
        --average_qual 20 \
        --length_limit 400 \
        --correction \
        --cut_mean_quality 10 \
        --low_complexity_filter \
        --complexity_threshold 20 \
        --thread 4
        #rm -f {params.html} {params.json}
        """

#          singularity exec {params.bind} {params.fastp_bin} fastp \
#         -i {input.R1} \
#         -I {input.R2} \
#         -o {output.R1} \
#         -O {output.R2} \
#         --json={params.json} \
#         --html={params.html} \
#         --trim_tail1=1 \
#         --cut_front \
#         --cut_tail \
#         --length_required 35 \
#         --average_qual 10 \
#         --length_limit 400 \
#         --correction \
#         --cut_mean_quality 10 \
#         --low_complexity_filter \
#         --complexity_threshold 20 \
#         --thread 4
#         #rm -f {params.html} {params.json}

#QC
#fastqc PE mode
#function return fastq1 and fastq2 files and adds the fastq path
rule fastqc_pe:
    input:
        R1 = "{outdir}/fastp/{{sample}}_1_trim.fastq.gz".format(outdir=config["outdir"]),
        R2 = "{outdir}/fastp/{{sample}}_2_trim.fastq.gz".format(outdir=config["outdir"])
    output:
        # to avoid the output name generated by fastqc (regex on the name) we use a flag file
        "{outdir}/fastqc/{{sample}}.OK.done".format(outdir=config["outdir"])
    threads:
        2
    resources:
        mem_mb=4000
    params:
        fqdir      = config["fq_dir"],
        outdir     = config["outdir"],
        fastqc_bin = config["fastqc_bin"],
        bind       = config["BIND"]
    shell:
        """
        #mkdir -p {params.outdir}/fastqc #snakemake create automaticly the folders
        singularity exec {params.bind} {params.fastqc_bin} fastqc -o {params.outdir}/fastqc -t {threads} {input.R1} {input.R2} && touch {output}
        """
#QC
# multiQC on fastqc outputs
rule multiqc_fastqc:
    input:
        expand("{outdir}/fastqc/{sample}.OK.done", outdir=config["outdir"], sample=samples['SampleName'])
    output:
        #report("{outdir}/multiqc/multiqc_report_fastqc.html".format(outdir=config["outdir"]), caption="report/multiqc.rst", category="Quality control")
        "{outdir}/multiqc/multiqc_report_fastqc.html".format(outdir=config["outdir"])
    threads:
        1
    params:
        outdir      = config["outdir"],
        multiqc_bin = config["multiqc_bin"],
        bind        = config["BIND"]
    shell:
        """
        #mkdir -p {params.outdir}/multiqc #snakemake create automaticly the folders
        singularity exec {params.bind} {params.multiqc_bin} multiqc --filename {output} {params.outdir}/fastqc
        """
#QC
#multiqc for bams
rule multiqc_bam:
    input:
        expand("{outdir}/mapped/{sample}.stats.txt", outdir=config["outdir"], sample=samples['SampleName'])
    output:
        "{outdir}/multiqc/multiqc_report_bam.html".format(outdir=config["outdir"])
        #"{outdir}/multiqc/multiqc_report.html".format(outdir=config["outdir"])
    threads:
        1
    params:
        outdir      = config["outdir"]+"/mapped/*.stats.txt",
        multiqc_bin = config["multiqc_bin"],
        bind        = config["BIND"]
    shell:
        """
        #mkdir -p {params.outdir}/multiqc #snakemake create automaticly the folders
        singularity exec {params.bind} {params.multiqc_bin} multiqc --filename {output} {input}
        """

#mapping with bwa PE mode
#reference indexing
#example with and without mark duplicate
#sam 2 bam sorted and statistics
rule bwa_index:
    input:
        genome = config["REFPATH"] + "/" + config["GENOME"]
    output:
        config["REFPATH"] + "/" + config["GENOME"] + ".amb",
        config["REFPATH"] + "/" + config["GENOME"] + ".ann",
        config["REFPATH"] + "/" + config["GENOME"] + ".bwt",
        config["REFPATH"] + "/" + config["GENOME"] + ".pac",
        config["REFPATH"] + "/" + config["GENOME"] + ".sa",
        config["REFPATH"] + "/" + config["GENOME"] + ".fai"
    params:
        bind         = config["BIND"],
        bwa_bin      = config["bwa_bin"],
        samtools_bin = config["samtools_bin"]
    message: "Building BWA index for reference genome {input.genome}\n"
    shell:
        """
        singularity exec {params.bind} {params.bwa_bin} bwa index -a bwtsw -b 500000000 {input.genome}
        singularity exec {params.bind} {params.samtools_bin} samtools faidx {input.genome}
        """

rule bwa_pe :
    input:
        R1 = "{outdir}/fastp/{{sample}}_1_trim.fastq.gz".format(outdir=config["outdir"]),
        R2 = "{outdir}/fastp/{{sample}}_2_trim.fastq.gz".format(outdir=config["outdir"]),
        #fake input used to force index building before alignement if not present
        idx = config["REFPATH"] + "/" + config["GENOME"] + ".bwt"
    output:
        bam   = "{outdir}/mapped/{{sample}}_sorted.bam".format(outdir=config["outdir"]),
    params:
        outdir       = config["outdir"],
        idxbase      = config["REFPATH"] + "/" + config["GENOME"],
        bind         = config["BIND"],
        bwa_bin      = config["bwa_bin"],
        samtools_bin = config["samtools_bin"],
        freebayes_bin= config["freebayes_bin"], # freebayes_bin used for sambamba for markdup
        rg           = "@RG\\tID:{sample}\\tSM:{sample}"
    threads: 10
    message: "Mapping reads {input.R1} to {params.idxbase} using BWA.\n"
    #converting to bam, sorting and removing dupplicates in a single command!
    shell:
        """
        singularity exec {params.bind} {params.bwa_bin} bwa mem \
        -t {threads} \
        -K 100000000 \
        {params.idxbase} \
        {input.R1} {input.R2} \
        | singularity exec {params.bind} {params.samtools_bin} samtools sort -@2 -m 6G -o {params.outdir}/{wildcards.sample}.bam.raw -

        #Without rm duplicate
        singularity exec {params.bind} {params.samtools_bin} samtools index -@ 10 {params.outdir}/{wildcards.sample}.bam.raw
        mv {params.outdir}/{wildcards.sample}.bam.raw.bai {output.bam}.bai
        mv {params.outdir}/{wildcards.sample}.bam.raw {output.bam}

        #with rm duplicate
        #singularity exec {params.bind} {params.samtools_bin} samtools rmdup -s {params.outdir}/{wildcards.sample}.bam.raw {output.bam}
        #singularity exec {params.bind} {params.samtools_bin} samtools index -@ 10 {output.bam}

        #singularity exec {params.bind} {params.freebayes_bin} sambamba markdup -t 10 {params.outdir}/{wildcards.sample}.bam.raw {output.bam}
        #singularity exec {params.bind} {params.samtools_bin} samtools index -@ 10 {output.bam}
        singularity exec {params.bind} {params.samtools_bin} samtools flagstat -@ 10 {output.bam}
        rm -f {params.outdir}/{wildcards.sample}.bam.raw*
        """

rule bam_stats:
    input:
        bam   = "{outdir}/mapped/{{sample}}_sorted.bam".format(outdir=config["outdir"])
    output:
        stats = "{outdir}/mapped/{{sample}}.stats.txt".format(outdir=config["outdir"])
    params:
        outdir       = config["outdir"],
        idxbase      = config["REFPATH"] + "/" + config["GENOME"],
        bind         = config["BIND"],
        bwa_bin      = config["bwa_bin"],
        samtools_bin = config["samtools_bin"]
    threads: 2
    shell:
        """
        singularity exec {params.bind} {params.samtools_bin} samtools stats -@ 2 {input.bam} > {output.stats}
        """

# SNP caller
#freebayes rule
rule freebayes:
    input:
        bam = "{outdir}/mapped/{{sample}}_sorted_parsed.bam".format(outdir=config["outdir"])
        #on merged bams
        #bam = "{outdir}/bams_merged_sorted.bam".format(outdir=config["outdir"])
    output:
        var = "{outdir}/variant/{{sample}}_varscan.vcf".format(outdir=config["outdir"])
        #on merged bams
        #var = "{outdir}/variant/SNPs4all_freebayes.vcf".format(outdir=config["outdir"])
    params:
        ref = config["REFPATH"] + "/" + config["GENOME"],
        bind = config["BIND"],
        samtools_bin = config["samtools_bin"],
        varscan_bin  = config["varscan_bin"],
        freebayes_bin = config["freebayes_bin"]
    message: "Looking for SNP in {input.bam} with Varscan \n"
    shell:
        """
        zero=$(singularity exec {params.bind} {params.samtools_bin} samtools flagstat {input.bam}|head -n1|sed 's/\s.*//g')
        if [ "$zero" -lt "1" ]
        then
                echo "WARNING ALLMINE {output.var} EMPTY"
                touch {output.var}
                exit 0
        fi
        singularity exec {params.bind} {params.freebayes_bin} \
        freebayes -f {params.ref} \
        --min-mapping-quality 30 \
        --min-base-quality 20 \
        --min-alternate-fraction 0.15 \
        --min-coverage 8 \
        --use-duplicate-reads \
        {input.bam} > {output.var}
        exit 0
        """