File size: 26,746 Bytes
92d2f89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c9878eb3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:29.255205Z",
     "iopub.status.busy": "2025-03-25T06:52:29.254564Z",
     "iopub.status.idle": "2025-03-25T06:52:29.419701Z",
     "shell.execute_reply": "2025-03-25T06:52:29.419374Z"
    }
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "sys.path.append(os.path.abspath(os.path.join(os.getcwd(), '../..')))\n",
    "\n",
    "# Path Configuration\n",
    "from tools.preprocess import *\n",
    "\n",
    "# Processing context\n",
    "trait = \"Autism_spectrum_disorder_(ASD)\"\n",
    "cohort = \"GSE42133\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Autism_spectrum_disorder_(ASD)\"\n",
    "in_cohort_dir = \"../../input/GEO/Autism_spectrum_disorder_(ASD)/GSE42133\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/GSE42133.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/gene_data/GSE42133.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/clinical_data/GSE42133.csv\"\n",
    "json_path = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "517ada7b",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "795cdabd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:29.421109Z",
     "iopub.status.busy": "2025-03-25T06:52:29.420962Z",
     "iopub.status.idle": "2025-03-25T06:52:29.804787Z",
     "shell.execute_reply": "2025-03-25T06:52:29.804420Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Disrupted functional neworks in autism underlie early brain maldevelopment and provide accurate classification\"\n",
      "!Series_summary\t\"The disrupted genetic mechanisms underlying neural abnormalities in Autism Spectrum Disorder remain mostly unknown and speculative. No biological marker nor genetic signature is currently available to assist with early diagnosis.\"\n",
      "!Series_summary\t\"We identified a blood-based gene expression signature relevant to the brain pathophysiology in autism. Also we identified genes that are differentially expressed in ASD subjects vs controls and gene modules that efficiently classify ASD and TD subjects.\"\n",
      "!Series_overall_design\t\"Leukocyte gene expression levels were analysed in autistic and tipically develoing infants and toddlers with the purpose to identify gene expression signatures relevant to the autistic brain and to assist in the classification of subjects.\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['dx (diagnosis): ASD', 'dx (diagnosis): Control'], 1: ['gender: male'], 2: ['cell type: leukocyte']}\n"
     ]
    }
   ],
   "source": [
    "from tools.preprocess import *\n",
    "# 1. Identify the paths to the SOFT file and the matrix file\n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "\n",
    "# 2. Read the matrix file to obtain background information and sample characteristics data\n",
    "background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']\n",
    "clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']\n",
    "background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)\n",
    "\n",
    "# 3. Obtain the sample characteristics dictionary from the clinical dataframe\n",
    "sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n",
    "\n",
    "# 4. Explicitly print out all the background information and the sample characteristics dictionary\n",
    "print(\"Background Information:\")\n",
    "print(background_info)\n",
    "print(\"Sample Characteristics Dictionary:\")\n",
    "print(sample_characteristics_dict)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61a37ba8",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5067dcbc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:29.806110Z",
     "iopub.status.busy": "2025-03-25T06:52:29.805989Z",
     "iopub.status.idle": "2025-03-25T06:52:29.816713Z",
     "shell.execute_reply": "2025-03-25T06:52:29.816431Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preview of selected clinical features:\n",
      "{'GSM1033105': [1.0], 'GSM1033106': [1.0], 'GSM1033107': [1.0], 'GSM1033108': [1.0], 'GSM1033109': [0.0], 'GSM1033110': [0.0], 'GSM1033111': [1.0], 'GSM1033112': [0.0], 'GSM1033113': [1.0], 'GSM1033114': [0.0], 'GSM1033115': [1.0], 'GSM1033116': [1.0], 'GSM1033117': [1.0], 'GSM1033118': [1.0], 'GSM1033119': [0.0], 'GSM1033120': [0.0], 'GSM1033121': [0.0], 'GSM1033122': [1.0], 'GSM1033123': [0.0], 'GSM1033124': [0.0], 'GSM1033125': [1.0], 'GSM1033126': [1.0], 'GSM1033127': [0.0], 'GSM1033128': [1.0], 'GSM1033129': [1.0], 'GSM1033130': [1.0], 'GSM1033131': [0.0], 'GSM1033132': [0.0], 'GSM1033133': [0.0], 'GSM1033134': [0.0], 'GSM1033135': [0.0], 'GSM1033136': [0.0], 'GSM1033137': [1.0], 'GSM1033138': [0.0], 'GSM1033139': [1.0], 'GSM1033140': [0.0], 'GSM1033141': [0.0], 'GSM1033142': [0.0], 'GSM1033143': [0.0], 'GSM1033144': [0.0], 'GSM1033145': [0.0], 'GSM1033146': [0.0], 'GSM1033147': [0.0], 'GSM1033148': [1.0], 'GSM1033149': [1.0], 'GSM1033150': [0.0], 'GSM1033152': [0.0], 'GSM1033153': [0.0], 'GSM1033154': [0.0], 'GSM1033155': [0.0], 'GSM1033156': [1.0], 'GSM1033157': [1.0], 'GSM1033158': [0.0], 'GSM1033159': [1.0], 'GSM1033160': [0.0], 'GSM1033161': [0.0], 'GSM1033162': [1.0], 'GSM1033163': [1.0], 'GSM1033164': [1.0], 'GSM1033165': [0.0], 'GSM1033166': [0.0], 'GSM1033167': [1.0], 'GSM1033168': [0.0], 'GSM1033169': [1.0], 'GSM1033170': [0.0], 'GSM1033171': [1.0], 'GSM1033172': [0.0], 'GSM1033173': [1.0], 'GSM1033174': [1.0], 'GSM1033175': [0.0], 'GSM1033176': [0.0], 'GSM1033177': [0.0], 'GSM1033178': [1.0], 'GSM1033179': [0.0], 'GSM1033180': [1.0], 'GSM1033181': [1.0], 'GSM1033182': [1.0], 'GSM1033183': [1.0], 'GSM1033184': [1.0], 'GSM1033185': [1.0], 'GSM1033186': [1.0], 'GSM1033187': [0.0], 'GSM1033188': [1.0], 'GSM1033189': [1.0], 'GSM1033190': [1.0], 'GSM1033191': [1.0], 'GSM1033192': [1.0], 'GSM1033193': [1.0], 'GSM1033194': [0.0], 'GSM1033195': [1.0], 'GSM1033196': [1.0], 'GSM1033197': [1.0], 'GSM1033198': [1.0], 'GSM1033199': [1.0], 'GSM1033200': [1.0], 'GSM1033201': [1.0], 'GSM1033202': [1.0], 'GSM1033203': [0.0], 'GSM1033204': [0.0], 'GSM1033205': [1.0], 'GSM1033206': [1.0], 'GSM1033207': [1.0], 'GSM1033208': [0.0], 'GSM1033209': [0.0], 'GSM1033211': [1.0], 'GSM1033212': [0.0], 'GSM1033213': [1.0], 'GSM1033214': [1.0], 'GSM1033215': [1.0], 'GSM1033216': [1.0], 'GSM1033217': [1.0], 'GSM1033218': [0.0], 'GSM1033219': [1.0], 'GSM1033220': [1.0], 'GSM1033221': [1.0], 'GSM1033222': [1.0], 'GSM1033223': [0.0], 'GSM1033224': [1.0], 'GSM1033225': [1.0], 'GSM1033227': [1.0], 'GSM1033228': [1.0], 'GSM1033229': [1.0], 'GSM1033230': [1.0], 'GSM1033231': [1.0], 'GSM1033232': [1.0], 'GSM1033233': [1.0], 'GSM1033234': [1.0], 'GSM1033235': [0.0], 'GSM1033236': [1.0], 'GSM1033237': [1.0], 'GSM1033238': [1.0], 'GSM1033239': [1.0], 'GSM1033240': [1.0], 'GSM1033241': [1.0], 'GSM1033242': [0.0], 'GSM1033243': [1.0], 'GSM1033244': [1.0], 'GSM1033245': [1.0], 'GSM1033246': [0.0], 'GSM1033247': [0.0], 'GSM1033248': [1.0], 'GSM1033249': [1.0], 'GSM1033250': [1.0], 'GSM1033251': [1.0], 'GSM1033253': [1.0], 'GSM1033254': [1.0], 'GSM1033255': [0.0]}\n",
      "Clinical data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/clinical_data/GSE42133.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Gene Expression Data Availability\n",
    "# Based on the background information, this dataset contains gene expression data from leukocytes\n",
    "is_gene_available = True\n",
    "\n",
    "# 2.1 Data Availability\n",
    "# Trait (diagnosis) is available in row 0\n",
    "trait_row = 0\n",
    "\n",
    "# Age is not available in the sample characteristics\n",
    "age_row = None\n",
    "\n",
    "# Gender appears to be constant (all male) as indicated in row 1\n",
    "# Since all subjects are male, this is not a useful variable for our associative study\n",
    "gender_row = None\n",
    "\n",
    "# 2.2 Data Type Conversion\n",
    "def convert_trait(value):\n",
    "    # Extract the value after the colon and trim whitespace\n",
    "    if \":\" in value:\n",
    "        value = value.split(\":\", 1)[1].strip()\n",
    "    \n",
    "    # Convert to binary: ASD = 1, Control = 0\n",
    "    if value.lower() == \"asd\":\n",
    "        return 1\n",
    "    elif value.lower() == \"control\":\n",
    "        return 0\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "def convert_age(value):\n",
    "    # Not needed as age data is not available\n",
    "    return None\n",
    "\n",
    "def convert_gender(value):\n",
    "    # Not needed as gender data is not variable (all male)\n",
    "    return None\n",
    "\n",
    "# 3. Save Metadata\n",
    "# Determine trait data availability\n",
    "is_trait_available = trait_row is not None\n",
    "\n",
    "# Save initial filtering information\n",
    "validate_and_save_cohort_info(\n",
    "    is_final=False,\n",
    "    cohort=cohort,\n",
    "    info_path=json_path,\n",
    "    is_gene_available=is_gene_available,\n",
    "    is_trait_available=is_trait_available\n",
    ")\n",
    "\n",
    "# 4. Clinical Feature Extraction (if applicable)\n",
    "if trait_row is not None:\n",
    "    # Since clinical_data is not defined in the prompt, assuming it was loaded in a previous step\n",
    "    # If not, we'd need to load it first\n",
    "    try:\n",
    "        # Extract clinical features\n",
    "        selected_clinical_df = geo_select_clinical_features(\n",
    "            clinical_df=clinical_data,  # Assuming clinical_data was loaded in a previous step\n",
    "            trait=trait,\n",
    "            trait_row=trait_row,\n",
    "            convert_trait=convert_trait,\n",
    "            age_row=age_row,\n",
    "            convert_age=convert_age,\n",
    "            gender_row=gender_row,\n",
    "            convert_gender=convert_gender\n",
    "        )\n",
    "        \n",
    "        # Preview the extracted clinical features\n",
    "        preview = preview_df(selected_clinical_df)\n",
    "        print(\"Preview of selected clinical features:\")\n",
    "        print(preview)\n",
    "        \n",
    "        # Save to CSV\n",
    "        selected_clinical_df.to_csv(out_clinical_data_file)\n",
    "        print(f\"Clinical data saved to {out_clinical_data_file}\")\n",
    "    except NameError:\n",
    "        print(\"Warning: clinical_data not found. Make sure it was loaded in a previous step.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e9eaecd",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f0aa0c26",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:29.817749Z",
     "iopub.status.busy": "2025-03-25T06:52:29.817646Z",
     "iopub.status.idle": "2025-03-25T06:52:30.498468Z",
     "shell.execute_reply": "2025-03-25T06:52:30.498085Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651199', 'ILMN_1651209',\n",
      "       'ILMN_1651210', 'ILMN_1651221', 'ILMN_1651228', 'ILMN_1651229',\n",
      "       'ILMN_1651230', 'ILMN_1651232', 'ILMN_1651235', 'ILMN_1651236',\n",
      "       'ILMN_1651237', 'ILMN_1651238', 'ILMN_1651249', 'ILMN_1651253',\n",
      "       'ILMN_1651254', 'ILMN_1651259', 'ILMN_1651260', 'ILMN_1651262'],\n",
      "      dtype='object', name='ID')\n"
     ]
    }
   ],
   "source": [
    "# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.\n",
    "gene_data = get_genetic_data(matrix_file)\n",
    "\n",
    "# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.\n",
    "print(gene_data.index[:20])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3765e18",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c8dbd866",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:30.499749Z",
     "iopub.status.busy": "2025-03-25T06:52:30.499627Z",
     "iopub.status.idle": "2025-03-25T06:52:30.501522Z",
     "shell.execute_reply": "2025-03-25T06:52:30.501233Z"
    }
   },
   "outputs": [],
   "source": [
    "# The identifiers starting with \"ILMN_\" are Illumina probe IDs, not human gene symbols.\n",
    "# These are microarray probe identifiers that need to be mapped to gene symbols.\n",
    "# Illumina probes (ILMN_xxxxxxx) require mapping to gene symbols for biological interpretation.\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdf14187",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bdccf79f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:30.502628Z",
     "iopub.status.busy": "2025-03-25T06:52:30.502519Z",
     "iopub.status.idle": "2025-03-25T06:52:42.585874Z",
     "shell.execute_reply": "2025-03-25T06:52:42.585506Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene annotation preview:\n",
      "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Species': [nan, nan, nan, nan, nan], 'Source': [nan, nan, nan, nan, nan], 'Search_Key': [nan, nan, nan, nan, nan], 'Transcript': [nan, nan, nan, nan, nan], 'ILMN_Gene': [nan, nan, nan, nan, nan], 'Source_Reference_ID': [nan, nan, nan, nan, nan], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Unigene_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': [nan, nan, nan, nan, nan], 'Symbol': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB'], 'Protein_Product': [nan, nan, nan, nan, 'thrB'], 'Probe_Id': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5090180.0, 6510136.0, 7560739.0, 1450438.0, 1240647.0], 'Probe_Type': [nan, nan, nan, nan, nan], 'Probe_Start': [nan, nan, nan, nan, nan], 'SEQUENCE': ['GAATAAAGAACAATCTGCTGATGATCCCTCCGTGGATCTGATTCGTGTAA', 'CCATGTGATACGAGGGCGCGTAGTTTGCATTATCGTTTTTATCGTTTCAA', 'CCGACAGATGTATGTAAGGCCAACGTGCTCAAATCTTCATACAGAAAGAT', 'TCTGTCACTGTCAGGAAAGTGGTAAAACTGCAACTCAATTACTGCAATGC', 'CTTGTGCCTGAGCTGTCAAAAGTAGAGCACGTCGCCGAGATGAAGGGCGC'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, nan, nan, nan], 'Definition': [nan, nan, nan, nan, nan], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan]}\n"
     ]
    }
   ],
   "source": [
    "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n",
    "gene_annotation = get_gene_annotation(soft_file)\n",
    "\n",
    "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n",
    "print(\"Gene annotation preview:\")\n",
    "print(preview_df(gene_annotation))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d08ac800",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "62b5f752",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:42.587160Z",
     "iopub.status.busy": "2025-03-25T06:52:42.587041Z",
     "iopub.status.idle": "2025-03-25T06:52:43.084912Z",
     "shell.execute_reply": "2025-03-25T06:52:43.084540Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene expression data after mapping:\n",
      "Shape: (21464, 147)\n",
      "First few gene symbols:\n",
      "Index(['A1BG', 'A1CF', 'A26C3', 'A2BP1', 'A2LD1', 'A2M', 'A2ML1', 'A3GALT2',\n",
      "       'A4GALT', 'A4GNT'],\n",
      "      dtype='object', name='Gene')\n"
     ]
    }
   ],
   "source": [
    "# 1. Identify the columns for probe IDs and gene symbols in the gene annotation dataframe\n",
    "# From the preview, we can see that 'ID' contains probe identifiers (ILMN_xxxxxxx) \n",
    "# and 'Symbol' contains gene symbols\n",
    "\n",
    "# 2. Get the gene mapping dataframe using get_gene_mapping from the library\n",
    "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n",
    "\n",
    "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n",
    "# This function properly handles many-to-many relationships as required\n",
    "gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=gene_mapping)\n",
    "\n",
    "# Preview the results to verify the mapping worked correctly\n",
    "print(\"Gene expression data after mapping:\")\n",
    "print(f\"Shape: {gene_data.shape}\")\n",
    "print(\"First few gene symbols:\")\n",
    "print(gene_data.index[:10])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c03ca10f",
   "metadata": {},
   "source": [
    "### Step 7: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ec2a6186",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:43.086231Z",
     "iopub.status.busy": "2025-03-25T06:52:43.086121Z",
     "iopub.status.idle": "2025-03-25T06:52:53.442185Z",
     "shell.execute_reply": "2025-03-25T06:52:53.441793Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preview of selected clinical features:\n",
      "{'GSM1033105': [1.0], 'GSM1033106': [1.0], 'GSM1033107': [1.0], 'GSM1033108': [1.0], 'GSM1033109': [0.0], 'GSM1033110': [0.0], 'GSM1033111': [1.0], 'GSM1033112': [0.0], 'GSM1033113': [1.0], 'GSM1033114': [0.0], 'GSM1033115': [1.0], 'GSM1033116': [1.0], 'GSM1033117': [1.0], 'GSM1033118': [1.0], 'GSM1033119': [0.0], 'GSM1033120': [0.0], 'GSM1033121': [0.0], 'GSM1033122': [1.0], 'GSM1033123': [0.0], 'GSM1033124': [0.0], 'GSM1033125': [1.0], 'GSM1033126': [1.0], 'GSM1033127': [0.0], 'GSM1033128': [1.0], 'GSM1033129': [1.0], 'GSM1033130': [1.0], 'GSM1033131': [0.0], 'GSM1033132': [0.0], 'GSM1033133': [0.0], 'GSM1033134': [0.0], 'GSM1033135': [0.0], 'GSM1033136': [0.0], 'GSM1033137': [1.0], 'GSM1033138': [0.0], 'GSM1033139': [1.0], 'GSM1033140': [0.0], 'GSM1033141': [0.0], 'GSM1033142': [0.0], 'GSM1033143': [0.0], 'GSM1033144': [0.0], 'GSM1033145': [0.0], 'GSM1033146': [0.0], 'GSM1033147': [0.0], 'GSM1033148': [1.0], 'GSM1033149': [1.0], 'GSM1033150': [0.0], 'GSM1033152': [0.0], 'GSM1033153': [0.0], 'GSM1033154': [0.0], 'GSM1033155': [0.0], 'GSM1033156': [1.0], 'GSM1033157': [1.0], 'GSM1033158': [0.0], 'GSM1033159': [1.0], 'GSM1033160': [0.0], 'GSM1033161': [0.0], 'GSM1033162': [1.0], 'GSM1033163': [1.0], 'GSM1033164': [1.0], 'GSM1033165': [0.0], 'GSM1033166': [0.0], 'GSM1033167': [1.0], 'GSM1033168': [0.0], 'GSM1033169': [1.0], 'GSM1033170': [0.0], 'GSM1033171': [1.0], 'GSM1033172': [0.0], 'GSM1033173': [1.0], 'GSM1033174': [1.0], 'GSM1033175': [0.0], 'GSM1033176': [0.0], 'GSM1033177': [0.0], 'GSM1033178': [1.0], 'GSM1033179': [0.0], 'GSM1033180': [1.0], 'GSM1033181': [1.0], 'GSM1033182': [1.0], 'GSM1033183': [1.0], 'GSM1033184': [1.0], 'GSM1033185': [1.0], 'GSM1033186': [1.0], 'GSM1033187': [0.0], 'GSM1033188': [1.0], 'GSM1033189': [1.0], 'GSM1033190': [1.0], 'GSM1033191': [1.0], 'GSM1033192': [1.0], 'GSM1033193': [1.0], 'GSM1033194': [0.0], 'GSM1033195': [1.0], 'GSM1033196': [1.0], 'GSM1033197': [1.0], 'GSM1033198': [1.0], 'GSM1033199': [1.0], 'GSM1033200': [1.0], 'GSM1033201': [1.0], 'GSM1033202': [1.0], 'GSM1033203': [0.0], 'GSM1033204': [0.0], 'GSM1033205': [1.0], 'GSM1033206': [1.0], 'GSM1033207': [1.0], 'GSM1033208': [0.0], 'GSM1033209': [0.0], 'GSM1033211': [1.0], 'GSM1033212': [0.0], 'GSM1033213': [1.0], 'GSM1033214': [1.0], 'GSM1033215': [1.0], 'GSM1033216': [1.0], 'GSM1033217': [1.0], 'GSM1033218': [0.0], 'GSM1033219': [1.0], 'GSM1033220': [1.0], 'GSM1033221': [1.0], 'GSM1033222': [1.0], 'GSM1033223': [0.0], 'GSM1033224': [1.0], 'GSM1033225': [1.0], 'GSM1033227': [1.0], 'GSM1033228': [1.0], 'GSM1033229': [1.0], 'GSM1033230': [1.0], 'GSM1033231': [1.0], 'GSM1033232': [1.0], 'GSM1033233': [1.0], 'GSM1033234': [1.0], 'GSM1033235': [0.0], 'GSM1033236': [1.0], 'GSM1033237': [1.0], 'GSM1033238': [1.0], 'GSM1033239': [1.0], 'GSM1033240': [1.0], 'GSM1033241': [1.0], 'GSM1033242': [0.0], 'GSM1033243': [1.0], 'GSM1033244': [1.0], 'GSM1033245': [1.0], 'GSM1033246': [0.0], 'GSM1033247': [0.0], 'GSM1033248': [1.0], 'GSM1033249': [1.0], 'GSM1033250': [1.0], 'GSM1033251': [1.0], 'GSM1033253': [1.0], 'GSM1033254': [1.0], 'GSM1033255': [0.0]}\n",
      "Clinical data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/clinical_data/GSE42133.csv\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalized gene data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/gene_data/GSE42133.csv\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For the feature 'Autism_spectrum_disorder_(ASD)', the least common label is '0.0' with 56 occurrences. This represents 38.10% of the dataset.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linked data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/GSE42133.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. We need to first create the selected_clinical_df using clinical_data from Step 1\n",
    "selected_clinical_df = geo_select_clinical_features(\n",
    "    clinical_df=clinical_data,\n",
    "    trait=trait,\n",
    "    trait_row=trait_row,\n",
    "    convert_trait=convert_trait,\n",
    "    age_row=age_row,\n",
    "    convert_age=convert_age,\n",
    "    gender_row=gender_row,\n",
    "    convert_gender=convert_gender\n",
    ")\n",
    "\n",
    "# Preview the extracted clinical features\n",
    "preview = preview_df(selected_clinical_df)\n",
    "print(\"Preview of selected clinical features:\")\n",
    "print(preview)\n",
    "\n",
    "# Save the clinical data\n",
    "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "selected_clinical_df.to_csv(out_clinical_data_file)\n",
    "print(f\"Clinical data saved to {out_clinical_data_file}\")\n",
    "\n",
    "# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n",
    "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n",
    "# Create directory if it doesn't exist\n",
    "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
    "normalized_gene_data.to_csv(out_gene_data_file)\n",
    "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n",
    "\n",
    "# 2. Link the clinical and genetic data with the 'geo_link_clinical_genetic_data' function from the library.\n",
    "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n",
    "\n",
    "# 3. Handle missing values in the linked data\n",
    "linked_data = handle_missing_values(linked_data, trait)\n",
    "\n",
    "# 4. Determine whether the trait and demographic features are severely biased\n",
    "trait_type = 'binary' if len(linked_data[trait].unique()) <= 2 else 'continuous'\n",
    "if trait_type == 'binary':\n",
    "    is_trait_biased = judge_binary_variable_biased(linked_data, trait)\n",
    "else:\n",
    "    is_trait_biased = judge_continuous_variable_biased(linked_data, trait)\n",
    "\n",
    "# Remove biased demographic features\n",
    "unbiased_linked_data = linked_data.copy()\n",
    "if 'Age' in unbiased_linked_data.columns:\n",
    "    age_biased = judge_continuous_variable_biased(unbiased_linked_data, 'Age')\n",
    "    if age_biased:\n",
    "        unbiased_linked_data = unbiased_linked_data.drop(columns=['Age'])\n",
    "        \n",
    "if 'Gender' in unbiased_linked_data.columns:\n",
    "    gender_biased = judge_binary_variable_biased(unbiased_linked_data, 'Gender')\n",
    "    if gender_biased:\n",
    "        unbiased_linked_data = unbiased_linked_data.drop(columns=['Gender'])\n",
    "\n",
    "# 5. Conduct quality check and save the cohort information.\n",
    "is_usable = validate_and_save_cohort_info(\n",
    "    is_final=True, \n",
    "    cohort=cohort, \n",
    "    info_path=json_path, \n",
    "    is_gene_available=True, \n",
    "    is_trait_available=True, \n",
    "    is_biased=is_trait_biased, \n",
    "    df=unbiased_linked_data, \n",
    "    note=\"Dataset contains gene expression data from iPSC-derived neurons of ASD patients and unaffected siblings.\"\n",
    ")\n",
    "\n",
    "# 6. If the linked data is usable, save it as a CSV file to 'out_data_file'.\n",
    "if is_usable:\n",
    "    os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n",
    "    unbiased_linked_data.to_csv(out_data_file)\n",
    "    print(f\"Linked data saved to {out_data_file}\")\n",
    "else:\n",
    "    print(\"The dataset was determined to be not usable for analysis.\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}