-
Notifications
You must be signed in to change notification settings - Fork 0
/
InPheC_Step3.py
556 lines (443 loc) · 22.2 KB
/
InPheC_Step3.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
"""
InPheC_Step3.py
This script is an integral part of the InPheRNo-ChIP project's step 3 process.
Key Operations:
- Combines multiple batches
- Averages posterior probabilities across different chains from step 2's PGM outputs.
- Applies gene-wise filters to the averaged data.
- Performs min-max normalization and form the final phenotype-relevant GRN.
Usage:
- Run the script from the command line "python InPheC_Step3.py" directly.
- For detailed argument options, use the help option: `python InPheC_Step3.py --help`
Note:
Ensure the Python environment is properly set up and all dependencies are installed before running this script.
"""
import argparse
import logging
import os
import platform
import random
import re
import sys
import time
import warnings
from multiprocessing import Pool
from typing import Any, Dict, Tuple, Union
import cloudpickle
import numpy as np
import pandas as pd
from tqdm import tqdm
from utils_logo import print_logo
class Step3ArgumentParser:
"""
Parses command-line arguments for the script.
This class configures and retrieves command-line options, returning the parsed arguments as an argparse.Namespace object.
It sets default values for input and output directories, the number of batches, and the number of burn-in iterations for posterior processing.
:return: An argparse.Namespace object containing all the command-line arguments.
:rtype: argparse.Namespace
:Example:
args = ArgumentParser.parse()
print(args.in_dir) # Prints the input directory specified in the command-line
"""
@staticmethod
def parse() -> argparse.Namespace:
"""
Sets up the command-line arguments and parses them.
:return: Parsed arguments from the command line.
:rtype: argparse.Namespace
"""
parser = argparse.ArgumentParser()
parser.add_argument("-id", "--in_dir", default="./step2")
parser.add_argument("-od", "--out_dir", default="./step3")
parser.add_argument(
"-nba", "--n_batches", type=int, default=13, help="number of the batches in the input folder."
)
parser.add_argument(
"-nb",
"--n_burn",
type=int,
default=400,
help="number of iterations to burn, \
it's not uncommon to discard the first 10 percent of the post-tuning draws as burn-in",
)
return parser.parse_args()
class FileNotFound(Exception):
"""
Custom exception for handling file-not-found errors.
This exception is raised when a required file is not found in the specified directory,
extending the base Exception class.
:param str message: The error message to display.
"""
pass
class ParameterExtractor:
"""
Extracts parameters from file names in the specified directory.
Designed to read file names in a given directory and extract key parameters such as T_prior, n_c, n_i, and n_t, which are essential for processing steps in the pipeline.
:param in_dir: Input directory where files are located.
:type in_dir: str
:return: A dictionary containing extracted parameters with keys like 'sample_fn', 'T_prior', 'n_c', 'n_i', and 'n_t'.
:rtype: Dict[str, Union[str, float]]
:raises FileNotFound: If no relevant pickle file is found in the directory.
"""
def __init__(self, in_dir: str):
"""
Initializes the ParameterExtractor with the specified input directory.
:param in_dir: Directory containing the files to extract parameters from.
:type in_dir: str
"""
self.in_dir = in_dir
def extract(self) -> Dict[str, Union[str, float]]:
"""
Extracts parameters from the filenames in the input directory.
:return: A dictionary containing the extracted parameters.
:rtype: Dict[str, Union[str, float]]
:raises FileNotFound: If no relevant pickle file is found in the directory.
"""
sample_file = next((f for f in os.listdir(self.in_dir) if f.startswith("RnCnP_") and f.endswith(".pkl")), None)
if sample_file is None:
raise FileNotFound("No pickle file found in directory.")
return {
"sample_fn": sample_file,
**dict(zip(["T_prior", "n_c", "n_i", "n_t"], map(float, re.findall(r"[-+]?\d*\.\d+|\d+", sample_file)))),
}
class PosteriorCombiner:
"""
Combines posterior distributions from multiple batches and chains.
Processes each batch, extracting and combining posterior distributions into a single DataFrame from pickle files.
:param in_dir: Input directory containing the pickle files.
:type in_dir: str
:param n_batches: Number of batches to process.
:type n_batches: int
:param n_burn: Number of burn-in iterations.
:type n_burn: int
:param common_params: Common parameters used across the batches.
:type common_params: dict
:return: Tuple containing paths to the combined posterior file and the in2pgm data.
:rtype: tuple
"""
def __init__(self, in_dir: str, n_batches: int, n_burn: int, common_params: dict):
"""
Initializes the PosteriorCombiner with the necessary parameters.
:param in_dir: Directory containing the input pickle files.
:param n_batches: Number of batches to process.
:param n_burn: Number of burn-in iterations to discard.
:param common_params: Common parameters used across the batches.
"""
self.in_dir = in_dir
self.n_batches = n_batches
self.n_burn = n_burn
self.common_params = common_params
def _ordinal(self, n: int) -> str:
"""
Converts an integer to its ordinal representation (e.g., 1st, 2nd, 3rd).
:param n: Number to convert.
:type n: int
:return: Ordinal string of the number.
:rtype: str
"""
suffix = ["th", "st", "nd", "rd", "th"][min(n % 10, 4)]
if 11 <= (n % 100) <= 13:
suffix = "th"
return str(n) + suffix
def _process_chain(self) -> Tuple[pd.DataFrame, pd.DataFrame]:
"""
Processes each batch within the chain, extracting and combining posterior distributions.
:return: Tuple of DataFrames containing the recovered posteriors and the genes in the PGM.
:rtype: Tuple[pd.DataFrame, pd.DataFrame]
:raises FileNotFound: If a pickle file is not found in the directory for a specific batch.
"""
logging.info("Processing all chains together")
T_recovered_allGenes_ls = []
genes_in2pgm = []
# Inner progress bar for tracking progress within the chain
for i_b in tqdm(range(self.n_batches), desc="Processing Batches", leave=False):
logging.debug(f"\t batch id = {i_b}") # Debug message
# Locate the pickle
file_suffix = f"batch{i_b}.pkl"
f = next(
(
filename
for filename in os.listdir(self.in_dir)
if os.path.isfile(os.path.join(self.in_dir, filename)) and filename.endswith(file_suffix)
),
None,
)
logging.info(f)
if f is None:
raise FileNotFound(f" Oops! No pickle file found in directory for batch {i_b}, please check.")
else:
with open(os.path.join(self.in_dir, f), "rb") as file:
genes_onebatch = cloudpickle.load(file)
for gene, values in genes_onebatch["trace"].items():
T_samples = values.posterior[
"T"
] # (4, 1000, 1, 23, 1): (n_chian, n_samples, n_genes, n_tfs, n_lineage)
T_burn = T_samples.sel(draw=slice(self.n_burn, None)) # (4, 900, 1, 23, 1)
# Pooling across all chains
T_pooled = np.mean(
T_burn, axis=(0, 1)
) # Averaging over chains and draws (not the same as concatenation of chains!)
jj_tfs = genes_onebatch["info_per_batch"][gene]["TF"]
T_recovered_jj = pd.DataFrame(T_pooled.values.reshape(1, -1), index=[gene], columns=list(jj_tfs))
T_recovered_allGenes_ls.append(T_recovered_jj)
# load genes_onebatch into genes_in2pgm
genes_in2pgm.append(pd.concat(genes_onebatch["info_per_batch"].values()))
return pd.concat(T_recovered_allGenes_ls, ignore_index=False), pd.concat(genes_in2pgm)
def combine(self, out_dir: str):
"""
Combines the posteriors and saves the results to specified output files.
:param out_dir: Directory where the output files will be saved.
:type out_dir: str
:return: Tuple of paths to the combined in2pgm and posterior files.
:rtype: Tuple[str, str]
"""
# Part 1: combine posteriors
result, inputs_to_pgm = self._process_chain()
logging.info(f"Completed combining chains: Saving to ./step3/step2-in2pgm.pkl")
# output: save the genes_in2pgm data outside the loop, since the in2pgm is the same for all processes/batches.
fout_in2pgm = os.path.join(out_dir, f"step2-in2pgm.pkl")
with open(fout_in2pgm, "wb") as handle:
cloudpickle.dump(inputs_to_pgm, handle)
# output: pickle the combined posteriors
fout_posterior = os.path.join(
out_dir, re.sub(r"(_noburn).*", f"_nb{self.n_burn}.pkl", self.common_params["sample_fn"])
)
with open(fout_posterior, "wb") as handle:
cloudpickle.dump(result, handle)
return fout_posterior
class DataProcessor:
"""
Processes and analyzes the combined posterior data from probabilistic graphical models.
This class loads and processes the in2pgm and posterior data, combines them, and applies filtering and normalization
to produce a final gene regulatory network (GRN).
:param fn1: Path to the first input file containing in2pgm data.
:type fn1: str
:param fn2: Path to the second input file containing posterior data.
:type fn2: str
"""
def __init__(self, fn: str):
self.fn = fn
self.pgmout = self._load_data()
def _load_data(self):
"""
Loads data from the specified files.
:return: Tuple of DataFrames containing the in2pgm data and posterior data.
:rtype: Tuple[pd.DataFrame, pd.DataFrame]
:raises ValueError: If either of the files is empty.
"""
# Check if files are empty
if os.path.getsize(self.fn) == 0:
raise ValueError(f"Empty file detected. Please delete files in ./step3/tmp and re-run step3.")
# load
with open(self.fn, "rb") as file:
data = cloudpickle.load(file)
return data
def combine_in2pgm_outposterior(self) -> pd.DataFrame:
"""
Combines the in2pgm and posterior data into a single DataFrame.
This function merges the two datasets on the TF and Gene columns, effectively creating an edge list for the GRN.
:return: Combined DataFrame with the in2pgm and posterior data.
:rtype: pd.DataFrame
"""
# convert the pivot table to an edge list
melted_df = self.pgmout.reset_index().melt(
id_vars="index", value_vars=self.pgmout.columns, var_name="TF", value_name="raw_posterior_mean"
)
melted_df = melted_df.rename(columns={"index": "Gene"})
# combine data1 and data2
combined_df = pd.merge(self.in2pgm, melted_df, on=["TF", "Gene"], how="inner")
return combined_df
def _calculate_max_differential_metrics(self, case: pd.Series, a_col: str) -> dict:
"""
Calculates differential metrics to identify significant thresholds for filtering.
This function computes differences and ratios between consecutive elements in a sorted series
to determine the most significant threshold.
:param case: Series of data points to analyze.
:type case: pd.Series
:param a_col: Column identifier in the DataFrame to calculate metrics.
:type a_col: str
:return: Dictionary of calculated metrics.
:rtype: Dict[str, float]
:raises ValueError: If the computed metrics do not agree on a single cutoff point.
"""
y = case[a_col].values
info = {
"Diff: yi-yj": [i - j for i, j in zip(y[:-1], y[1:])],
"ratio_1: (yi-yj) / y_max": [(i - j) / max(y) for i, j in zip(y[:-1], y[1:])],
"ratio_4: (yi-yj) / (y_max-y_ref)": [(i - j) / (max(y) - min(y)) for i, j in zip(y[:-1], y[1:])],
}
maxx_dict = pd.DataFrame.from_dict(info).idxmax().to_dict()
maxLoc = set(maxx_dict.values())
if len(maxLoc) > 1:
raise ValueError("3 metric do not agree with each other")
return {"metric-Diff/R1/R4": y[maxx_dict["ratio_4: (yi-yj) / (y_max-y_ref)"] + 1]}
def _apply_normalization_to_each_gene(self, df, coi) -> pd.DataFrame:
"""
Applies min-max normalization to each gene in the DataFrame.
:param df: DataFrame containing raw scores to normalize.
:type df: pd.DataFrame
:param coi: Column of interest in the DataFrame to normalize.
:type coi: str
:return: DataFrame with normalization applied.
:rtype: pd.DataFrame
"""
df["min_max_normalized"] = df.groupby("Gene")[coi].transform(lambda x: (x - x.min()) / (x.max() - x.min()))
return df
def _apply_filters_to_each_gene(self, df, coi, Tp_val) -> tuple[pd.DataFrame, pd.DataFrame]:
"""
Applies filtering based on differential metrics and threshold values to each gene.
This function determines filtering criteria and then applies these filters to identify significant gene-TF interactions.
:param df: DataFrame to apply filters.
:type df: pd.DataFrame
:param coi: Column of interest where filtering is applied.
:type coi: str
:param Tp_val: Threshold prior value used in filtering.
:type Tp_val: float
:return: Tuple containing the filtered DataFrame and a DataFrame with filter criteria.
:rtype: Tuple[pd.DataFrame, pd.DataFrame]
"""
# ---- find filtering criterion ----
# filter 1: calculates differences and ratios between consecutive elements in a sorted series
filter_criterion = self._calculate_max_differential_metrics(df, coi)
# filter 2: get value of T_prior
filter_criterion["T_prior"] = Tp_val
# filter 3: get Tij values of 3 controls in each gene
ref_tfs = df[df["TF"].str.startswith("ref_")]
ref_tf_values = dict(zip(ref_tfs["TF"], ref_tfs[coi]))
filter_criterion.update(ref_tf_values)
# find the maximum value of the filter criterion
max_value = max(filter_criterion.values())
# ---- apply filters ----
mask_df = df.copy()
mask_df["final_importance_score"] = mask_df[mask_df[coi] > max_value][coi]
# mask the value < max to 0
# mask_df["final_importance_score"] = mask_df[coi].where(df[coi] > max_value, 0)
filter_criterion["max_val"] = max_value
return mask_df, pd.DataFrame([filter_criterion]) # List wrapping ensures a single-row DataFrame
def form_final_grn(self, col_raw_score, Tp_val) -> pd.DataFrame:
"""
Forms the final gene regulatory network (GRN) based on normalized and filtered data.
:param col_raw_score: Column name of the raw scores used for final processing.
:type col_raw_score: str
:param Tp_val: Threshold prior value used in forming the GRN.
:type Tp_val: float
:return: Tuple containing the final GRN DataFrame and gene-specific thresholds.
:rtype: pd.DataFrame
"""
processed_dfs = []
# Group by 'Gene' and apply normalization and filtering to each group
long_format_df = self.pgmout.reset_index().melt(id_vars='index', var_name='TF', value_name=col_raw_score).rename(columns={'index': 'Gene'})
for gene, group_df in tqdm(long_format_df.groupby("Gene")):
# sort the group by 'raw_posterior_mean' in descending order
group_df = group_df.sort_values(by=col_raw_score, ascending=False)
norm_df = self._apply_normalization_to_each_gene(group_df, col_raw_score)
flt_norm_df, _ = self._apply_filters_to_each_gene(norm_df, "min_max_normalized", Tp_val)
# Append the processed group to the list
processed_dfs.append(flt_norm_df)
# Combine all processed groups into a single df
tmp_df = pd.concat(processed_dfs, ignore_index=True)
# Sparse network
formatted_df = tmp_df[~tmp_df['TF'].str.startswith('ref_')].fillna('').pivot(index='Gene', columns='TF', values='final_importance_score').apply(pd.to_numeric, errors='coerce')
return formatted_df
class Utils:
@staticmethod
def configure_logging(log_dir: str) -> None:
"""
Configures the logging system for the script, directing log output to a file in a specified directory.
:param log_dir: Directory where the log file will be stored.
:type log_dir: str
"""
os.makedirs(log_dir, exist_ok=True)
logging.basicConfig(
level=logging.INFO, # Set the log level
format="%(asctime)s - %(levelname)s - %(message)s", # Set the log format
filename=os.path.join(log_dir, "step3.log"), # Optional: Specify a file to write logs to
filemode="a", # 'w' for overwrite, 'a' for append
)
@staticmethod
def check_pkl_file_count(in_dir: str, expected_count: int):
"""
Checks if the number of .pkl files in the input directory matches the expected number.
:param in_dir: Directory to check for .pkl files.
:type in_dir: str
:param expected_count: The number of .pkl files expected in the directory.
:type expected_count: int
:raises ValueError: If the number of .pkl files does not match the expected count.
"""
pkl_files = [f for f in os.listdir(in_dir) if f.endswith(".pkl")]
if len(pkl_files) != expected_count:
raise ValueError(
f"Number of .pkl files found ({len(pkl_files)}) does not match the expected number ({expected_count}). "
"Please ensure that step 2 was completed successfully, all batches were processed, "
"and the `-nba` argument is correctly set according to the batch size and the total number of genes analyzed in step 2."
)
@staticmethod
def print_python_details():
"""
Prints detailed information about the Python environment running the script.
Outputs various Python-related system information, including version, compiler, and other system details.
"""
# Print the rest of the details in original language
print("\nThank you for using InPheRNo-ChIP! Python Environment Details:")
print("----------------------------")
print("Python version:", platform.python_version())
print("Compiler:", platform.python_compiler())
print("Build:", platform.python_build())
print("System:", platform.system())
print("Release:", platform.release())
print("Machine:", platform.machine())
print("Processor:", platform.processor())
print("sys.version:", sys.version.replace("\n", " "))
print("-------------Bye-------------")
def step3_main() -> None:
"""
Executes the main functionality of the Step 3 script in the InPheRNo-ChIP project. This function manages the workflow
that combines and processes posterior distributions from probabilistic graphical models (PGMs) generated in Step 2.
The main steps include:
- Configuring logging for debugging and information tracking.
- Parsing command line arguments to get directories and processing parameters.
- Checking the count of .pkl files to ensure all expected files are present.
- Creating necessary directories for outputs.
- Extracting and logging parameters from file names.
- Checking for and combining existing processed data or processing new data.
- Merging, filtering, and normalizing data to form the final Gene Regulatory Network (GRN).
- Saving the final outputs in an Excel file with multiple sheets for easy review.
:raises FileNotFoundError: If required files are not found in the specified directories.
:raises ValueError: If processing parameters are incorrect or if files are empty.
"""
print_logo("Running step3")
# Use the Utils class for configuring logging
Utils.configure_logging("./logs")
args = Step3ArgumentParser.parse()
# Check if the number of .pkl files matches the number of batches
Utils.check_pkl_file_count(args.in_dir, args.n_batches)
# output directory
os.makedirs(args.out_dir, exist_ok=True)
intermediate_folder = os.path.join(args.out_dir, "tmp")
os.makedirs(intermediate_folder, exist_ok=True)
# Initialize the process timer
start_time = time.time()
common_params = ParameterExtractor(in_dir=args.in_dir).extract()
logging.info(
f"Running [step3A.py], input folder: {args.in_dir}; \nn_chain = {common_params['n_c']}, n_batch = {args.n_batches}"
)
# ----- part a. combine posteriors -----
combiner = PosteriorCombiner(
in_dir=args.in_dir, n_batches=args.n_batches, n_burn=args.n_burn, common_params=common_params
)
faddr_posterior = combiner.combine(out_dir=intermediate_folder)
logging.info(
f"Step 3 part a - posteriors combination is completed, TIME:{time.time() - start_time}. \n Now applying filtering and normalization..."
)
# ----- part b. filter and normalize -----
postprocess = DataProcessor(faddr_posterior)
sort_by = "raw_posterior_mean"
out_grn = postprocess.form_final_grn(sort_by, common_params["T_prior"])
# save to excel with 2 tabs
base_name = os.path.basename(faddr_posterior) # Extracts 'posterior_file.pkl'
fout = os.path.join(args.out_dir, f"{base_name.replace('.pkl', '_minMax.csv')}")
out_grn.to_csv(fout)
# Use the Utils class to print Python environment details at the end
Utils.print_python_details()
if __name__ == "__main__":
step3_main()