-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-snakefiles.py
executable file
·137 lines (105 loc) · 3.79 KB
/
find-snakefiles.py
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
#! /usr/bin/env python
"""
Find all files matching 'Snakefile' or 'snakefile.*', and write shell
code to run them. Very much a WIP.
TODO?
- set bash foo
- write individual shell scripts => support parallel??
- support isolated build/etc directories
"""
import sys
import os
import argparse
import subprocess
from pathlib import Path
PATH_FILTERS = []
remove_out = lambda p: str(p).endswith('.out')
PATH_FILTERS.append(remove_out)
remove_bak = lambda p: str(p).endswith('~') or str(p).endswith('.bak')
PATH_FILTERS.append(remove_bak)
remove_dot = lambda p: any(pp.startswith('.') for pp in p.parts)
PATH_FILTERS.append(remove_dot)
def read_snakefile_metadata(filename):
d = {}
with open(filename, 'rt') as fp:
lines = []
for line in fp:
line = line.strip()
if not line.startswith('#'):
break
line = line[1:].strip()
lines.append(line)
d['expect_fail'] = False
if 'expect_fail' in lines:
d['expect_fail'] = True
d['ignore'] = False
if 'ignore' in lines:
d['ignore'] = True
for x in lines:
if x.startswith('targets:'):
x = x[8:]
d['targets'] = x.strip()
return d
def main():
p = argparse.ArgumentParser()
p.add_argument('dirs', nargs='+')
p.add_argument('-o', '--output', default=None)
p.add_argument('-l', '--list-snakefiles', action='store_true')
p.add_argument('-d', '--debug', action='store_true')
p.add_argument('-q', '--quiet', action='store_true')
p.add_argument('-k', '--keyword-limit-pattern', default=None,
help="specify keyword required in filename to run")
args = p.parse_args()
snakefiles = []
for dirname in args.dirs:
pp = Path(dirname)
snakefiles.extend(pp.glob('**/Snakefile'))
snakefiles.extend(pp.glob('**/snakefile.*'))
# filter:
snakefiles_filtered = []
for ss in snakefiles:
if any(f(ss) for f in PATH_FILTERS):
if args.debug:
print(f"(removing '{str(ss)}' from paths because of a filter)",
file=sys.stderr)
continue
if args.keyword_limit_pattern:
if args.keyword_limit_pattern not in str(ss):
continue
# keep!
snakefiles_filtered.append(ss)
snakefiles = snakefiles_filtered
print(f"found {len(snakefiles)} snakefiles to run!", file=sys.stderr)
if args.list_snakefiles:
print("\n".join([ str(ss) for ss in snakefiles ]))
if args.output:
print(f"Saving run script to '{args.output}'", file=sys.stderr)
output = open(args.output, 'wt')
print("#! /bin/bash", file=output)
print("failed=0", file=output)
for snakefile in snakefiles:
targets = ''
metadata = read_snakefile_metadata(snakefile)
if 'ignore' in metadata and metadata['ignore']:
if not args.quiet:
print(f"(IGNORING '{snakefile}' per metadata)",
file=sys.stderr)
continue
if 'targets' in metadata:
targets = metadata['targets']
dirname, filename = os.path.split(snakefile)
if metadata['expect_fail']:
print(f"""
cd {dirname} > /dev/null
snakemake -s {filename} -j 1 -p {targets} >& {filename}.out && {{ echo fail {snakefile}; failed=1; }} || echo success {snakefile}
cd - > /dev/null
""", file=output)
else:
print(f"""
cd {dirname} > /dev/null
snakemake -s {filename} -j 1 -p {targets} >& {filename}.out && echo success {snakefile} || {{ echo fail {snakefile}; failed=1; }}
cd - > /dev/null
""", file=output)
print("exit $failed", file=output)
if __name__ == '__main__':
sys.exit(main())