Shreshth Gandhi commited on
Commit
67bec88
·
1 Parent(s): 0ff1204

Add notebook for loading data

Browse files
Files changed (1) hide show
  1. tutorials/loading_data.ipynb +329 -0
tutorials/loading_data.ipynb ADDED
@@ -0,0 +1,329 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "ee3ff49b",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Tutorial: Creating an AnnData object from Tahoe-100M dataset\n",
9
+ "This notebook demonstrates how to create an `AnnData` object using the Tahoe-100M dataset on Hugging Face."
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 1,
15
+ "id": "4df26b4a",
16
+ "metadata": {},
17
+ "outputs": [],
18
+ "source": [
19
+ "# !pip install datasets anndata scipy pandas duckdb"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": 2,
25
+ "id": "f5566528",
26
+ "metadata": {},
27
+ "outputs": [
28
+ {
29
+ "name": "stderr",
30
+ "output_type": "stream",
31
+ "text": [
32
+ "/usr/lib/python3/dist-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
33
+ " from .autonotebook import tqdm as notebook_tqdm\n"
34
+ ]
35
+ }
36
+ ],
37
+ "source": [
38
+ "from datasets import load_dataset\n",
39
+ "from scipy.sparse import csr_matrix\n",
40
+ "import anndata\n",
41
+ "import pandas as pd"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "markdown",
46
+ "id": "601a878d",
47
+ "metadata": {},
48
+ "source": [
49
+ "## Helper function\n",
50
+ "Define a function to construct the AnnData object from a data generator."
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": 3,
56
+ "id": "e72e75ce",
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "\n",
61
+ "def create_anndata_from_generator(generator, gene_vocab, sample_size=None):\n",
62
+ " sorted_vocab_items = sorted(gene_vocab.items())\n",
63
+ " token_ids, gene_names = zip(*sorted_vocab_items)\n",
64
+ " token_id_to_col_idx = {token_id: idx for idx, token_id in enumerate(token_ids)}\n",
65
+ "\n",
66
+ " data, indices, indptr = [], [], [0]\n",
67
+ " obs_data = []\n",
68
+ "\n",
69
+ " for i, cell in enumerate(generator):\n",
70
+ " if sample_size is not None and i >= sample_size:\n",
71
+ " break\n",
72
+ " genes = cell['genes']\n",
73
+ " expressions = cell['expressions']\n",
74
+ " if expressions[0] < 0: \n",
75
+ " genes = genes[1:]\n",
76
+ " expressions = expressions[1:]\n",
77
+ "\n",
78
+ " col_indices = [token_id_to_col_idx[gene] for gene in genes if gene in token_id_to_col_idx]\n",
79
+ " valid_expressions = [expr for gene, expr in zip(genes, expressions) if gene in token_id_to_col_idx]\n",
80
+ "\n",
81
+ " data.extend(valid_expressions)\n",
82
+ " indices.extend(col_indices)\n",
83
+ " indptr.append(len(data))\n",
84
+ "\n",
85
+ " obs_entry = {k: v for k, v in cell.items() if k not in ['genes', 'expressions']}\n",
86
+ " obs_data.append(obs_entry)\n",
87
+ "\n",
88
+ " expr_matrix = csr_matrix((data, indices, indptr), shape=(len(indptr) - 1, len(gene_names)))\n",
89
+ " obs_df = pd.DataFrame(obs_data)\n",
90
+ "\n",
91
+ " adata = anndata.AnnData(X=expr_matrix, obs=obs_df)\n",
92
+ " adata.var.index = pd.Index(gene_names, name='ensembl_id')\n",
93
+ "\n",
94
+ " return adata\n"
95
+ ]
96
+ },
97
+ {
98
+ "cell_type": "markdown",
99
+ "id": "3d3f44c4",
100
+ "metadata": {},
101
+ "source": [
102
+ "## Load Tahoe-100M dataset"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": 4,
108
+ "id": "66329f53",
109
+ "metadata": {},
110
+ "outputs": [],
111
+ "source": [
112
+ "\n",
113
+ "# Stream the main dataset\n",
114
+ "tahoe_100m_ds = load_dataset(\"vevotx/Tahoe-100M\", streaming=True, split=\"train\")\n"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "markdown",
119
+ "id": "4de64af6",
120
+ "metadata": {},
121
+ "source": [
122
+ "## Load gene metadata to construct gene vocabulary"
123
+ ]
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "execution_count": 5,
128
+ "id": "482f8461",
129
+ "metadata": {},
130
+ "outputs": [],
131
+ "source": [
132
+ "\n",
133
+ "gene_metadata = load_dataset(\"vevotx/Tahoe-100M\", name=\"gene_metadata\", split=\"train\")\n",
134
+ "gene_vocab = {entry[\"token_id\"]: entry[\"ensembl_id\"] for entry in gene_metadata}\n"
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "markdown",
139
+ "id": "18d80c71",
140
+ "metadata": {},
141
+ "source": [
142
+ "## Create AnnData object from dataset"
143
+ ]
144
+ },
145
+ {
146
+ "cell_type": "code",
147
+ "execution_count": 6,
148
+ "id": "cf137497",
149
+ "metadata": {},
150
+ "outputs": [
151
+ {
152
+ "name": "stderr",
153
+ "output_type": "stream",
154
+ "text": [
155
+ "/usr/lib/python3/dist-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.\n",
156
+ " warnings.warn(\"Transforming to str index.\", ImplicitModificationWarning)\n"
157
+ ]
158
+ },
159
+ {
160
+ "data": {
161
+ "text/plain": [
162
+ "AnnData object with n_obs × n_vars = 1000 × 62710\n",
163
+ " obs: 'drug', 'sample', 'BARCODE_SUB_LIB_ID', 'cell_line_id', 'moa-fine', 'canonical_smiles', 'pubchem_cid', 'plate'"
164
+ ]
165
+ },
166
+ "execution_count": 6,
167
+ "metadata": {},
168
+ "output_type": "execute_result"
169
+ }
170
+ ],
171
+ "source": [
172
+ "\n",
173
+ "adata = create_anndata_from_generator(tahoe_100m_ds, gene_vocab, sample_size=1000)\n",
174
+ "adata\n"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "markdown",
179
+ "id": "0069a2bb",
180
+ "metadata": {},
181
+ "source": [
182
+ "## Inspect metadata (`obs`)"
183
+ ]
184
+ },
185
+ {
186
+ "cell_type": "code",
187
+ "execution_count": 7,
188
+ "id": "9a3180c0",
189
+ "metadata": {},
190
+ "outputs": [
191
+ {
192
+ "data": {
193
+ "text/html": [
194
+ "<div>\n",
195
+ "<style scoped>\n",
196
+ " .dataframe tbody tr th:only-of-type {\n",
197
+ " vertical-align: middle;\n",
198
+ " }\n",
199
+ "\n",
200
+ " .dataframe tbody tr th {\n",
201
+ " vertical-align: top;\n",
202
+ " }\n",
203
+ "\n",
204
+ " .dataframe thead th {\n",
205
+ " text-align: right;\n",
206
+ " }\n",
207
+ "</style>\n",
208
+ "<table border=\"1\" class=\"dataframe\">\n",
209
+ " <thead>\n",
210
+ " <tr style=\"text-align: right;\">\n",
211
+ " <th></th>\n",
212
+ " <th>drug</th>\n",
213
+ " <th>sample</th>\n",
214
+ " <th>BARCODE_SUB_LIB_ID</th>\n",
215
+ " <th>cell_line_id</th>\n",
216
+ " <th>moa-fine</th>\n",
217
+ " <th>canonical_smiles</th>\n",
218
+ " <th>pubchem_cid</th>\n",
219
+ " <th>plate</th>\n",
220
+ " </tr>\n",
221
+ " </thead>\n",
222
+ " <tbody>\n",
223
+ " <tr>\n",
224
+ " <th>0</th>\n",
225
+ " <td>8-Hydroxyquinoline</td>\n",
226
+ " <td>smp_1783</td>\n",
227
+ " <td>01_001_052-lib_1105</td>\n",
228
+ " <td>CVCL_0480</td>\n",
229
+ " <td>unclear</td>\n",
230
+ " <td>C1=CC2=C(C(=C1)O)N=CC=C2</td>\n",
231
+ " <td>1923.0</td>\n",
232
+ " <td>plate4</td>\n",
233
+ " </tr>\n",
234
+ " <tr>\n",
235
+ " <th>1</th>\n",
236
+ " <td>8-Hydroxyquinoline</td>\n",
237
+ " <td>smp_1783</td>\n",
238
+ " <td>01_001_105-lib_1105</td>\n",
239
+ " <td>CVCL_0546</td>\n",
240
+ " <td>unclear</td>\n",
241
+ " <td>C1=CC2=C(C(=C1)O)N=CC=C2</td>\n",
242
+ " <td>1923.0</td>\n",
243
+ " <td>plate4</td>\n",
244
+ " </tr>\n",
245
+ " <tr>\n",
246
+ " <th>2</th>\n",
247
+ " <td>8-Hydroxyquinoline</td>\n",
248
+ " <td>smp_1783</td>\n",
249
+ " <td>01_001_165-lib_1105</td>\n",
250
+ " <td>CVCL_1717</td>\n",
251
+ " <td>unclear</td>\n",
252
+ " <td>C1=CC2=C(C(=C1)O)N=CC=C2</td>\n",
253
+ " <td>1923.0</td>\n",
254
+ " <td>plate4</td>\n",
255
+ " </tr>\n",
256
+ " <tr>\n",
257
+ " <th>3</th>\n",
258
+ " <td>8-Hydroxyquinoline</td>\n",
259
+ " <td>smp_1783</td>\n",
260
+ " <td>01_003_094-lib_1105</td>\n",
261
+ " <td>CVCL_1717</td>\n",
262
+ " <td>unclear</td>\n",
263
+ " <td>C1=CC2=C(C(=C1)O)N=CC=C2</td>\n",
264
+ " <td>1923.0</td>\n",
265
+ " <td>plate4</td>\n",
266
+ " </tr>\n",
267
+ " <tr>\n",
268
+ " <th>4</th>\n",
269
+ " <td>8-Hydroxyquinoline</td>\n",
270
+ " <td>smp_1783</td>\n",
271
+ " <td>01_003_164-lib_1105</td>\n",
272
+ " <td>CVCL_1056</td>\n",
273
+ " <td>unclear</td>\n",
274
+ " <td>C1=CC2=C(C(=C1)O)N=CC=C2</td>\n",
275
+ " <td>1923.0</td>\n",
276
+ " <td>plate4</td>\n",
277
+ " </tr>\n",
278
+ " </tbody>\n",
279
+ "</table>\n",
280
+ "</div>"
281
+ ],
282
+ "text/plain": [
283
+ " drug sample BARCODE_SUB_LIB_ID cell_line_id moa-fine \\\n",
284
+ "0 8-Hydroxyquinoline smp_1783 01_001_052-lib_1105 CVCL_0480 unclear \n",
285
+ "1 8-Hydroxyquinoline smp_1783 01_001_105-lib_1105 CVCL_0546 unclear \n",
286
+ "2 8-Hydroxyquinoline smp_1783 01_001_165-lib_1105 CVCL_1717 unclear \n",
287
+ "3 8-Hydroxyquinoline smp_1783 01_003_094-lib_1105 CVCL_1717 unclear \n",
288
+ "4 8-Hydroxyquinoline smp_1783 01_003_164-lib_1105 CVCL_1056 unclear \n",
289
+ "\n",
290
+ " canonical_smiles pubchem_cid plate \n",
291
+ "0 C1=CC2=C(C(=C1)O)N=CC=C2 1923.0 plate4 \n",
292
+ "1 C1=CC2=C(C(=C1)O)N=CC=C2 1923.0 plate4 \n",
293
+ "2 C1=CC2=C(C(=C1)O)N=CC=C2 1923.0 plate4 \n",
294
+ "3 C1=CC2=C(C(=C1)O)N=CC=C2 1923.0 plate4 \n",
295
+ "4 C1=CC2=C(C(=C1)O)N=CC=C2 1923.0 plate4 "
296
+ ]
297
+ },
298
+ "execution_count": 7,
299
+ "metadata": {},
300
+ "output_type": "execute_result"
301
+ }
302
+ ],
303
+ "source": [
304
+ "adata.obs.head()"
305
+ ]
306
+ }
307
+ ],
308
+ "metadata": {
309
+ "kernelspec": {
310
+ "display_name": "Python 3 (ipykernel)",
311
+ "language": "python",
312
+ "name": "python3"
313
+ },
314
+ "language_info": {
315
+ "codemirror_mode": {
316
+ "name": "ipython",
317
+ "version": 3
318
+ },
319
+ "file_extension": ".py",
320
+ "mimetype": "text/x-python",
321
+ "name": "python",
322
+ "nbconvert_exporter": "python",
323
+ "pygments_lexer": "ipython3",
324
+ "version": "3.11.10"
325
+ }
326
+ },
327
+ "nbformat": 4,
328
+ "nbformat_minor": 5
329
+ }