-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnakefile
186 lines (160 loc) · 4.76 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
185
186
configfile: "config.yaml"
import os
from snakemake.utils import makedirs
#################################
# author: Carolina Pita Barros #
# carolina.pitabarros@wur.nl #
# date: March 2021 #
#################################
pipeline = "population-mapping"
include: "rules/create_file_log.smk"
if "OUTDIR" in config:
print("\nSaving to " + config["OUTDIR"] + "\n")
workdir: config["OUTDIR"]
makedirs("logs_slurm")
ASSEMBLY = config["ASSEMBLY"]
def define_to_map_together(given_path):
TO_MAP_TOGETHER = []
FULLPATHS=[]
SAMPLES=[]
for root, dirs, files in os.walk(given_path):
for name in files:
if name.endswith("fq.gz"):
fullpath = os.path.join(root, name)
sample = fullpath.rsplit("/", 2)[1]
name_sub = name.rsplit("_", 2)[0]
if name_sub not in TO_MAP_TOGETHER:
TO_MAP_TOGETHER.append(name_sub)
FULLPATHS.append(fullpath)
if sample not in SAMPLES:
SAMPLES.append(sample)
return(TO_MAP_TOGETHER, FULLPATHS, SAMPLES)
MAP = {} # to be mapped together
def create_bwa_map_input(given_path, map_dict):
maptogether, fullpaths, samples=define_to_map_together(given_path)
for var in maptogether:
temp = []
for file in fullpaths:
if var in file:
temp.append(file)
map_dict[var] = temp
return(map_dict, samples)
ALL_SAMPLES = []
for file in config["PATHS_WITH_FILES"].values():
MAP, samples = create_bwa_map_input(file, MAP)
ALL_SAMPLES = ALL_SAMPLES + samples
localrules: create_file_log
rule all:
input:
files_log,
expand("mapping_stats/qualimap/{sample_merged}/genome_results.txt", sample_merged=ALL_SAMPLES),
"mapping_stats/sample_quality_summary.tsv"
def create_input_names(wildcards):
return(MAP[wildcards.sample])
def create_names_to_merge(wildcards):
input_files = []
mapped_dir = expand("mapped_reads/{sample}.bam", sample=MAP.keys())
for file in mapped_dir:
file_nodir = file.rsplit("/")[1]
if file_nodir.startswith(wildcards.sample_merged):
input_files.append(file)
return(input_files)
rule bwa_index:
input:
ASSEMBLY
output:
multiext(ASSEMBLY, ".amb", ".ann", ".bwt.2bit.64", ".pac", ".0123")
shell:
"bwa-mem2 index {input}"
rule bwa_map:
input:
assembly = ASSEMBLY,
idx = rules.bwa_index.output,
reads=create_input_names,
output:
temp("mapped_reads/{sample}.bam")
resources:
cpus=16
group:
"group_all"
message:
"Rule {rule} processing"
shell:
"""
module load samtools
rg="$(basename $(dirname "{input.reads[0]}"))"
echo $rg
bwa-mem2 mem -t {resources.cpus} -R "@RG\\tID:$rg\\tSM:$rg" {input.assembly} {input.reads} | samblaster -r | samtools view -b - > {output}
"""
rule merge_mapped:
input:
create_names_to_merge
output:
temp("merged_reads/{sample_merged}.bam")
message:
"Rule {rule} processing"
group:
'group'
run:
if len(input) >1:
shell("module load samtools && samtools merge -@ 16 {output} {input}")
else:
shell("mv {input} {output}")
rule samtools_sort:
input:
rules.merge_mapped.output
output:
"processed_reads/{sample_merged}.sorted.bam"
message:
"Rule {rule} processing"
group:
'group'
shell:
"""
module load samtools
samtools sort -m 2G -@ 7 -O bam {input} > {output}
"""
rule samtools_index:
input:
rules.samtools_sort.output
output:
"processed_reads/{sample_merged}.sorted.bam.bai"
message:
"Rule {rule} processing"
group:
"group"
shell:
"""
module load samtools
samtools index -@ 16 {input}
"""
rule qualimap_report:
input:
check=rules.samtools_index.output,
bam=rules.samtools_sort.output
output:
"mapping_stats/qualimap/{sample_merged}/genome_results.txt"
params:
outdir = "mapping_stats/qualimap/{sample_merged}/"
message:
"Rule {rule} processing"
group:
"group"
shell:
"""
unset DISPLAY
qualimap bamqc -bam {input.bam} --java-mem-size=16G -nt 8 -outformat PDF -outdir {params.outdir}
"""
rule qualimap_summary:
input:
expand("mapping_stats/qualimap/{sample_merged}/genome_results.txt", sample_merged = ALL_SAMPLES)
output:
"mapping_stats/sample_quality_summary.tsv"
message:
'Rule {rule} processing'
group:
"group"
params:
scripts_dir = os.path.join(workflow.basedir, "scripts/")
shell:
'sh {params.scripts_dir}create_qualimap_summary.sh'