Add normalized Parquet train/test splits
Browse files- README.md +135 -0
- data/test-00000-of-00001.parquet +3 -0
- data/train-00000-of-00001.parquet +3 -0
- dataset_summary.json +64 -0
- scripts/prepare_cath_dataset.py +319 -0
README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
pretty_name: CATH Domain Classification
|
| 3 |
+
license: cc-by-4.0
|
| 4 |
+
tags:
|
| 5 |
+
- biology
|
| 6 |
+
- proteins
|
| 7 |
+
- protein-structure
|
| 8 |
+
- cath
|
| 9 |
+
- parquet
|
| 10 |
+
configs:
|
| 11 |
+
- config_name: default
|
| 12 |
+
data_files:
|
| 13 |
+
- split: train
|
| 14 |
+
path: data/train-*.parquet
|
| 15 |
+
- split: test
|
| 16 |
+
path: data/test-*.parquet
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# CATH Domain Classification
|
| 20 |
+
|
| 21 |
+
This repository contains CATH v4.4.0 protein domain classification data packaged for the Hugging Face Dataset Viewer and the `datasets` API.
|
| 22 |
+
|
| 23 |
+
The original raw CATH files are preserved in this dataset repository. The added Parquet files normalize the domain list, domain FASTA sequences, hierarchy names, and nonredundant subset membership into a single tabular dataset with deterministic train/test splits.
|
| 24 |
+
|
| 25 |
+
## Splits
|
| 26 |
+
|
| 27 |
+
| Split | Rows |
|
| 28 |
+
|---|---:|
|
| 29 |
+
| train | 541,123 |
|
| 30 |
+
| test | 60,205 |
|
| 31 |
+
| total | 601,328 |
|
| 32 |
+
|
| 33 |
+
The split is deterministic and S35-cluster-aware: all domains sharing the same CATH homologous superfamily and S35 cluster key are kept in the same split. This avoids putting close S35-cluster relatives in both train and test.
|
| 34 |
+
|
| 35 |
+
## Dataset Statistics
|
| 36 |
+
|
| 37 |
+
| Metric | Value |
|
| 38 |
+
|---|---:|
|
| 39 |
+
| Domains | 601,328 |
|
| 40 |
+
| Unique S35 cluster keys | 37,350 |
|
| 41 |
+
| Unique homologous superfamilies | 6,630 |
|
| 42 |
+
| Unique topologies | 1,472 |
|
| 43 |
+
| Unique architectures | 43 |
|
| 44 |
+
| Train/test S35 cluster overlap | 0 |
|
| 45 |
+
| Unknown structure-resolution sentinel rows | 9,726 |
|
| 46 |
+
|
| 47 |
+
Class distribution:
|
| 48 |
+
|
| 49 |
+
| CATH class | Rows |
|
| 50 |
+
|---|---:|
|
| 51 |
+
| Alpha Beta | 305,361 |
|
| 52 |
+
| Mainly Beta | 158,943 |
|
| 53 |
+
| Mainly Alpha | 126,178 |
|
| 54 |
+
| Few Secondary Structures | 6,034 |
|
| 55 |
+
| Special | 4,812 |
|
| 56 |
+
|
| 57 |
+
Sequence length ranges from 9 to 1,275 amino acids, with a median length of 140.
|
| 58 |
+
|
| 59 |
+
## Load With `datasets`
|
| 60 |
+
|
| 61 |
+
```python
|
| 62 |
+
from datasets import load_dataset
|
| 63 |
+
|
| 64 |
+
ds = load_dataset("LiteFold/CATH")
|
| 65 |
+
print(ds)
|
| 66 |
+
|
| 67 |
+
train = ds["train"]
|
| 68 |
+
test = ds["test"]
|
| 69 |
+
|
| 70 |
+
print(train[0])
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
Load one split directly:
|
| 74 |
+
|
| 75 |
+
```python
|
| 76 |
+
from datasets import load_dataset
|
| 77 |
+
|
| 78 |
+
train = load_dataset("LiteFold/CATH", split="train")
|
| 79 |
+
test = load_dataset("LiteFold/CATH", split="test")
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
Stream rows without downloading the full dataset first:
|
| 83 |
+
|
| 84 |
+
```python
|
| 85 |
+
from datasets import load_dataset
|
| 86 |
+
|
| 87 |
+
streamed = load_dataset("LiteFold/CATH", split="train", streaming=True)
|
| 88 |
+
first_row = next(iter(streamed))
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
Filter to one of the CATH nonredundant representative subsets:
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
from datasets import load_dataset
|
| 95 |
+
|
| 96 |
+
ds = load_dataset("LiteFold/CATH", split="train")
|
| 97 |
+
s35_train = ds.filter(lambda row: row["in_s35_nonredundant_subset"])
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
## Main Columns
|
| 101 |
+
|
| 102 |
+
| Column | Description |
|
| 103 |
+
|---|---|
|
| 104 |
+
| `domain_id` | CATH domain identifier, for example `1oaiA00`. |
|
| 105 |
+
| `pdb_id`, `chain_id`, `pdb_chain_id` | Parsed PDB and chain identifiers. |
|
| 106 |
+
| `cath_version` | CATH release version from the FASTA headers. |
|
| 107 |
+
| `cath_code` | Full CATH classification code at homologous superfamily level. |
|
| 108 |
+
| `class_*`, `architecture_*`, `topology_*`, `homologous_superfamily_*` | Numeric codes, full hierarchy codes, names, and example domains. |
|
| 109 |
+
| `s35_cluster_id`, `s60_cluster_id`, `s95_cluster_id`, `s100_cluster_id` | CATH sequence cluster identifiers from the domain list. |
|
| 110 |
+
| `s35_cluster_key` | Composite key used for leakage-aware splitting. |
|
| 111 |
+
| `domain_length` | Domain length from the CATH domain list. |
|
| 112 |
+
| `raw_structure_resolution_angstrom` | Original structure resolution value. |
|
| 113 |
+
| `structure_resolution_angstrom` | Resolution with CATH's `999.000` unknown sentinel converted to null. |
|
| 114 |
+
| `sequence` | Amino acid sequence from `cath-domain-seqs.fa`. |
|
| 115 |
+
| `sequence_length` | Length of `sequence`. |
|
| 116 |
+
| `sequence_range` | Source FASTA residue range, including discontinuous ranges such as `2-78_187-208`. |
|
| 117 |
+
| `sequence_range_start`, `sequence_range_end` | Parsed min start and max end residue positions when available. |
|
| 118 |
+
| `sequence_segment_count` | Number of underscore-separated residue segments in `sequence_range`. |
|
| 119 |
+
| `in_s35_nonredundant_subset`, `in_s60_nonredundant_subset`, `in_s95_nonredundant_subset`, `in_s100_nonredundant_subset` | Whether the domain appears in the corresponding CATH nonredundant subset list. |
|
| 120 |
+
|
| 121 |
+
## Source Files Used
|
| 122 |
+
|
| 123 |
+
- `cath-domain-list.txt`
|
| 124 |
+
- `cath-domain-list-S35.txt`
|
| 125 |
+
- `cath-domain-list-S60.txt`
|
| 126 |
+
- `cath-domain-list-S95.txt`
|
| 127 |
+
- `cath-domain-list-S100.txt`
|
| 128 |
+
- `cath-domain-seqs.fa`
|
| 129 |
+
- `cath-names.txt`
|
| 130 |
+
|
| 131 |
+
The raw PDB tarball remains in the repository, but it is not embedded in the Parquet table.
|
| 132 |
+
|
| 133 |
+
## Reproducibility
|
| 134 |
+
|
| 135 |
+
The processed files were generated from the raw files already present in this repository. The preparation script parses CATH v4.4.0 source rows, joins sequences and hierarchy names, adds subset flags, and writes `data/train-00000-of-00001.parquet` and `data/test-00000-of-00001.parquet`.
|
data/test-00000-of-00001.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fb8ba4b2893786c781942ae0b522ad297615dbef2ddf97506ed76744ee80af8e
|
| 3 |
+
size 3355072
|
data/train-00000-of-00001.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a7cbeb1520ef5f5b1c48024b4b5ac8d49c2808c459b8a83664c7247d74955eed
|
| 3 |
+
size 28221171
|
dataset_summary.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"source": "LiteFold/CATH",
|
| 3 |
+
"cath_version": "4.4.0",
|
| 4 |
+
"total_rows": 601328,
|
| 5 |
+
"splits": {
|
| 6 |
+
"train": 541123,
|
| 7 |
+
"test": 60205
|
| 8 |
+
},
|
| 9 |
+
"test_size_requested": 0.1,
|
| 10 |
+
"split_strategy": "deterministic S35-cluster-aware split using sha256(s35_cluster_key)",
|
| 11 |
+
"unique_s35_clusters": 37350,
|
| 12 |
+
"columns": [
|
| 13 |
+
"domain_id",
|
| 14 |
+
"pdb_id",
|
| 15 |
+
"chain_id",
|
| 16 |
+
"pdb_chain_id",
|
| 17 |
+
"domain_suffix",
|
| 18 |
+
"domain_index",
|
| 19 |
+
"cath_version",
|
| 20 |
+
"cath_code",
|
| 21 |
+
"class_number",
|
| 22 |
+
"class_code",
|
| 23 |
+
"class_name",
|
| 24 |
+
"class_example_domain_id",
|
| 25 |
+
"architecture_number",
|
| 26 |
+
"architecture_code",
|
| 27 |
+
"architecture_name",
|
| 28 |
+
"architecture_example_domain_id",
|
| 29 |
+
"topology_number",
|
| 30 |
+
"topology_code",
|
| 31 |
+
"topology_name",
|
| 32 |
+
"topology_example_domain_id",
|
| 33 |
+
"homologous_superfamily_number",
|
| 34 |
+
"homologous_superfamily_code",
|
| 35 |
+
"homologous_superfamily_name",
|
| 36 |
+
"homologous_superfamily_example_domain_id",
|
| 37 |
+
"s35_cluster_id",
|
| 38 |
+
"s60_cluster_id",
|
| 39 |
+
"s95_cluster_id",
|
| 40 |
+
"s100_cluster_id",
|
| 41 |
+
"s100_sequence_count",
|
| 42 |
+
"s35_cluster_key",
|
| 43 |
+
"domain_length",
|
| 44 |
+
"raw_structure_resolution_angstrom",
|
| 45 |
+
"structure_resolution_angstrom",
|
| 46 |
+
"structure_resolution_is_unknown",
|
| 47 |
+
"sequence",
|
| 48 |
+
"sequence_length",
|
| 49 |
+
"sequence_range",
|
| 50 |
+
"sequence_range_start",
|
| 51 |
+
"sequence_range_end",
|
| 52 |
+
"sequence_segment_count",
|
| 53 |
+
"in_s35_nonredundant_subset",
|
| 54 |
+
"in_s60_nonredundant_subset",
|
| 55 |
+
"in_s95_nonredundant_subset",
|
| 56 |
+
"in_s100_nonredundant_subset"
|
| 57 |
+
],
|
| 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Build viewer-friendly Parquet splits for LiteFold/CATH."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import argparse
|
| 7 |
+
import hashlib
|
| 8 |
+
import json
|
| 9 |
+
import re
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
import pandas as pd
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
DOMAIN_COLUMNS = [
|
| 16 |
+
"domain_id",
|
| 17 |
+
"class_number",
|
| 18 |
+
"architecture_number",
|
| 19 |
+
"topology_number",
|
| 20 |
+
"homologous_superfamily_number",
|
| 21 |
+
"s35_cluster_id",
|
| 22 |
+
"s60_cluster_id",
|
| 23 |
+
"s95_cluster_id",
|
| 24 |
+
"s100_cluster_id",
|
| 25 |
+
"s100_sequence_count",
|
| 26 |
+
"domain_length",
|
| 27 |
+
"raw_structure_resolution_angstrom",
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def iter_data_lines(path: Path):
|
| 32 |
+
with path.open("r", encoding="utf-8") as handle:
|
| 33 |
+
for line in handle:
|
| 34 |
+
line = line.rstrip("\n")
|
| 35 |
+
if not line or line.startswith("#"):
|
| 36 |
+
continue
|
| 37 |
+
yield line
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def parse_domain_list(path: Path) -> pd.DataFrame:
|
| 41 |
+
records = []
|
| 42 |
+
for line in iter_data_lines(path):
|
| 43 |
+
parts = line.split()
|
| 44 |
+
if len(parts) != len(DOMAIN_COLUMNS):
|
| 45 |
+
raise ValueError(f"Expected {len(DOMAIN_COLUMNS)} columns in {path}, got {len(parts)}: {line}")
|
| 46 |
+
records.append(parts)
|
| 47 |
+
|
| 48 |
+
df = pd.DataFrame(records, columns=DOMAIN_COLUMNS)
|
| 49 |
+
int_columns = [
|
| 50 |
+
"class_number",
|
| 51 |
+
"architecture_number",
|
| 52 |
+
"topology_number",
|
| 53 |
+
"homologous_superfamily_number",
|
| 54 |
+
"s35_cluster_id",
|
| 55 |
+
"s60_cluster_id",
|
| 56 |
+
"s95_cluster_id",
|
| 57 |
+
"s100_cluster_id",
|
| 58 |
+
"s100_sequence_count",
|
| 59 |
+
"domain_length",
|
| 60 |
+
]
|
| 61 |
+
for col in int_columns:
|
| 62 |
+
df[col] = df[col].astype("int64")
|
| 63 |
+
df["raw_structure_resolution_angstrom"] = df["raw_structure_resolution_angstrom"].astype("float64")
|
| 64 |
+
df["structure_resolution_is_unknown"] = df["raw_structure_resolution_angstrom"].eq(999.0)
|
| 65 |
+
df["structure_resolution_angstrom"] = df["raw_structure_resolution_angstrom"].mask(
|
| 66 |
+
df["structure_resolution_is_unknown"]
|
| 67 |
+
)
|
| 68 |
+
df["structure_resolution_angstrom"] = df["structure_resolution_angstrom"].astype("Float64")
|
| 69 |
+
|
| 70 |
+
df["pdb_id"] = df["domain_id"].str.slice(0, 4)
|
| 71 |
+
df["chain_id"] = df["domain_id"].str.slice(4, 5)
|
| 72 |
+
df["pdb_chain_id"] = df["domain_id"].str.slice(0, 5)
|
| 73 |
+
df["domain_suffix"] = df["domain_id"].str.slice(5, 7)
|
| 74 |
+
df["domain_index"] = df["domain_suffix"].astype("int64")
|
| 75 |
+
|
| 76 |
+
df["class_code"] = df["class_number"].astype(str)
|
| 77 |
+
df["architecture_code"] = df["class_code"] + "." + df["architecture_number"].astype(str)
|
| 78 |
+
df["topology_code"] = df["architecture_code"] + "." + df["topology_number"].astype(str)
|
| 79 |
+
df["homologous_superfamily_code"] = (
|
| 80 |
+
df["topology_code"] + "." + df["homologous_superfamily_number"].astype(str)
|
| 81 |
+
)
|
| 82 |
+
df["cath_code"] = df["homologous_superfamily_code"]
|
| 83 |
+
df["s35_cluster_key"] = df["homologous_superfamily_code"] + ":S35:" + df["s35_cluster_id"].astype(str)
|
| 84 |
+
return df
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def parse_names(path: Path) -> pd.DataFrame:
|
| 88 |
+
records = []
|
| 89 |
+
for line in iter_data_lines(path):
|
| 90 |
+
if ":" not in line:
|
| 91 |
+
continue
|
| 92 |
+
left, name = line.split(":", 1)
|
| 93 |
+
parts = left.split()
|
| 94 |
+
if len(parts) < 2:
|
| 95 |
+
continue
|
| 96 |
+
records.append(
|
| 97 |
+
{
|
| 98 |
+
"cath_node_code": parts[0],
|
| 99 |
+
"example_domain_id": parts[1],
|
| 100 |
+
"cath_node_name": name.strip(),
|
| 101 |
+
}
|
| 102 |
+
)
|
| 103 |
+
return pd.DataFrame.from_records(records)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
_RANGE_SEGMENT_RE = re.compile(r"^\s*(-?\d+)(?:\([A-Za-z0-9]+\))?-(-?\d+)(?:\([A-Za-z0-9]+\))?\s*$")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def parse_range_summary(sequence_range: str) -> tuple[int | None, int | None, int]:
|
| 110 |
+
starts: list[int] = []
|
| 111 |
+
ends: list[int] = []
|
| 112 |
+
segments = [segment for segment in sequence_range.split("_") if segment]
|
| 113 |
+
for segment in segments:
|
| 114 |
+
match = _RANGE_SEGMENT_RE.match(segment)
|
| 115 |
+
if not match:
|
| 116 |
+
continue
|
| 117 |
+
starts.append(int(match.group(1)))
|
| 118 |
+
ends.append(int(match.group(2)))
|
| 119 |
+
if not starts:
|
| 120 |
+
return None, None, len(segments)
|
| 121 |
+
return min(starts), max(ends), len(segments)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def parse_fasta(path: Path) -> pd.DataFrame:
|
| 125 |
+
records = []
|
| 126 |
+
header: str | None = None
|
| 127 |
+
sequence_chunks: list[str] = []
|
| 128 |
+
|
| 129 |
+
def flush() -> None:
|
| 130 |
+
if header is None:
|
| 131 |
+
return
|
| 132 |
+
try:
|
| 133 |
+
_, version, payload = header.split("|", 2)
|
| 134 |
+
domain_id, sequence_range = payload.split("/", 1)
|
| 135 |
+
except ValueError as exc:
|
| 136 |
+
raise ValueError(f"Unexpected FASTA header in {path}: {header}") from exc
|
| 137 |
+
sequence = "".join(sequence_chunks)
|
| 138 |
+
start, end, segment_count = parse_range_summary(sequence_range)
|
| 139 |
+
records.append(
|
| 140 |
+
{
|
| 141 |
+
"domain_id": domain_id,
|
| 142 |
+
"cath_version": version.replace("_", "."),
|
| 143 |
+
"sequence": sequence,
|
| 144 |
+
"sequence_length": len(sequence),
|
| 145 |
+
"sequence_range": sequence_range,
|
| 146 |
+
"sequence_range_start": start,
|
| 147 |
+
"sequence_range_end": end,
|
| 148 |
+
"sequence_segment_count": segment_count,
|
| 149 |
+
}
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
with path.open("r", encoding="utf-8") as handle:
|
| 153 |
+
for line in handle:
|
| 154 |
+
line = line.strip()
|
| 155 |
+
if not line:
|
| 156 |
+
continue
|
| 157 |
+
if line.startswith(">"):
|
| 158 |
+
flush()
|
| 159 |
+
header = line[1:]
|
| 160 |
+
sequence_chunks = []
|
| 161 |
+
else:
|
| 162 |
+
sequence_chunks.append(line)
|
| 163 |
+
flush()
|
| 164 |
+
return pd.DataFrame.from_records(records)
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def subset_domain_ids(path: Path) -> set[str]:
|
| 168 |
+
return {line.split()[0] for line in iter_data_lines(path)}
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def stable_hash_int(value: str) -> int:
|
| 172 |
+
return int(hashlib.sha256(value.encode("utf-8")).hexdigest()[:16], 16)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def add_cluster_aware_split(df: pd.DataFrame, test_size: float) -> pd.DataFrame:
|
| 176 |
+
cluster_counts = (
|
| 177 |
+
df.groupby("s35_cluster_key", sort=False)
|
| 178 |
+
.size()
|
| 179 |
+
.rename("row_count")
|
| 180 |
+
.reset_index()
|
| 181 |
+
)
|
| 182 |
+
cluster_counts["hash"] = cluster_counts["s35_cluster_key"].map(stable_hash_int)
|
| 183 |
+
cluster_counts = cluster_counts.sort_values(["hash", "s35_cluster_key"], kind="mergesort")
|
| 184 |
+
|
| 185 |
+
target_rows = round(len(df) * test_size)
|
| 186 |
+
test_keys: set[str] = set()
|
| 187 |
+
test_rows = 0
|
| 188 |
+
for row in cluster_counts.itertuples(index=False):
|
| 189 |
+
if test_rows >= target_rows:
|
| 190 |
+
break
|
| 191 |
+
test_keys.add(row.s35_cluster_key)
|
| 192 |
+
test_rows += int(row.row_count)
|
| 193 |
+
|
| 194 |
+
df = df.copy()
|
| 195 |
+
df["split"] = df["s35_cluster_key"].map(lambda key: "test" if key in test_keys else "train")
|
| 196 |
+
return df
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def build_dataset(raw_dir: Path, out_dir: Path, test_size: float) -> dict:
|
| 200 |
+
domains = parse_domain_list(raw_dir / "cath-domain-list.txt")
|
| 201 |
+
names = parse_names(raw_dir / "cath-names.txt")
|
| 202 |
+
names_by_code = names.set_index("cath_node_code")
|
| 203 |
+
|
| 204 |
+
for level, code_col in [
|
| 205 |
+
("class", "class_code"),
|
| 206 |
+
("architecture", "architecture_code"),
|
| 207 |
+
("topology", "topology_code"),
|
| 208 |
+
("homologous_superfamily", "homologous_superfamily_code"),
|
| 209 |
+
]:
|
| 210 |
+
domains[f"{level}_name"] = domains[code_col].map(names_by_code["cath_node_name"])
|
| 211 |
+
domains[f"{level}_example_domain_id"] = domains[code_col].map(names_by_code["example_domain_id"])
|
| 212 |
+
|
| 213 |
+
sequences = parse_fasta(raw_dir / "cath-domain-seqs.fa")
|
| 214 |
+
df = domains.merge(sequences, on="domain_id", how="left", validate="one_to_one")
|
| 215 |
+
|
| 216 |
+
missing_sequences = int(df["sequence"].isna().sum())
|
| 217 |
+
if missing_sequences:
|
| 218 |
+
raise ValueError(f"{missing_sequences} domain-list rows did not have FASTA sequences")
|
| 219 |
+
|
| 220 |
+
for subset in ["S35", "S60", "S95", "S100"]:
|
| 221 |
+
ids = subset_domain_ids(raw_dir / f"cath-domain-list-{subset}.txt")
|
| 222 |
+
df[f"in_{subset.lower()}_nonredundant_subset"] = df["domain_id"].isin(ids)
|
| 223 |
+
|
| 224 |
+
df = add_cluster_aware_split(df, test_size)
|
| 225 |
+
|
| 226 |
+
ordered_columns = [
|
| 227 |
+
"domain_id",
|
| 228 |
+
"pdb_id",
|
| 229 |
+
"chain_id",
|
| 230 |
+
"pdb_chain_id",
|
| 231 |
+
"domain_suffix",
|
| 232 |
+
"domain_index",
|
| 233 |
+
"cath_version",
|
| 234 |
+
"cath_code",
|
| 235 |
+
"class_number",
|
| 236 |
+
"class_code",
|
| 237 |
+
"class_name",
|
| 238 |
+
"class_example_domain_id",
|
| 239 |
+
"architecture_number",
|
| 240 |
+
"architecture_code",
|
| 241 |
+
"architecture_name",
|
| 242 |
+
"architecture_example_domain_id",
|
| 243 |
+
"topology_number",
|
| 244 |
+
"topology_code",
|
| 245 |
+
"topology_name",
|
| 246 |
+
"topology_example_domain_id",
|
| 247 |
+
"homologous_superfamily_number",
|
| 248 |
+
"homologous_superfamily_code",
|
| 249 |
+
"homologous_superfamily_name",
|
| 250 |
+
"homologous_superfamily_example_domain_id",
|
| 251 |
+
"s35_cluster_id",
|
| 252 |
+
"s60_cluster_id",
|
| 253 |
+
"s95_cluster_id",
|
| 254 |
+
"s100_cluster_id",
|
| 255 |
+
"s100_sequence_count",
|
| 256 |
+
"s35_cluster_key",
|
| 257 |
+
"domain_length",
|
| 258 |
+
"raw_structure_resolution_angstrom",
|
| 259 |
+
"structure_resolution_angstrom",
|
| 260 |
+
"structure_resolution_is_unknown",
|
| 261 |
+
"sequence",
|
| 262 |
+
"sequence_length",
|
| 263 |
+
"sequence_range",
|
| 264 |
+
"sequence_range_start",
|
| 265 |
+
"sequence_range_end",
|
| 266 |
+
"sequence_segment_count",
|
| 267 |
+
"in_s35_nonredundant_subset",
|
| 268 |
+
"in_s60_nonredundant_subset",
|
| 269 |
+
"in_s95_nonredundant_subset",
|
| 270 |
+
"in_s100_nonredundant_subset",
|
| 271 |
+
"split",
|
| 272 |
+
]
|
| 273 |
+
df = df[ordered_columns].sort_values(["split", "domain_id"], kind="mergesort")
|
| 274 |
+
|
| 275 |
+
data_dir = out_dir / "data"
|
| 276 |
+
data_dir.mkdir(parents=True, exist_ok=True)
|
| 277 |
+
split_counts = {}
|
| 278 |
+
for split in ["train", "test"]:
|
| 279 |
+
split_df = df[df["split"].eq(split)].drop(columns=["split"])
|
| 280 |
+
split_counts[split] = len(split_df)
|
| 281 |
+
split_df.to_parquet(
|
| 282 |
+
data_dir / f"{split}-00000-of-00001.parquet",
|
| 283 |
+
index=False,
|
| 284 |
+
compression="zstd",
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
summary = {
|
| 288 |
+
"source": "LiteFold/CATH",
|
| 289 |
+
"cath_version": str(df["cath_version"].iloc[0]),
|
| 290 |
+
"total_rows": len(df),
|
| 291 |
+
"splits": split_counts,
|
| 292 |
+
"test_size_requested": test_size,
|
| 293 |
+
"split_strategy": "deterministic S35-cluster-aware split using sha256(s35_cluster_key)",
|
| 294 |
+
"unique_s35_clusters": int(df["s35_cluster_key"].nunique()),
|
| 295 |
+
"columns": [column for column in ordered_columns if column != "split"],
|
| 296 |
+
"subset_rows": {
|
| 297 |
+
"s35": int(df["in_s35_nonredundant_subset"].sum()),
|
| 298 |
+
"s60": int(df["in_s60_nonredundant_subset"].sum()),
|
| 299 |
+
"s95": int(df["in_s95_nonredundant_subset"].sum()),
|
| 300 |
+
"s100": int(df["in_s100_nonredundant_subset"].sum()),
|
| 301 |
+
},
|
| 302 |
+
}
|
| 303 |
+
(out_dir / "dataset_summary.json").write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
|
| 304 |
+
return summary
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
def main() -> None:
|
| 308 |
+
parser = argparse.ArgumentParser()
|
| 309 |
+
parser.add_argument("--raw-dir", type=Path, default=Path("LiteFold_CATH_raw"))
|
| 310 |
+
parser.add_argument("--out-dir", type=Path, default=Path("LiteFold_CATH_processed"))
|
| 311 |
+
parser.add_argument("--test-size", type=float, default=0.10)
|
| 312 |
+
args = parser.parse_args()
|
| 313 |
+
|
| 314 |
+
summary = build_dataset(args.raw_dir, args.out_dir, args.test_size)
|
| 315 |
+
print(json.dumps(summary, indent=2))
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
if __name__ == "__main__":
|
| 319 |
+
main()
|