Shreshth Gandhi commited on
Commit
c17073b
·
1 Parent(s): 963e940

Update notebook

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