-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_subset.py
154 lines (138 loc) · 3.92 KB
/
make_subset.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import argparse
import re
import pickle
import pandas as pd
import itertools
from pathlib import Path
from tqdm import tqdm
from subset_comparisons import (
handle_filters,
matcher,
make_subset_comparisons
)
from path_to_sample import sample_re
from build_graph import build_graph
from gene_matches_tables import read_table
from collections.abc import Iterable
from typing import Iterator
def handle_arguments():
parser = argparse.ArgumentParser(
description="subset a previous analysis, creating a new graph"
)
parser.add_argument(
"-O",
"--output-dir",
type=Path,
required=True,
help="directory in which to store subset comparisons and graph"
)
parser.add_argument(
"-I",
"--input-dir",
type=Path,
required=True,
help="directory for full set of data"
)
parser.add_argument(
"--exclude",
"-x",
nargs="+",
default=[],
help="samples to exclude (default is none)"
)
parser.add_argument(
"--include",
"-y",
nargs="+",
default=[],
help="samples to include (default is all)"
)
parser.add_argument(
"--include-regex",
"-Y",
type=re.compile,
help="regular expression specifying which sample names to include"
)
parser.add_argument(
"--sample-name-regex",
"-r",
help="regular expression to apply to sample names",
type=re.compile,
default=sample_re
)
parser.add_argument(
"--include-file",
type=Path,
help="file containing samples to include"
)
parser.add_argument(
"--exclude-file",
type=Path,
help="file containing samples to exclude"
)
parser.add_argument(
"--show-included",
action="store_true",
help="show which samples would be included and exit"
)
parser.add_argument(
"--show-parsed-paths",
action="store_true",
help="show parsed paths"
)
return parser.parse_args()
def multi_glob(path: Path, globs: Iterable[str]) -> Iterator[Path]:
return itertools.chain(*map(path.glob, globs))
def get_table_files(path: Path) -> Iterator[Path]:
return multi_glob(path, ["*.pkl", "*.h5"])
def main():
args = handle_arguments()
include = handle_filters(args.include, args.include_file)
if not include:
include = None
exclude = handle_filters(args.exclude, args.exclude_file)
if not exclude:
exclude = None
matches = matcher(
include,
exclude,
args.include_regex
)
inputs = list(get_table_files(args.input_dir / "od2"))
if args.show_included or args.show_parsed_paths:
for df_path in inputs:
df = read_table(df_path, head=1)
if args.show_parsed_paths:
print(
*(
args.sample_name_regex.search(
Path(df[x + "sample"].iloc[0]).name
).group(1)
for x in ["q", "s"]
)
)
if args.show_included and all(
matches(
args.sample_name_regex.search(
Path(df[x + "sample"].iloc[0]).name
).group(1)
)
for x in ["q", "s"]
):
print(df_path)
else:
args.output_dir.mkdir(exist_ok=True)
od2 = args.output_dir / "od2"
od2.mkdir(exist_ok=True)
graph = build_graph(
make_subset_comparisons(
tqdm(inputs),
od2,
matches,
args.sample_name_regex
)
)
with open(args.output_dir / "graph.pkl", "wb") as f:
pickle.dump(graph, f, pickle.HIGHEST_PROTOCOL)
if __name__ == "__main__":
main()