-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize_config.py
More file actions
219 lines (183 loc) · 9.1 KB
/
Copy pathinitialize_config.py
File metadata and controls
219 lines (183 loc) · 9.1 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
import os
import shutil
import configparser
from pathlib import Path
def find_x3dna_bottom_up(start_path):
"""
Looks for X3DNA by searching downward from start_path, then walking up
through parent directories, and finally checking standard system directories.
"""
# 1. Search downward from the current package directory first
try:
for path in start_path.glob("**/x3dna*"):
if path.is_dir() and ((path / "bin" / "find_pair").exists() or (path / "bin" / "fiber").exists()):
return path
except (PermissionError, FileNotFoundError):
pass
# 2. Walk upwards through parent directories and scan inside them
current = start_path.parent
# Stop when we hit the root directory
while current != current.parent:
try:
# Look for an x3dna folder inside this parent level (non-recursive to avoid huge sweeps)
for path in current.glob("x3dna*"):
if path.is_dir() and ((path / "bin" / "find_pair").exists() or (path / "bin" / "fiber").exists()):
return path
# Also do a quick 1-level deep check in case it's in a sibling folder (e.g., ../software/x3dna)
for path in current.glob("*/x3dna*"):
if path.is_dir() and ((path / "bin" / "find_pair").exists() or (path / "bin" / "fiber").exists()):
return path
except (PermissionError, FileNotFoundError):
pass
current = current.parent
# 3. Global fallbacks if it's completely outside the user's project tree
fallback_roots = [Path.home(), Path("/opt"), Path("/usr/local")]
for root in fallback_roots:
if not root.exists() or root == start_path or start_path in root.parents:
continue # Skip if we already covered it
try:
for path in root.glob("**/x3dna*"):
if path.is_dir() and ((path / "bin" / "find_pair").exists() or (path / "bin" / "fiber").exists()):
return path
except (PermissionError, FileNotFoundError):
continue
return None
def generate_dynamic_config():
# 1. Locate the absolute path of this package deployment
pkg_root = Path(__file__).resolve().parent
config_path = pkg_root / "ModCRElib" / "configure" / "config.ini"
if not config_path.exists():
print(f"Error: Base configuration file not found at {config_path}")
return
# 2. Extract active Conda environment values
conda_prefix = os.environ.get("CONDA_PREFIX")
if not conda_prefix:
print("Warning: No active Conda environment detected ($CONDA_PREFIX is empty).")
print("Falling back to standard system-wide PATH evaluation.\n")
# 3. Read the existing config template
config = configparser.ConfigParser(allow_no_value=True)
config.read(config_path)
print(f"Automating configuration updates for: {config_path}")
print(f"Package Root detected at: {pkg_root}\n")
# 4. Automate static file asset locations
if "Paths" not in config:
config["Paths"] = {}
config["Paths"]["scripts_path"] = str(pkg_root)
config["Paths"]["src_path"] = str(pkg_root / "ModCRElib")
config["Paths"]["pdb_dir"] = str(pkg_root / "pdb")
config["Paths"]["pbm_dir"] = str(pkg_root / "pbm")
config["Paths"]["files_path"] = str(pkg_root / "files")
config["Paths"]["modpy_path"] = str(pkg_root / "modpy")
config["Paths"]["sbilib_path"] = str(pkg_root / "SBILib")
# Sub-files mapping
config["Paths"]["TF_GOMF"] = str(pkg_root / "files" / "TF_molecular_function_w.txt")
config["Paths"]["TF_GOBP"] = str(pkg_root / "files" / "TF_biological_process_w.txt")
config["Paths"]["nTF_GOMF"] = str(pkg_root / "files" / "NonTF_molecular_function.txt")
config["Paths"]["nTF_GOBP"] = str(pkg_root / "files" / "NonTF_biological_process.txt")
config["Paths"]["posKW"] = str(pkg_root / "files" / "Positive_keywords.txt")
config["Paths"]["negKW"] = str(pkg_root / "files" / "Negative_keywords.txt")
config["Paths"]["species"] = str(pkg_root / "files" / "speclist.txt")
# Historical paths
config["Paths"]["jaspar_dir"] = str(pkg_root / "files")
config["Paths"]["cisbp_dir"] = str(pkg_root / "files")
# 5. Automatically locate external executables using PATH/Conda environment
binary_mappings = {
"python_path": "python",
"blast_path": "blastp",
"clustalo_path": "clustalo",
"dssp_path": "mkdssp",
"emboss_path": "matcher",
"hmmer_path": "hmmscan",
"meme_path": "meme",
"tmalign_path": "TMalign",
"weblogo_path": "weblogo",
"cd-hit": "cd-hit",
"ghostscript_path": "gs",
"mmseqs": "mmseqs",
}
for config_key, binary_name in binary_mappings.items():
found_path = shutil.which(binary_name)
if found_path:
found_path = str(Path(shutil.which(binary_name)).parent)
if not found_path and binary_name == "mkdssp":
found_path = shutil.which("dssp")
if found_path:
config["Paths"][config_key] = found_path
print(f"✓ Found {binary_name} -> {found_path}")
else:
config["Paths"][config_key] = ""
print(f"✗ Could not automatically locate binary: {binary_name}")
# =========================================================
# 6. DYNAMIC AND BOTTOM-UP X3DNA SETUP
# =========================================================
x3dna_env = os.environ.get("X3DNA")
x3dna_detected_path = ""
if x3dna_env:
x3dna_detected_path = str(Path(x3dna_env) / "bin")
print(f"✓ Found X3DNA via environment path -> {x3dna_detected_path}")
else:
# Check standard system execution paths
find_pair_path = shutil.which("find_pair") or shutil.which("fiber")
if find_pair_path:
x3dna_detected_path = str(Path(find_pair_path).parent)
print(f"✓ Found X3DNA via active system PATH -> {x3dna_detected_path}")
else:
# Deep proximity file scan strategy
print("⚠ X3DNA not found in active PATH. Initiating proximity search mapping...")
deep_search_result = find_x3dna_bottom_up(pkg_root)
if deep_search_result:
x3dna_detected_path = str(deep_search_result / "bin")
print(f"✓ Dynamically discovered local X3DNA setup -> {x3dna_detected_path}")
# Commit evaluated location back to config object
if x3dna_detected_path:
config["Paths"]["x3dna_path"] = x3dna_detected_path
else:
config["Paths"]["x3dna_path"] = ""
print("✗ X3DNA path could not be automatically determined anywhere.")
# =========================================================
# ROBUST DYNAMIC MODELLER DETECTOR
# =========================================================
modeller_bin_path = ""
custom_env_root = os.environ.get("MODELLER_ROOT") or os.environ.get("MODELLER_HOME")
if custom_env_root and Path(custom_env_root).exists():
root_path = Path(custom_env_root).resolve()
candidate_bin = root_path / "bin" / "modpy.sh"
if candidate_bin.exists():
modeller_bin_path = str(Path(candidate_bin).parent)
config["Paths"]["modeller_path"] = modeller_bin_path
print(f"✓ Found Modeller via environment variable ($MODELLER_ROOT) -> {root_path}")
if not modeller_bin_path:
modpy_executable = shutil.which("modpy.sh")
if modpy_executable:
resolved_bin = Path(modpy_executable).resolve()
root_path = resolved_bin.parent.parent
if (root_path / "modlib").exists():
modeller_bin_path = str(resolved_bin)
print(f"✓ Found Modeller via active system PATH -> {root_path}")
if not modeller_bin_path:
print("✗ Modeller 10.3 custom installation could not be detected automatically.")
# =========================================================
# 7. INTERACTIVE CLUSTER CONFIGURATION POPULATION
# =========================================================
print("\n--- Cluster Configuration Setup ---")
print("Please provide the following details. Press Enter to keep the default value [in brackets].\n")
if "Cluster" not in config:
config["Cluster"] = {}
cluster_fields = [
"cluster_name", "cluster_queue", "cluster_submit", "cluster_qstat",
"max_jobs_in_queue", "min_jobs_in_queue", "command_queue"#,
#"server_host", "server_user", "server_passwd", "server_directory", "server_python"
]
for field in cluster_fields:
current_val = config["Cluster"].get(field, "None")
user_input = input(f"Enter {field} [{current_val}]: ").strip()
if user_input:
config["Cluster"][field] = user_input
else:
config["Cluster"][field] = current_val
# 8. Write the populated data back to the file
with open(config_path, "w") as configfile:
config.write(configfile)
print("\n[SUCCESS] config.ini successfully updated with local environment configuration.")
if __name__ == "__main__":
generate_dynamic_config()