-
Notifications
You must be signed in to change notification settings - Fork 13
/
Snakefile
280 lines (227 loc) · 9.4 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
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
import pandas as pd
from snakemake.utils import min_version, validate
##### set minimum snakemake version #####
min_version("5.10.0")
##### load config and sample sheets #####
#
class ansitxt:
RED = '\033[31m'
ENDC = '\033[0m'
BOLD = '\033[1m'
def warning(msg):
print(f"\n{ansitxt.BOLD}{ansitxt.RED}{msg}{ansitxt.ENDC}\n",file=sys.stderr)
try:
configfile: "config.yaml"
except WorkflowError:
warning("ERROR: config.yaml does not exist or is incorrectly formatted. Please see the README file for details. Quitting now.")
sys.exit(1)
OUTDIR = config["outdir"]
LOGDIR = config["logdir"]
try:
samples = pd.read_csv(config["samples"], sep="\t", comment="#").set_index("sample", drop=False)
validate(samples, schema="schemas/samples.schema.yaml")
except FileNotFoundError:
warning(f"ERROR: the samples file ({config['samples']}) does not exist. Please see the README file for details. Quitting now.")
sys.exit(1)
try:
if config["input_type"] == "fastq":
units = pd.read_csv(config["units"], dtype=str, sep="\t", comment="#").set_index(["sample", "unit"], drop=False)
validate(units, schema="schemas/units_fastq.schema.yaml")
units.index = units.index.set_levels([i.astype(str) for i in units.index.levels]) # enforce str in index
if config["input_type"] == "matrix":
units = pd.read_csv(config["units"], dtype=str, sep="\t", comment="#").set_index("sample", drop=False)
if config["technology"] == "10x":
validate(units, schema="schemas/units_matrix_10x.schema.yaml")
elif config["technology"] == "standard":
validate(units, schema="schemas/units_matrix_standard.schema.yaml")
except FileNotFoundError:
warning(f"ERROR: the units file ({config['units']}) does not exist. Please see the README file for details. Quitting now.")
sys.exit(1)
if config["single_samples"] == True and any([config["parameters"]["seurat_merge"]["enabled"], config["parameters"]["seurat_integration"]["enabled"]]) == False:
warning("WARNING: 'single_samples' option is set as True while not asking for merge or integration.\nSetting 'single_samples' option to False.")
def confirm():
prompt = f"{ansitxt.BOLD}Continue anyway? [y/N]{ansitxt.ENDC} "
while 1:
sys.stdout.write(prompt)
choice = input().lower()
if choice in ['yes','y']:
return True
elif choice in ['','no','n']:
return False
else:
sys.stdout.write("Please respond with 'yes' or 'no': ")
def get_resource(rule,resource):
try:
return config["resources"][rule][resource]
except KeyError:
return config["resources"]["default"][resource]
def process_samples(samples):
integrated = []
merged = []
if config["single_samples"] == True:
if any([config["parameters"]["seurat_merge"]["enabled"], config["parameters"]["seurat_integration"]["enabled"]]) == False:
return samples
else:
if config["parameters"]["seurat_merge"]["enabled"] == True:
merged = ["merged"]
if config["parameters"]["seurat_integration"]["enabled"] == True:
integrated = ["integrated"]
return merged + integrated
else:
return samples
def get_output_degs(wc):
if config["parameters"]["seurat_degs"]["enabled"] == True:
samples = process_samples([u.sample for u in units.itertuples()])
file = expand("{OUTDIR}/seurat/{sample}/4_degs/seurat_degs.rds", sample=samples,OUTDIR=OUTDIR)
else:
file = []
return file
def get_output_gs(wc):
if config["parameters"]["seurat_gs"]["enabled"] == True:
samples = process_samples([u.sample for u in units.itertuples()])
file = expand("{OUTDIR}/seurat/{sample}/5_gs/seurat_complete.rds", sample=samples,OUTDIR=OUTDIR)
else:
file = []
return file
def get_output_ti(wc):
if config["parameters"]["slingshot"]["enabled"] == True:
samples = process_samples([u.sample for u in units.itertuples()])
file = expand("{OUTDIR}/slingshot/{sample}/6_traj_in/slingshot_sce_objects.RData", sample=samples,OUTDIR=OUTDIR)
else:
file = []
return file
def get_output_fa(wc):
if config["parameters"]["vision"]["enabled"] == True:
samples = process_samples([u.sample for u in units.itertuples()])
file = expand("{OUTDIR}/vision/{sample}/7_func_analysis/vision_object.rds", sample=samples,OUTDIR=OUTDIR)
else:
file = []
return file
def get_integration(wc):
file = []
samples =list(set([u.sample for u in units.itertuples()]))
if config["parameters"]["seurat_integration"]["enabled"] == True:
if len(samples) == 1:
print(f"\nWARNING: found only one sample. {ansitxt.RED}{ansitxt.BOLD}Deactivating Seurat integration.{ansitxt.ENDC}")
print("You can remove this warning by disabling Seurat integration in config.yaml.")
if not confirm():
sys.exit(1)
else:
config["parameters"]["seurat_integration"]["enabled"] = False
else:
file = expand("{OUTDIR}/seurat/integrated/2_normalization/seurat_normalized-pcs.rds",OUTDIR=OUTDIR)
return file
def get_merge(wc):
file = []
samples =list(set([u.sample for u in units.itertuples()]))
if config["parameters"]["seurat_merge"]["enabled"] == True:
if len(samples) == 1:
print(f"\nWARNING: found only one sample. {ansitxt.RED}{ansitxt.BOLD}Deactivating Seurat merge.{ansitxt.ENDC}")
print("You can remove this warning by disabling Seurat merge in config.yaml.")
if not confirm():
sys.exit(1)
else:
config["parameters"]["seurat_merge"]["enabled"] = False
else:
file = expand("{OUTDIR}/seurat/merged/1_preprocessing/seurat_post-qc.rds",OUTDIR=OUTDIR)
return file
def get_integration_input_sm(wc):
file = expand("{OUTDIR}/seurat/{unit.sample}/1_preprocessing/seurat_post-qc.rds", unit=units.itertuples(),OUTDIR=OUTDIR)
file = list(set(file))
return file
def get_merge_input(wc):
file = expand("{OUTDIR}/seurat/{unit.sample}/1_preprocessing/seurat_post-qc.rds", unit=units.itertuples(),OUTDIR=OUTDIR)
file = list(set(file))
return file
def get_velocity_matrices(wc):
if config["input_type"] == "matrix":
file = []
elif config["input_type"] == "fastq":
if config["parameters"]["velocyto"]["enabled"] == True:
file = expand("{OUTDIR}/star/{unit.sample}/Solo.out/Velocyto/raw/spliced/matrix.mtx", unit=units.itertuples(),OUTDIR=OUTDIR)
else:
file = []
else:
file = []
return file
def do_velocity(wc):
if config["input_type"] == "matrix":
file = []
elif config["input_type"] == "fastq":
if config["parameters"]["velocyto"]["enabled"] == True:
samples = process_samples([u.sample for u in units.itertuples()])
file = expand("{OUTDIR}/velocyto/{sample}/8_RNA_velocity/seurat_velocity.rds", sample=samples,OUTDIR=OUTDIR)
else:
file = []
else:
file = []
return file
def get_multiqc(wc):
if config["input_type"] == "fastq":
file = f"{OUTDIR}/qc/multiqc_report.html"
else:
file = []
return file
def seurat_input(wc):
if config["input_type"] == "matrix":
file = []
elif config["input_type"] == "fastq":
file = f"{OUTDIR}/star/{{sample}}/Solo.out/Gene/Summary.csv"
return file
def get_output_normalization(wc):
samples = process_samples([u.sample for u in units.itertuples()])
file = expand("{OUTDIR}/seurat/{sample}/2_normalization/seurat_normalized-pcs.rds", sample=samples,OUTDIR=OUTDIR)
return file
def get_output_find_clus(wc):
samples = process_samples([u.sample for u in units.itertuples()])
file = expand("{OUTDIR}/seurat/{sample}/3_clustering/seurat_find-clusters.rds", sample=samples,OUTDIR=OUTDIR)
return file
def get_anndata_check(wc):
samples = process_samples([u.sample for u in units.itertuples()])
file = expand("{OUTDIR}/scanpy/{sample}/check.txt", sample=samples,OUTDIR=OUTDIR)
return file
def get_output_qc(wc):
samples = [u.sample for u in units.itertuples()]
if config["parameters"]["seurat_merge"]["enabled"] == True:
samples = samples + ['merged']
file = expand("{OUTDIR}/seurat/{sample}/1_preprocessing/seurat_post-qc.rds", sample=samples,OUTDIR=OUTDIR)
return file
rule all:
input:
get_integration,
get_merge,
get_multiqc,
get_output_find_clus,
get_output_degs,
get_output_gs,
get_output_ti,
get_output_fa,
do_velocity,
get_velocity_matrices,
get_anndata_check
rule expression_matrix:
input:
get_velocity_matrices,
get_multiqc
rule qc_expression_matrix:
input:
get_output_qc,
get_merge,
get_multiqc
rule normalized_expression_matrix:
input:
get_output_normalization,
get_integration,
get_merge,
get_multiqc
##### setup singularity #####
# this container defines the underlying OS for each job when using the workflow
# with --use-conda --use-singularity
singularity: "docker://continuumio/miniconda3"
##### setup report #####
report: "report/conf/report.rst"
##### load rules #####
include: "rules/align.smk"
include: "rules/qc.smk"
include: "rules/analyse.smk"
include: "rules/velocyto.smk"