ProteinMPNN / src /split_dataset.py
leebecca's picture
Upload folder using huggingface_hub
6bb897c verified
import pandas as pd
import shutil
import os
import glob
# ============================================================
# CONFIGURATION - Edit these paths as needed
# ============================================================
PDB_ROOT = "pdb"
OUTPUT_ROOT = "pdb_2021aug02"
TRAIN_CSV = "train.csv"
VALIDATION_CSV = "validation.csv"
TEST_CSV = "test.csv"
SPLITS = {
"training": TRAIN_CSV,
"validation": VALIDATION_CSV,
"test": TEST_CSV
}
# ============================================================
# HELPER FUNCTIONS
# ============================================================
def get_subfolder(name):
"""
Extract subfolder from filename.
E.g., '1nb4_A' -> 'nb' (characters at index 1 and 2)
"""
# Strip extension if present
base = os.path.splitext(name)[0]
return base[1:3].lower() # e.g., '1nb4_A' -> 'nb'
def read_csv_entries(csv_file):
"""
Read comma-separated entries from a single-line CSV.
E.g., 5naf_A,5naf_B,5naf_C,...
"""
entries = []
with open(csv_file, 'r') as f:
for line in f:
entries.extend(line.strip().split(','))
return [e.strip() for e in entries if e.strip()]
def find_pt_file(pdb_root, name):
"""
Locate the .pt file given the entry name.
E.g., '1nb4_A' -> pdb_root/nb/1nb4_A.pt
"""
subfolder = get_subfolder(name)
pt_path = os.path.join(pdb_root, subfolder, f"{name}.pt")
if os.path.exists(pt_path):
return pt_path, subfolder
return None, subfolder
def copy_file(src, dst_dir, filename):
"""
Copy file to destination directory, creating dirs if needed.
"""
os.makedirs(dst_dir, exist_ok=True)
dst = os.path.join(dst_dir, filename)
shutil.copy2(src, dst)
return dst
# ============================================================
# MAIN
# ============================================================
def main():
stats = {}
for split_name, csv_file in SPLITS.items():
print(f"\n{'='*50}")
print(f"Processing: {split_name.upper()} <- {csv_file}")
print(f"{'='*50}")
if not os.path.exists(csv_file):
print(f" [WARNING] CSV file not found: {csv_file}")
continue
entries = read_csv_entries(csv_file)
print(f" Total entries found in CSV: {len(entries)}")
copied = 0
missing = []
for name in entries:
src_path, subfolder = find_pt_file(PDB_ROOT, name)
if src_path:
# Destination: e.g., OUTPUT_ROOT/validation/nb/1nb4_A.pt
dst_dir = os.path.join(OUTPUT_ROOT, split_name, subfolder)
copy_file(src_path, dst_dir, f"{name}.pt")
copied += 1
else:
missing.append(name)
print(f"Copied : {copied}")
print(f"Missing : {len(missing)}")
if missing:
missing_log = os.path.join(OUTPUT_ROOT, f"{split_name}_missing.txt")
with open(missing_log, "w") as f:
f.write("\n".join(missing))
print(f"Missing entries logged to: {missing_log}")
stats[split_name] = {"copied": copied, "missing": len(missing)}
# ---- Summary ----
print(f"\n{'='*50}")
print("SUMMARY")
print(f"{'='*50}")
for split, s in stats.items():
print(f" {split:<12}: {s['copied']} copied, {s['missing']} missing")
if __name__ == "__main__":
main()