akshayansamy commited on
Commit
944e4a0
·
verified ·
1 Parent(s): 3f8b960

Upload src/01_build_list.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/01_build_list.py +148 -0
src/01_build_list.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Scan pieces/2026_01_26 for cullpdb_* list and .fasta pairs. Writes:
4
+ 1) cullpdb_list_fasta_index.csv - paired only, with full paths
5
+ 2) cullpdb_full_compiled_list.csv - every list and every fasta found (full compiled list)
6
+ """
7
+ import csv
8
+ import re
9
+ from pathlib import Path
10
+ from typing import Optional
11
+
12
+ SCRIPT_DIR = Path(__file__).resolve().parent
13
+ BASE = SCRIPT_DIR.parent
14
+ CULLPDB_DIR = BASE / "pieces" / "2026_01_26"
15
+ CURATED_DIR = BASE / "curated_csv"
16
+ OUT_CSV = CURATED_DIR / "cullpdb_list_fasta_index.csv"
17
+ FULL_LIST_CSV = CURATED_DIR / "cullpdb_full_compiled_list.csv"
18
+
19
+ # cullpdb_pc20.0_res0.0-1.0_noBrks_len40-10000_R0.2_Xray_d2026_01_26_chains300
20
+ PAT = re.compile(
21
+ r"^cullpdb_pc([\d.]+)_res([\d.]+)-([\d.]+)_"
22
+ r"(?:(noBrks)_)?"
23
+ r"len40-10000_R([\d.]+)_"
24
+ r"(.+?)_d\d{4}_\d{2}_\d{2}_chains(\d+)$"
25
+ )
26
+
27
+
28
+ def parse_basename(name: str) -> Optional[dict]:
29
+ base = name.removesuffix(".fasta")
30
+ m = PAT.match(base)
31
+ if not m:
32
+ return None
33
+ pc, res_min, res_max, no_brks, r_cutoff, methods, n_chains = m.groups()
34
+ n = int(n_chains)
35
+ no_brk = no_brks is not None
36
+ return {
37
+ "list_basename": base,
38
+ "fasta_basename": base + ".fasta",
39
+ "n_chains": n,
40
+ "pc": float(pc),
41
+ "resolution": f"{res_min}-{res_max}",
42
+ "no_breaks": "yes" if no_brk else "no",
43
+ "R": float(r_cutoff),
44
+ "Nmethods": methods,
45
+ }
46
+
47
+
48
+ def main():
49
+ dir_path = Path(CULLPDB_DIR)
50
+ if not dir_path.is_dir():
51
+ print(f"Missing cullpdb dir: {dir_path}")
52
+ return
53
+ dir_path = dir_path.resolve()
54
+ # Collect every basename that has a list and/or fasta
55
+ bases = set()
56
+ for p in dir_path.iterdir():
57
+ if not p.is_file():
58
+ continue
59
+ name = p.name
60
+ if not name.startswith("cullpdb_") or "len40-10000" not in name:
61
+ continue
62
+ base = name.removesuffix(".fasta")
63
+ bases.add(base)
64
+
65
+ # Full compiled list: one row per basename, list path + fasta path (full), and parsed params
66
+ full_rows = []
67
+ paired_rows = []
68
+ for base in sorted(bases):
69
+ list_path = dir_path / base
70
+ fasta_path = dir_path / (base + ".fasta")
71
+ list_exists = list_path.exists()
72
+ fasta_exists = fasta_path.exists()
73
+ parsed = parse_basename(base)
74
+ if not parsed:
75
+ full_rows.append({
76
+ "list_basename": base,
77
+ "fasta_basename": base + ".fasta",
78
+ "list_path": str(list_path) if list_exists else "",
79
+ "fasta_path": str(fasta_path) if fasta_exists else "",
80
+ "list_exists": list_exists,
81
+ "fasta_exists": fasta_exists,
82
+ "paired": list_exists and fasta_exists,
83
+ "n_chains": "",
84
+ "pc": "",
85
+ "resolution": "",
86
+ "no_breaks": "",
87
+ "R": "",
88
+ "Nmethods": "",
89
+ })
90
+ continue
91
+ row_full = {
92
+ "list_basename": base,
93
+ "fasta_basename": base + ".fasta",
94
+ "list_path": str(list_path) if list_exists else "",
95
+ "fasta_path": str(fasta_path) if fasta_exists else "",
96
+ "list_exists": list_exists,
97
+ "fasta_exists": fasta_exists,
98
+ "paired": list_exists and fasta_exists,
99
+ "n_chains": parsed["n_chains"],
100
+ "pc": parsed["pc"],
101
+ "resolution": parsed["resolution"],
102
+ "no_breaks": parsed["no_breaks"],
103
+ "R": parsed["R"],
104
+ "Nmethods": parsed["Nmethods"],
105
+ }
106
+ full_rows.append(row_full)
107
+ if list_exists and fasta_exists:
108
+ paired_rows.append({
109
+ "list_basename": base,
110
+ "fasta_basename": base + ".fasta",
111
+ "list_path": str(list_path),
112
+ "fasta_path": str(fasta_path),
113
+ "n_chains": parsed["n_chains"],
114
+ "pc": parsed["pc"],
115
+ "resolution": parsed["resolution"],
116
+ "no_breaks": parsed["no_breaks"],
117
+ "R": parsed["R"],
118
+ "Nmethods": parsed["Nmethods"],
119
+ })
120
+
121
+ CURATED_DIR.mkdir(parents=True, exist_ok=True)
122
+ # 1) Paired index with full paths
123
+ fieldnames = [
124
+ "list_basename", "fasta_basename", "list_path", "fasta_path",
125
+ "n_chains", "pc", "resolution", "no_breaks", "R", "Nmethods",
126
+ ]
127
+ with open(OUT_CSV, "w", newline="") as f:
128
+ w = csv.DictWriter(f, fieldnames=fieldnames)
129
+ w.writeheader()
130
+ w.writerows(paired_rows)
131
+ print(f"Wrote {OUT_CSV} with {len(paired_rows)} list↔fasta pairs (full paths).")
132
+
133
+ # 2) Full compiled list: every basename, list/fasta paths and exists flags
134
+ full_fieldnames = [
135
+ "list_basename", "fasta_basename", "list_path", "fasta_path",
136
+ "list_exists", "fasta_exists", "paired",
137
+ "n_chains", "pc", "resolution", "no_breaks", "R", "Nmethods",
138
+ ]
139
+ with open(FULL_LIST_CSV, "w", newline="") as f:
140
+ w = csv.DictWriter(f, fieldnames=full_fieldnames)
141
+ w.writeheader()
142
+ w.writerows(full_rows)
143
+ n_paired = sum(1 for r in full_rows if r["paired"])
144
+ print(f"Wrote {FULL_LIST_CSV} with {len(full_rows)} entries (full compiled list); {n_paired} paired.")
145
+
146
+
147
+ if __name__ == "__main__":
148
+ main()