File size: 3,456 Bytes
6bb897c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()