-
Notifications
You must be signed in to change notification settings - Fork 11
/
Snakefile
206 lines (172 loc) · 7.1 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
"""
Snakefile for doing the higher stages of data processing (everything beyond build_raw)
This includes:
- building the tcm
- dsp parameter generation
- building dsp
- hit pars generation
- building hit
- building evt
- the same for partition level tiers
"""
import pathlib
import os
import json
import sys
import glob
from datetime import datetime
from collections import OrderedDict
import logging
import scripts.util as ds
from scripts.util.pars_loading import pars_catalog
from scripts.util.patterns import get_pattern_tier_raw
from scripts.util.utils import (
subst_vars_in_snakemake_config,
runcmd,
config_path,
chan_map_path,
filelist_path,
metadata_path,
tmp_log_path,
pars_path,
)
# Set with `snakemake --configfile=/path/to/your/config.json`
# configfile: "have/to/specify/path/to/your/config.json"
subst_vars_in_snakemake_config(workflow, config)
check_in_cycle = True
setup = config["setups"]["l200"]
configs = config_path(setup)
chan_maps = chan_map_path(setup)
meta = metadata_path(setup)
swenv = runcmd(setup)
part = ds.dataset_file(setup, os.path.join(configs, "partitions.json"))
basedir = workflow.basedir
wildcard_constraints:
experiment=r"\w+",
period=r"p\d{2}",
run=r"r\d{3}",
datatype=r"\w{3}",
timestamp=r"\d{8}T\d{6}Z",
include: "rules/filelist_gen.smk"
include: "rules/chanlist_gen.smk"
include: "rules/common.smk"
include: "rules/main.smk"
include: "rules/tcm.smk"
include: "rules/dsp.smk"
include: "rules/psp.smk"
include: "rules/hit.smk"
include: "rules/pht.smk"
include: "rules/pht_fast.smk"
include: "rules/evt.smk"
include: "rules/skm.smk"
include: "rules/blinding_calibration.smk"
include: "rules/qc_phy.smk"
# Log parameter catalogs in validity.jsonl files
hit_par_cat_file = os.path.join(pars_path(setup), "hit", "validity.jsonl")
if os.path.isfile(hit_par_cat_file):
os.remove(os.path.join(pars_path(setup), "hit", "validity.jsonl"))
pathlib.Path(os.path.dirname(hit_par_cat_file)).mkdir(parents=True, exist_ok=True)
ds.pars_key_resolve.write_to_jsonl(hit_par_catalog, hit_par_cat_file)
pht_par_cat_file = os.path.join(pars_path(setup), "pht", "validity.jsonl")
if os.path.isfile(pht_par_cat_file):
os.remove(os.path.join(pars_path(setup), "pht", "validity.jsonl"))
pathlib.Path(os.path.dirname(pht_par_cat_file)).mkdir(parents=True, exist_ok=True)
ds.pars_key_resolve.write_to_jsonl(pht_par_catalog, pht_par_cat_file)
dsp_par_cat_file = os.path.join(pars_path(setup), "dsp", "validity.jsonl")
if os.path.isfile(dsp_par_cat_file):
os.remove(dsp_par_cat_file)
pathlib.Path(os.path.dirname(dsp_par_cat_file)).mkdir(parents=True, exist_ok=True)
ds.pars_key_resolve.write_to_jsonl(dsp_par_catalog, dsp_par_cat_file)
psp_par_cat_file = os.path.join(pars_path(setup), "psp", "validity.jsonl")
if os.path.isfile(psp_par_cat_file):
os.remove(psp_par_cat_file)
pathlib.Path(os.path.dirname(psp_par_cat_file)).mkdir(parents=True, exist_ok=True)
ds.pars_key_resolve.write_to_jsonl(psp_par_catalog, psp_par_cat_file)
localrules:
gen_filelist,
autogen_output,
onstart:
print("INFO: starting workflow")
# Make sure some packages are initialized before we begin to avoid race conditions
for pkg in ["dspeed", "lgdo", "matplotlib"]:
shell('{swenv} python3 -B -c "import ' + pkg + '"')
# Log parameter catalogs in validity.jsonl files
hit_par_cat_file = os.path.join(pars_path(setup), "hit", "validity.jsonl")
if os.path.isfile(hit_par_cat_file):
os.remove(os.path.join(pars_path(setup), "hit", "validity.jsonl"))
pathlib.Path(os.path.dirname(hit_par_cat_file)).mkdir(parents=True, exist_ok=True)
ds.pars_key_resolve.write_to_jsonl(hit_par_catalog, hit_par_cat_file)
pht_par_cat_file = os.path.join(pars_path(setup), "pht", "validity.jsonl")
if os.path.isfile(pht_par_cat_file):
os.remove(os.path.join(pars_path(setup), "pht", "validity.jsonl"))
pathlib.Path(os.path.dirname(pht_par_cat_file)).mkdir(parents=True, exist_ok=True)
ds.pars_key_resolve.write_to_jsonl(pht_par_catalog, pht_par_cat_file)
dsp_par_cat_file = os.path.join(pars_path(setup), "dsp", "validity.jsonl")
if os.path.isfile(dsp_par_cat_file):
os.remove(dsp_par_cat_file)
pathlib.Path(os.path.dirname(dsp_par_cat_file)).mkdir(parents=True, exist_ok=True)
ds.pars_key_resolve.write_to_jsonl(dsp_par_catalog, dsp_par_cat_file)
psp_par_cat_file = os.path.join(pars_path(setup), "psp", "validity.jsonl")
if os.path.isfile(psp_par_cat_file):
os.remove(psp_par_cat_file)
pathlib.Path(os.path.dirname(psp_par_cat_file)).mkdir(parents=True, exist_ok=True)
ds.pars_key_resolve.write_to_jsonl(psp_par_catalog, psp_par_cat_file)
onsuccess:
from snakemake.report import auto_report
rep_dir = f"{log_path(setup)}/report-{datetime.strftime(datetime.utcnow(), '%Y%m%dT%H%M%SZ')}"
pathlib.Path(rep_dir).mkdir(parents=True, exist_ok=True)
# auto_report(workflow.persistence.dag, f"{rep_dir}/report.html")
with open(os.path.join(rep_dir, "dag.txt"), "w") as f:
f.writelines(str(workflow.persistence.dag))
# shell(f"cat {rep_dir}/dag.txt | dot -Tpdf > {rep_dir}/dag.pdf")
with open(f"{rep_dir}/rg.txt", "w") as f:
f.writelines(str(workflow.persistence.dag.rule_dot()))
# shell(f"cat {rep_dir}/rg.txt | dot -Tpdf > {rep_dir}/rg.pdf")
# remove .gen files
files = glob.glob("*.gen")
for file in files:
if os.path.isfile(file):
os.remove(file)
# remove filelists
files = glob.glob(os.path.join(filelist_path(setup), "*"))
for file in files:
if os.path.isfile(file):
os.remove(file)
if os.path.exists(filelist_path(setup)):
os.rmdir(filelist_path(setup))
# remove logs
files = glob.glob(os.path.join(tmp_log_path(setup), "*", "*.log"))
for file in files:
if os.path.isfile(file):
os.remove(file)
dirs = glob.glob(os.path.join(tmp_log_path(setup), "*"))
for d in dirs:
if os.path.isdir(d):
os.rmdir(d)
if os.path.exists(tmp_log_path(setup)):
os.rmdir(tmp_log_path(setup))
rule gen_filelist:
"""Generate file list.
It is a checkpoint so when it is run it will update the dag passed on the
files it finds as an output. It does this by taking in the search pattern,
using this to find all the files that match this pattern, deriving the keys
from the files found and generating the list of new files needed.
"""
input:
lambda wildcards: get_filelist(
wildcards,
setup,
get_pattern_tier_raw(setup),
ignore_keys_file=os.path.join(configs, "ignore_keys.keylist"),
analysis_runs_file=os.path.join(configs, "analysis_runs.json"),
),
output:
os.path.join(filelist_path(setup), "{label}-{tier}.filelist"),
run:
if len(input) == 0:
print(
"WARNING: No files found for the given pattern\nmake sure pattern follows the format: all-{experiment}-{period}-{run}-{datatype}-{timestamp}-{tier}.gen"
)
with open(output[0], "w") as f:
for fn in input:
f.write(f"{fn}\n")