-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
263 lines (209 loc) · 7.22 KB
/
main.py
File metadata and controls
263 lines (209 loc) · 7.22 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
__version__ = "0.0.1"
import time
from datetime import timedelta
from functools import reduce
from pathlib import Path
import numpy as np
import pandas as pd
pops = [
"AAFA",
"AFA",
"AFB",
"AINDI",
"AISC",
"ALANAM",
"AMIND",
"API",
"CARB",
"CARHIS",
"CARIBI",
"CAU",
"EURCAU",
"FILII",
"HAWI",
"HIS",
"JAPI",
"KORI",
"MENAFC",
"MSWHIS",
"NAM",
"NCHI",
"SCAHIS",
"SCAMB",
"SCSEAI",
"VIET",
]
loci_combinations = [
"A",
"C",
"B",
"DRB1",
"DRBX",
"DQA1",
"DQB1",
"DPA1",
"DPB1",
"A~C~B",
"A~B~DRB1",
"A~C~B~DRB1",
"A~B~DRB1~DQB1",
"C~B~DRB1~DQB1",
"A~C~B~DRB1~DQB1",
"A~C~B~DRBX~DRB1~DQB1",
"DPA1~DPB1",
"DQA1~DQB1",
"DRB1~DQB1",
"DRBX~DRB1",
"DRBX~DQB1",
"DRBX~DRB1~DQA1~DQB1",
"A~C~B~DRBX~DRB1~DQB1~DPB1",
"DRBX~DRB1~DQA1~DQB1~DPA1~DPB1",
"A~C~B~DRBX~DRB1~DQA1~DQB1~DPA1~DPB1",
]
def determine_loci_order():
# Sample haplotype from the frequency file
full_loci_haplotype = "A*03:01~C*07:02~B*07:02~DRB5*01:01~DRB1*15:01~DQB1*06:02~DQA1*01:02~DPA1*01:03~DPB1*04:01" # noqa: E501
loci = list(map(lambda a: a.split("*")[0], full_loci_haplotype.split("~")))
drbx_index = loci.index("DRB5")
loci_order = "~".join(loci[:drbx_index] + ["DRBX"] + loci[drbx_index + 1 :])
return loci_order
def df_from_freq_files(freqs_folder="freqs/"):
freq_dir = Path(freqs_folder)
# Glob for all .freqs.gz files
csv_gz_files = sorted(freq_dir.glob("*.freqs.gz"))
freq_dfs = dict()
print("Total files:", len(csv_gz_files))
for csv_gz_file in csv_gz_files:
population = csv_gz_file.name.removesuffix(".freqs.gz")
print(population, csv_gz_file)
freq_df = pd.read_csv(
csv_gz_file,
usecols=["Haplo", "Freq"],
dtype={"Haplo": str, "Freq": np.float64},
compression="gzip",
engine="pyarrow",
)
# Rename the Freq column to be population
freq_df = freq_df.rename(
columns={"Haplo": "Haplotype", "Freq": f"{population}-Freq"}
)
freq_dfs[population] = freq_df
return freq_dfs
def merge_all_with_total(freq_dfs):
print("Merging...")
merged_df = reduce(
lambda left, right: pd.merge(
left, right, on="Haplotype", how="outer", copy=False
),
freq_dfs.values(),
).fillna(0.0)
# Create a TotalFreq column that sums all the freq for all population
merged_df["TotalFreq"] = merged_df.filter(like="-Freq").sum(axis=1)
return merged_df
def keep_top_million(merged_df):
# Keep the top million
final_df = merged_df.sort_values(by="TotalFreq", ascending=False).head(1_000_000)
# Rename frequency column to only be population
final_df.columns = final_df.columns.str.replace(r"-Freq$", "", regex=True)
return final_df
def generate_excel_names(loci_set):
filename = loci_set + ".xlsx"
# Max length of Excel sheet is 31
sheet_name = loci_set[: 31 - 3] + "..." if len(loci_set) > 31 else loci_set
return filename, sheet_name
def get_output_dir(dirname):
output_dir = Path(dirname)
if not output_dir.exists():
output_dir.mkdir()
return output_dir
def save_to_excel(final_df, filename, sheet_name):
# Save to an Excel File
file_path = get_output_dir("excel") / filename
print("Frequencies: Saving to file:", file_path)
final_df.to_excel(file_path, index=False, sheet_name=sheet_name)
def create_loci_combo_freqs(final_df, loci_combo):
loci_columns = loci_combo.split("~")
loci_combo_df = (
final_df.groupby(loci_columns)[pops + ["TotalFreq"]].sum().reset_index()
)
loci_combo_df = loci_combo_df.sort_values(by="TotalFreq", ascending=False).head(
1_000_000
) # Keep top million
if len(loci_columns) == 1:
loci_combo_df = loci_combo_df.rename(columns={loci_combo: "Haplotype"})
else:
loci_combo_df.insert(
0, "Haplotype", loci_combo_df[loci_columns].agg("~".join, axis=1)
)
loci_combo_df = loci_combo_df.drop(loci_columns, axis=1)
print(loci_combo_df.head())
return loci_combo_df
def save_summary(df, loci_combo):
# Basic summary stats
summary = df.describe()
summary.loc["sum"] = df.sum()
print(summary)
filename = f"{loci_combo}-summary.xlsx"
file_path = get_output_dir("excel") / filename
print("Summary: Saving to file:", file_path)
summary.to_excel(file_path)
def create_full_locus_all_pops_df(freqs_dir):
freq_dfs = df_from_freq_files(freqs_dir)
merged_df = merge_all_with_total(freq_dfs)
final_df = keep_top_million(merged_df)
return final_df
def make_freqs(freqs_dir, file_format):
print("Hello from nmdp-bioinformatics!")
loci_set = determine_loci_order()
full_locus_freqs_df = create_full_locus_all_pops_df(freqs_dir)
# Save the 9-locus combined as a parquet file
full_locus_freqs_df.to_parquet(f"{loci_set}-haplotype.parquet", index=False)
full_locus_freqs_df = pd.read_parquet(f"{loci_set}-haplotype.parquet")
full_locus_freqs_df[loci_set.split("~")] = full_locus_freqs_df[
"Haplotype"
].str.split("~", expand=True)
#
# For all locus combo, generate Excel files
for i, loci_combo in enumerate(loci_combinations, start=1):
start_time = time.perf_counter()
print(f"{i}/{len(loci_combinations)}:", loci_combo, "=>", loci_combo.split("~"))
loci_combo_df = create_loci_combo_freqs(full_locus_freqs_df, loci_combo)
if file_format == "excel":
generate_excel_file(loci_combo, loci_combo_df)
elif file_format == "csv":
generate_csv_file(loci_combo, loci_combo_df)
elif file_format == "all":
generate_excel_file(loci_combo, loci_combo_df)
generate_csv_file(loci_combo, loci_combo_df)
save_summary(loci_combo_df, loci_combo)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
# Format the elapsed time
formatted_time = str(timedelta(seconds=elapsed_time))
print(f"Time to process {loci_combo}: {formatted_time}")
def generate_csv_file(loci_combo, loci_combo_df):
filename = f"{loci_combo}.csv.gz"
file_path = get_output_dir("csv") / filename
print("CSV: Saving to file:", file_path)
loci_combo_df.to_csv(file_path, index=False, compression="gzip")
def generate_excel_file(loci_combo, loci_combo_df):
filename, sheet_name = generate_excel_names(loci_combo)
save_to_excel(loci_combo_df, f"Total-{filename}", sheet_name)
loci_combo_df.drop("TotalFreq")
save_to_excel(loci_combo_df, filename, sheet_name)
def main():
import argparse
parser = argparse.ArgumentParser(
prog="make-freqs",
usage="make-freqs -d <frequency directory>",
description="Make frequency Files",
)
parser.add_argument("-d", "--freqs-dir", default="./freqs")
parser.add_argument("-f", "--format", default="excel")
args = parser.parse_args()
file_format = args.format.lower()
if file_format not in ("all", "excel", "csv"):
print(f"{file_format} format not yet supported")
make_freqs(args.freqs_dir, file_format)
if __name__ == "__main__":
main()