Add normalized Parquet train/test accession index
Browse files- README.md +127 -0
- data/test/test-00000-of-00002.parquet +3 -0
- data/test/test-00001-of-00002.parquet +3 -0
- data/train/train-00000-of-00012.parquet +3 -0
- data/train/train-00001-of-00012.parquet +3 -0
- data/train/train-00002-of-00012.parquet +3 -0
- data/train/train-00003-of-00012.parquet +3 -0
- data/train/train-00004-of-00012.parquet +3 -0
- data/train/train-00005-of-00012.parquet +3 -0
- data/train/train-00006-of-00012.parquet +3 -0
- data/train/train-00007-of-00012.parquet +3 -0
- data/train/train-00008-of-00012.parquet +3 -0
- data/train/train-00009-of-00012.parquet +3 -0
- data/train/train-00010-of-00012.parquet +3 -0
- data/train/train-00011-of-00012.parquet +3 -0
- dataset_summary.json +52 -0
- metadata/archive_metadata.parquet +3 -0
- scripts/prepare_alphafolddb_dataset.py +281 -0
README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
pretty_name: AlphaFoldDB Prediction Index
|
| 3 |
+
license: cc-by-4.0
|
| 4 |
+
tags:
|
| 5 |
+
- biology
|
| 6 |
+
- proteins
|
| 7 |
+
- protein-structure
|
| 8 |
+
- alphafold
|
| 9 |
+
- alphafolddb
|
| 10 |
+
- parquet
|
| 11 |
+
configs:
|
| 12 |
+
- config_name: default
|
| 13 |
+
data_files:
|
| 14 |
+
- split: train
|
| 15 |
+
path: data/train/*.parquet
|
| 16 |
+
- split: test
|
| 17 |
+
path: data/test/*.parquet
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# AlphaFoldDB Prediction Index
|
| 21 |
+
|
| 22 |
+
This repository contains the AlphaFold Protein Structure Database bulk-download files mirrored under `LiteFold/AlphaFoldDB`. The added Parquet files make the accession index loadable in the Hugging Face Dataset Viewer and the `datasets` API.
|
| 23 |
+
|
| 24 |
+
The default dataset table is built from `accession_ids.csv`. Each row represents one AlphaFold DB prediction entry and includes its UniProt accession, AlphaFold DB identifier, residue range, latest model version, derived sequence length, parsed fragment number, and deterministic split bucket.
|
| 25 |
+
|
| 26 |
+
The raw FASTA file and structure tar archives are preserved in the repository but are not embedded in the default Parquet table.
|
| 27 |
+
|
| 28 |
+
## Splits
|
| 29 |
+
|
| 30 |
+
| Split | Rows | Parquet files |
|
| 31 |
+
|---|---:|---:|
|
| 32 |
+
| train | 222,017,452 | 12 |
|
| 33 |
+
| test | 24,672,064 | 2 |
|
| 34 |
+
| total | 246,689,516 | 14 |
|
| 35 |
+
|
| 36 |
+
The split is deterministic: `hash(uniprot_accession) % 10 == 0` goes to `test`; buckets `1` through `9` go to `train`.
|
| 37 |
+
|
| 38 |
+
## Dataset Statistics
|
| 39 |
+
|
| 40 |
+
| Metric | Value |
|
| 41 |
+
|---|---:|
|
| 42 |
+
| Rows | 246,689,516 |
|
| 43 |
+
| Minimum sequence length | 5 |
|
| 44 |
+
| Approximate median sequence length | 278 |
|
| 45 |
+
| Mean sequence length | 328.55 |
|
| 46 |
+
| Maximum sequence length | 4,186 |
|
| 47 |
+
| Rows without parsed fragment number | 5,619,027 |
|
| 48 |
+
|
| 49 |
+
Latest-version distribution:
|
| 50 |
+
|
| 51 |
+
| Latest version | Rows |
|
| 52 |
+
|---|---:|
|
| 53 |
+
| 1 | 5,271,725 |
|
| 54 |
+
| 2 | 347,302 |
|
| 55 |
+
| 6 | 241,070,489 |
|
| 56 |
+
|
| 57 |
+
The mirrored `download_metadata.json` describes 48 bulk archive files: 16 proteome archives, 30 global-health archives, and 2 Swiss-Prot archives.
|
| 58 |
+
|
| 59 |
+
## Load With `datasets`
|
| 60 |
+
|
| 61 |
+
```python
|
| 62 |
+
from datasets import load_dataset
|
| 63 |
+
|
| 64 |
+
ds = load_dataset("LiteFold/AlphaFoldDB")
|
| 65 |
+
print(ds)
|
| 66 |
+
|
| 67 |
+
row = ds["train"][0]
|
| 68 |
+
print(row)
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
Load one split directly:
|
| 72 |
+
|
| 73 |
+
```python
|
| 74 |
+
from datasets import load_dataset
|
| 75 |
+
|
| 76 |
+
train = load_dataset("LiteFold/AlphaFoldDB", split="train")
|
| 77 |
+
test = load_dataset("LiteFold/AlphaFoldDB", split="test")
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
Stream rows without materializing the full table locally:
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
from datasets import load_dataset
|
| 84 |
+
|
| 85 |
+
streamed = load_dataset("LiteFold/AlphaFoldDB", split="train", streaming=True)
|
| 86 |
+
first_row = next(iter(streamed))
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
Construct an AlphaFold DB entry URL from a row:
|
| 90 |
+
|
| 91 |
+
```python
|
| 92 |
+
entry_url = f"https://alphafold.ebi.ac.uk/entry/{row['alphafold_id']}"
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
Filter to current v6 entries:
|
| 96 |
+
|
| 97 |
+
```python
|
| 98 |
+
from datasets import load_dataset
|
| 99 |
+
|
| 100 |
+
train = load_dataset("LiteFold/AlphaFoldDB", split="train")
|
| 101 |
+
v6_train = train.filter(lambda row: row["latest_version"] == 6)
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
For large jobs, prefer streaming or process the Parquet files with a columnar engine such as DuckDB, PyArrow, Polars, or Spark.
|
| 105 |
+
|
| 106 |
+
## Columns
|
| 107 |
+
|
| 108 |
+
| Column | Description |
|
| 109 |
+
|---|---|
|
| 110 |
+
| `uniprot_accession` | UniProt accession from `accession_ids.csv`. |
|
| 111 |
+
| `alphafold_id` | AlphaFold DB identifier, for example `AF-Q5VSL9-F1`. |
|
| 112 |
+
| `latest_version` | Latest available AlphaFold DB model version for the entry. |
|
| 113 |
+
| `first_residue_index` | First residue index in UniProt numbering. |
|
| 114 |
+
| `last_residue_index` | Last residue index in UniProt numbering. |
|
| 115 |
+
| `sequence_length` | Derived as `last_residue_index - first_residue_index + 1`. |
|
| 116 |
+
| `fragment_number` | Parsed `F<number>` suffix from `alphafold_id`, nullable when the suffix is absent or nonstandard. |
|
| 117 |
+
| `is_fragmented_prediction` | Whether `fragment_number` is greater than 1. |
|
| 118 |
+
| `split_bucket` | Deterministic bucket from `hash(uniprot_accession) % 10`; bucket 0 is test. |
|
| 119 |
+
|
| 120 |
+
## Source Files Used
|
| 121 |
+
|
| 122 |
+
- `accession_ids.csv`
|
| 123 |
+
- `download_metadata.json`
|
| 124 |
+
- `README.txt`
|
| 125 |
+
- `CHANGELOG.txt`
|
| 126 |
+
|
| 127 |
+
The processed files were generated from the raw files already present in this repository. The preparation script is included at `scripts/prepare_alphafolddb_dataset.py`.
|
data/test/test-00000-of-00002.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:56076b3a34849a8c058e57ee1a00a9ad5a857a1dff22be2208c53d0a0cf5240c
|
| 3 |
+
size 267557174
|
data/test/test-00001-of-00002.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:686fe293d4105b230b7493e10a04735686a06b2d1eaffdc53752b1ae3dd22544
|
| 3 |
+
size 59583977
|
data/train/train-00000-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f9a2215bc4ec481e685cbc70e27415b86484133fbcee7592006101d996d13be2
|
| 3 |
+
size 262057530
|
data/train/train-00001-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:60510115338ae3fc65553183b85426f8dff3b83d8a04011a51f85034dca2dca6
|
| 3 |
+
size 262013686
|
data/train/train-00002-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:53f2eeb4257f550f5513fd1b8ca5b4e3919a8b728b9bca5e797973a9e1533c5d
|
| 3 |
+
size 262074376
|
data/train/train-00003-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6d30239c9bc7b862dfe62826276ae768786a86b2de35e67d14a6c644af50c278
|
| 3 |
+
size 262022848
|
data/train/train-00004-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:157784c8c22e1bffac41fae6637ed9a7eb013c4f591a1d72669008af7b4986b2
|
| 3 |
+
size 262091863
|
data/train/train-00005-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b49c238e679cf8f380106d53d786af834909261cc7dbddd84d632e2d9e701d1c
|
| 3 |
+
size 262062355
|
data/train/train-00006-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:196d7be70d12857e7e888227f645011e96a15a8bbeb4e83527a0736aaa7f27b3
|
| 3 |
+
size 262033933
|
data/train/train-00007-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a6a34ad2b61ce52d2125c814d34c4359d52ac366e1cec6f4bb000b0180f87467
|
| 3 |
+
size 262056787
|
data/train/train-00008-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4dcbfcc9b71397eb384253bd2d13322031f4b4777d239a6f62e2f1074d53faf7
|
| 3 |
+
size 261982412
|
data/train/train-00009-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e4bbc53516bad9e32b20a081b446353c5226cfe384e39d3b0708da003de4d0a8
|
| 3 |
+
size 262061205
|
data/train/train-00010-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:14f8ee760e9d630e8df6a6d395dc66e26323ec749ee064d21814a4e062d4915f
|
| 3 |
+
size 262017198
|
data/train/train-00011-of-00012.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:239059c7efb0d35975c255a9b0b3526d94e75af947bf8cff1bf54d848ac2e557
|
| 3 |
+
size 144308419
|
dataset_summary.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"source": "LiteFold/AlphaFoldDB",
|
| 3 |
+
"table_source_file": "accession_ids.csv",
|
| 4 |
+
"split_strategy": "deterministic hash(uniprot_accession) % 10; bucket 0 is test, buckets 1-9 are train",
|
| 5 |
+
"splits": {
|
| 6 |
+
"train": 222017452,
|
| 7 |
+
"test": 24672064
|
| 8 |
+
},
|
| 9 |
+
"total_rows": 246689516,
|
| 10 |
+
"sequence_length": {
|
| 11 |
+
"min": 5,
|
| 12 |
+
"median_approx": 278,
|
| 13 |
+
"mean": 328.55367611974236,
|
| 14 |
+
"max": 4186
|
| 15 |
+
},
|
| 16 |
+
"fragmented_predictions": 0,
|
| 17 |
+
"rows_without_fragment_number": 5619027,
|
| 18 |
+
"latest_version": {
|
| 19 |
+
"min": 1,
|
| 20 |
+
"max": 6,
|
| 21 |
+
"counts": {
|
| 22 |
+
"1": 5271725,
|
| 23 |
+
"2": 347302,
|
| 24 |
+
"6": 241070489
|
| 25 |
+
}
|
| 26 |
+
},
|
| 27 |
+
"parquet_files": {
|
| 28 |
+
"train": 12,
|
| 29 |
+
"test": 2
|
| 30 |
+
},
|
| 31 |
+
"columns": [
|
| 32 |
+
"uniprot_accession",
|
| 33 |
+
"alphafold_id",
|
| 34 |
+
"latest_version",
|
| 35 |
+
"first_residue_index",
|
| 36 |
+
"last_residue_index",
|
| 37 |
+
"sequence_length",
|
| 38 |
+
"fragment_number",
|
| 39 |
+
"is_fragmented_prediction",
|
| 40 |
+
"split_bucket"
|
| 41 |
+
],
|
| 42 |
+
"archive_metadata": {
|
| 43 |
+
"archive_rows": 48,
|
| 44 |
+
"archive_types": {
|
| 45 |
+
"global_health": 30,
|
| 46 |
+
"proteome": 16,
|
| 47 |
+
"swissprot": 2
|
| 48 |
+
},
|
| 49 |
+
"archive_predicted_structures": 1656090,
|
| 50 |
+
"archive_size_bytes": 153231764480
|
| 51 |
+
}
|
| 52 |
+
}
|
metadata/archive_metadata.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d7b5f0a8896848c10772ed41dbe7a44fab4afd74b59c6cbbfde4dd1fbd557dc2
|
| 3 |
+
size 11119
|
scripts/prepare_alphafolddb_dataset.py
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Build viewer-friendly Parquet splits for LiteFold/AlphaFoldDB."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import argparse
|
| 7 |
+
import json
|
| 8 |
+
import re
|
| 9 |
+
import shutil
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
import duckdb
|
| 13 |
+
import pandas as pd
|
| 14 |
+
import pyarrow.parquet as pq
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
CSV_COLUMNS_SQL = (
|
| 18 |
+
"{"
|
| 19 |
+
"'uniprot_accession':'VARCHAR',"
|
| 20 |
+
"'first_residue_index':'BIGINT',"
|
| 21 |
+
"'last_residue_index':'BIGINT',"
|
| 22 |
+
"'alphafold_id':'VARCHAR',"
|
| 23 |
+
"'latest_version':'BIGINT'"
|
| 24 |
+
"}"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def sql_string(path: Path) -> str:
|
| 29 |
+
return "'" + path.as_posix().replace("'", "''") + "'"
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def entry_source_sql(accession_csv: Path) -> str:
|
| 33 |
+
source = (
|
| 34 |
+
f"read_csv({sql_string(accession_csv)}, "
|
| 35 |
+
f"header=false, columns={CSV_COLUMNS_SQL}, sample_size=100000, "
|
| 36 |
+
f"strict_mode=false, parallel=false)"
|
| 37 |
+
)
|
| 38 |
+
return f"""
|
| 39 |
+
SELECT
|
| 40 |
+
uniprot_accession,
|
| 41 |
+
alphafold_id,
|
| 42 |
+
latest_version,
|
| 43 |
+
first_residue_index,
|
| 44 |
+
last_residue_index,
|
| 45 |
+
sequence_length,
|
| 46 |
+
fragment_number,
|
| 47 |
+
coalesce(fragment_number > 1, false) AS is_fragmented_prediction,
|
| 48 |
+
split_bucket
|
| 49 |
+
FROM (
|
| 50 |
+
SELECT
|
| 51 |
+
uniprot_accession,
|
| 52 |
+
alphafold_id,
|
| 53 |
+
latest_version::UTINYINT AS latest_version,
|
| 54 |
+
first_residue_index::INTEGER AS first_residue_index,
|
| 55 |
+
last_residue_index::INTEGER AS last_residue_index,
|
| 56 |
+
(last_residue_index - first_residue_index + 1)::INTEGER AS sequence_length,
|
| 57 |
+
try_cast(nullif(regexp_extract(alphafold_id, '-F([0-9]+)$', 1), '') AS INTEGER) AS fragment_number,
|
| 58 |
+
(hash(uniprot_accession) % 10)::UTINYINT AS split_bucket
|
| 59 |
+
FROM {source}
|
| 60 |
+
)
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def copy_split(
|
| 65 |
+
con: duckdb.DuckDBPyConnection,
|
| 66 |
+
base_sql: str,
|
| 67 |
+
split: str,
|
| 68 |
+
condition: str,
|
| 69 |
+
split_dir: Path,
|
| 70 |
+
target_file_size: str,
|
| 71 |
+
row_group_size: int,
|
| 72 |
+
) -> None:
|
| 73 |
+
split_dir.mkdir(parents=True, exist_ok=True)
|
| 74 |
+
copy_sql = f"""
|
| 75 |
+
COPY (
|
| 76 |
+
SELECT *
|
| 77 |
+
FROM ({base_sql})
|
| 78 |
+
WHERE {condition}
|
| 79 |
+
)
|
| 80 |
+
TO {sql_string(split_dir)}
|
| 81 |
+
(
|
| 82 |
+
FORMAT PARQUET,
|
| 83 |
+
COMPRESSION ZSTD,
|
| 84 |
+
ROW_GROUP_SIZE {row_group_size},
|
| 85 |
+
FILE_SIZE_BYTES {sql_string(Path(target_file_size))}
|
| 86 |
+
)
|
| 87 |
+
"""
|
| 88 |
+
con.execute(copy_sql)
|
| 89 |
+
|
| 90 |
+
files = sorted(
|
| 91 |
+
split_dir.glob("data_*.parquet"),
|
| 92 |
+
key=lambda path: int(re.search(r"data_(\d+)\.parquet$", path.name).group(1)),
|
| 93 |
+
)
|
| 94 |
+
total = len(files)
|
| 95 |
+
for index, path in enumerate(files):
|
| 96 |
+
path.rename(split_dir / f"{split}-{index:05d}-of-{total:05d}.parquet")
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def write_archive_metadata(raw_dir: Path, out_dir: Path) -> dict:
|
| 100 |
+
metadata_path = raw_dir / "download_metadata.json"
|
| 101 |
+
if not metadata_path.exists():
|
| 102 |
+
return {}
|
| 103 |
+
|
| 104 |
+
records = json.loads(metadata_path.read_text(encoding="utf-8"))
|
| 105 |
+
df = pd.DataFrame.from_records(records)
|
| 106 |
+
if df.empty:
|
| 107 |
+
return {}
|
| 108 |
+
|
| 109 |
+
for column in [
|
| 110 |
+
"archive_name",
|
| 111 |
+
"species",
|
| 112 |
+
"common_name",
|
| 113 |
+
"reference_proteome",
|
| 114 |
+
"label",
|
| 115 |
+
"type",
|
| 116 |
+
]:
|
| 117 |
+
if column not in df.columns:
|
| 118 |
+
df[column] = None
|
| 119 |
+
if "latin_common_name" not in df.columns:
|
| 120 |
+
df["latin_common_name"] = None
|
| 121 |
+
|
| 122 |
+
df["archive_path"] = "latest/" + df["archive_name"]
|
| 123 |
+
df["size_gb"] = df["size_bytes"].astype("float64") / 1_000_000_000
|
| 124 |
+
ordered_columns = [
|
| 125 |
+
"archive_name",
|
| 126 |
+
"archive_path",
|
| 127 |
+
"type",
|
| 128 |
+
"species",
|
| 129 |
+
"common_name",
|
| 130 |
+
"latin_common_name",
|
| 131 |
+
"reference_proteome",
|
| 132 |
+
"label",
|
| 133 |
+
"num_predicted_structures",
|
| 134 |
+
"size_bytes",
|
| 135 |
+
"size_gb",
|
| 136 |
+
]
|
| 137 |
+
df = df[ordered_columns]
|
| 138 |
+
|
| 139 |
+
metadata_dir = out_dir / "metadata"
|
| 140 |
+
metadata_dir.mkdir(parents=True, exist_ok=True)
|
| 141 |
+
df.to_parquet(metadata_dir / "archive_metadata.parquet", index=False, compression="zstd")
|
| 142 |
+
|
| 143 |
+
return {
|
| 144 |
+
"archive_rows": int(len(df)),
|
| 145 |
+
"archive_types": df["type"].value_counts(dropna=False).to_dict(),
|
| 146 |
+
"archive_predicted_structures": int(df["num_predicted_structures"].sum()),
|
| 147 |
+
"archive_size_bytes": int(df["size_bytes"].sum()),
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def parquet_row_count(paths: list[Path]) -> int:
|
| 152 |
+
return sum(pq.ParquetFile(path).metadata.num_rows for path in paths)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def build_dataset(raw_dir: Path, out_dir: Path, target_file_size: str, row_group_size: int) -> dict:
|
| 156 |
+
accession_csv = raw_dir / "accession_ids.csv"
|
| 157 |
+
if not accession_csv.exists():
|
| 158 |
+
raise FileNotFoundError(f"Missing {accession_csv}")
|
| 159 |
+
|
| 160 |
+
data_dir = out_dir / "data"
|
| 161 |
+
if data_dir.exists():
|
| 162 |
+
shutil.rmtree(data_dir)
|
| 163 |
+
data_dir.mkdir(parents=True, exist_ok=True)
|
| 164 |
+
|
| 165 |
+
con = duckdb.connect()
|
| 166 |
+
con.execute("SET preserve_insertion_order=false")
|
| 167 |
+
con.execute("PRAGMA disable_progress_bar")
|
| 168 |
+
base_sql = entry_source_sql(accession_csv)
|
| 169 |
+
|
| 170 |
+
copy_split(
|
| 171 |
+
con,
|
| 172 |
+
base_sql,
|
| 173 |
+
"train",
|
| 174 |
+
"split_bucket <> 0",
|
| 175 |
+
data_dir / "train",
|
| 176 |
+
target_file_size,
|
| 177 |
+
row_group_size,
|
| 178 |
+
)
|
| 179 |
+
copy_split(
|
| 180 |
+
con,
|
| 181 |
+
base_sql,
|
| 182 |
+
"test",
|
| 183 |
+
"split_bucket = 0",
|
| 184 |
+
data_dir / "test",
|
| 185 |
+
target_file_size,
|
| 186 |
+
row_group_size,
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
archive_summary = write_archive_metadata(raw_dir, out_dir)
|
| 190 |
+
train_files = sorted((data_dir / "train").glob("*.parquet"))
|
| 191 |
+
test_files = sorted((data_dir / "test").glob("*.parquet"))
|
| 192 |
+
split_counts = {
|
| 193 |
+
"train": parquet_row_count(train_files),
|
| 194 |
+
"test": parquet_row_count(test_files),
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
parquet_glob = sql_string(data_dir / "*" / "*.parquet")
|
| 198 |
+
stats_row = con.execute(
|
| 199 |
+
f"""
|
| 200 |
+
SELECT
|
| 201 |
+
count(*)::UBIGINT AS total_rows,
|
| 202 |
+
min(sequence_length)::INTEGER AS min_sequence_length,
|
| 203 |
+
approx_quantile(sequence_length, 0.5)::INTEGER AS median_sequence_length,
|
| 204 |
+
avg(sequence_length)::DOUBLE AS mean_sequence_length,
|
| 205 |
+
max(sequence_length)::INTEGER AS max_sequence_length,
|
| 206 |
+
sum(CASE WHEN is_fragmented_prediction THEN 1 ELSE 0 END)::UBIGINT AS fragmented_predictions,
|
| 207 |
+
sum(CASE WHEN fragment_number IS NULL THEN 1 ELSE 0 END)::UBIGINT AS rows_without_fragment_number,
|
| 208 |
+
min(latest_version)::UTINYINT AS min_latest_version,
|
| 209 |
+
max(latest_version)::UTINYINT AS max_latest_version
|
| 210 |
+
FROM read_parquet({parquet_glob})
|
| 211 |
+
"""
|
| 212 |
+
).fetchone()
|
| 213 |
+
version_counts = {
|
| 214 |
+
str(version): int(count)
|
| 215 |
+
for version, count in con.execute(
|
| 216 |
+
f"""
|
| 217 |
+
SELECT latest_version, count(*) AS row_count
|
| 218 |
+
FROM read_parquet({parquet_glob})
|
| 219 |
+
GROUP BY 1
|
| 220 |
+
ORDER BY 1
|
| 221 |
+
"""
|
| 222 |
+
).fetchall()
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
summary = {
|
| 226 |
+
"source": "LiteFold/AlphaFoldDB",
|
| 227 |
+
"table_source_file": "accession_ids.csv",
|
| 228 |
+
"split_strategy": "deterministic hash(uniprot_accession) % 10; bucket 0 is test, buckets 1-9 are train",
|
| 229 |
+
"splits": {
|
| 230 |
+
"train": split_counts.get("train", 0),
|
| 231 |
+
"test": split_counts.get("test", 0),
|
| 232 |
+
},
|
| 233 |
+
"total_rows": int(stats_row[0]),
|
| 234 |
+
"sequence_length": {
|
| 235 |
+
"min": int(stats_row[1]),
|
| 236 |
+
"median_approx": int(stats_row[2]),
|
| 237 |
+
"mean": float(stats_row[3]),
|
| 238 |
+
"max": int(stats_row[4]),
|
| 239 |
+
},
|
| 240 |
+
"fragmented_predictions": int(stats_row[5]),
|
| 241 |
+
"rows_without_fragment_number": int(stats_row[6]),
|
| 242 |
+
"latest_version": {
|
| 243 |
+
"min": int(stats_row[7]),
|
| 244 |
+
"max": int(stats_row[8]),
|
| 245 |
+
"counts": version_counts,
|
| 246 |
+
},
|
| 247 |
+
"parquet_files": {
|
| 248 |
+
"train": len(train_files),
|
| 249 |
+
"test": len(test_files),
|
| 250 |
+
},
|
| 251 |
+
"columns": [
|
| 252 |
+
"uniprot_accession",
|
| 253 |
+
"alphafold_id",
|
| 254 |
+
"latest_version",
|
| 255 |
+
"first_residue_index",
|
| 256 |
+
"last_residue_index",
|
| 257 |
+
"sequence_length",
|
| 258 |
+
"fragment_number",
|
| 259 |
+
"is_fragmented_prediction",
|
| 260 |
+
"split_bucket",
|
| 261 |
+
],
|
| 262 |
+
"archive_metadata": archive_summary,
|
| 263 |
+
}
|
| 264 |
+
(out_dir / "dataset_summary.json").write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
|
| 265 |
+
return summary
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def main() -> None:
|
| 269 |
+
parser = argparse.ArgumentParser()
|
| 270 |
+
parser.add_argument("--raw-dir", type=Path, default=Path("LiteFold_AlphaFoldDB_raw"))
|
| 271 |
+
parser.add_argument("--out-dir", type=Path, default=Path("LiteFold_AlphaFoldDB_processed"))
|
| 272 |
+
parser.add_argument("--target-file-size", default="256MB")
|
| 273 |
+
parser.add_argument("--row-group-size", type=int, default=1_000_000)
|
| 274 |
+
args = parser.parse_args()
|
| 275 |
+
|
| 276 |
+
summary = build_dataset(args.raw_dir, args.out_dir, args.target_file_size, args.row_group_size)
|
| 277 |
+
print(json.dumps(summary, indent=2))
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
if __name__ == "__main__":
|
| 281 |
+
main()
|