File size: 5,316 Bytes
944e4a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
"""
Scan pieces/2026_01_26 for cullpdb_* list and .fasta pairs. Writes:
1) cullpdb_list_fasta_index.csv - paired only, with full paths
2) cullpdb_full_compiled_list.csv - every list and every fasta found (full compiled list)
"""
import csv
import re
from pathlib import Path
from typing import Optional

SCRIPT_DIR = Path(__file__).resolve().parent
BASE = SCRIPT_DIR.parent
CULLPDB_DIR = BASE / "pieces" / "2026_01_26"
CURATED_DIR = BASE / "curated_csv"
OUT_CSV = CURATED_DIR / "cullpdb_list_fasta_index.csv"
FULL_LIST_CSV = CURATED_DIR / "cullpdb_full_compiled_list.csv"

# cullpdb_pc20.0_res0.0-1.0_noBrks_len40-10000_R0.2_Xray_d2026_01_26_chains300
PAT = re.compile(
    r"^cullpdb_pc([\d.]+)_res([\d.]+)-([\d.]+)_"
    r"(?:(noBrks)_)?"
    r"len40-10000_R([\d.]+)_"
    r"(.+?)_d\d{4}_\d{2}_\d{2}_chains(\d+)$"
)


def parse_basename(name: str) -> Optional[dict]:
    base = name.removesuffix(".fasta")
    m = PAT.match(base)
    if not m:
        return None
    pc, res_min, res_max, no_brks, r_cutoff, methods, n_chains = m.groups()
    n = int(n_chains)
    no_brk = no_brks is not None
    return {
        "list_basename": base,
        "fasta_basename": base + ".fasta",
        "n_chains": n,
        "pc": float(pc),
        "resolution": f"{res_min}-{res_max}",
        "no_breaks": "yes" if no_brk else "no",
        "R": float(r_cutoff),
        "Nmethods": methods,
    }


def main():
    dir_path = Path(CULLPDB_DIR)
    if not dir_path.is_dir():
        print(f"Missing cullpdb dir: {dir_path}")
        return
    dir_path = dir_path.resolve()
    # Collect every basename that has a list and/or fasta
    bases = set()
    for p in dir_path.iterdir():
        if not p.is_file():
            continue
        name = p.name
        if not name.startswith("cullpdb_") or "len40-10000" not in name:
            continue
        base = name.removesuffix(".fasta")
        bases.add(base)

    # Full compiled list: one row per basename, list path + fasta path (full), and parsed params
    full_rows = []
    paired_rows = []
    for base in sorted(bases):
        list_path = dir_path / base
        fasta_path = dir_path / (base + ".fasta")
        list_exists = list_path.exists()
        fasta_exists = fasta_path.exists()
        parsed = parse_basename(base)
        if not parsed:
            full_rows.append({
                "list_basename": base,
                "fasta_basename": base + ".fasta",
                "list_path": str(list_path) if list_exists else "",
                "fasta_path": str(fasta_path) if fasta_exists else "",
                "list_exists": list_exists,
                "fasta_exists": fasta_exists,
                "paired": list_exists and fasta_exists,
                "n_chains": "",
                "pc": "",
                "resolution": "",
                "no_breaks": "",
                "R": "",
                "Nmethods": "",
            })
            continue
        row_full = {
            "list_basename": base,
            "fasta_basename": base + ".fasta",
            "list_path": str(list_path) if list_exists else "",
            "fasta_path": str(fasta_path) if fasta_exists else "",
            "list_exists": list_exists,
            "fasta_exists": fasta_exists,
            "paired": list_exists and fasta_exists,
            "n_chains": parsed["n_chains"],
            "pc": parsed["pc"],
            "resolution": parsed["resolution"],
            "no_breaks": parsed["no_breaks"],
            "R": parsed["R"],
            "Nmethods": parsed["Nmethods"],
        }
        full_rows.append(row_full)
        if list_exists and fasta_exists:
            paired_rows.append({
                "list_basename": base,
                "fasta_basename": base + ".fasta",
                "list_path": str(list_path),
                "fasta_path": str(fasta_path),
                "n_chains": parsed["n_chains"],
                "pc": parsed["pc"],
                "resolution": parsed["resolution"],
                "no_breaks": parsed["no_breaks"],
                "R": parsed["R"],
                "Nmethods": parsed["Nmethods"],
            })

    CURATED_DIR.mkdir(parents=True, exist_ok=True)
    # 1) Paired index with full paths
    fieldnames = [
        "list_basename", "fasta_basename", "list_path", "fasta_path",
        "n_chains", "pc", "resolution", "no_breaks", "R", "Nmethods",
    ]
    with open(OUT_CSV, "w", newline="") as f:
        w = csv.DictWriter(f, fieldnames=fieldnames)
        w.writeheader()
        w.writerows(paired_rows)
    print(f"Wrote {OUT_CSV} with {len(paired_rows)} list↔fasta pairs (full paths).")

    # 2) Full compiled list: every basename, list/fasta paths and exists flags
    full_fieldnames = [
        "list_basename", "fasta_basename", "list_path", "fasta_path",
        "list_exists", "fasta_exists", "paired",
        "n_chains", "pc", "resolution", "no_breaks", "R", "Nmethods",
    ]
    with open(FULL_LIST_CSV, "w", newline="") as f:
        w = csv.DictWriter(f, fieldnames=full_fieldnames)
        w.writeheader()
        w.writerows(full_rows)
    n_paired = sum(1 for r in full_rows if r["paired"])
    print(f"Wrote {FULL_LIST_CSV} with {len(full_rows)} entries (full compiled list); {n_paired} paired.")


if __name__ == "__main__":
    main()