-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.smk
146 lines (112 loc) · 5.4 KB
/
common.smk
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
__author__ = "{{ author }}"
__copyright__ = "Copyright {{ year }}, {{ author }}"
__email__ = "{{ email }}"
__license__ = "GPL-3"
import itertools
import numpy as np
import pathlib
import pandas as pd
import yaml
from snakemake.utils import validate
from snakemake.utils import min_version
from hydra_genetics.utils.resources import load_resources
from hydra_genetics.utils.samples import *
from hydra_genetics.utils.units import *
min_version("{{ min_snakemake_version }}")
### Set and validate config file
if not workflow.overwrite_configfiles:
sys.exit("At least one config file must be passed using --configfile/--configfiles, by command line or a profile!")
try:
validate(config, schema="../schemas/config.schema.yaml")
except WorkflowError as we:
# Probably a validation error, but the original exception in lost in
# snakemake. Pull out the most relevant information instead of a potentially
# *very* long error message.
if not we.args[0].lower().startswith("error validating config file"):
raise
error_msg = "\n".join(we.args[0].splitlines()[:2])
parent_rule_ = we.args[0].splitlines()[3].split()[-1]
if parent_rule_ == "schema:":
sys.exit(error_msg)
else:
schema_hiearachy = parent_rule_.split()[-1]
schema_section = ".".join(re.findall(r"\['([^']+)'\]", schema_hiearachy)[1::2])
sys.exit(f"{error_msg} in {schema_section}")
### Read and validate resources file
config = load_resources(config, config["resources"])
validate(config, schema="../schemas/resources.schema.yaml")
### Read and validate samples file
samples = pd.read_table(config["samples"], dtype=str).set_index("sample", drop=False)
validate(samples, schema="../schemas/samples.schema.yaml")
### Read and validate units file
units = (
pandas.read_table(config["units"], dtype=str)
.set_index(["sample", "type", "flowcell", "lane", "barcode"], drop=False)
.sort_index()
)
validate(units, schema="../schemas/units.schema.yaml")
### Read and validate output file
with open(config["output"]) as output:
if config["output"].endswith("json"):
output_spec = json.load(output)
elif config["output"].endswith("yaml") or config["output"].endswith("yml"):
output_spec = yaml.safe_load(output.read())
validate(output_spec, schema="../schemas/output_files.schema.yaml")
### Set wildcard constraints
wildcard_constraints:
sample="|".join(samples.index),
type="N|T|R",
def compile_output_file_list(wildcards):
outdir = pathlib.Path(output_spec.get("directory", "./"))
output_files = []
for f in output_spec["files"]:
# Please remember to add any additional values down below
# that the output strings should be formatted with.
outputpaths = set(
[
f["output"].format(sample=sample, type=unit_type)
for sample in get_samples(samples)
for unit_type in get_unit_types(units, sample)
]
)
for op in outputpaths:
output_files.append(outdir / Path(op))
return output_files
def generate_copy_rules(output_spec):
output_directory = pathlib.Path(output_spec.get("directory", "./"))
rulestrings = []
for f in output_spec["files"]:
if f["input"] is None:
continue
rule_name = "_copy_{}".format("_".join(re.split(r"\W{1,}", f["name"].strip().lower())))
input_file = pathlib.Path(f["input"])
output_file = output_directory / pathlib.Path(f["output"])
mem_mb = config.get("_copy", {}).get("mem_mb", config["default_resources"]["mem_mb"])
mem_per_cpu = config.get("_copy", {}).get("mem_per_cpu", config["default_resources"]["mem_per_cpu"])
partition = config.get("_copy", {}).get("partition", config["default_resources"]["partition"])
threads = config.get("_copy", {}).get("threads", config["default_resources"]["threads"])
time = config.get("_copy", {}).get("time", config["default_resources"]["time"])
copy_container = config.get("_copy", {}).get("container", config["default_container"])
rule_code = "\n".join(
[
f'@workflow.rule(name="{rule_name}")',
f'@workflow.input("{input_file}")',
f'@workflow.output("{output_file}")',
f'@workflow.log("logs/{rule_name}_{output_file.name}.log")',
f'@workflow.container("{copy_container}")',
f'@workflow.resources(time="{time}", threads={threads}, mem_mb="{mem_mb}", '
f'mem_per_cpu={mem_per_cpu}, partition="{partition}")',
f'@workflow.shellcmd("{copy_container}")',
"@workflow.run\n",
f"def __rule_{rule_name}(input, output, params, wildcards, threads, resources, "
"log, version, rule, conda_env, container_img, singularity_args, use_singularity, "
"env_modules, bench_record, jobid, is_shell, bench_iteration, cleanup_scripts, "
"shadow_dir, edit_notebook, conda_base_path, basedir, runtime_sourcecache_path, "
"__is_snakemake_rule_func=True):",
'\tshell("(cp --preserve=timestamps {input[0]} {output[0]}) &> {log}", bench_record=bench_record, '
"bench_iteration=bench_iteration)\n\n",
]
)
rulestrings.append(rule_code)
exec(compile("\n".join(rulestrings), "copy_result_files", "exec"), workflow.globals)
generate_copy_rules(output_spec)