PISCES-CulledPDB / src /01_build_list.py
akshayansamy's picture
Upload src/01_build_list.py with huggingface_hub
944e4a0 verified
#!/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()