Skip to content

Commit 33cb3c2

Browse files
Merge pull request #39 from keisuke-yanagisawa/yaml_default
Refactor Default Value Handling in Configuration Files
2 parents c4aaba5 + 7fb388d commit 33cb3c2

File tree

5 files changed

+51
-33
lines changed

5 files changed

+51
-33
lines changed

script/alignresenv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def selector(a: Atom):
5656

5757
sup = SuperImposer()
5858
tmppdb = Path(tempfile.mkstemp(suffix=".pdb")[1])
59-
with uPDB.PDBIOhelper(str(tmppdb)) as pdbio:
59+
with uPDB.PDBIOhelper(tmppdb) as pdbio:
6060
for model in tqdm(struct, desc="[align res. env.]", disable=not verbose):
6161

6262
# print(struct, i)

script/test_generate_msmd_system.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ def test_create_frcmod(self):
6363

6464
def test_invalid_atomtype(self):
6565
with self.assertRaises(ValueError):
66-
_create_frcmod(self.mol2file, "INVALID") # type: ignore
66+
_create_frcmod(self.mol2file, Path("INVALID")) # type: ignore
6767

6868
def test_mol2file_does_not_exist(self):
6969
with self.assertRaises(FileNotFoundError):
70-
_create_frcmod("INVALID", self.atomtype) # type: ignore
70+
_create_frcmod(Path("INVALID"), self.atomtype) # type: ignore
7171

7272
def test_invalid_mol2file(self):
7373
with self.assertRaises(ValueError):

script/utilities/Bio/PDB.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class PDBIOhelper:
137137
これは1つずつモデルを保存していきます。
138138
"""
139139

140-
def __init__(self, path: str):
140+
def __init__(self, path: Path):
141141
self.path = expandpath(path)
142142
self.open()
143143

@@ -449,7 +449,7 @@ def concatenate_structures(structs: List[Structure]) -> Structure:
449449

450450
with tempfile.NamedTemporaryFile("w") as f:
451451

452-
out_helper = PDBIOhelper(f.name)
452+
out_helper = PDBIOhelper(Path(f.name))
453453
for struct in structs:
454454
out_helper.save(struct)
455455
out_helper.close()

script/utilities/Bio/test_PDB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_pdb_io_helper(self):
3030
reader = PDB.MultiModelPDBReader(str(self.pdbfile))
3131
models = [model for model in reader]
3232
tmp_output_pdb = Path(tempfile.mkstemp(suffix=".pdb")[1])
33-
writer = PDB.PDBIOhelper(str(tmp_output_pdb))
33+
writer = PDB.PDBIOhelper(tmp_output_pdb)
3434
for model in models:
3535
writer.save(model)
3636
writer.close() # This is important, otherwise the unittest will hang up

script/utilities/util.py

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
import yaml
88

99
from .logger import logger
10+
import collections.abc
1011

1112

13+
def update_dict(d: dict, u: dict):
14+
for k, v in u.items():
15+
if isinstance(v, collections.abc.Mapping):
16+
d[k] = update_dict(d.get(k, {}), v) # type: ignore
17+
else:
18+
d[k] = v
19+
return d
20+
1221
def getabsolutepath(path: Path) -> Path:
1322
"""
1423
Get absolute path from relative path
@@ -67,29 +76,6 @@ def expand_index(ind_info: Union[str, int]):
6776
return ret
6877

6978

70-
def set_default(setting: dict) -> None:
71-
"""
72-
Set default values (in place) for some fields in setting
73-
"""
74-
if "multiprocessing" not in setting["general"]:
75-
setting["general"]["multiprocessing"] = -1
76-
if "valid_dist" not in setting["map"]:
77-
setting["map"]["valid_dist"] = 5
78-
if "threshold" not in setting["probe_profile"]:
79-
setting["probe_profile"]["threshold"] = 0.001
80-
if "env_dist" not in setting["probe_profile"]:
81-
setting["probe_profile"]["resenv"]["env_dist"] = 4
82-
83-
if "dt" not in setting["exprorer_msmd"]["general"]:
84-
setting["exprorer_msmd"]["general"]["dt"] = 0.002
85-
if "temperature" not in setting["exprorer_msmd"]["general"]:
86-
setting["exprorer_msmd"]["general"]["temperature"] = 300
87-
if "pressure" not in setting["exprorer_msmd"]["general"]:
88-
setting["exprorer_msmd"]["general"]["pressure"] = 1.0
89-
if "num_process_per_gpu" not in setting["general"]:
90-
setting["general"]["num_process_per_gpu"] = 1
91-
92-
9379
def ensure_compatibility_v1_1(setting: dict):
9480
"""
9581
Ensure compatibility with exprorer_msmd v1.1
@@ -110,6 +96,38 @@ def ensure_compatibility_v1_1(setting: dict):
11096

11197

11298
def parse_yaml(yamlpath: Path) -> dict:
99+
setting: dict = {
100+
"general": {
101+
"workdir": Path(""),
102+
"multiprocessing": -1,
103+
"num_process_per_gpu": 1,
104+
},
105+
"input": {
106+
"protein": {
107+
"pdb": Path(""),
108+
},
109+
"probe": {
110+
"cid": "",
111+
},
112+
},
113+
"exprorer_msmd": {
114+
"general": {
115+
"dt": 0.002,
116+
"temperature": 300,
117+
"pressure": 1.0,
118+
},
119+
},
120+
"map": {
121+
"snapshot": "",
122+
"valid_dist": 5.0,
123+
},
124+
"probe_profile": {
125+
"threshold": 0.001,
126+
"resenv": {
127+
"env_dist": 4.0,
128+
},
129+
},
130+
}
113131
YAML_PATH = getabsolutepath(yamlpath)
114132
YAML_DIR_PATH = YAML_PATH.parent
115133
if not YAML_PATH.exists():
@@ -119,10 +137,11 @@ def parse_yaml(yamlpath: Path) -> dict:
119137
if not os.path.splitext(YAML_PATH)[1][1:] == "yaml":
120138
raise ValueError("YAML file must have .yaml extension: %s" % YAML_PATH)
121139
with YAML_PATH.open() as fin:
122-
setting: dict = yaml.safe_load(fin) # type: ignore
140+
yaml_dict: dict = yaml.safe_load(fin) # type: ignore
141+
if yaml_dict is not None:
142+
update_dict(setting, yaml_dict)
123143

124144
ensure_compatibility_v1_1(setting)
125-
set_default(setting)
126145

127146
if "mol2" not in setting["input"]["probe"] or setting["input"]["probe"]["mol2"] is None:
128147
setting["input"]["probe"]["mol2"] = setting["input"]["probe"]["cid"] + ".mol2"
@@ -169,9 +188,8 @@ def parse_yaml(yamlpath: Path) -> dict:
169188
)
170189
setting["input"]["probe"]["mol2"] = Path(setting["input"]["probe"]["mol2"])
171190

172-
if setting["input"]["protein"]["ssbond"] is None:
191+
if "ssbond" not in setting["input"]["protein"] or setting["input"]["protein"]["ssbond"] is None:
173192
setting["input"]["protein"]["ssbond"] = []
174193

175194
setting["general"]["yaml"] = YAML_PATH
176-
177195
return setting

0 commit comments

Comments
 (0)