-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouchpycli_batch.sh
More file actions
181 lines (148 loc) · 3.65 KB
/
touchpycli_batch.sh
File metadata and controls
181 lines (148 loc) · 3.65 KB
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
#!/usr/bin/bash
filename=$1
if [[ $# -eq 0 ]]; then
>&2 echo "Error: no arguments provided"
>&2 echo "USAGE: $(basename $0) [NEW_FILE_NAME]"
exit 1
fi
touch "${filename}.py"
chmod +x "${filename}.py"
template() {
cat <<'EOF'
#!/usr/bin/env python3
"""
BATCH Description
Input:
...
Output:
...
Purpose:
...
Prerequisites:
...
\033[1m\033[31mWARNING:\033[0m
...
"""
# TODO:
# - [ ]
from concurrent.futures import ProcessPoolExecutor
from functools import partial
from Bio import SeqIO
from itertools import combinations
from pathlib import Path
from typing import TextIO, NamedTuple
import argparse
import gzip
import logging
import numpy as np
import polars as pl
import shutil
import subprocess
import sys
# =================================================================
# CLI args
# =================================================================
class Args(NamedTuple):
indir: Path
outdir: Path
cpu: int
no_parallel: bool
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-i",
"--indir",
type=Path,
metavar="FILE",
required=True,
help="Path to input dir [Required]",
)
parser.add_argument(
"-o",
"--outdir",
type=Path,
required=False,
default=".",
metavar="DIR",
help="Output target directory [Optional][Default: cwd]",
)
parser.add_argument(
"-c",
"--cpu",
type=int,
default=None,
metavar="N",
required=False,
help="No. of CPUs to use for parallelism [Default: max available]",
)
parser.add_argument(
"--no_parallel",
action="store_true",
help="Run script synchronously, not in parallel",
)
args = Args(**vars(parser.parse_args()))
return args
# =============================================================
# Util
# =============================================================
def open_gz(file: Path) -> TextIO:
"""Utility function: open file, even if it is gzipped"""
if file.suffix == ".gz":
return gzip.open(file, "rt")
else:
return open(file, "r")
# =============================================================
# Core funcs.
# =============================================================
def funca(
infile: Path,
outdir: Path,
args: Args,
) -> None:
"""Description
---
Args:
arg1 (dtype): description
Returns:
dtype: description
"""
# stuff
print("Hello world")
# =============================================================
def main() -> None:
"""Workflow:
---
main
├── args
└── func
│
"""
# get cli args
args = parse_args()
# get all input files
file_searcher = Path(args.indir).glob("*.faa")
infiles = sorted([f for f in file_searcher if f.is_file()])
############### no parallel processing ##################
if args.parallel:
for infile in infiles:
funca(infile=infile, outdir=args.outdir, args=args)
return
################# PARALLEL PROCESSING ###################
# make partial func
partial_funca = partial(
funca,
outdir=args.outdir,
args=args,
)
# ProcessPoolExecutor
with ProcessPoolExecutor(max_workers=args.cpu) as exe:
list(exe.map(partial_funca, infiles))
# =============================================================
if __name__ == "__main__":
sys.exit(main())
EOF
}
template >"${filename}.py"