-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflupipe.Snakefile
executable file
·286 lines (226 loc) · 8.98 KB
/
flupipe.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
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
# IMPORT MODULES
import re
import os
import pprint
import sys
import yaml
# DEBUGGING
## use as DEBUG(variable) for debugging at runtime
pp = pprint.PrettyPrinter(indent=4)
DEBUG = pp.pprint
def default_if_not(key, _dict, default):
try:
return _dict[key]
except KeyError:
return default
# INPUT CHECK
## preparing checks
def checkConfigKey(key, config):
if key in config and config[key] not in ["", False, None]:
return True
return False
def isFileConfig(key, config):
if not checkConfigKey(key, config) or not os.path.isfile(config[key]):
return False
return True
err = []
## check if config was provided
if not config:
err.append("input error: no config data provided.")
## checking sample yaml file [mandatory file]
if not isFileConfig('samples', config):
err.append("input error: missing or invalid sample file definition.")
## checking reference file [mandatory file]
# if 'reference' not in config or not config['reference']:
# config['reference'] = os.path.join(workflow.basedir, "data", "KM517573.1.fasta")
# if not isFileConfig('reference', config):
# err.append("input error: reference file does not exists")
## checking primer files [optional file]
if not checkConfigKey('primer', config):
sys.stderr.write("note: amplicon primer clipping turned off\n")
elif not os.path.isfile(config['primer']):
err.append("input error: primer file does not exists")
## checking adapter file [optional file]
if not checkConfigKey('adapter', config):
sys.stderr.write("note: adapter clipping turned off\n")
elif not os.path.isfile(config['adapter']):
err.append("input error: adapter file does not exists.")
## checking kraken database [optional folder]
if not checkConfigKey('krakenDb', config):
sys.stderr.write("note: taxonomic read filtering turned off\n")
elif not os.path.isdir(config["krakenDb"]):
err.append("input error: kraken database does not exists.")
## checking segment database [optional folder]
if not checkConfigKey('segmentdb', config):
sys.stderr.write("note: automatic reference detection turned off - no segment database\n")
elif not os.path.isdir(config["segmentdb"]):
err.append("input error: segment database does not exists.")
## checking annotation file [optional file]
if not checkConfigKey('var_annotation', config):
sys.stderr.write("note: variation inspection turned off\n")
if not checkConfigKey('cns_annotation', config):
sys.stderr.write("note: consensus genome annotation turned off\n")
elif not os.path.isfile(config['cns_annotation']):
err.append("input error: gff file does not exist.")
## input error reporting
if err:
sys.stderr.write("\n".join(err) + "\n")
sys.exit(1)
# CONSTANT DEFINITIONS
## sample files
SAMPLES = dict()
with open(config["samples"], 'r') as handle:
SAMPLES = yaml.safe_load(handle)
SAMPLES = {str(x[0]): x[1] for x in SAMPLES.items()}
#segment names
ALL_SEGMENTS= ["HA", "MP", "NA", "NP", "NS", "PA", "PB1", "PB2"]
## additional files or folders
VAR_ANNOT = default_if_not("var_annotation", config, None)
RUN_ANNOT = isFileConfig('cns_annotation', config)
CNS_ANNOT = default_if_not("cns_annotation", config, None)
KRAKEN_DB = default_if_not("krakenDb", config, None)
SEGMENT_DB = default_if_not("segmentdb", config, None)
#KRAKEN_TAX_ID = default_if_not("krakenTaxID", config, None)
## read clipping parameters
PRIMER = default_if_not("primer", config, None)
## variant calling
VAR_CALL_COV = config['var_call_cov']
VAR_CALL_COUNT = config['var_call_count']
VAR_CALL_FRAC = config['var_call_frac']
## variant hard filtering
VAR_FILTER_MQM = config['var_filter_mqm']
VAR_FILTER_SAP = config['var_filter_sap']
VAR_FILTER_QUAL = config['var_filter_qual']
#reference sequence provided
REFERENCE = default_if_not("reference", config, None)
## read filtering
PCR_DEDUP = default_if_not("pcr_dedup", config, None)
READ_FILTER_QUAL = default_if_not("read_filter_qual", config, 20)
READ_FILTER_LEN = default_if_not("read_filter_len", config, 50)
## consensus generation
CNS_MIN_COV = config['cns_min_cov']
CNS_GT_ADJUST = default_if_not("cns_gt_adjust", config, None)
## reporting parameters
REPORT_RUNID = default_if_not("run_id", config, "")
## output folders
PROJFOLDER = os.path.join(config["output"], "results")
IUPAC_CNS_FOLDER = os.path.join(PROJFOLDER, "consensuses_iupac")
#MASKED_CNS_FOLDER = os.path.join(PROJFOLDER, "consensuses_masked")
DATAFOLDER = ["logs", "trimmed"]
if KRAKEN_DB:
DATAFOLDER.extend(["classified", "filtered"])
DATAFOLDER.extend(["mapping"])
if PCR_DEDUP:
DATAFOLDER.extend(["dedup"])
DATAFOLDER.extend(["mapping_stats", "variant_calling", "masking", "reporting"])
DATAFOLDER = { x[1]: os.path.join(PROJFOLDER, "intermediate_data", str(x[0]).zfill(2) + "_" + x[1]) for x in enumerate(DATAFOLDER) }
if not KRAKEN_DB:
DATAFOLDER["classified"] = PROJFOLDER
DATAFOLDER["filtered"] = PROJFOLDER
## files
#REFERENCE = os.path.join(DATAFOLDER["mapping"], "reference.fasta")
ADAPTERS = default_if_not("adapter", config, None) # the adpater file cannot be provided since it is copyright protected ILLUMINA!
## ref indexes
#PICARD_INDEX = os.path.splitext(REFERENCE)[0] + '.dict'
#SAMTOOLS_INDEX = REFERENCE + ".fai"
#BWA_INDEX = REFERENCE + ".bwt"
# SANITY CHECKS FOR SAMPLES & THRESHOLDS
## kraken input test
#if KRAKEN_DB and not KRAKEN_TAX_ID:
# err.append("input error: kraken database defined but no TaxID")
## variant and cns threshold test
if VAR_CALL_FRAC < 0 or VAR_CALL_FRAC > 1:
err.append("input error: the value of var_call_frac cannot be lower than 0 or greater than 1")
if CNS_MIN_COV < VAR_CALL_COV:
err.append("input error: var_call_cov cannot be smaller than cns_min_cov.\nThey are {varcall} and {cns}".format(varcall=V, cns=CNS_MIN_COV))
if CNS_GT_ADJUST and (CNS_GT_ADJUST <= 0.5 or CNS_GT_ADJUST > 1):
err.append("input error: the value of cns_gt_adjust has to be greater than 0.5 and not greater than 1")
## sanity error report
if err:
sys.stderr.write("\n".join(err) + "\n")
sys.exit(1)
# GENERAL FUNCTIONS
def getFastq(wildcards):
return SAMPLES[str(wildcards.sample)]["read1"], SAMPLES[str(wildcards.sample)]["read2"]
# RULE ALL
def input_all(wildcards):
files = []
## consensus
for sample in SAMPLES:
if sample.startswith("NPC"):
files.append(os.path.join(DATAFOLDER["classified"], sample, sample + ".kraken.report.txt"))
else:
files.append(os.path.join(IUPAC_CNS_FOLDER, sample + ".iupac_consensus.fasta"))
## masked consensus
#for sample in SAMPLES:
# files.append(os.path.join(MASKED_CNS_FOLDER, sample + ".masked_consensus.fasta"))
## variant annotation
#if VAR_ANNOT:
# for sample in SAMPLES:
# files.append(os.path.join(DATAFOLDER["variant_calling"], sample, sample + ".annotation.html"))
#if RUN_ANNOT:
# annotate iupac consensus
# for sample in SAMPLES:
# files.append(os.path.join(IUPAC_CNS_FOLDER, sample + ".iupac_consensus.gff"))
# annotate merged consensus
#for sample in SAMPLES:
#files.append(os.path.join(MASKED_CNS_FOLDER, sample + ".masked_consensus.gff"))
## report
files.append(os.path.join(PROJFOLDER, "qc_report.html"))
return files
rule all:
input:
input_all
# RULE IMPORT
## general rules
include: "rules/get_version.smk"
####
#reference detection
#minimap against segmentDBs
include: "rules/map_reads_minimapp2.smk"
#get refs fastas from segment DB
include: "rules/r_stats_minimapp2.smk"
include: "rules/seqkit_extract_ref.smk"
#map again + stats
include: "rules/best_Hits_map_reads_minimapp2.smk"
include: "rules/bam_stats_refDetec.smk"
#get final reference sequence for flupipe input
include: "rules/r_stats_final_ref.smk"
include: "rules/seqkit_extract_final_ref.smk"
include: "rules/combine_final_refs.smk"
#final perpare ref script
include: "rules/prepare_reference.smk"
####
## indexing
include: "rules/index_samtools.smk"
include: "rules/index_picard.smk"
include: "rules/index_bwa.smk"
include: "rules/index_bam.smk"
include: "rules/index_tabix.smk"
## amplicon primer clipping
include: "rules/trim_reads.smk"
## taxonomic read classification
include: "rules/classify_reads.smk"
include: "rules/filter_reads.smk"
## read mapping
include: "rules/map_reads.smk"
# PCR_DEDUP:
include: "rules/dedup_reads.smk"
include: "rules/sort_bam.smk"
include: "rules/get_read_cov.smk"
## variant calling
include: "rules/call_vars_lofreq.smk"
#include: "rules/bgzip_vcf.smk"
## consensus generation
include: "rules/create_consensus.smk"
## statistics
include: "rules/get_bamstats.smk"
include: "rules/get_insert_size.smk"
include: "rules/combine_readlength.smk"
include: "rules/getReadLength.smk"
## report
include: "rules/create_report.smk"
##extra rules to cover a specific variant case, good quality variant fails due to strand bias variant filter, most likely at the beginning or end of the sequence
#instead of the reference base an N is placed at this position
include: "rules/lofreq_secial_variant_case.smk"
include: "rules/R_filter_variants_special_variant_case.smk"