jkh commited on
Commit
d83bc30
·
verified ·
1 Parent(s): 78792a7

Add dataset loading script to fix load_dataset functionality

Browse files
Files changed (1) hide show
  1. skeletal_muscle_atlas.py +250 -0
skeletal_muscle_atlas.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ HuggingFace Dataset Loading Script for Skeletal Muscle Aging Atlas
3
+
4
+ This script defines how to load the skeletal muscle dataset parquet files
5
+ into a structured HuggingFace dataset with multiple configurations.
6
+ """
7
+
8
+ import datasets
9
+ import pandas as pd
10
+ from typing import Dict, List, Any
11
+
12
+
13
+ # Dataset metadata
14
+ _CITATION = """
15
+ @article{kedlian2024human,
16
+ title={Human skeletal muscle aging atlas},
17
+ author={Kedlian, Veronika R and Wang, Yaning and Liu, Tianliang and Chen, Xiaoping and Bolt, Liam and Tudor, Catherine and Shen, Zhuojian and Fasouli, Eirini S and Prigmore, Elena and Kleshchevnikov, Vitalii and others},
18
+ journal={Nature Aging},
19
+ volume={4},
20
+ pages={727--744},
21
+ year={2024},
22
+ publisher={Nature Publishing Group},
23
+ doi={10.1038/s43587-024-00613-3},
24
+ url={https://www.nature.com/articles/s43587-024-00613-3}
25
+ }
26
+ """
27
+
28
+ _DESCRIPTION = """
29
+ A comprehensive single-cell RNA-seq atlas of human skeletal muscle aging across the lifespan (15-75 years).
30
+ This dataset provides 183,161 cells from 17 donors with gene expression, metadata, and pre-computed embeddings.
31
+ """
32
+
33
+ _HOMEPAGE = "https://www.muscleageingcellatlas.org/"
34
+
35
+ _LICENSE = "MIT"
36
+
37
+ _URLS = {
38
+ "expression": "skeletal_muscle_10x_expression.parquet",
39
+ "sample_metadata": "skeletal_muscle_10x_sample_metadata.parquet",
40
+ "feature_metadata": "skeletal_muscle_10x_feature_metadata.parquet",
41
+ "projection_pca": "skeletal_muscle_10x_projection_X_pca.parquet",
42
+ "projection_umap": "skeletal_muscle_10x_projection_X_umap.parquet",
43
+ "projection_tsne": "skeletal_muscle_10x_projection_X_tsne.parquet",
44
+ "projection_scvi": "skeletal_muscle_10x_projection_X_scVI.parquet",
45
+ "unstructured_metadata": "skeletal_muscle_10x_unstructured_metadata.json"
46
+ }
47
+
48
+
49
+ class SkeletalMuscleAtlasConfig(datasets.BuilderConfig):
50
+ """Configuration for Skeletal Muscle Atlas dataset."""
51
+
52
+ def __init__(self, **kwargs):
53
+ super().__init__(**kwargs)
54
+
55
+
56
+ class SkeletalMuscleAtlas(datasets.GeneratorBasedBuilder):
57
+ """Skeletal Muscle Aging Atlas dataset."""
58
+
59
+ BUILDER_CONFIGS = [
60
+ SkeletalMuscleAtlasConfig(
61
+ name="expression",
62
+ version=datasets.Version("1.0.0"),
63
+ description="Gene expression matrix (183,161 cells × 29,400 genes)",
64
+ ),
65
+ SkeletalMuscleAtlasConfig(
66
+ name="sample_metadata",
67
+ version=datasets.Version("1.0.0"),
68
+ description="Cell-level metadata (age, cell type, sex, etc.)",
69
+ ),
70
+ SkeletalMuscleAtlasConfig(
71
+ name="feature_metadata",
72
+ version=datasets.Version("1.0.0"),
73
+ description="Gene-level metadata (symbols, IDs, etc.)",
74
+ ),
75
+ SkeletalMuscleAtlasConfig(
76
+ name="projection_pca",
77
+ version=datasets.Version("1.0.0"),
78
+ description="PCA embeddings (50 components)",
79
+ ),
80
+ SkeletalMuscleAtlasConfig(
81
+ name="projection_umap",
82
+ version=datasets.Version("1.0.0"),
83
+ description="UMAP embeddings (2D visualization)",
84
+ ),
85
+ SkeletalMuscleAtlasConfig(
86
+ name="projection_tsne",
87
+ version=datasets.Version("1.0.0"),
88
+ description="t-SNE embeddings (2D visualization)",
89
+ ),
90
+ SkeletalMuscleAtlasConfig(
91
+ name="projection_scvi",
92
+ version=datasets.Version("1.0.0"),
93
+ description="scVI embeddings (30D latent space)",
94
+ ),
95
+ SkeletalMuscleAtlasConfig(
96
+ name="all",
97
+ version=datasets.Version("1.0.0"),
98
+ description="All data combined (expression + metadata + embeddings)",
99
+ ),
100
+ ]
101
+
102
+ DEFAULT_CONFIG_NAME = "all"
103
+
104
+ def _info(self):
105
+ if self.config.name == "expression":
106
+ # Dynamic features for expression matrix
107
+ features = datasets.Features({
108
+ "cell_id": datasets.Value("string"),
109
+ **{f"gene_{i}": datasets.Value("float32") for i in range(29400)}
110
+ })
111
+ elif self.config.name == "sample_metadata":
112
+ features = datasets.Features({
113
+ "cell_id": datasets.Value("string"),
114
+ "Age_group": datasets.Value("string"),
115
+ "age_numeric": datasets.Value("float32"),
116
+ "Sex": datasets.Value("string"),
117
+ "annotation_level0": datasets.Value("string"),
118
+ "annotation_level1": datasets.Value("string"),
119
+ "annotation_level2": datasets.Value("string"),
120
+ "DonorID": datasets.Value("string"),
121
+ "batch": datasets.Value("string"),
122
+ # Add other metadata columns as needed
123
+ })
124
+ elif self.config.name == "feature_metadata":
125
+ features = datasets.Features({
126
+ "gene_id": datasets.Value("string"),
127
+ "SYMBOL": datasets.Value("string"),
128
+ "ENSEMBL": datasets.Value("string"),
129
+ "n_cells": datasets.Value("int32"),
130
+ })
131
+ elif self.config.name.startswith("projection_"):
132
+ # Dynamic features for embeddings
133
+ if self.config.name == "projection_pca":
134
+ n_dims = 50
135
+ elif self.config.name == "projection_scvi":
136
+ n_dims = 30
137
+ else: # umap, tsne
138
+ n_dims = 2
139
+
140
+ features = datasets.Features({
141
+ "cell_id": datasets.Value("string"),
142
+ **{f"dim_{i}": datasets.Value("float32") for i in range(n_dims)}
143
+ })
144
+ else: # "all" configuration
145
+ features = datasets.Features({
146
+ "cell_id": datasets.Value("string"),
147
+ "data_type": datasets.Value("string"),
148
+ "data": datasets.Value("string"), # JSON string of the data
149
+ })
150
+
151
+ return datasets.DatasetInfo(
152
+ description=_DESCRIPTION,
153
+ features=features,
154
+ homepage=_HOMEPAGE,
155
+ license=_LICENSE,
156
+ citation=_CITATION,
157
+ )
158
+
159
+ def _split_generators(self, dl_manager):
160
+ """Download and prepare the data."""
161
+
162
+ # Download all files
163
+ downloaded_files = dl_manager.download(_URLS)
164
+
165
+ return [
166
+ datasets.SplitGenerator(
167
+ name=datasets.Split.TRAIN,
168
+ gen_kwargs={
169
+ "files": downloaded_files,
170
+ "config_name": self.config.name,
171
+ },
172
+ ),
173
+ ]
174
+
175
+ def _generate_examples(self, files: Dict[str, str], config_name: str):
176
+ """Generate examples based on the configuration."""
177
+
178
+ if config_name == "expression":
179
+ # Load expression matrix
180
+ df = pd.read_parquet(files["expression"])
181
+
182
+ for idx, (cell_id, row) in enumerate(df.iterrows()):
183
+ yield idx, {
184
+ "cell_id": str(cell_id),
185
+ **{f"gene_{i}": float(row.iloc[i]) for i in range(len(row))}
186
+ }
187
+
188
+ elif config_name == "sample_metadata":
189
+ # Load sample metadata
190
+ df = pd.read_parquet(files["sample_metadata"])
191
+
192
+ for idx, (cell_id, row) in enumerate(df.iterrows()):
193
+ yield idx, {
194
+ "cell_id": str(cell_id),
195
+ "Age_group": str(row.get("Age_group", "")),
196
+ "age_numeric": float(row.get("age_numeric", 0.0)),
197
+ "Sex": str(row.get("Sex", "")),
198
+ "annotation_level0": str(row.get("annotation_level0", "")),
199
+ "annotation_level1": str(row.get("annotation_level1", "")),
200
+ "annotation_level2": str(row.get("annotation_level2", "")),
201
+ "DonorID": str(row.get("DonorID", "")),
202
+ "batch": str(row.get("batch", "")),
203
+ }
204
+
205
+ elif config_name == "feature_metadata":
206
+ # Load feature metadata
207
+ df = pd.read_parquet(files["feature_metadata"])
208
+
209
+ for idx, (gene_id, row) in enumerate(df.iterrows()):
210
+ yield idx, {
211
+ "gene_id": str(gene_id),
212
+ "SYMBOL": str(row.get("SYMBOL", "")),
213
+ "ENSEMBL": str(row.get("ENSEMBL", "")),
214
+ "n_cells": int(row.get("n_cells", 0)),
215
+ }
216
+
217
+ elif config_name.startswith("projection_"):
218
+ # Load projection data
219
+ projection_key = config_name # e.g., "projection_pca"
220
+ df = pd.read_parquet(files[projection_key])
221
+
222
+ for idx, (cell_id, row) in enumerate(df.iterrows()):
223
+ yield idx, {
224
+ "cell_id": str(cell_id),
225
+ **{f"dim_{i}": float(row.iloc[i]) for i in range(len(row))}
226
+ }
227
+
228
+ elif config_name == "all":
229
+ # Load all data types and provide a unified interface
230
+ sample_idx = 0
231
+
232
+ # Expression data
233
+ expr_df = pd.read_parquet(files["expression"])
234
+ for cell_id, row in expr_df.iterrows():
235
+ yield sample_idx, {
236
+ "cell_id": str(cell_id),
237
+ "data_type": "expression",
238
+ "data": row.to_json(),
239
+ }
240
+ sample_idx += 1
241
+
242
+ # Sample metadata
243
+ meta_df = pd.read_parquet(files["sample_metadata"])
244
+ for cell_id, row in meta_df.iterrows():
245
+ yield sample_idx, {
246
+ "cell_id": str(cell_id),
247
+ "data_type": "sample_metadata",
248
+ "data": row.to_json(),
249
+ }
250
+ sample_idx += 1