File size: 24,336 Bytes
32677ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d0569e62",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:40:33.304790Z",
     "iopub.status.busy": "2025-03-25T08:40:33.304384Z",
     "iopub.status.idle": "2025-03-25T08:40:33.471695Z",
     "shell.execute_reply": "2025-03-25T08:40:33.471372Z"
    }
   },
   "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 = \"Eczema\"\n",
    "cohort = \"GSE123088\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Eczema\"\n",
    "in_cohort_dir = \"../../input/GEO/Eczema/GSE123088\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Eczema/GSE123088.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Eczema/gene_data/GSE123088.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Eczema/clinical_data/GSE123088.csv\"\n",
    "json_path = \"../../output/preprocess/Eczema/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bdd2df7",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ff77bcd7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:40:33.473107Z",
     "iopub.status.busy": "2025-03-25T08:40:33.472959Z",
     "iopub.status.idle": "2025-03-25T08:40:33.747524Z",
     "shell.execute_reply": "2025-03-25T08:40:33.747184Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"A validated single-cell-based strategy to identify diagnostic and therapeutic targets in complex diseases\"\n",
      "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n",
      "!Series_overall_design\t\"Refer to individual Series\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['cell type: CD4+ T cells'], 1: ['primary diagnosis: ASTHMA', 'primary diagnosis: ATHEROSCLEROSIS', 'primary diagnosis: BREAST_CANCER', 'primary diagnosis: CHRONIC_LYMPHOCYTIC_LEUKEMIA', 'primary diagnosis: CROHN_DISEASE', 'primary diagnosis: ATOPIC_ECZEMA', 'primary diagnosis: HEALTHY_CONTROL', 'primary diagnosis: INFLUENZA', 'primary diagnosis: OBESITY', 'primary diagnosis: PSORIASIS', 'primary diagnosis: SEASONAL_ALLERGIC_RHINITIS', 'primary diagnosis: TYPE_1_DIABETES', 'primary diagnosis: ACUTE_TONSILLITIS', 'primary diagnosis: ULCERATIVE_COLITIS', 'primary diagnosis: Breast cancer', 'primary diagnosis: Control'], 2: ['Sex: Male', 'diagnosis2: ATOPIC_ECZEMA', 'Sex: Female', 'diagnosis2: ATHEROSCLEROSIS', 'diagnosis2: ASTHMA_OBESITY', 'diagnosis2: ASTHMA', 'diagnosis2: ASTMHA_SEASONAL_ALLERGIC_RHINITIS', 'diagnosis2: OBESITY'], 3: ['age: 56', 'Sex: Male', 'age: 20', 'age: 51', 'age: 37', 'age: 61', 'age: 31', 'age: 41', 'age: 80', 'age: 53', 'age: 73', 'age: 60', 'age: 76', 'age: 77', 'age: 74', 'age: 69', 'age: 81', 'age: 70', 'age: 82', 'age: 67', 'age: 78', 'age: 72', 'age: 66', 'age: 36', 'age: 45', 'age: 65', 'age: 48', 'age: 50', 'age: 24', 'age: 42'], 4: [nan, 'age: 63', 'age: 74', 'age: 49', 'age: 60', 'age: 68', 'age: 38', 'age: 16', 'age: 12', 'age: 27']}\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": "6e5170a7",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "42ed76ba",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:40:33.748684Z",
     "iopub.status.busy": "2025-03-25T08:40:33.748580Z",
     "iopub.status.idle": "2025-03-25T08:40:33.773690Z",
     "shell.execute_reply": "2025-03-25T08:40:33.773407Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preview of clinical features:\n",
      "{'GSM3494884': [nan, 56.0, 1.0], 'GSM3494885': [nan, nan, nan], 'GSM3494886': [nan, 20.0, 0.0], 'GSM3494887': [nan, 51.0, 0.0], 'GSM3494888': [nan, 37.0, 1.0], 'GSM3494889': [nan, 61.0, 1.0], 'GSM3494890': [nan, nan, nan], 'GSM3494891': [nan, 31.0, 1.0], 'GSM3494892': [nan, 56.0, 0.0], 'GSM3494893': [nan, 41.0, 0.0], 'GSM3494894': [nan, 61.0, 0.0], 'GSM3494895': [nan, nan, nan], 'GSM3494896': [nan, 80.0, 1.0], 'GSM3494897': [nan, 53.0, 1.0], 'GSM3494898': [nan, 61.0, 1.0], 'GSM3494899': [nan, 73.0, 1.0], 'GSM3494900': [nan, 60.0, 1.0], 'GSM3494901': [nan, 76.0, 1.0], 'GSM3494902': [nan, 77.0, 0.0], 'GSM3494903': [nan, 74.0, 0.0], 'GSM3494904': [nan, 69.0, 1.0], 'GSM3494905': [nan, 77.0, 0.0], 'GSM3494906': [nan, 81.0, 0.0], 'GSM3494907': [nan, 70.0, 0.0], 'GSM3494908': [nan, 82.0, 0.0], 'GSM3494909': [nan, 69.0, 0.0], 'GSM3494910': [nan, 82.0, 0.0], 'GSM3494911': [nan, 67.0, 0.0], 'GSM3494912': [nan, 67.0, 0.0], 'GSM3494913': [nan, 78.0, 0.0], 'GSM3494914': [nan, 67.0, 0.0], 'GSM3494915': [nan, 74.0, 1.0], 'GSM3494916': [nan, nan, nan], 'GSM3494917': [nan, 51.0, 1.0], 'GSM3494918': [nan, 72.0, 1.0], 'GSM3494919': [nan, 66.0, 1.0], 'GSM3494920': [nan, 80.0, 0.0], 'GSM3494921': [nan, 36.0, 1.0], 'GSM3494922': [nan, 67.0, 0.0], 'GSM3494923': [nan, 31.0, 0.0], 'GSM3494924': [nan, 31.0, 0.0], 'GSM3494925': [nan, 45.0, 0.0], 'GSM3494926': [nan, 56.0, 0.0], 'GSM3494927': [nan, 65.0, 0.0], 'GSM3494928': [nan, 53.0, 0.0], 'GSM3494929': [nan, 48.0, 0.0], 'GSM3494930': [nan, 50.0, 0.0], 'GSM3494931': [nan, 76.0, 1.0], 'GSM3494932': [1.0, nan, nan], 'GSM3494933': [1.0, 24.0, 0.0], 'GSM3494934': [1.0, 42.0, 0.0], 'GSM3494935': [1.0, 76.0, 1.0], 'GSM3494936': [1.0, 22.0, 1.0], 'GSM3494937': [1.0, nan, nan], 'GSM3494938': [1.0, 23.0, 0.0], 'GSM3494939': [0.0, 34.0, 1.0], 'GSM3494940': [0.0, 43.0, 1.0], 'GSM3494941': [0.0, 47.0, 1.0], 'GSM3494942': [0.0, 24.0, 0.0], 'GSM3494943': [0.0, 55.0, 1.0], 'GSM3494944': [0.0, 48.0, 1.0], 'GSM3494945': [0.0, 58.0, 1.0], 'GSM3494946': [0.0, 30.0, 0.0], 'GSM3494947': [0.0, 28.0, 1.0], 'GSM3494948': [0.0, 41.0, 0.0], 'GSM3494949': [0.0, 63.0, 1.0], 'GSM3494950': [0.0, 55.0, 0.0], 'GSM3494951': [0.0, 55.0, 0.0], 'GSM3494952': [0.0, 67.0, 1.0], 'GSM3494953': [0.0, 47.0, 0.0], 'GSM3494954': [0.0, 46.0, 0.0], 'GSM3494955': [0.0, 49.0, 1.0], 'GSM3494956': [0.0, 23.0, 1.0], 'GSM3494957': [0.0, 68.0, 1.0], 'GSM3494958': [0.0, 39.0, 1.0], 'GSM3494959': [0.0, 24.0, 1.0], 'GSM3494960': [0.0, 36.0, 0.0], 'GSM3494961': [0.0, 58.0, 0.0], 'GSM3494962': [0.0, 38.0, 0.0], 'GSM3494963': [0.0, 27.0, 0.0], 'GSM3494964': [0.0, 67.0, 0.0], 'GSM3494965': [0.0, 61.0, 1.0], 'GSM3494966': [0.0, 69.0, 1.0], 'GSM3494967': [0.0, 63.0, 1.0], 'GSM3494968': [0.0, 60.0, 0.0], 'GSM3494969': [0.0, 17.0, 1.0], 'GSM3494970': [0.0, 10.0, 0.0], 'GSM3494971': [0.0, 9.0, 1.0], 'GSM3494972': [0.0, 13.0, 0.0], 'GSM3494973': [0.0, 10.0, 1.0], 'GSM3494974': [0.0, 13.0, 0.0], 'GSM3494975': [0.0, 15.0, 1.0], 'GSM3494976': [0.0, 12.0, 1.0], 'GSM3494977': [0.0, 13.0, 1.0], 'GSM3494978': [nan, 81.0, 0.0], 'GSM3494979': [nan, 94.0, 0.0], 'GSM3494980': [nan, 51.0, 1.0], 'GSM3494981': [nan, 40.0, 1.0], 'GSM3494982': [nan, nan, nan], 'GSM3494983': [nan, 97.0, 1.0], 'GSM3494984': [nan, 23.0, 1.0], 'GSM3494985': [nan, 93.0, 0.0], 'GSM3494986': [nan, 58.0, 1.0], 'GSM3494987': [nan, 28.0, 0.0], 'GSM3494988': [nan, 54.0, 1.0], 'GSM3494989': [nan, 15.0, 1.0], 'GSM3494990': [nan, 8.0, 1.0], 'GSM3494991': [nan, 11.0, 1.0], 'GSM3494992': [nan, 12.0, 1.0], 'GSM3494993': [nan, 8.0, 0.0], 'GSM3494994': [nan, 14.0, 1.0], 'GSM3494995': [nan, 8.0, 0.0], 'GSM3494996': [nan, 10.0, 1.0], 'GSM3494997': [nan, 14.0, 1.0], 'GSM3494998': [nan, 13.0, 1.0], 'GSM3494999': [nan, 40.0, 0.0], 'GSM3495000': [nan, 52.0, 0.0], 'GSM3495001': [nan, 42.0, 0.0], 'GSM3495002': [nan, 29.0, 0.0], 'GSM3495003': [nan, 43.0, 0.0], 'GSM3495004': [nan, 41.0, 0.0], 'GSM3495005': [nan, 54.0, 1.0], 'GSM3495006': [nan, 42.0, 1.0], 'GSM3495007': [nan, 49.0, 1.0], 'GSM3495008': [nan, 45.0, 0.0], 'GSM3495009': [nan, 56.0, 1.0], 'GSM3495010': [nan, 64.0, 0.0], 'GSM3495011': [nan, 71.0, 0.0], 'GSM3495012': [nan, 48.0, 0.0], 'GSM3495013': [nan, 20.0, 1.0], 'GSM3495014': [nan, 53.0, 0.0], 'GSM3495015': [nan, 32.0, 0.0], 'GSM3495016': [nan, 26.0, 0.0], 'GSM3495017': [nan, 28.0, 0.0], 'GSM3495018': [nan, 47.0, 0.0], 'GSM3495019': [nan, 24.0, 0.0], 'GSM3495020': [nan, 48.0, 0.0], 'GSM3495021': [nan, nan, nan], 'GSM3495022': [nan, 19.0, 0.0], 'GSM3495023': [nan, 41.0, 0.0], 'GSM3495024': [nan, 38.0, 0.0], 'GSM3495025': [nan, nan, nan], 'GSM3495026': [nan, 15.0, 0.0], 'GSM3495027': [nan, 12.0, 1.0], 'GSM3495028': [nan, 13.0, 0.0], 'GSM3495029': [nan, nan, nan], 'GSM3495030': [nan, 11.0, 1.0], 'GSM3495031': [nan, nan, nan], 'GSM3495032': [nan, 16.0, 1.0], 'GSM3495033': [nan, 11.0, 1.0], 'GSM3495034': [nan, nan, nan], 'GSM3495035': [nan, 35.0, 0.0], 'GSM3495036': [nan, 26.0, 0.0], 'GSM3495037': [nan, 39.0, 0.0], 'GSM3495038': [nan, 46.0, 0.0], 'GSM3495039': [nan, 42.0, 0.0], 'GSM3495040': [nan, 20.0, 1.0], 'GSM3495041': [nan, 69.0, 1.0], 'GSM3495042': [nan, 69.0, 0.0], 'GSM3495043': [nan, 47.0, 1.0], 'GSM3495044': [nan, 47.0, 1.0], 'GSM3495045': [nan, 56.0, 0.0], 'GSM3495046': [nan, 54.0, 0.0], 'GSM3495047': [nan, 53.0, 0.0], 'GSM3495048': [nan, 50.0, 0.0], 'GSM3495049': [nan, 22.0, 1.0], 'GSM3495050': [nan, 62.0, 0.0], 'GSM3495051': [nan, 74.0, 0.0], 'GSM3495052': [0.0, 57.0, 0.0], 'GSM3495053': [0.0, 47.0, 0.0], 'GSM3495054': [nan, 70.0, 0.0], 'GSM3495055': [nan, 50.0, 0.0], 'GSM3495056': [0.0, 52.0, 0.0], 'GSM3495057': [nan, 43.0, 0.0], 'GSM3495058': [0.0, 57.0, 0.0], 'GSM3495059': [nan, 53.0, 0.0], 'GSM3495060': [nan, 70.0, 0.0], 'GSM3495061': [0.0, 41.0, 0.0], 'GSM3495062': [nan, 61.0, 0.0], 'GSM3495063': [0.0, 39.0, 0.0], 'GSM3495064': [0.0, 58.0, 0.0], 'GSM3495065': [nan, 55.0, 0.0], 'GSM3495066': [nan, 63.0, 0.0], 'GSM3495067': [0.0, 60.0, 0.0], 'GSM3495068': [nan, 43.0, 0.0], 'GSM3495069': [nan, 68.0, 0.0], 'GSM3495070': [nan, 67.0, 0.0], 'GSM3495071': [nan, 50.0, 0.0], 'GSM3495072': [nan, 67.0, 0.0], 'GSM3495073': [0.0, 51.0, 0.0], 'GSM3495074': [0.0, 59.0, 0.0], 'GSM3495075': [0.0, 44.0, 0.0], 'GSM3495076': [nan, 35.0, 0.0], 'GSM3495077': [nan, 83.0, 0.0], 'GSM3495078': [nan, 78.0, 0.0], 'GSM3495079': [nan, 88.0, 0.0], 'GSM3495080': [nan, 41.0, 0.0], 'GSM3495081': [0.0, 60.0, 0.0], 'GSM3495082': [nan, 72.0, 0.0], 'GSM3495083': [nan, 53.0, 0.0]}\n",
      "Clinical data saved to ../../output/preprocess/Eczema/clinical_data/GSE123088.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Determine gene expression data availability\n",
    "# This dataset appears to be a SuperSeries combining several studies\n",
    "# Since it mentions CD4+ T cells and includes various diagnoses, it likely contains gene expression data\n",
    "is_gene_available = True\n",
    "\n",
    "# 2.1 Data Availability\n",
    "# Trait (Eczema) appears in row 1 as \"primary diagnosis: ATOPIC_ECZEMA\"\n",
    "trait_row = 1\n",
    "\n",
    "# Age appears in row 3 and continues in row 4\n",
    "age_row = 3\n",
    "\n",
    "# Gender/Sex appears in rows 2 and 3\n",
    "gender_row = 2\n",
    "\n",
    "# 2.2 Data Type Conversion Functions\n",
    "def convert_trait(value):\n",
    "    if pd.isna(value):\n",
    "        return None\n",
    "    \n",
    "    # Extract value after colon\n",
    "    if \":\" in value:\n",
    "        value = value.split(\":\", 1)[1].strip()\n",
    "    \n",
    "    # Check if Eczema is present in any form\n",
    "    if \"ATOPIC_ECZEMA\" in value:\n",
    "        return 1\n",
    "    elif \"HEALTHY_CONTROL\" in value or \"Control\" in value:\n",
    "        return 0\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "def convert_age(value):\n",
    "    if pd.isna(value):\n",
    "        return None\n",
    "    \n",
    "    # Extract value after colon\n",
    "    if \":\" in value:\n",
    "        value = value.split(\":\", 1)[1].strip()\n",
    "    \n",
    "    try:\n",
    "        return float(value)\n",
    "    except:\n",
    "        return None\n",
    "\n",
    "def convert_gender(value):\n",
    "    if pd.isna(value):\n",
    "        return None\n",
    "    \n",
    "    # Extract value after colon\n",
    "    if \":\" in value:\n",
    "        value = value.split(\":\", 1)[1].strip()\n",
    "    \n",
    "    if value.lower() == \"female\":\n",
    "        return 0\n",
    "    elif value.lower() == \"male\":\n",
    "        return 1\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "# 3. Save Metadata\n",
    "# Determine if trait data is available\n",
    "is_trait_available = trait_row is not None\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 trait_row is not None)\n",
    "if trait_row is not None:\n",
    "    # Extract clinical features\n",
    "    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 features\n",
    "    preview = preview_df(clinical_df)\n",
    "    print(\"Preview of 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",
    "    clinical_df.to_csv(out_clinical_data_file)\n",
    "    print(f\"Clinical data saved to {out_clinical_data_file}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3e3d004",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3539d90c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:40:33.774766Z",
     "iopub.status.busy": "2025-03-25T08:40:33.774664Z",
     "iopub.status.idle": "2025-03-25T08:40:34.269550Z",
     "shell.execute_reply": "2025-03-25T08:40:34.269177Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matrix file found: ../../input/GEO/Eczema/GSE123088/GSE123088_series_matrix.txt.gz\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene data shape: (24166, 204)\n",
      "First 20 gene/probe identifiers:\n",
      "Index(['1', '2', '3', '9', '10', '12', '13', '14', '15', '16', '18', '19',\n",
      "       '20', '21', '22', '23', '24', '25', '26', '27'],\n",
      "      dtype='object', name='ID')\n"
     ]
    }
   ],
   "source": [
    "# 1. Get the SOFT and matrix file paths again \n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "print(f\"Matrix file found: {matrix_file}\")\n",
    "\n",
    "# 2. Use the get_genetic_data function from the library to get the gene_data\n",
    "try:\n",
    "    gene_data = get_genetic_data(matrix_file)\n",
    "    print(f\"Gene data shape: {gene_data.shape}\")\n",
    "    \n",
    "    # 3. Print the first 20 row IDs (gene or probe identifiers)\n",
    "    print(\"First 20 gene/probe identifiers:\")\n",
    "    print(gene_data.index[:20])\n",
    "except Exception as e:\n",
    "    print(f\"Error extracting gene data: {e}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8529df35",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "db623bcf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:40:34.270808Z",
     "iopub.status.busy": "2025-03-25T08:40:34.270704Z",
     "iopub.status.idle": "2025-03-25T08:40:34.272513Z",
     "shell.execute_reply": "2025-03-25T08:40:34.272257Z"
    }
   },
   "outputs": [],
   "source": [
    "# These identifiers appear to be numeric IDs, not human gene symbols.\n",
    "# They are likely probe IDs or some other form of identifiers that need to be mapped.\n",
    "# Looking at the first 20 identifiers, they are simply numbers like '1', '2', '3', etc.\n",
    "# These are not standard human gene symbols, which would typically be alphanumeric like 'BRCA1', 'TP53', etc.\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58e26c6f",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "02a6fc9b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:40:34.273629Z",
     "iopub.status.busy": "2025-03-25T08:40:34.273533Z",
     "iopub.status.idle": "2025-03-25T08:40:40.135514Z",
     "shell.execute_reply": "2025-03-25T08:40:40.135140Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Gene annotation preview:\n",
      "Columns in gene annotation: ['ID', 'ENTREZ_GENE_ID', 'SPOT_ID']\n",
      "{'ID': ['1', '2', '3', '9', '10'], 'ENTREZ_GENE_ID': ['1', '2', '3', '9', '10'], 'SPOT_ID': [1.0, 2.0, 3.0, 9.0, 10.0]}\n",
      "\n",
      "Searching for platform information in SOFT file:\n",
      "Platform ID not found in first 100 lines\n",
      "\n",
      "Searching for gene symbol information in SOFT file:\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No explicit gene symbol references found in first 1000 lines\n",
      "\n",
      "Checking for additional annotation files in the directory:\n",
      "[]\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. Analyze the gene annotation dataframe to identify which columns contain the gene identifiers and gene symbols\n",
    "print(\"\\nGene annotation preview:\")\n",
    "print(f\"Columns in gene annotation: {gene_annotation.columns.tolist()}\")\n",
    "print(preview_df(gene_annotation, n=5))\n",
    "\n",
    "# Let's look for platform information in the SOFT file to understand the annotation better\n",
    "print(\"\\nSearching for platform information in SOFT file:\")\n",
    "with gzip.open(soft_file, 'rt') as f:\n",
    "    for i, line in enumerate(f):\n",
    "        if '!Series_platform_id' in line:\n",
    "            print(line.strip())\n",
    "            break\n",
    "        if i > 100:  # Limit search to first 100 lines\n",
    "            print(\"Platform ID not found in first 100 lines\")\n",
    "            break\n",
    "\n",
    "# Check if the SOFT file includes any reference to gene symbols\n",
    "print(\"\\nSearching for gene symbol information in SOFT file:\")\n",
    "with gzip.open(soft_file, 'rt') as f:\n",
    "    gene_symbol_lines = []\n",
    "    for i, line in enumerate(f):\n",
    "        if 'GENE_SYMBOL' in line or 'gene_symbol' in line.lower() or 'symbol' in line.lower():\n",
    "            gene_symbol_lines.append(line.strip())\n",
    "        if i > 1000 and len(gene_symbol_lines) > 0:  # Limit search but ensure we found something\n",
    "            break\n",
    "    \n",
    "    if gene_symbol_lines:\n",
    "        print(\"Found references to gene symbols:\")\n",
    "        for line in gene_symbol_lines[:5]:  # Show just first 5 matches\n",
    "            print(line)\n",
    "    else:\n",
    "        print(\"No explicit gene symbol references found in first 1000 lines\")\n",
    "\n",
    "# Look for alternative annotation files or references in the directory\n",
    "print(\"\\nChecking for additional annotation files in the directory:\")\n",
    "all_files = os.listdir(in_cohort_dir)\n",
    "print([f for f in all_files if 'annotation' in f.lower() or 'platform' in f.lower() or 'gpl' in f.lower()])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd741756",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "09706985",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:40:40.136702Z",
     "iopub.status.busy": "2025-03-25T08:40:40.136583Z",
     "iopub.status.idle": "2025-03-25T08:40:48.121950Z",
     "shell.execute_reply": "2025-03-25T08:40:48.121583Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Gene mapping dataframe preview:\n",
      "{'ID': ['1', '2', '3', '9', '10'], 'Gene': ['1', '2', '3', '9', '10']}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Gene expression data after mapping:\n",
      "Shape: (0, 204)\n",
      "First 10 gene identifiers: []\n",
      "Gene data saved to ../../output/preprocess/Eczema/gene_data/GSE123088.csv\n"
     ]
    }
   ],
   "source": [
    "# Looking at the annotation data, we can see it includes:\n",
    "# ID: probe identifiers that match gene_data index\n",
    "# ENTREZ_GENE_ID: Entrez Gene IDs which can serve as gene identifiers\n",
    "\n",
    "# 1. Identify the appropriate columns for mapping\n",
    "# From the preview, we can see that ID column in annotation matches the index in gene_data\n",
    "# ENTREZ_GENE_ID appears to be the closest to gene identifiers we have\n",
    "\n",
    "# Since the ENTREZ_GENE_ID is numeric, we'll check if it can be mapped to gene symbols\n",
    "# We'll use the gene_mapping function from the library with necessary columns\n",
    "mapping_df = get_gene_mapping(gene_annotation, 'ID', 'ENTREZ_GENE_ID')\n",
    "\n",
    "print(\"\\nGene mapping dataframe preview:\")\n",
    "print(preview_df(mapping_df, n=5))\n",
    "\n",
    "# 3. Apply the gene mapping to convert probe-level data to gene-level data\n",
    "gene_data = apply_gene_mapping(gene_data, mapping_df)\n",
    "\n",
    "print(\"\\nGene expression data after mapping:\")\n",
    "print(f\"Shape: {gene_data.shape}\")\n",
    "print(f\"First 10 gene identifiers: {list(gene_data.index[:10])}\")\n",
    "\n",
    "# Save the processed gene data to the output file\n",
    "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
    "gene_data.to_csv(out_gene_data_file)\n",
    "print(f\"Gene data saved to {out_gene_data_file}\")"
   ]
  }
 ],
 "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
}