-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
185 lines (147 loc) · 5.29 KB
/
Snakefile
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
from os import path, makedirs, rename, remove
# Show the rulegraph
# snakemake -s Snakefile --rulegraph | dot -Tpng > rulegraph.png
configfile: "config.yaml"
onsuccess:
print("Workflow completed without any error")
onerror:
print("An error occurred")
# Should an excel file with barcodes and sample names be coverted to TSV ?
# Please note that it is assumed that the 5th and 2nd columns of sheet one
# in the Excel file are the sample names and barcodes, respectively
isExcel=config['isExcel'] # True or False
EXCEL_FILE=config['EXCEL_FILE']
SAMPLES=config['SAMPLES']
rule all:
input:
"02.Join_fastq/indices.fastq",
"07.Count_Seqs/seqs_stat.txt"
rule Join_fastq:
input:
forward_index = "01.raw_data/index1.fastq",
reverse_index = "01.raw_data/index2.fastq"
output: "02.Join_fastq/indices.fastq"
params:
program=config['programs_path']['usearch']
threads: 1
shell:
"""
{params.program} \
-fastq_join {input.forward_index} \
-reverse {input.reverse_index} \
-join_padgap "" \
-threads {threads} \
-fastqout {output}
"""
# Convert barcodes file from TSV to FASTA
rule Parse_barcodes:
input: EXCEL_FILE #if isExcel else "01.raw_data/sample2barcode.tsv"
output: "03.Parse_barcodes/bar.fasta"
params:
isExcel=isExcel,
program=config['programs_path']['xlsx2csv']
threads: 1
shell:
"""
ISEXCEL={params.isExcel}
if [ ${{ISEXCEL}} == True ]; then
{params.program} -d "tab" -s 1 {input} | \
awk 'BEGIN{{FS=OFS="\t"}} NR>1{{print($5,$2)}}' | \
awk '{{print ">"$1"\\n"$2}}' > {output}
else
awk '{{print ">"$1"\\n"$2}}' {input} > {output}
fi
"""
rule Reformat_barcodes:
input: rules.Parse_barcodes.output
output: "04.Reformat_barcodes/bar.fasta"
threads: 1
script:
"./reformat_barcode.py"
# Demultiplex the reads per sample using the reformated barcodes
rule Demultiplex:
input:
barcodes=rules.Reformat_barcodes.output,
indices=rules.Join_fastq.output,
forward="01.raw_data/read1.fastq",
rev="01.raw_data/read2.fastq"
output:
forward="05.Demultiplex/demux_R1.fastq",
rev="05.Demultiplex/demux_R2.fastq"
params:
program=config['programs_path']['usearch']
threads: 1
shell:
"""
{params.program} \
-fastx_demux {input.forward} \
-reverse {input.rev} \
-index {input.indices} \
-barcodes {input.barcodes} \
-fastqout {output.forward} \
-output2 {output.rev}
"""
# Separate the demultiplexed fastq into folders containing
# the forward and reverse reads for each sample
rule Split_forward:
input: rules.Demultiplex.output.forward
output: expand("06.Split/{sample}/{sample}_R1.fastq.gz", sample=SAMPLES)
params:
program=config['programs_path']['parallel'],
samples=SAMPLES
threads: 10
shell:
"""
function split_samples(){{
local OUTDIR=$1
local DEMUX_FILE=$2
local SAMPLE=$4
local DIRECTION=$3
[ -d ${{OUTDIR}}/${{SAMPLE}} ] || mkdir -p ${{OUTDIR}}/${{SAMPLE}}
grep -A 3 \
--no-group-separator "sample=${{SAMPLE}};" ${{DEMUX_FILE}} \
> "${{OUTDIR}}/${{SAMPLE}}/${{SAMPLE}}_${{DIRECTION}}.fastq" \
&& gzip "${{OUTDIR}}/${{SAMPLE}}/${{SAMPLE}}_${{DIRECTION}}.fastq"
}}
export -f split_samples
{params.program} -j 10 "split_samples 06.Split/ {input} R1 {{}}" \
::: {params.samples}
"""
rule Split_reverse:
input: rules.Demultiplex.output.rev
output: expand("06.Split/{sample}/{sample}_R2.fastq.gz", sample=SAMPLES)
params:
program=config['programs_path']['parallel'],
samples=SAMPLES
threads: 10
shell:
"""
function split_samples(){{
local OUTDIR=$1
local DEMUX_FILE=$2
local SAMPLE=$4
local DIRECTION=$3
[ -d ${{OUTDIR}}/${{SAMPLE}} ] || mkdir -p ${{OUTDIR}}/${{SAMPLE}}
grep -A 3 \
--no-group-separator "sample=${{SAMPLE}};" ${{DEMUX_FILE}} \
> "${{OUTDIR}}/${{SAMPLE}}/${{SAMPLE}}_${{DIRECTION}}.fastq" \
&& gzip "${{OUTDIR}}/${{SAMPLE}}/${{SAMPLE}}_${{DIRECTION}}.fastq"
}}
export -f split_samples
{params.program} -j 10 "split_samples 06.Split/ {input} R2 {{}}" \
::: {params.samples}
"""
rule Count_Seqs:
input: expand(["06.Split/{sample}/{sample}_R1.fastq.gz", "06.Split/{sample}/{sample}_R2.fastq.gz"], sample=SAMPLES)
output: "07.Count_Seqs/seqs_stat.txt"
params:
program=config['programs_path']['seqkit']
shell:
"""
# Get the stats on the sequences using seqkit
{params.program} stats {input} > temp.txt
# Sort the sequence statistics
(sed -n '1p' temp.txt; awk 'NR>1{{print}}' temp.txt | \
sort -V -k1,1) > {output} \
&& rm temp.txt
"""