-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_pop.py
167 lines (137 loc) · 4.87 KB
/
generate_pop.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
155
156
157
158
159
160
161
162
163
164
165
166
167
import pathlib
import os
import argparse
from typing import IO
from tqdm import tqdm
from utils import (
extract_project_file_name,
is_valid_namespace,
is_valid_project,
write_pop_cfg,
)
PEP_VERSION = "2.0.0"
DELIM = ","
HEADER_COLS = ["sample_name", "namespace", "project_name", "type", "location"]
def build_argparser():
parser = argparse.ArgumentParser(
description="Generate a POP (a PEP of PEPs) given a directory."
)
parser.add_argument("-o", "--out", type=str, default=".", help="path to POP output")
parser.add_argument(
"-c",
"--config",
type=str,
dest="config_file_name",
default="pop.yaml",
help="name of POP configuration file",
)
parser.add_argument(
"-s",
"--samples",
type=str,
dest="samples_file_name",
default="peps.csv",
help="name of sample table file",
)
parser.add_argument(
"-p",
"--peps",
type=str,
dest="path_to_peps",
help="Path to a folder/repository of PEPs",
)
parser.add_argument(
"-g",
"--geo",
type=str,
dest="path_to_geo",
help="Path to a file that contains geo accession ids",
)
return parser
def parse_pep_dir(path_to_peps: str, fh: IO):
"""Parse a given directory of PEPs"""
# traverse directory
for name in tqdm(os.listdir(path_to_peps), desc="Analyzing repository", leave=True):
# build a path to the namespace
path_to_namespace = f"{path_to_peps}/{name}"
if is_valid_namespace(path_to_namespace):
# traverse projects
for proj in tqdm(
os.listdir(path_to_namespace), desc=f"Analyzing {name}", leave=True
):
# build path to project
path_to_proj = f"{path_to_namespace}/{proj}"
if is_valid_project(path_to_proj):
# build cfg file
cfg_file = (
f"{path_to_proj}/{extract_project_file_name(path_to_proj)}"
)
sample_table_row = DELIM.join(
[f"{name}-{proj}", name, proj, "path", cfg_file]
)
fh.write(sample_table_row + "\n")
def parse_geo_list(path_to_geo: str, fh: IO):
"""Parse a list of geo accession IDs given a path to a list of them"""
with open(path_to_geo) as geo_f:
accession_ids = geo_f.read().splitlines()
for accession in accession_ids:
sample_table_row = DELIM.join(
[f"geo-{accession}", "geo", accession, "geo", accession]
)
fh.write(sample_table_row + "\n")
def generate_pop(
path_to_peps: str = None,
path_to_geo: str = None,
cfg_name: str = "pop.yaml",
sample_table_path: str = "peps.csv",
):
"""
Given a directory of PEPs in the namespace/project format, generate one unifying PEP of PEPs (POP)
to be used as input to looper for indexing.
"""
if all([path_to_peps is None, path_to_geo is None]):
raise ValueError(
"A path to peps **or** path to a file with geo accession ids must be supplied"
)
# check path to peps exists
if path_to_peps and not os.path.exists(path_to_peps):
raise FileNotFoundError(f"Path to PEPs does not exist: '{path_to_peps}'")
# check path to geo exists
if path_to_geo and not os.path.exists(path_to_geo):
raise FileNotFoundError(
f"Path to geo accesion list does not exist: '{path_to_geo}'"
)
# create a path to cfg if necessary
if not os.path.exists(cfg_name):
filepath = pathlib.Path(cfg_name)
filepath.parent.mkdir(parents=True, exist_ok=True)
# create a path to the sample table if necessary
if not os.path.exists(sample_table_path):
filepath = pathlib.Path(sample_table_path)
filepath.parent.mkdir(parents=True, exist_ok=True)
# init the cfg file
write_pop_cfg(cfg_name, sample_table_path)
with open(sample_table_path, "w") as fh:
# write the csv header
fh.write(DELIM.join(HEADER_COLS) + "\n")
# if a path to peps was supplied, parase it
if path_to_peps:
print("Parsing peps")
parse_pep_dir(path_to_peps, fh)
if path_to_geo:
print("Parsing geo accessions")
parse_geo_list(path_to_geo, fh)
def main():
parser = build_argparser()
args = parser.parse_args()
if not os.path.exists(args.out):
filepath = pathlib.Path(args.out)
filepath.parent.mkdir(exist_ok=True)
cfg_file_name = f"{args.out}/{args.config_file_name}"
samples_file_name = f"{args.out}/{args.samples_file_name}"
generate_pop(args.path_to_peps, args.path_to_geo, cfg_file_name, samples_file_name)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Goodbye.")