-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathenrich_data.py
More file actions
59 lines (43 loc) · 1.77 KB
/
Copy pathenrich_data.py
File metadata and controls
59 lines (43 loc) · 1.77 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
import json
from rdkit import Chem
from rdkit.Chem import Draw
from jsonschema import validate
from documentation_user.examples.mof_synthesis.utils import collectMetadata
# Load JSON document with synthesis data
with open("ecmofsynthesis.json") as f:
data = json.load(f)
with open("ecmofsynthesis.schema.json") as f:
schema = json.load(f)
# Validate the JSON document
validate(data, schema)
print("Data validated successfully")
result_data = []
# Iterate over the JSON document and enrich it with metadata
for index, entry in enumerate(data[("ecmofsynthesis")]):
metalSaltName = entry["metal_salt_name"]
linkerName = entry["linker_name"]
# Collect metadata for both molecules
metalSaltMetadata = collectMetadata(metalSaltName)
linkerMetadata = collectMetadata(linkerName)
# draw both molecules and save to disk
if "smiles" in metalSaltMetadata:
mol1 = Chem.MolFromSmiles(metalSaltMetadata["smiles"])
Draw.MolToFile(mol1, f"metal_salt_{metalSaltName}.png")
if "smiles" in linkerMetadata:
mol2 = Chem.MolFromSmiles(linkerMetadata["smiles"])
Draw.MolToFile(mol2, f"linker_{linkerName}.png")
# Append metadata to the entry
result_entry = entry
result_entry["metal_salt"] = metalSaltMetadata
result_entry["linker"] = linkerMetadata
result_data.append(result_entry)
# Save the result data
full_result = {"ecmofsynthesis": result_data}
with open("ecmofsynthesis_enriched.json", "w") as f:
json.dump(full_result, f, indent=4)
print("Enriched data saved to ecmofsynthesis_enriched.json")
# Validate enriched data according to the schema for enriched files
with open("ecmofsynthesis_enriched.schema.json") as f:
schema = json.load(f)
validate(full_result, schema)
print("Enriched data validated successfully")