|
|
import pandas as pd |
|
|
import argparse |
|
|
import os |
|
|
|
|
|
def safe_float(value): |
|
|
try: |
|
|
|
|
|
clean_value = value.replace("*^-", "e-").replace("*^", "e") |
|
|
return float(clean_value) |
|
|
except Exception as e: |
|
|
raise ValueError(f"Failed to parse float from '{value}': {e}") |
|
|
|
|
|
def process_xyz(filepath): |
|
|
data = {} |
|
|
with open(filepath, 'r') as f: |
|
|
lines = f.readlines() |
|
|
|
|
|
data["n_atoms"] = int(lines[0].strip()) |
|
|
values = lines[1].split() |
|
|
data["ID"] = values[1] |
|
|
|
|
|
|
|
|
data["SMILES_GDB17"] = lines[-2].strip().split()[0] |
|
|
|
|
|
|
|
|
data["Rotational_Constant_A"] = safe_float(values[2]) |
|
|
data["Rotational_Constant_B"] = safe_float(values[3]) |
|
|
data["Rotational_Constant_C"] = safe_float(values[4]) |
|
|
data["Dipole_Moment"] = safe_float(values[5]) |
|
|
data["Isotropic_polarizability"] = safe_float(values[6]) |
|
|
data["Energy_of_HOMO"] = safe_float(values[7]) |
|
|
data["Energy_of_LUMO"] = safe_float(values[8]) |
|
|
data["LUMO_HOMO_GAP"] = safe_float(values[9]) |
|
|
data["Electronic_spatial_extent"] = safe_float(values[10]) |
|
|
data["Zero_point_vibrational_energy"] = safe_float(values[11]) |
|
|
data["Internal_energy_at_0_K"] = safe_float(values[12]) |
|
|
data["Internal_energy_at_298.15_K"] = safe_float(values[13]) |
|
|
data["Enthalpy_at_298.15_K"] = safe_float(values[14]) |
|
|
data["Free_energy_at_298.15_K"] = safe_float(values[15]) |
|
|
data["Heat_capacity_at_298.15_K"] = safe_float(values[16]) |
|
|
|
|
|
for i in range(data["n_atoms"]): |
|
|
atom = lines[2 + i].split() |
|
|
data[f"element_{i}"] = atom[0] |
|
|
data[f"x_{i}"] = safe_float(atom[1]) |
|
|
data[f"y_{i}"] = safe_float(atom[2]) |
|
|
data[f"z_{i}"] = safe_float(atom[3]) |
|
|
data[f"charge_{i}"] = safe_float(atom[4]) |
|
|
|
|
|
|
|
|
return data |
|
|
|
|
|
|
|
|
def main(): |
|
|
parser = argparse.ArgumentParser() |
|
|
parser.add_argument("--file_list", required=True, help="Path to .txt file listing input .xyz files") |
|
|
parser.add_argument("--output_file", required=True, help="Path to output .parquet file (checkpointed)") |
|
|
parser.add_argument("--checkpoint_every", type=int, default=100, help="Save after every N molecules") |
|
|
args = parser.parse_args() |
|
|
print("Processing",args.file_list) |
|
|
with open(args.file_list, 'r') as f: |
|
|
files = [line.strip() for line in f if line.strip()] |
|
|
|
|
|
records = {} |
|
|
for i, path in enumerate(files): |
|
|
try: |
|
|
data = process_xyz(path) |
|
|
n_atoms=data["n_atoms"] |
|
|
records[n_atoms]=data |
|
|
except Exception as e: |
|
|
print(f" Failed on {path}: {e}") |
|
|
|
|
|
if (i + 1) % args.checkpoint_every == 0: |
|
|
df = pd.DataFrame(records) |
|
|
df.to_parquet(args.output_file, index=False) |
|
|
print(f"Checkpointed {len(df)} molecules at {i+1}/{len(files)}") |
|
|
|
|
|
|
|
|
if records: |
|
|
df = pd.DataFrame(records) |
|
|
df.to_parquet(args.output_file, index=False) |
|
|
print(f" Final write: {len(df)} molecules → {args.output_file}") |
|
|
else: |
|
|
print(" No valid data to save.") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |