File size: 39,186 Bytes
736e4a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1abb29e6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:30:53.306713Z",
     "iopub.status.busy": "2025-03-25T06:30:53.306494Z",
     "iopub.status.idle": "2025-03-25T06:30:53.470255Z",
     "shell.execute_reply": "2025-03-25T06:30:53.469862Z"
    }
   },
   "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 = \"Anorexia_Nervosa\"\n",
    "cohort = \"GSE60190\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Anorexia_Nervosa\"\n",
    "in_cohort_dir = \"../../input/GEO/Anorexia_Nervosa/GSE60190\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Anorexia_Nervosa/GSE60190.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Anorexia_Nervosa/gene_data/GSE60190.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Anorexia_Nervosa/clinical_data/GSE60190.csv\"\n",
    "json_path = \"../../output/preprocess/Anorexia_Nervosa/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6a8532d",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "934aaedd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:30:53.471751Z",
     "iopub.status.busy": "2025-03-25T06:30:53.471606Z",
     "iopub.status.idle": "2025-03-25T06:30:53.829505Z",
     "shell.execute_reply": "2025-03-25T06:30:53.828937Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Genetic Neuropathology of Obsessive Psychiatric Syndromes\"\n",
      "!Series_summary\t\"Anorexia nervosa (AN), bulimia nervosa (BN), and obsessive-compulsive disorder (OCD) are complex psychiatric disorders with shared obsessive features, thought to arise from the interaction of multiple genes of small effect with environmental factors.  Potential candidate genes for AN, BN, and OCD have been identified through clinical association and neuroimaging studies; however, recent genome-wide association studies of eating disorders (ED) so far have failed to report significant findings. Additionally, few if any studies have interrogated postmortem brain tissue for evidence of eQTLs associated with candidate genes, which has particular promise as an approach to elucidating molecular mechanisms of association. We therefore selected single nucleotide polymorphisms (SNPs) based on candidate gene studies for AN, BN, and OCD from the literature, and examined the association of these SNPs with gene expression across the lifespan in prefrontal cortex of a non-psychiatric control cohort (N=268).   Several risk-predisposing SNPs were significantly associated with gene expression among control subjects. We then measured gene expression in the prefrontal cortex of cases previously diagnosed with obsessive psychiatric disorders, e.g., eating disorders (ED; N=15), and obsessive-compulsive disorder/obsessive-compulsive personality disorder or tics (OCD/OCPD/Tic; N=16), and non-psychiatric controls (N=102) and identified 6 and 286 genes that were differentially expressed between ED compared to controls and OCD cases compared to controls, respectively (FDR < 5%). However, none of the clinical risk SNPs were among the eQTLs and none were significantly associated with gene expression within the broad obsessive cohort, suggesting larger sample sizes or other brain regions may be required to identify candidate molecular mechanisms of clinical association in postmortem brain datasets.\"\n",
      "!Series_overall_design\t\"Gene expression data from the dorsolateral prefrontal cortex (DLPFC) from postmortem tissue on 133 subjects - 15 eating disorder (ED) patients, 16 obessive compulsive disorder (OCD) patients, and 102 non-psychiatric controls - run on the Illumina HumanHT-12 v3 microarray\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['rin: 7.4', 'rin: 8.6', 'rin: 7.8', 'rin: 8.2', 'rin: 8.5', 'rin: 8.3', 'rin: 8.1', 'rin: 8.8', 'rin: 8.7', 'rin: 7.5', 'rin: 9', 'rin: 7.1', 'rin: 7.2', 'rin: 7.7', 'rin: 8.9', 'rin: 6.7', 'rin: 6', 'rin: 8.4', 'rin: 7.3', 'rin: 8', 'rin: 9.1', 'rin: 7.9', 'rin: 9.7', 'rin: 9.2', 'rin: 6.5', 'rin: 7', 'rin: 7.6', 'rin: 6.6', 'rin: 5.4', 'rin: 5.6'], 1: ['ocd: ED', 'ocd: Control', 'ocd: OCD'], 2: ['rinmatched: 1', 'rinmatched: 0'], 3: ['dx: Bipolar', 'dx: Control', 'dx: MDD', 'dx: Tics', 'dx: OCD', 'dx: ED'], 4: ['ph: 6.18', 'ph: 6.59', 'ph: 6.37', 'ph: 6.6', 'ph: 6.38', 'ph: 6.02', 'ph: 6.87', 'ph: 6.95', 'ph: 6.82', 'ph: 6.27', 'ph: 6.53', 'ph: 6.55', 'ph: 6', 'ph: 6.13', 'ph: 6.08', 'ph: 6.29', 'ph: 6.98', 'ph: 5.91', 'ph: 6.06', 'ph: 6.9', 'ph: 6.83', 'ph: 6.36', 'ph: 6.84', 'ph: 6.74', 'ph: 6.28', 'ph: 6.49', 'ph: 6.7', 'ph: 6.63', 'ph: 6.48', 'ph: 6.62'], 5: ['age: 50.421917', 'age: 27.49863', 'age: 30.627397', 'age: 61.167123', 'age: 32.69589', 'age: 39.213698', 'age: 58.605479', 'age: 49.2', 'age: 41.041095', 'age: 51.750684', 'age: 50.89863', 'age: 26.745205', 'age: 29.104109', 'age: 39.301369', 'age: 48.978082', 'age: 57.884931', 'age: 28.364383', 'age: 24.041095', 'age: 19.268493', 'age: 27.230136', 'age: 46.605479', 'age: 23.443835', 'age: 51.038356', 'age: 39.663013', 'age: 46.109589', 'age: 77.989041', 'age: 46.967123', 'age: 63.241095', 'age: 62.306849', 'age: 83.641095'], 6: ['pmi: 27', 'pmi: 19.5', 'pmi: 71.5', 'pmi: 22.5', 'pmi: 64', 'pmi: 28', 'pmi: 18', 'pmi: 29', 'pmi: 49', 'pmi: 13', 'pmi: 26.5', 'pmi: 16.5', 'pmi: 35', 'pmi: 19', 'pmi: 20.5', 'pmi: 9.5', 'pmi: 65.5', 'pmi: 68', 'pmi: 17.5', 'pmi: 44', 'pmi: 34', 'pmi: 21.5', 'pmi: 67.5', 'pmi: 26', 'pmi: 46.5', 'pmi: 33.5', 'pmi: 24.5', 'pmi: 30.5', 'pmi: 29.5', 'pmi: 51.5'], 7: ['Sex: F', 'Sex: M'], 8: ['race: CAUC'], 9: ['batch1: 16', 'batch1: 18', 'batch1: 19', 'batch1: 20', 'batch1: 21', 'batch1: 9', 'batch1: 10', 'batch1: 12', 'batch1: 14', 'batch1: 23', 'batch1: 24', 'batch1: 25', 'batch1: 26', 'batch1: 27', 'batch1: 29', 'batch1: 33', 'batch1: 32', 'batch1: 31', 'batch1: 36', 'batch1: 37', 'batch1: 38', 'batch1: 39', 'batch1: 40', 'batch1: 41', 'batch1: 42', 'batch1: 44', 'batch1: 45', 'batch1: 48', 'batch1: 53', 'batch1: 59']}\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": "9c7086cc",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c3c94d3c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:30:53.830965Z",
     "iopub.status.busy": "2025-03-25T06:30:53.830854Z",
     "iopub.status.idle": "2025-03-25T06:30:53.859085Z",
     "shell.execute_reply": "2025-03-25T06:30:53.858635Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preview of clinical features:\n",
      "{'GSM1467273': [0.0, 50.421917, 0.0], 'GSM1467274': [0.0, 27.49863, 1.0], 'GSM1467275': [0.0, 30.627397, 1.0], 'GSM1467276': [0.0, 61.167123, 1.0], 'GSM1467277': [0.0, 32.69589, 1.0], 'GSM1467278': [0.0, 39.213698, 0.0], 'GSM1467279': [0.0, 58.605479, 1.0], 'GSM1467280': [0.0, 49.2, 1.0], 'GSM1467281': [0.0, 41.041095, 1.0], 'GSM1467282': [0.0, 51.750684, 1.0], 'GSM1467283': [0.0, 50.89863, 1.0], 'GSM1467284': [0.0, 26.745205, 1.0], 'GSM1467285': [0.0, 29.104109, 1.0], 'GSM1467286': [0.0, 39.301369, 0.0], 'GSM1467287': [0.0, 48.978082, 1.0], 'GSM1467288': [0.0, 57.884931, 1.0], 'GSM1467289': [0.0, 28.364383, 1.0], 'GSM1467290': [0.0, 24.041095, 1.0], 'GSM1467291': [0.0, 19.268493, 0.0], 'GSM1467292': [0.0, 27.230136, 0.0], 'GSM1467293': [0.0, 46.605479, 1.0], 'GSM1467294': [0.0, 23.443835, 0.0], 'GSM1467295': [0.0, 51.038356, 1.0], 'GSM1467296': [0.0, 39.663013, 1.0], 'GSM1467297': [0.0, 46.109589, 1.0], 'GSM1467298': [0.0, 77.989041, 1.0], 'GSM1467299': [0.0, 46.967123, 1.0], 'GSM1467300': [0.0, 63.241095, 1.0], 'GSM1467301': [0.0, 62.306849, 1.0], 'GSM1467302': [0.0, 83.641095, 0.0], 'GSM1467303': [0.0, 42.838356, 1.0], 'GSM1467304': [0.0, 51.386301, 1.0], 'GSM1467305': [0.0, 66.715068, 0.0], 'GSM1467306': [0.0, 51.939726, 0.0], 'GSM1467307': [0.0, 34.339726, 1.0], 'GSM1467308': [0.0, 50.109589, 1.0], 'GSM1467309': [0.0, 18.758904, 0.0], 'GSM1467310': [0.0, 16.649315, 0.0], 'GSM1467311': [0.0, 16.353424, 1.0], 'GSM1467312': [0.0, 42.065753, 1.0], 'GSM1467313': [0.0, 16.726027, 0.0], 'GSM1467314': [0.0, 34.465753, 1.0], 'GSM1467315': [0.0, 34.254794, 1.0], 'GSM1467316': [0.0, 47.484931, 1.0], 'GSM1467317': [0.0, 43.756164, 1.0], 'GSM1467318': [0.0, 49.210958, 1.0], 'GSM1467319': [0.0, 57.482191, 0.0], 'GSM1467320': [0.0, 46.561643, 1.0], 'GSM1467321': [0.0, 49.561643, 1.0], 'GSM1467322': [0.0, 28.589041, 1.0], 'GSM1467323': [0.0, 38.410958, 0.0], 'GSM1467324': [0.0, 30.032876, 1.0], 'GSM1467325': [0.0, 56.09041, 1.0], 'GSM1467326': [0.0, 46.915068, 1.0], 'GSM1467327': [0.0, 49.021917, 0.0], 'GSM1467328': [0.0, 71.109589, 0.0], 'GSM1467329': [0.0, 17.235616, 0.0], 'GSM1467330': [0.0, 16.583561, 1.0], 'GSM1467331': [0.0, 16.934246, 1.0], 'GSM1467332': [0.0, 16.8, 1.0], 'GSM1467333': [0.0, 18.117808, 1.0], 'GSM1467334': [0.0, 18.660273, 1.0], 'GSM1467335': [0.0, 16.69589, 0.0], 'GSM1467336': [0.0, 75.572602, 0.0], 'GSM1467337': [0.0, 59.260273, 0.0], 'GSM1467338': [0.0, 55.545205, 1.0], 'GSM1467339': [0.0, 41.778082, 1.0], 'GSM1467340': [0.0, 57.454794, 1.0], 'GSM1467341': [0.0, 45.284931, 1.0], 'GSM1467342': [0.0, 56.304109, 0.0], 'GSM1467343': [0.0, 39.654794, 0.0], 'GSM1467344': [0.0, 55.945205, 1.0], 'GSM1467345': [0.0, 38.232876, 1.0], 'GSM1467346': [0.0, 58.109589, 1.0], 'GSM1467347': [0.0, 40.021917, 1.0], 'GSM1467348': [0.0, 50.504109, 1.0], 'GSM1467349': [0.0, 36.550684, 1.0], 'GSM1467350': [0.0, 45.117808, 1.0], 'GSM1467351': [0.0, 83.545205, 1.0], 'GSM1467352': [0.0, 18.786301, 1.0], 'GSM1467353': [0.0, 48.567123, 0.0], 'GSM1467354': [0.0, 38.331506, 0.0], 'GSM1467355': [0.0, 48.101369, 1.0], 'GSM1467356': [0.0, 18.39452, 1.0], 'GSM1467357': [0.0, 60.843835, 1.0], 'GSM1467358': [0.0, 61.372602, 1.0], 'GSM1467359': [0.0, 52.038356, 1.0], 'GSM1467360': [0.0, 59.254794, 1.0], 'GSM1467361': [1.0, 41.567123, 0.0], 'GSM1467362': [0.0, 50.358904, 1.0], 'GSM1467363': [0.0, 31.558904, 1.0], 'GSM1467364': [0.0, 45.701369, 0.0], 'GSM1467365': [0.0, 44.731506, 1.0], 'GSM1467366': [0.0, 34.39726, 0.0], 'GSM1467367': [1.0, 31.613698, 0.0], 'GSM1467368': [0.0, 54.846575, 1.0], 'GSM1467369': [0.0, 84.057534, 0.0], 'GSM1467370': [0.0, 66.79452, 0.0], 'GSM1467371': [0.0, 53.323287, 1.0], 'GSM1467372': [0.0, 30.043835, 0.0], 'GSM1467373': [0.0, 55.435616, 1.0], 'GSM1467374': [0.0, 45.676712, 1.0], 'GSM1467375': [0.0, 54.334246, 1.0], 'GSM1467376': [0.0, 63.558904, 1.0], 'GSM1467377': [0.0, 45.224657, 0.0], 'GSM1467378': [0.0, 23.69589, 1.0], 'GSM1467379': [0.0, 67.865753, 1.0], 'GSM1467380': [0.0, 16.753424, 1.0], 'GSM1467381': [0.0, 18.424657, 1.0], 'GSM1467382': [0.0, 17.09041, 0.0], 'GSM1467383': [0.0, 16.183561, 1.0], 'GSM1467384': [0.0, 33.260273, 1.0], 'GSM1467385': [0.0, 54.424657, 1.0], 'GSM1467386': [0.0, 45.378082, 1.0], 'GSM1467387': [0.0, 52.523287, 1.0], 'GSM1467388': [0.0, 35.273972, 1.0], 'GSM1467389': [0.0, 22.630136, 1.0], 'GSM1467390': [0.0, 20.863013, 1.0], 'GSM1467391': [0.0, 26.531506, 0.0], 'GSM1467392': [0.0, 24.627397, 1.0], 'GSM1467393': [0.0, 53.978082, 1.0], 'GSM1467394': [0.0, 34.961643, 1.0], 'GSM1467395': [0.0, 18.731506, 1.0], 'GSM1467396': [1.0, 30.726027, 0.0], 'GSM1467397': [0.0, 63.471232, 1.0], 'GSM1467398': [0.0, 54.808219, 1.0], 'GSM1467399': [0.0, 57.512328, 1.0], 'GSM1467400': [0.0, 57.610958, 1.0], 'GSM1467401': [0.0, 44.958904, 1.0], 'GSM1467402': [0.0, 35.684931, 0.0], 'GSM1467403': [0.0, 63.0, 1.0], 'GSM1467404': [0.0, 38.780821, 1.0], 'GSM1467405': [0.0, 45.978082, 1.0]}\n",
      "Clinical features saved to ../../output/preprocess/Anorexia_Nervosa/clinical_data/GSE60190.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Gene Expression Data Availability\n",
    "# Based on the background information, this dataset contains gene expression data from DLPFC tissue\n",
    "# run on the Illumina HumanHT-12 v3 microarray, which is gene expression data.\n",
    "is_gene_available = True\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "\n",
    "# 2.1 Trait Data (Anorexia Nervosa)\n",
    "# Looking at the sample characteristics, we can see that row 3 contains diagnoses information\n",
    "# and row 1 contains OCD status which includes \"ED\" (eating disorder)\n",
    "# Since our trait is Anorexia_Nervosa, and the dataset mentions ED (eating disorders)\n",
    "# we'll use row 3 (dx field) which has more specific diagnostic categories\n",
    "trait_row = 3\n",
    "\n",
    "# 2.2 Age Data\n",
    "# Row 5 contains age information\n",
    "age_row = 5\n",
    "\n",
    "# 2.3 Gender Data\n",
    "# Row 7 contains Sex information\n",
    "gender_row = 7\n",
    "\n",
    "# 3. Define conversion functions\n",
    "\n",
    "def convert_trait(value):\n",
    "    \"\"\"Convert trait value to binary format (0=Control, 1=Anorexia_Nervosa)\"\"\"\n",
    "    if value is None:\n",
    "        return None\n",
    "    \n",
    "    # Extract value after colon if present\n",
    "    if ':' in value:\n",
    "        value = value.split(':', 1)[1].strip()\n",
    "    \n",
    "    # Check if the value indicates Anorexia Nervosa\n",
    "    # From the sample characteristics, 'ED' refers to eating disorder patients\n",
    "    if value == 'ED':\n",
    "        return 1\n",
    "    elif value == 'Control':\n",
    "        return 0\n",
    "    else:\n",
    "        # Other diagnoses are not our target trait\n",
    "        return 0\n",
    "\n",
    "def convert_age(value):\n",
    "    \"\"\"Convert age value to continuous format\"\"\"\n",
    "    if value is None:\n",
    "        return None\n",
    "    \n",
    "    # Extract value after colon if present\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",
    "    \"\"\"Convert gender value to binary format (0=Female, 1=Male)\"\"\"\n",
    "    if value is None:\n",
    "        return None\n",
    "    \n",
    "    # Extract value after colon if present\n",
    "    if ':' in value:\n",
    "        value = value.split(':', 1)[1].strip()\n",
    "    \n",
    "    if value == 'F':\n",
    "        return 0\n",
    "    elif value == 'M':\n",
    "        return 1\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "# 3. Save Metadata\n",
    "# Determine trait data availability\n",
    "is_trait_available = trait_row is not None\n",
    "\n",
    "# Initial filtering on the usability of the dataset\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\n",
    "if trait_row is not None:\n",
    "    # Extract clinical features\n",
    "    clinical_features_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 clinical features dataframe\n",
    "    preview = preview_df(clinical_features_df)\n",
    "    print(\"Preview of clinical features:\")\n",
    "    print(preview)\n",
    "    \n",
    "    # Create directory if it doesn't exist\n",
    "    os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "    \n",
    "    # Save clinical features as CSV\n",
    "    clinical_features_df.to_csv(out_clinical_data_file)\n",
    "    print(f\"Clinical features saved to {out_clinical_data_file}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa934a62",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1f830a02",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:30:53.860250Z",
     "iopub.status.busy": "2025-03-25T06:30:53.860144Z",
     "iopub.status.idle": "2025-03-25T06:30:54.493483Z",
     "shell.execute_reply": "2025-03-25T06:30:54.492840Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matrix file found: ../../input/GEO/Anorexia_Nervosa/GSE60190/GSE60190_series_matrix.txt.gz\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene data shape: (48679, 133)\n",
      "First 20 gene/probe identifiers:\n",
      "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. 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": "4c7ef891",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2ef8ad48",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:30:54.494837Z",
     "iopub.status.busy": "2025-03-25T06:30:54.494715Z",
     "iopub.status.idle": "2025-03-25T06:30:54.497141Z",
     "shell.execute_reply": "2025-03-25T06:30:54.496692Z"
    }
   },
   "outputs": [],
   "source": [
    "# Based on the gene identifiers observed in the gene expression data, \n",
    "# these are Illumina probe IDs (starting with ILMN_) rather than standard human gene symbols.\n",
    "# Illumina probe IDs need to be mapped to gene symbols for proper analysis.\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13ae29a8",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "602100c6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:30:54.498341Z",
     "iopub.status.busy": "2025-03-25T06:30:54.498234Z",
     "iopub.status.idle": "2025-03-25T06:31:11.923921Z",
     "shell.execute_reply": "2025-03-25T06:31:11.923469Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Platform title found: Illumina HumanHT-12 V3.0 expression beadchip\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Gene annotation preview:\n",
      "{'ID': ['ILMN_1725881', 'ILMN_1910180', 'ILMN_1804174', 'ILMN_1796063', 'ILMN_1811966', 'ILMN_1668162', 'ILMN_1715600', 'ILMN_1912287', 'ILMN_1793729', 'ILMN_1889125'], 'nuID': ['rp13_p1x6D80lNLk3c', 'NEX0oqCV8.er4HVfU4', 'KyqQynMZxJcruyylEU', 'xXl7eXuF7sbPEp.KFI', '9ckqJrioiaej9_ajeQ', 'rtCnUep15THUpc_0e4', 'QtVBXBWhekTEIT0kjo', 'EuUnlPkeXRP9fyO.iQ', '0flyIEROp.olYSF6n4', 'fegCQD_j_69DUU38dI'], 'Species': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Source': ['RefSeq', 'Unigene', 'RefSeq', 'RefSeq', 'RefSeq', 'RefSeq', 'RefSeq', 'Unigene', 'RefSeq', 'Unigene'], 'Search_Key': ['ILMN_44919', 'ILMN_127219', 'ILMN_139282', 'ILMN_5006', 'ILMN_38756', 'ILMN_7652', 'ILMN_35097', 'ILMN_77451', 'ILMN_18382', 'ILMN_108888'], 'Transcript': ['ILMN_44919', 'ILMN_127219', 'ILMN_139282', 'ILMN_5006', 'ILMN_38756', 'ILMN_7652', 'ILMN_35097', 'ILMN_77451', 'ILMN_18382', 'ILMN_108888'], 'ILMN_Gene': ['LOC23117', 'HS.575038', 'FCGR2B', 'TRIM44', 'LOC653895', 'DGAT2L3', 'LOC387701', 'HS.133181', 'C15ORF39', 'HS.545755'], 'Source_Reference_ID': ['XM_933824.1', 'Hs.575038', 'XM_938851.1', 'NM_017583.3', 'XM_936379.1', 'NM_001013579.1', 'XM_373469.3', 'Hs.133181', 'NM_015492.4', 'Hs.545755'], 'RefSeq_ID': ['XM_933824.1', nan, 'XM_938851.1', 'NM_017583.3', 'XM_936379.1', 'NM_001013579.1', 'XM_373469.3', nan, 'NM_015492.4', nan], 'Unigene_ID': [nan, 'Hs.575038', nan, nan, nan, nan, nan, 'Hs.133181', nan, 'Hs.545755'], 'Entrez_Gene_ID': [23117.0, nan, 2213.0, 54765.0, 653895.0, 158833.0, 387701.0, nan, 56905.0, nan], 'GI': [89040007.0, 10437021.0, 88952550.0, 29029528.0, 89033487.0, 61888901.0, 89031576.0, 27826545.0, 153251858.0, 1999235.0], 'Accession': ['XM_933824.1', 'AK024680', 'XM_938851.1', 'NM_017583.3', 'XM_936379.1', 'NM_001013579.1', 'XM_373469.3', 'BX093329', 'NM_015492.4', 'AA346998'], 'Symbol': ['LOC23117', nan, 'FCGR2B', 'TRIM44', 'LOC653895', 'DGAT2L3', 'LOC387701', nan, 'C15orf39', nan], 'Protein_Product': ['XP_938917.1', nan, 'XP_943944.1', 'NP_060053.2', 'XP_941472.1', 'NP_001013597.1', 'XP_373469.1', nan, 'NP_056307.2', nan], 'Array_Address_Id': [1710221.0, 5900364.0, 2480717.0, 1300239.0, 4480719.0, 6020725.0, 3870215.0, 2710020.0, 870110.0, 290020.0], 'Probe_Type': ['I', 'S', 'I', 'S', 'S', 'S', 'A', 'S', 'S', 'S'], 'Probe_Start': [122.0, 1409.0, 1643.0, 2901.0, 25.0, 782.0, 301.0, 324.0, 3585.0, 139.0], 'SEQUENCE': ['GGCTCCTCTTTGGGCTCCTACTGGAATTTATCAGCCATCAGTGCATCTCT', 'ACACCTTCAGGAGGGAAGCCCTTATTTCTGGGTTGAACTCCCCTTCCATG', 'TAGGGGCAATAGGCTATACGCTACAGCCTAGGTGTGTAGTAGGCCACACC', 'CCTGCCTGTCTGCCTGTGACCTGTGTACGTATTACAGGCTTTAGGACCAG', 'CTAGCAGGGAGCGGTGAGGGAGAGCGGCTGGATTTCTTGCGGGATCTGCA', 'GTCAAGGCTCCACTGGGCTCCTGCCATACTCCAGGCCTATTGTCACTGTG', 'GTCCCCAACCCTAACCCGGACCTGGCACATACAAGACATTCAGCAGATGG', 'GTGCCAGCTGCCATTGCACTGCCTCACATTTTCCTTTAGATGTTTGAGCA', 'CTTGCCTAGAGAACACACATGGGCTTTGGAGCCCGACAGACCTGGGCTTG', 'CTGGAAAAGCAAAATTTGGATTTGTGGTTCAATCCACCATCTTTACTCAG'], 'Chromosome': ['16', nan, nan, '11', nan, 'X', '10', nan, '15', nan], 'Probe_Chr_Orientation': ['-', nan, nan, '+', nan, '+', '+', nan, '+', nan], 'Probe_Coordinates': ['21766363-21766363:21769901-21769949', nan, nan, '35786070-35786119', nan, '63280932-63280981', '92811754-92811767:92811768-92811803', nan, '73290721-73290770', nan], 'Cytoband': ['16p12.2a', nan, '1q23.3b', '11p13a', '10q11.23b', 'Xq13.1b', nan, nan, '15q24.2a', nan], 'Definition': ['PREDICTED: Homo sapiens KIAA0220-like protein, transcript variant 11 (LOC23117), mRNA.', 'Homo sapiens cDNA: FLJ21027 fis, clone CAE07110', 'PREDICTED: Homo sapiens Fc fragment of IgG, low affinity IIb, receptor (CD32) (FCGR2B), mRNA.', 'Homo sapiens tripartite motif-containing 44 (TRIM44), mRNA.', 'PREDICTED: Homo sapiens similar to protein geranylgeranyltransferase type I, beta subunit (LOC653895), mRNA.', 'Homo sapiens diacylglycerol O-acyltransferase 2-like 3 (DGAT2L3), mRNA.', 'PREDICTED: Homo sapiens hypothetical LOC387701 (LOC387701), mRNA.', 'BX093329 Soares_parathyroid_tumor_NbHPA Homo sapiens cDNA clone IMAGp998A124183 ; IMAGE:1648403, mRNA sequence', 'Homo sapiens chromosome 15 open reading frame 39 (C15orf39), mRNA.', 'EST53225 Fetal heart II Homo sapiens cDNA 3 end, mRNA sequence'], 'Ontology_Component': [nan, nan, nan, 'intracellular [goid 5622] [evidence IEA]', nan, 'membrane [goid 16020] [evidence IEA]; integral to membrane [goid 16021] [evidence IEA]; endoplasmic reticulum [goid 5783] [evidence IEA]', nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan, 'lipid biosynthesis [goid 8610] [evidence IEA]; lipid metabolism [goid 6629] [evidence IEA]', nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, 'zinc ion binding [goid 8270] [evidence IEA]; metal ion binding [goid 46872] [evidence IEA]', nan, 'acyltransferase activity [goid 8415] [evidence IEA]; transferase activity [goid 16740] [evidence IEA]', nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, 'MGC3490; MC7; HSA249128; DIPB', nan, 'AWAT1; DGA2', nan, nan, 'DKFZP434H132; FLJ46337; MGC117209', nan], 'Obsolete_Probe_Id': [nan, nan, nan, 'MGC3490; MC7; HSA249128; DIPB', nan, 'AWAT1; DGA2', nan, nan, 'DKFZP434H132; FLJ46337; MGC117209', nan], 'GB_ACC': ['XM_933824.1', 'AK024680', 'XM_938851.1', 'NM_017583.3', 'XM_936379.1', 'NM_001013579.1', 'XM_373469.3', 'BX093329', 'NM_015492.4', 'AA346998']}\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",
    "# Check if there are any platforms defined in the SOFT file that might contain annotation data\n",
    "with gzip.open(soft_file, 'rt') as f:\n",
    "    soft_content = f.read()\n",
    "\n",
    "# Look for platform sections in the SOFT file\n",
    "platform_sections = re.findall(r'^!Platform_title\\s*=\\s*(.+)$', soft_content, re.MULTILINE)\n",
    "if platform_sections:\n",
    "    print(f\"Platform title found: {platform_sections[0]}\")\n",
    "\n",
    "# Try to extract more annotation data by reading directly from the SOFT file\n",
    "# Look for lines that might contain gene symbol mappings\n",
    "symbol_pattern = re.compile(r'ID_REF\\s+Symbol|ID\\s+Gene Symbol', re.IGNORECASE)\n",
    "annotation_lines = []\n",
    "with gzip.open(soft_file, 'rt') as f:\n",
    "    for line in f:\n",
    "        if symbol_pattern.search(line):\n",
    "            annotation_lines.append(line)\n",
    "            # Collect the next few lines to see the annotation structure\n",
    "            for _ in range(10):\n",
    "                annotation_lines.append(next(f, ''))\n",
    "\n",
    "if annotation_lines:\n",
    "    print(\"Found potential gene symbol mappings:\")\n",
    "    for line in annotation_lines:\n",
    "        print(line.strip())\n",
    "\n",
    "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n",
    "print(\"\\nGene annotation preview:\")\n",
    "print(preview_df(gene_annotation, n=10))\n",
    "\n",
    "# If we need an alternative source of mapping, check if there are any other annotation files in the cohort directory\n",
    "cohort_files = os.listdir(in_cohort_dir)\n",
    "annotation_files = [f for f in cohort_files if 'annotation' in f.lower() or 'platform' in f.lower()]\n",
    "if annotation_files:\n",
    "    print(\"\\nAdditional annotation files found in the cohort directory:\")\n",
    "    for file in annotation_files:\n",
    "        print(file)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65852f68",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7763dede",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:31:11.925251Z",
     "iopub.status.busy": "2025-03-25T06:31:11.925129Z",
     "iopub.status.idle": "2025-03-25T06:31:14.081426Z",
     "shell.execute_reply": "2025-03-25T06:31:14.080964Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene mapping dataframe shape: (36157, 2)\n",
      "First 10 rows of gene mapping:\n",
      "              ID       Gene\n",
      "0   ILMN_1725881   LOC23117\n",
      "2   ILMN_1804174     FCGR2B\n",
      "3   ILMN_1796063     TRIM44\n",
      "4   ILMN_1811966  LOC653895\n",
      "5   ILMN_1668162    DGAT2L3\n",
      "6   ILMN_1715600  LOC387701\n",
      "8   ILMN_1793729   C15orf39\n",
      "10  ILMN_2296644    PCDHGA9\n",
      "11  ILMN_1711283    PCDHGA9\n",
      "12  ILMN_1682799   STAMBPL1\n",
      "Gene expression data shape after mapping: (19091, 133)\n",
      "First 10 gene symbols after mapping:\n",
      "Index(['A1BG', 'A1CF', 'A26A1', 'A26B1', 'A26C1B', 'A26C3', 'A2BP1', 'A2M',\n",
      "       'A2ML1', 'A3GALT2'],\n",
      "      dtype='object', name='Gene')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene expression data shape after normalizing symbols: (18298, 133)\n",
      "First 10 normalized gene symbols:\n",
      "Index(['A1BG', 'A1CF', 'A2M', 'A2ML1', 'A3GALT2', 'A4GALT', 'A4GNT', 'AAA1',\n",
      "       'AAAS', 'AACS'],\n",
      "      dtype='object', name='Gene')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene expression data saved to ../../output/preprocess/Anorexia_Nervosa/gene_data/GSE60190.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Observe the gene identifiers and determine the appropriate columns\n",
    "# From the preview, we can see the gene expression data uses 'ILMN_XXXXX' identifiers (Illumina probe IDs)\n",
    "# In the annotation dataframe, the 'ID' column contains these same Illumina probe IDs\n",
    "# The 'Symbol' column contains the gene symbols we need to map to\n",
    "\n",
    "# 2. Get a gene mapping dataframe by extracting the relevant columns\n",
    "# Use the get_gene_mapping function to create a mapping dataframe\n",
    "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n",
    "print(f\"Gene mapping dataframe shape: {gene_mapping.shape}\")\n",
    "print(\"First 10 rows of gene mapping:\")\n",
    "print(gene_mapping.head(10))\n",
    "\n",
    "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n",
    "# This handles probes mapping to multiple genes and genes with multiple probes\n",
    "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n",
    "print(f\"Gene expression data shape after mapping: {gene_data.shape}\")\n",
    "print(\"First 10 gene symbols after mapping:\")\n",
    "print(gene_data.index[:10])\n",
    "\n",
    "# Normalize gene symbols to ensure consistency across datasets\n",
    "gene_data = normalize_gene_symbols_in_index(gene_data)\n",
    "print(f\"Gene expression data shape after normalizing symbols: {gene_data.shape}\")\n",
    "print(\"First 10 normalized gene symbols:\")\n",
    "print(gene_data.index[:10])\n",
    "\n",
    "# Save the gene expression data\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 expression data saved to {out_gene_data_file}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85e8e14e",
   "metadata": {},
   "source": [
    "### Step 7: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6310ce66",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:31:14.082688Z",
     "iopub.status.busy": "2025-03-25T06:31:14.082569Z",
     "iopub.status.idle": "2025-03-25T06:31:19.170808Z",
     "shell.execute_reply": "2025-03-25T06:31:19.170355Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene data already normalized and saved to ../../output/preprocess/Anorexia_Nervosa/gene_data/GSE60190.csv\n",
      "Loaded clinical data shape: (3, 133)\n",
      "                  GSM1467273  GSM1467274  GSM1467275  GSM1467276  GSM1467277  \\\n",
      "Anorexia_Nervosa    0.000000     0.00000    0.000000    0.000000     0.00000   \n",
      "Age                50.421917    27.49863   30.627397   61.167123    32.69589   \n",
      "Gender              0.000000     1.00000    1.000000    1.000000     1.00000   \n",
      "\n",
      "                  GSM1467278  GSM1467279  GSM1467280  GSM1467281  GSM1467282  \\\n",
      "Anorexia_Nervosa    0.000000    0.000000         0.0    0.000000    0.000000   \n",
      "Age                39.213698   58.605479        49.2   41.041095   51.750684   \n",
      "Gender              0.000000    1.000000         1.0    1.000000    1.000000   \n",
      "\n",
      "                  ...  GSM1467396  GSM1467397  GSM1467398  GSM1467399  \\\n",
      "Anorexia_Nervosa  ...    1.000000    0.000000    0.000000    0.000000   \n",
      "Age               ...   30.726027   63.471232   54.808219   57.512328   \n",
      "Gender            ...    0.000000    1.000000    1.000000    1.000000   \n",
      "\n",
      "                  GSM1467400  GSM1467401  GSM1467402  GSM1467403  GSM1467404  \\\n",
      "Anorexia_Nervosa    0.000000    0.000000    0.000000         0.0    0.000000   \n",
      "Age                57.610958   44.958904   35.684931        63.0   38.780821   \n",
      "Gender              1.000000    1.000000    0.000000         1.0    1.000000   \n",
      "\n",
      "                  GSM1467405  \n",
      "Anorexia_Nervosa    0.000000  \n",
      "Age                45.978082  \n",
      "Gender              1.000000  \n",
      "\n",
      "[3 rows x 133 columns]\n",
      "Linked data shape: (133, 18301)\n",
      "Linked data preview (first 5 rows, 5 columns):\n",
      "            Anorexia_Nervosa        Age  Gender       A1BG       A1CF\n",
      "GSM1467273               0.0  50.421917     0.0  14.962093  22.217464\n",
      "GSM1467274               0.0  27.498630     1.0  14.918877  22.188462\n",
      "GSM1467275               0.0  30.627397     1.0  14.925598  22.199076\n",
      "GSM1467276               0.0  61.167123     1.0  14.907169  22.233160\n",
      "GSM1467277               0.0  32.695890     1.0  14.868744  22.283053\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data shape after handling missing values: (133, 18301)\n",
      "For the feature 'Anorexia_Nervosa', the least common label is '1.0' with 3 occurrences. This represents 2.26% of the dataset.\n",
      "The distribution of the feature 'Anorexia_Nervosa' in this dataset is severely biased.\n",
      "\n",
      "Quartiles for 'Age':\n",
      "  25%: 30.043835\n",
      "  50% (Median): 45.224657\n",
      "  75%: 54.424657\n",
      "Min: 16.183561\n",
      "Max: 84.057534\n",
      "The distribution of the feature 'Age' in this dataset is fine.\n",
      "\n",
      "For the feature 'Gender', the least common label is '0.0' with 36 occurrences. This represents 27.07% of the dataset.\n",
      "The distribution of the feature 'Gender' in this dataset is fine.\n",
      "\n",
      "A new JSON file was created at: ../../output/preprocess/Anorexia_Nervosa/cohort_info.json\n",
      "Dataset is not usable for analysis. No linked data file saved.\n"
     ]
    }
   ],
   "source": [
    "# 1. Normalize gene symbols in the gene expression data - we already did this in step 6\n",
    "# No need to do it again\n",
    "print(f\"Gene data already normalized and saved to {out_gene_data_file}\")\n",
    "\n",
    "# 2. Load clinical data that was already saved in Step 2\n",
    "clinical_data_df = pd.read_csv(out_clinical_data_file, index_col=0)\n",
    "print(f\"Loaded clinical data shape: {clinical_data_df.shape}\")\n",
    "print(clinical_data_df.head())\n",
    "\n",
    "# 2. Link clinical and genetic data\n",
    "linked_data = geo_link_clinical_genetic_data(clinical_data_df, gene_data)\n",
    "print(f\"Linked data shape: {linked_data.shape}\")\n",
    "print(\"Linked data preview (first 5 rows, 5 columns):\")\n",
    "print(linked_data.iloc[:5, :5] if not linked_data.empty else \"Linked data is empty\")\n",
    "\n",
    "# 3. Handle missing values\n",
    "linked_data = handle_missing_values(linked_data, trait)\n",
    "print(f\"Data shape after handling missing values: {linked_data.shape}\")\n",
    "\n",
    "# 4. Check for bias in features\n",
    "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n",
    "\n",
    "# 5. Validate and save 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_biased,\n",
    "    df=linked_data,\n",
    "    note=\"Dataset contains gene expression data from dorsolateral prefrontal cortex of postmortem tissue with Eating Disorders.\"\n",
    ")\n",
    "\n",
    "# 6. Save the linked data if usable\n",
    "if is_usable:\n",
    "    os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n",
    "    linked_data.to_csv(out_data_file)\n",
    "    print(f\"Linked data saved to {out_data_file}\")\n",
    "else:\n",
    "    print(\"Dataset is not usable for analysis. No linked data file saved.\")"
   ]
  }
 ],
 "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
}