Add normalized Parquet train/test NCBI shard index
Browse files- README.md +120 -66
- data/test-00000-of-00001.parquet +3 -0
- data/train-00000-of-00001.parquet +3 -0
- dataset_summary.json +51 -0
- metadata/source_files.parquet +3 -0
- scripts/prepare_ncbi_dataset.py +231 -0
README.md
CHANGED
|
@@ -1,97 +1,151 @@
|
|
| 1 |
---
|
|
|
|
| 2 |
license: other
|
| 3 |
-
pretty_name: NCBI RefSeq Protein
|
| 4 |
-
size_categories:
|
| 5 |
-
- 100M<n<1B
|
| 6 |
-
task_categories:
|
| 7 |
-
- other
|
| 8 |
-
language:
|
| 9 |
-
- en
|
| 10 |
tags:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
---
|
| 18 |
|
| 19 |
-
# NCBI RefSeq Protein
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
(internal repo). Original source: <https://www.ncbi.nlm.nih.gov/refseq/>.
|
| 25 |
|
| 26 |
-
##
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
| 31 |
-
|
|
| 32 |
-
|
|
| 33 |
-
|
|
| 34 |
-
|
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
```
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
```
|
| 46 |
|
| 47 |
-
|
| 48 |
-
`sequence_uniprotkb_uniprot_sprot.fasta.gz`.
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
interest under `sequences/`):
|
| 54 |
|
| 55 |
```bash
|
| 56 |
hf download LiteFold/NCBI --repo-type dataset \
|
| 57 |
-
--include 'sequences/
|
| 58 |
--local-dir ./ncbi_refseq_protein
|
| 59 |
-
zstd -dc ./ncbi_refseq_protein/sequences/<source_slug>/shard-*.fasta.zst | head
|
| 60 |
```
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
```python
|
| 65 |
-
from huggingface_hub import snapshot_download
|
| 66 |
from pathlib import Path
|
| 67 |
import zstandard as zstd
|
| 68 |
|
| 69 |
-
|
| 70 |
-
repo_id="LiteFold/NCBI",
|
| 71 |
-
repo_type="dataset",
|
| 72 |
-
allow_patterns=["sequences/*/shard-*.fasta.zst"],
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
dctx = zstd.ZstdDecompressor()
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
buf = b""
|
| 79 |
-
while chunk := reader.read(1 << 20):
|
| 80 |
-
buf += chunk
|
| 81 |
-
*lines, buf = buf.split(b"\n")
|
| 82 |
-
for line in lines:
|
| 83 |
-
... # naive splitter; swap in your FASTA parser
|
| 84 |
```
|
| 85 |
|
| 86 |
-
##
|
| 87 |
-
|
| 88 |
-
Public Domain (US Government work, NCBI RefSeq policy).
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
pretty_name: NCBI RefSeq Protein Shard Index
|
| 3 |
license: other
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
tags:
|
| 5 |
+
- biology
|
| 6 |
+
- proteins
|
| 7 |
+
- sequences
|
| 8 |
+
- fasta
|
| 9 |
+
- ncbi
|
| 10 |
+
- refseq
|
| 11 |
+
- parquet
|
| 12 |
+
configs:
|
| 13 |
+
- config_name: default
|
| 14 |
+
data_files:
|
| 15 |
+
- split: train
|
| 16 |
+
path: data/train-*.parquet
|
| 17 |
+
- split: test
|
| 18 |
+
path: data/test-*.parquet
|
| 19 |
---
|
| 20 |
|
| 21 |
+
# NCBI RefSeq Protein Shard Index
|
| 22 |
|
| 23 |
+
This dataset contains the original NCBI RefSeq protein FASTA shards plus a viewer-friendly file/shard index. The full sequence data is stored as 1,725 `.fasta.zst` shards and the per-record metadata JSONL files are very large, so the default Dataset Viewer table indexes repository files instead of expanding all 459,415,871 protein records.
|
| 24 |
|
| 25 |
+
Use the original `sequences/.../shard-*.fasta.zst` files for complete FASTA records. Use the default Parquet table for Dataset Viewer previews, source discovery, file sizes, record counts, and download patterns.
|
|
|
|
| 26 |
|
| 27 |
+
## Splits
|
| 28 |
|
| 29 |
+
The split is deterministic by file ID: `sha256(file_id) % 10`. Bucket `0` is `test`; buckets `1` through `9` are `train`.
|
| 30 |
+
|
| 31 |
+
| Split | Rows |
|
| 32 |
+
|---|---:|
|
| 33 |
+
| train | 4,676 |
|
| 34 |
+
| test | 502 |
|
| 35 |
+
| total | 5,178 |
|
| 36 |
+
|
| 37 |
+
## Source Statistics
|
| 38 |
+
|
| 39 |
+
| Field | Value |
|
| 40 |
+
|---|---:|
|
| 41 |
+
| Source FASTA files | 1,725 |
|
| 42 |
+
| RefSeq protein records | 459,415,871 |
|
| 43 |
+
| Residues | 179,203,453,293 |
|
| 44 |
+
| Sequence shards | 1,725 |
|
| 45 |
+
| Compressed sequence shard bytes | 78,108,688,857 |
|
| 46 |
+
| Metadata JSONL bytes | 158,533,041,909 |
|
| 47 |
+
|
| 48 |
+
## Usage
|
| 49 |
+
|
| 50 |
+
```bash
|
| 51 |
+
pip install datasets
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
Load the shard index:
|
| 55 |
+
|
| 56 |
+
```python
|
| 57 |
+
from datasets import load_dataset
|
| 58 |
+
|
| 59 |
+
ds = load_dataset("LiteFold/NCBI")
|
| 60 |
+
print(ds)
|
| 61 |
+
print(ds["train"][0])
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
Load one split:
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
from datasets import load_dataset
|
| 68 |
+
|
| 69 |
+
train = load_dataset("LiteFold/NCBI", split="train")
|
| 70 |
+
test = load_dataset("LiteFold/NCBI", split="test")
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
List sequence shards:
|
| 74 |
|
| 75 |
+
```python
|
| 76 |
+
from datasets import load_dataset
|
| 77 |
|
| 78 |
+
index = load_dataset("LiteFold/NCBI", split="train")
|
| 79 |
+
shards = index.filter(lambda row: row["is_sequence_shard"])
|
| 80 |
+
print(shards[0]["path"])
|
| 81 |
```
|
| 82 |
+
|
| 83 |
+
Find a source FASTA and its files:
|
| 84 |
+
|
| 85 |
+
```python
|
| 86 |
+
from datasets import load_dataset
|
| 87 |
+
|
| 88 |
+
index = load_dataset("LiteFold/NCBI", split="train")
|
| 89 |
+
rows = index.filter(lambda row: row["source_file"] == "sequence/ncbi_refseq/release_complete/complete.1486.protein.faa.gz")
|
| 90 |
+
for row in rows:
|
| 91 |
+
print(row["role"], row["path"], row["size_bytes"])
|
| 92 |
```
|
| 93 |
|
| 94 |
+
Download all sequence shards:
|
|
|
|
| 95 |
|
| 96 |
+
```bash
|
| 97 |
+
hf download LiteFold/NCBI --repo-type dataset \
|
| 98 |
+
--include 'sequences/*/shard-*.fasta.zst' \
|
| 99 |
+
--local-dir ./ncbi_refseq_protein
|
| 100 |
+
```
|
| 101 |
|
| 102 |
+
Download one source shard:
|
|
|
|
| 103 |
|
| 104 |
```bash
|
| 105 |
hf download LiteFold/NCBI --repo-type dataset \
|
| 106 |
+
--include 'sequences/sequence_ncbi_refseq_release_complete_complete.1486.protein.faa.gz/shard-*.fasta.zst' \
|
| 107 |
--local-dir ./ncbi_refseq_protein
|
|
|
|
| 108 |
```
|
| 109 |
|
| 110 |
+
Stream a downloaded shard with Python:
|
| 111 |
|
| 112 |
```python
|
|
|
|
| 113 |
from pathlib import Path
|
| 114 |
import zstandard as zstd
|
| 115 |
|
| 116 |
+
shard = next(Path("./ncbi_refseq_protein").rglob("shard-*.fasta.zst"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
dctx = zstd.ZstdDecompressor()
|
| 118 |
+
with shard.open("rb") as f, dctx.stream_reader(f) as reader:
|
| 119 |
+
print(reader.read(1024).decode("utf-8", errors="replace"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
```
|
| 121 |
|
| 122 |
+
## Columns
|
|
|
|
|
|
|
| 123 |
|
| 124 |
+
| Column | Description |
|
| 125 |
+
|---|---|
|
| 126 |
+
| `file_id` | Stable row ID, equal to the repository path. |
|
| 127 |
+
| `repo_id` | Hugging Face dataset repository. |
|
| 128 |
+
| `source_sha` | Source repository commit used to build the index. |
|
| 129 |
+
| `dataset_id` | Source dataset identifier from the manifest. |
|
| 130 |
+
| `source_slug` | Source slug from the original pipeline manifest. |
|
| 131 |
+
| `source_file` | Original source FASTA file path. |
|
| 132 |
+
| `path` | File path in the repository. |
|
| 133 |
+
| `role` | File role, such as `sequence_shard`, `metadata_records`, or `source_manifest`. |
|
| 134 |
+
| `shard_index` | Numeric shard index for sequence shards. |
|
| 135 |
+
| `size_bytes` | File size in bytes. |
|
| 136 |
+
| `compression` | Compression format, when applicable. |
|
| 137 |
+
| `records_in_source` | Protein record count for the source FASTA file. |
|
| 138 |
+
| `residues_in_source` | Residue count for the source FASTA file. |
|
| 139 |
+
| `shards_in_source` | Shard count for the source FASTA file. |
|
| 140 |
+
| `records_total` | Total protein record count from the aggregate manifest. |
|
| 141 |
+
| `residues_total` | Total residue count from the aggregate manifest. |
|
| 142 |
+
| `total_shards` | Total sequence shard count. |
|
| 143 |
+
| `is_sequence_shard` | Whether the row points to a FASTA shard. |
|
| 144 |
+
| `is_metadata_records` | Whether the row points to a per-record metadata JSONL. |
|
| 145 |
+
| `download_pattern` | Recommended path or glob for downloading. |
|
| 146 |
+
| `access_note` | Note describing the index scope. |
|
| 147 |
+
| `split_bucket` | Deterministic split bucket from `sha256(file_id) % 10`. |
|
| 148 |
+
|
| 149 |
+
## Preparation
|
| 150 |
+
|
| 151 |
+
The normalization script used to create the Parquet files is included at `scripts/prepare_ncbi_dataset.py`.
|
data/test-00000-of-00001.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a2413e996b0b7e15126f9ecf2c67a524cec72cf14d4acf5b64dcf44468d13737
|
| 3 |
+
size 25551
|
data/train-00000-of-00001.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:96c33ed34fe37b3ee0912adce611d10c6a66eaaa9273762a3137d1ad834bf92d
|
| 3 |
+
size 120593
|
dataset_summary.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"source": "LiteFold/NCBI",
|
| 3 |
+
"source_sha": "ca26266937eac32428823d12614ba3c639a36ba0",
|
| 4 |
+
"viewer_table_scope": "file/shard index",
|
| 5 |
+
"dataset_id": "ncbi_refseq_protein",
|
| 6 |
+
"source_count": 1725,
|
| 7 |
+
"records_total": 459415871,
|
| 8 |
+
"residues_total": 179203453293,
|
| 9 |
+
"total_shards": 1725,
|
| 10 |
+
"index_rows": 5178,
|
| 11 |
+
"sequence_shard_rows": 1725,
|
| 12 |
+
"sequence_shard_bytes": 78108688857,
|
| 13 |
+
"metadata_records_bytes": 158533041909,
|
| 14 |
+
"splits": {
|
| 15 |
+
"train": 4676,
|
| 16 |
+
"test": 502
|
| 17 |
+
},
|
| 18 |
+
"split_strategy": "deterministic sha256(file_id) % 10; bucket 0 is test, buckets 1-9 are train",
|
| 19 |
+
"role_counts": {
|
| 20 |
+
"git_attributes": 1,
|
| 21 |
+
"readme": 1,
|
| 22 |
+
"aggregate_manifest": 1,
|
| 23 |
+
"source_manifest": 1725,
|
| 24 |
+
"metadata_records": 1725,
|
| 25 |
+
"sequence_shard": 1725
|
| 26 |
+
},
|
| 27 |
+
"columns": [
|
| 28 |
+
"file_id",
|
| 29 |
+
"repo_id",
|
| 30 |
+
"source_sha",
|
| 31 |
+
"dataset_id",
|
| 32 |
+
"source_slug",
|
| 33 |
+
"source_file",
|
| 34 |
+
"path",
|
| 35 |
+
"role",
|
| 36 |
+
"shard_index",
|
| 37 |
+
"size_bytes",
|
| 38 |
+
"compression",
|
| 39 |
+
"records_in_source",
|
| 40 |
+
"residues_in_source",
|
| 41 |
+
"shards_in_source",
|
| 42 |
+
"records_total",
|
| 43 |
+
"residues_total",
|
| 44 |
+
"total_shards",
|
| 45 |
+
"is_sequence_shard",
|
| 46 |
+
"is_metadata_records",
|
| 47 |
+
"download_pattern",
|
| 48 |
+
"access_note",
|
| 49 |
+
"split_bucket"
|
| 50 |
+
]
|
| 51 |
+
}
|
metadata/source_files.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:27e6ef2ea64d53271d9441e1c864b26d84048d65d52d5d68a8edaef28693cf44
|
| 3 |
+
size 120243
|
scripts/prepare_ncbi_dataset.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Build viewer-friendly file/shard index Parquet splits for LiteFold/NCBI."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import argparse
|
| 7 |
+
import hashlib
|
| 8 |
+
import json
|
| 9 |
+
import os
|
| 10 |
+
import re
|
| 11 |
+
import shutil
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
from typing import Any
|
| 14 |
+
|
| 15 |
+
import pyarrow as pa
|
| 16 |
+
import pyarrow.parquet as pq
|
| 17 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
INDEX_COLUMNS = [
|
| 21 |
+
"file_id",
|
| 22 |
+
"repo_id",
|
| 23 |
+
"source_sha",
|
| 24 |
+
"dataset_id",
|
| 25 |
+
"source_slug",
|
| 26 |
+
"source_file",
|
| 27 |
+
"path",
|
| 28 |
+
"role",
|
| 29 |
+
"shard_index",
|
| 30 |
+
"size_bytes",
|
| 31 |
+
"compression",
|
| 32 |
+
"records_in_source",
|
| 33 |
+
"residues_in_source",
|
| 34 |
+
"shards_in_source",
|
| 35 |
+
"records_total",
|
| 36 |
+
"residues_total",
|
| 37 |
+
"total_shards",
|
| 38 |
+
"is_sequence_shard",
|
| 39 |
+
"is_metadata_records",
|
| 40 |
+
"download_pattern",
|
| 41 |
+
"access_note",
|
| 42 |
+
"split_bucket",
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
SCHEMA = pa.schema(
|
| 47 |
+
[
|
| 48 |
+
pa.field("file_id", pa.string()),
|
| 49 |
+
pa.field("repo_id", pa.string()),
|
| 50 |
+
pa.field("source_sha", pa.string()),
|
| 51 |
+
pa.field("dataset_id", pa.string()),
|
| 52 |
+
pa.field("source_slug", pa.string()),
|
| 53 |
+
pa.field("source_file", pa.string()),
|
| 54 |
+
pa.field("path", pa.string()),
|
| 55 |
+
pa.field("role", pa.string()),
|
| 56 |
+
pa.field("shard_index", pa.int64()),
|
| 57 |
+
pa.field("size_bytes", pa.int64()),
|
| 58 |
+
pa.field("compression", pa.string()),
|
| 59 |
+
pa.field("records_in_source", pa.int64()),
|
| 60 |
+
pa.field("residues_in_source", pa.int64()),
|
| 61 |
+
pa.field("shards_in_source", pa.int64()),
|
| 62 |
+
pa.field("records_total", pa.int64()),
|
| 63 |
+
pa.field("residues_total", pa.int64()),
|
| 64 |
+
pa.field("total_shards", pa.int64()),
|
| 65 |
+
pa.field("is_sequence_shard", pa.bool_()),
|
| 66 |
+
pa.field("is_metadata_records", pa.bool_()),
|
| 67 |
+
pa.field("download_pattern", pa.string()),
|
| 68 |
+
pa.field("access_note", pa.string()),
|
| 69 |
+
pa.field("split_bucket", pa.int64()),
|
| 70 |
+
]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def load_token() -> str | None:
|
| 75 |
+
for key in ("HF_TOKEN", "HUGGINGFACE_HUB_TOKEN"):
|
| 76 |
+
value = os.environ.get(key)
|
| 77 |
+
if value:
|
| 78 |
+
return value
|
| 79 |
+
env_path = Path(".env")
|
| 80 |
+
if env_path.exists():
|
| 81 |
+
for line in env_path.read_text().splitlines():
|
| 82 |
+
stripped = line.strip()
|
| 83 |
+
if not stripped or stripped.startswith("#") or "=" not in stripped:
|
| 84 |
+
continue
|
| 85 |
+
key, value = stripped.split("=", 1)
|
| 86 |
+
if key.strip() in {"HF_TOKEN", "HUGGINGFACE_HUB_TOKEN"}:
|
| 87 |
+
value = value.strip().strip('"').strip("'")
|
| 88 |
+
if value:
|
| 89 |
+
return value
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def stable_bucket(value: str, buckets: int = 10) -> int:
|
| 94 |
+
digest = hashlib.sha256(value.encode("utf-8")).hexdigest()[:16]
|
| 95 |
+
return int(digest, 16) % buckets
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def role_for_path(path: str) -> tuple[str, str | None, int | None, bool, bool]:
|
| 99 |
+
shard_match = re.search(r"sequences/([^/]+)/shard-(\d+)\.fasta\.zst$", path)
|
| 100 |
+
if shard_match:
|
| 101 |
+
return "sequence_shard", shard_match.group(1), int(shard_match.group(2)), True, False
|
| 102 |
+
metadata_match = re.search(r"metadata/(.+)\.records\.jsonl$", path)
|
| 103 |
+
if metadata_match:
|
| 104 |
+
return "metadata_records", metadata_match.group(1), None, False, True
|
| 105 |
+
manifest_match = re.search(r"manifests/(.+)\.json$", path)
|
| 106 |
+
if manifest_match:
|
| 107 |
+
return "source_manifest", manifest_match.group(1), None, False, False
|
| 108 |
+
if path == "_MANIFEST.json":
|
| 109 |
+
return "aggregate_manifest", None, None, False, False
|
| 110 |
+
if path == "README.md":
|
| 111 |
+
return "readme", None, None, False, False
|
| 112 |
+
if path == ".gitattributes":
|
| 113 |
+
return "git_attributes", None, None, False, False
|
| 114 |
+
return "other", None, None, False, False
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def compression_for_path(path: str) -> str | None:
|
| 118 |
+
if path.endswith(".fasta.zst"):
|
| 119 |
+
return "zstd"
|
| 120 |
+
return None
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def build_dataset(repo_id: str, raw_dir: Path, out_dir: Path) -> dict[str, Any]:
|
| 124 |
+
token = load_token()
|
| 125 |
+
api = HfApi(token=token)
|
| 126 |
+
info = api.dataset_info(repo_id, files_metadata=True)
|
| 127 |
+
raw_dir.mkdir(parents=True, exist_ok=True)
|
| 128 |
+
manifest_path = Path(
|
| 129 |
+
hf_hub_download(
|
| 130 |
+
repo_id=repo_id,
|
| 131 |
+
repo_type="dataset",
|
| 132 |
+
filename="_MANIFEST.json",
|
| 133 |
+
local_dir=raw_dir,
|
| 134 |
+
token=token,
|
| 135 |
+
)
|
| 136 |
+
)
|
| 137 |
+
manifest = json.loads(manifest_path.read_text())
|
| 138 |
+
dataset_id = str(manifest["dataset_id"])
|
| 139 |
+
total_records = int(manifest["total_records"])
|
| 140 |
+
total_residues = int(manifest["total_residues"])
|
| 141 |
+
total_shards = int(manifest["total_shards"])
|
| 142 |
+
|
| 143 |
+
sources_by_slug = {source["source_slug"]: source for source in manifest["sources"]}
|
| 144 |
+
|
| 145 |
+
rows = []
|
| 146 |
+
for sibling in sorted(info.siblings or [], key=lambda item: item.rfilename):
|
| 147 |
+
path = sibling.rfilename
|
| 148 |
+
role, source_slug, shard_index, is_sequence_shard, is_metadata_records = role_for_path(path)
|
| 149 |
+
source = sources_by_slug.get(source_slug or "")
|
| 150 |
+
file_id = path
|
| 151 |
+
rows.append(
|
| 152 |
+
{
|
| 153 |
+
"file_id": file_id,
|
| 154 |
+
"repo_id": repo_id,
|
| 155 |
+
"source_sha": info.sha,
|
| 156 |
+
"dataset_id": dataset_id,
|
| 157 |
+
"source_slug": source_slug,
|
| 158 |
+
"source_file": source.get("source_file") if source else None,
|
| 159 |
+
"path": path,
|
| 160 |
+
"role": role,
|
| 161 |
+
"shard_index": shard_index,
|
| 162 |
+
"size_bytes": int(getattr(sibling, "size", 0) or 0),
|
| 163 |
+
"compression": compression_for_path(path),
|
| 164 |
+
"records_in_source": int(source["records"]) if source else None,
|
| 165 |
+
"residues_in_source": int(source["residues"]) if source else None,
|
| 166 |
+
"shards_in_source": int(source["shards"]) if source else None,
|
| 167 |
+
"records_total": total_records,
|
| 168 |
+
"residues_total": total_residues,
|
| 169 |
+
"total_shards": total_shards,
|
| 170 |
+
"is_sequence_shard": is_sequence_shard,
|
| 171 |
+
"is_metadata_records": is_metadata_records,
|
| 172 |
+
"download_pattern": f"sequences/{source_slug}/shard-*.fasta.zst" if is_sequence_shard else path,
|
| 173 |
+
"access_note": "File/shard index for NCBI RefSeq protein; download sequence shards for FASTA records.",
|
| 174 |
+
"split_bucket": stable_bucket(file_id),
|
| 175 |
+
}
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
if out_dir.exists():
|
| 179 |
+
shutil.rmtree(out_dir)
|
| 180 |
+
data_dir = out_dir / "data"
|
| 181 |
+
metadata_dir = out_dir / "metadata"
|
| 182 |
+
data_dir.mkdir(parents=True, exist_ok=True)
|
| 183 |
+
metadata_dir.mkdir(parents=True, exist_ok=True)
|
| 184 |
+
|
| 185 |
+
train_rows = sorted((row for row in rows if row["split_bucket"] != 0), key=lambda row: row["path"])
|
| 186 |
+
test_rows = sorted((row for row in rows if row["split_bucket"] == 0), key=lambda row: row["path"])
|
| 187 |
+
|
| 188 |
+
pq.write_table(pa.Table.from_pylist(train_rows, schema=SCHEMA), data_dir / "train-00000-of-00001.parquet", compression="zstd")
|
| 189 |
+
pq.write_table(pa.Table.from_pylist(test_rows, schema=SCHEMA), data_dir / "test-00000-of-00001.parquet", compression="zstd")
|
| 190 |
+
pq.write_table(pa.Table.from_pylist(rows, schema=SCHEMA), metadata_dir / "source_files.parquet", compression="zstd")
|
| 191 |
+
|
| 192 |
+
sequence_bytes = sum(int(row["size_bytes"]) for row in rows if row["is_sequence_shard"])
|
| 193 |
+
metadata_bytes = sum(int(row["size_bytes"]) for row in rows if row["is_metadata_records"])
|
| 194 |
+
role_counts: dict[str, int] = {}
|
| 195 |
+
for row in rows:
|
| 196 |
+
role_counts[row["role"]] = role_counts.get(row["role"], 0) + 1
|
| 197 |
+
|
| 198 |
+
summary = {
|
| 199 |
+
"source": repo_id,
|
| 200 |
+
"source_sha": info.sha,
|
| 201 |
+
"viewer_table_scope": "file/shard index",
|
| 202 |
+
"dataset_id": dataset_id,
|
| 203 |
+
"source_count": int(manifest["source_count"]),
|
| 204 |
+
"records_total": total_records,
|
| 205 |
+
"residues_total": total_residues,
|
| 206 |
+
"total_shards": total_shards,
|
| 207 |
+
"index_rows": len(rows),
|
| 208 |
+
"sequence_shard_rows": sum(1 for row in rows if row["is_sequence_shard"]),
|
| 209 |
+
"sequence_shard_bytes": sequence_bytes,
|
| 210 |
+
"metadata_records_bytes": metadata_bytes,
|
| 211 |
+
"splits": {"train": len(train_rows), "test": len(test_rows)},
|
| 212 |
+
"split_strategy": "deterministic sha256(file_id) % 10; bucket 0 is test, buckets 1-9 are train",
|
| 213 |
+
"role_counts": role_counts,
|
| 214 |
+
"columns": INDEX_COLUMNS,
|
| 215 |
+
}
|
| 216 |
+
(out_dir / "dataset_summary.json").write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
|
| 217 |
+
return summary
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def main() -> None:
|
| 221 |
+
parser = argparse.ArgumentParser()
|
| 222 |
+
parser.add_argument("--repo-id", default="LiteFold/NCBI")
|
| 223 |
+
parser.add_argument("--raw-dir", type=Path, default=Path("LiteFold_NCBI_raw"))
|
| 224 |
+
parser.add_argument("--out-dir", type=Path, default=Path("LiteFold_NCBI_processed"))
|
| 225 |
+
args = parser.parse_args()
|
| 226 |
+
summary = build_dataset(args.repo_id, args.raw_dir, args.out_dir)
|
| 227 |
+
print(json.dumps(summary, indent=2))
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
if __name__ == "__main__":
|
| 231 |
+
main()
|