Skip to content

Commit 03dc485

Browse files
authored
changed the code a bit according to Omer's requests. Doesn't effect results. (#38)
## Use full path for file path * changed the grim.run_impute function in the run_impute_def file to handle full path. Also now we're passing the em_mr/hap_pop_pair value in grim.grim in impute function. Then in grim.run_impute_def also add at the end of the values in the function ,hap_pop_pair = False,graph =None. Then we're returning the graph at the end. In the middle of the function we're creating the graph if it's None. * Update run_impute_def.py
1 parent d24780b commit 03dc485

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

grim/grim.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def graph_freqs(conf_file="", for_em=False, em_pop=None):
5454
)
5555

5656

57-
def impute(conf_file=""):
57+
def impute(conf_file="", hap_pop_pair = False, graph = None):
5858

5959
project_dir_in_file, project_dir_graph = "", ""
6060
if conf_file == "":
@@ -70,7 +70,8 @@ def impute(conf_file=""):
7070
project_dir_in_file = (
7171
os.path.dirname(os.path.realpath(__file__)).replace("/grim", "") + "/"
7272
)
73-
run_impute(conf_file, project_dir_graph, project_dir_in_file)
73+
graph = run_impute(conf_file, project_dir_graph, project_dir_in_file, hap_pop_pair, graph)
74+
return graph
7475

7576

7677
def impute_instance(config, graph, count_by_prob=None):

grim/run_impute_def.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pathlib
55
import sys
66
import os
7+
from pathlib import Path
78

89
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
910

@@ -15,10 +16,34 @@
1516
# pr.enable()
1617

1718

19+
def full_path(output, original_path):
20+
"""
21+
Modifies the given path by replacing the last segment with the output directory
22+
and appending the original last segment to the new path.
23+
24+
Parameters:
25+
original_path (str): The original file path.
26+
output (str): The new directory name to replace the last segment.
27+
28+
Returns:
29+
str: The modified file path.
30+
"""
31+
# Convert to Path object
32+
path = Path(original_path)
33+
34+
# Construct the new path
35+
new_path = path.parent / output / path.name
36+
37+
# Return the new path as a string
38+
return str(new_path)
39+
40+
1841
def run_impute(
1942
conf_file="../conf/minimal-configuration.json",
2043
project_dir_graph="",
2144
project_dir_in_file="",
45+
hap_pop_pair = False,
46+
graph = None
2247
):
2348

2449
configuration_file = conf_file
@@ -56,18 +81,12 @@ def run_impute(
5681
+ json_conf.get("edges_csv_file"),
5782
"imputation_input_file": project_dir_in_file
5883
+ json_conf.get("imputation_in_file"),
59-
"imputation_out_umug_freq_file": output_dir
60-
+ json_conf.get("imputation_out_umug_freq_filename"),
61-
"imputation_out_umug_pops_file": output_dir
62-
+ json_conf.get("imputation_out_umug_pops_filename"),
63-
"imputation_out_hap_freq_file": output_dir
64-
+ json_conf.get("imputation_out_hap_freq_filename"),
65-
"imputation_out_hap_pops_file": output_dir
66-
+ json_conf.get("imputation_out_hap_pops_filename"),
67-
"imputation_out_miss_file": output_dir
68-
+ json_conf.get("imputation_out_miss_filename"),
69-
"imputation_out_problem_file": output_dir
70-
+ json_conf.get("imputation_out_problem_filename"),
84+
"imputation_out_umug_freq_file": full_path(output_dir, json_conf.get("imputation_out_umug_freq_filename")),
85+
"imputation_out_umug_pops_file": full_path(output_dir, json_conf.get("imputation_out_umug_pops_filename")),
86+
"imputation_out_hap_freq_file": full_path(output_dir, json_conf.get("imputation_out_hap_freq_filename")),
87+
"imputation_out_hap_pops_file": full_path(output_dir, json_conf.get("imputation_out_hap_pops_filename")),
88+
"imputation_out_miss_file": full_path(output_dir, json_conf.get("imputation_out_miss_filename")),
89+
"imputation_out_problem_file": full_path(output_dir, json_conf.get("imputation_out_problem_filename")),
7190
"factor_missing_data": json_conf.get("factor_missing_data", 0.01),
7291
"loci_map": json_conf.get(
7392
"loci_map", {"A": 1, "B": 3, "C": 2, "DQB1": 4, "DRB1": 5}
@@ -161,18 +180,21 @@ def run_impute(
161180

162181
config["full_loci"] = "".join(sorted(all_loci_set))
163182
# Perform imputation
164-
graph = Graph(config)
165-
graph.build_graph(
166-
config["node_file"], config["top_links_file"], config["edges_file"]
167-
)
183+
if graph==None:
184+
graph = Graph(config)
185+
graph.build_graph(
186+
config["node_file"], config["top_links_file"], config["edges_file"]
187+
)
168188
imputation = Imputation(graph, config)
169189

170190
# Create output directory if it doesn't exist
171191
pathlib.Path(output_dir).mkdir(parents=False, exist_ok=True)
172192

173193
# Write out the results from imputation
174-
imputation.impute_file(config)
194+
imputation.impute_file(config, em_mr=hap_pop_pair)
175195

176196
# Profiler end
177197
# pr.disable()
178198
# pr.print_stats(sort="time")
199+
200+
return graph

0 commit comments

Comments
 (0)