File size: 37,849 Bytes
82732bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9963c90d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T03:51:37.067266Z",
     "iopub.status.busy": "2025-03-25T03:51:37.067161Z",
     "iopub.status.idle": "2025-03-25T03:51:37.258251Z",
     "shell.execute_reply": "2025-03-25T03:51:37.257806Z"
    }
   },
   "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 = \"Rheumatoid_Arthritis\"\n",
    "cohort = \"GSE224842\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Rheumatoid_Arthritis\"\n",
    "in_cohort_dir = \"../../input/GEO/Rheumatoid_Arthritis/GSE224842\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Rheumatoid_Arthritis/GSE224842.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Rheumatoid_Arthritis/gene_data/GSE224842.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Rheumatoid_Arthritis/clinical_data/GSE224842.csv\"\n",
    "json_path = \"../../output/preprocess/Rheumatoid_Arthritis/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fba5089",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f7f7d0ab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T03:51:37.259891Z",
     "iopub.status.busy": "2025-03-25T03:51:37.259706Z",
     "iopub.status.idle": "2025-03-25T03:51:37.365669Z",
     "shell.execute_reply": "2025-03-25T03:51:37.365369Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Gene expression profiles of peripheral blood mononuclear cells before abatacept treatment in rheumatoid arthritis patients.\"\n",
      "!Series_summary\t\"To explore markers which predict the efficacy of abatacept in rheumatoid arthritis, peripheral blood mononuclear cells were obtained before abatacept treatment.\"\n",
      "!Series_overall_design\t\"30 rheumatoid arthritis patients receiving abatacept were participated in the study. Blood samples were obtained before the initiation of abatacept treatment. Density-gradient separeted peripheral blood mononuclear cells were subjected the DNA microarray analyses.\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['disease state: rheumatoid arthritis'], 1: ['cell type: PBMC']}\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": "c2128886",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1fc72b76",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T03:51:37.366956Z",
     "iopub.status.busy": "2025-03-25T03:51:37.366853Z",
     "iopub.status.idle": "2025-03-25T03:51:37.373632Z",
     "shell.execute_reply": "2025-03-25T03:51:37.373344Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preview of clinical features:\n",
      "{'GSM7034090': [1.0], 'GSM7034091': [1.0], 'GSM7034092': [1.0], 'GSM7034093': [1.0], 'GSM7034094': [1.0], 'GSM7034095': [1.0], 'GSM7034096': [1.0], 'GSM7034097': [1.0], 'GSM7034098': [1.0], 'GSM7034099': [1.0], 'GSM7034100': [1.0], 'GSM7034101': [1.0], 'GSM7034102': [1.0], 'GSM7034103': [1.0], 'GSM7034104': [1.0], 'GSM7034105': [1.0], 'GSM7034106': [1.0], 'GSM7034107': [1.0], 'GSM7034108': [1.0], 'GSM7034109': [1.0], 'GSM7034110': [1.0], 'GSM7034111': [1.0], 'GSM7034112': [1.0], 'GSM7034113': [1.0], 'GSM7034114': [1.0], 'GSM7034115': [1.0], 'GSM7034116': [1.0], 'GSM7034117': [1.0], 'GSM7034118': [1.0], 'GSM7034119': [1.0]}\n",
      "Clinical features saved to ../../output/preprocess/Rheumatoid_Arthritis/clinical_data/GSE224842.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Gene Expression Data Availability\n",
    "# Based on the background information, this dataset contains gene expression profiles of PBMCs\n",
    "# The title mentions \"Gene expression profiles\" and the overall design mentions \"DNA microarray analyses\"\n",
    "is_gene_available = True\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "# 2.1 Data Availability\n",
    "# Looking at the sample characteristics dictionary\n",
    "# For trait: All samples have \"rheumatoid arthritis\" (row 0)\n",
    "trait_row = 0\n",
    "\n",
    "# For age: Not explicitly mentioned in the sample characteristics\n",
    "age_row = None\n",
    "\n",
    "# For gender: Not explicitly mentioned in the sample characteristics\n",
    "gender_row = None\n",
    "\n",
    "# 2.2 Data Type Conversion\n",
    "# For trait: Convert to binary (1 for RA, 0 for control)\n",
    "# But all samples in this dataset have RA (no controls), so it'll be constant\n",
    "def convert_trait(value):\n",
    "    if not value or not isinstance(value, str):\n",
    "        return None\n",
    "    \n",
    "    # Extract value after colon if present\n",
    "    if ':' in value:\n",
    "        value = value.split(':', 1)[1].strip().lower()\n",
    "    else:\n",
    "        value = value.strip().lower()\n",
    "    \n",
    "    if 'rheumatoid arthritis' in value or 'ra' in value:\n",
    "        return 1\n",
    "    elif 'control' in value or 'healthy' in value or 'normal' in value:\n",
    "        return 0\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "# Since age_row and gender_row are None, we don't need conversion functions for them\n",
    "convert_age = None\n",
    "convert_gender = None\n",
    "\n",
    "# 3. Save Metadata\n",
    "# Perform initial filtering on data usability\n",
    "# trait_row is not None, so trait data is available\n",
    "is_trait_available = trait_row is not None\n",
    "initial_check = 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",
    "# trait_row is not None, so clinical data is available\n",
    "if trait_row is not None:\n",
    "    try:\n",
    "        # Load clinical data (assumed to be defined in a previous step)\n",
    "        # Extract clinical features\n",
    "        clinical_features = geo_select_clinical_features(\n",
    "            clinical_df=clinical_data,  # This should be defined 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 extracted clinical features\n",
    "        preview = preview_df(clinical_features)\n",
    "        print(\"Preview of clinical features:\")\n",
    "        print(preview)\n",
    "        \n",
    "        # Save the clinical features to a CSV file\n",
    "        os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "        clinical_features.to_csv(out_clinical_data_file)\n",
    "        print(f\"Clinical features saved to {out_clinical_data_file}\")\n",
    "    except NameError:\n",
    "        print(\"Clinical data not available from previous steps.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36869e4b",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "32899081",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T03:51:37.374888Z",
     "iopub.status.busy": "2025-03-25T03:51:37.374790Z",
     "iopub.status.idle": "2025-03-25T03:51:37.524159Z",
     "shell.execute_reply": "2025-03-25T03:51:37.523831Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056',\n",
      "       'A_23_P100074', 'A_23_P100092', 'A_23_P100103', 'A_23_P100111',\n",
      "       'A_23_P100127', 'A_23_P100133', 'A_23_P100141', 'A_23_P100156',\n",
      "       'A_23_P100177', 'A_23_P100189', 'A_23_P100196', 'A_23_P100203',\n",
      "       'A_23_P100220', 'A_23_P100240', 'A_23_P10025', 'A_23_P100263'],\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": "63270660",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2b399577",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T03:51:37.525198Z",
     "iopub.status.busy": "2025-03-25T03:51:37.525089Z",
     "iopub.status.idle": "2025-03-25T03:51:37.526943Z",
     "shell.execute_reply": "2025-03-25T03:51:37.526664Z"
    }
   },
   "outputs": [],
   "source": [
    "# Looking at the gene identifiers, these appear to be Agilent microarray probe IDs\n",
    "# (format \"A_23_P######\") rather than human gene symbols.\n",
    "# These identifiers need to be mapped to gene symbols for biological interpretation.\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6df4d81c",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "eb48cb99",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T03:51:37.527888Z",
     "iopub.status.busy": "2025-03-25T03:51:37.527787Z",
     "iopub.status.idle": "2025-03-25T03:51:39.815946Z",
     "shell.execute_reply": "2025-03-25T03:51:39.815515Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene annotation preview:\n",
      "{'ID': ['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074'], 'SPOT_ID': ['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074'], 'CONTROL_TYPE': ['FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': ['NM_207446', 'NM_005829', 'NM_014848', 'NM_194272', 'NM_020371'], 'GB_ACC': ['NM_207446', 'NM_005829', 'NM_014848', 'NM_194272', 'NM_020371'], 'GENE': [400451.0, 10239.0, 9899.0, 348093.0, 57099.0], 'GENE_SYMBOL': ['FAM174B', 'AP3S2', 'SV2B', 'RBPMS2', 'AVEN'], 'GENE_NAME': ['family with sequence similarity 174, member B', 'adaptor-related protein complex 3, sigma 2 subunit', 'synaptic vesicle glycoprotein 2B', 'RNA binding protein with multiple splicing 2', 'apoptosis, caspase activation inhibitor'], 'UNIGENE_ID': ['Hs.27373', 'Hs.632161', 'Hs.21754', 'Hs.436518', 'Hs.555966'], 'ENSEMBL_ID': ['ENST00000557398', nan, 'ENST00000557410', 'ENST00000300069', 'ENST00000306730'], 'TIGR_ID': [nan, nan, nan, nan, nan], 'ACCESSION_STRING': ['ref|NM_207446|ens|ENST00000557398|ens|ENST00000553393|ens|ENST00000327355', 'ref|NM_005829|ref|NM_001199058|ref|NR_023361|ref|NR_037582', 'ref|NM_014848|ref|NM_001167580|ens|ENST00000557410|ens|ENST00000330276', 'ref|NM_194272|ens|ENST00000300069|gb|AK127873|gb|AK124123', 'ref|NM_020371|ens|ENST00000306730|gb|AF283508|gb|BC010488'], 'CHROMOSOMAL_LOCATION': ['chr15:93160848-93160789', 'chr15:90378743-90378684', 'chr15:91838329-91838388', 'chr15:65032375-65032316', 'chr15:34158739-34158680'], 'CYTOBAND': ['hs|15q26.1', 'hs|15q26.1', 'hs|15q26.1', 'hs|15q22.31', 'hs|15q14'], 'DESCRIPTION': ['Homo sapiens family with sequence similarity 174, member B (FAM174B), mRNA [NM_207446]', 'Homo sapiens adaptor-related protein complex 3, sigma 2 subunit (AP3S2), transcript variant 1, mRNA [NM_005829]', 'Homo sapiens synaptic vesicle glycoprotein 2B (SV2B), transcript variant 1, mRNA [NM_014848]', 'Homo sapiens RNA binding protein with multiple splicing 2 (RBPMS2), mRNA [NM_194272]', 'Homo sapiens apoptosis, caspase activation inhibitor (AVEN), mRNA [NM_020371]'], 'GO_ID': ['GO:0016020(membrane)|GO:0016021(integral to membrane)', 'GO:0005794(Golgi apparatus)|GO:0006886(intracellular protein transport)|GO:0008565(protein transporter activity)|GO:0016020(membrane)|GO:0016192(vesicle-mediated transport)|GO:0030117(membrane coat)|GO:0030659(cytoplasmic vesicle membrane)|GO:0031410(cytoplasmic vesicle)', 'GO:0001669(acrosomal vesicle)|GO:0006836(neurotransmitter transport)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0022857(transmembrane transporter activity)|GO:0030054(cell junction)|GO:0030672(synaptic vesicle membrane)|GO:0031410(cytoplasmic vesicle)|GO:0045202(synapse)', 'GO:0000166(nucleotide binding)|GO:0003676(nucleic acid binding)', 'GO:0005515(protein binding)|GO:0005622(intracellular)|GO:0005624(membrane fraction)|GO:0006915(apoptosis)|GO:0006916(anti-apoptosis)|GO:0012505(endomembrane system)|GO:0016020(membrane)'], 'SEQUENCE': ['ATCTCATGGAAAAGCTGGATTCCTCTGCCTTACGCAGAAACACCCGGGCTCCATCTGCCA', 'TCAAGTATTGGCCTGACATAGAGTCCTTAAGACAAGCAAAGACAAGCAAGGCAAGCACGT', 'ATGTCGGCTGTGGAGGGTTAAAGGGATGAGGCTTTCCTTTGTTTAGCAAATCTGTTCACA', 'CCCTGTCAGATAAGTTTAATGTTTAGTTTGAGGCATGAAGAAGAAAAGGGTTTCCATTCT', 'GACCAGCCAGTTTACAAGCATGTCTCAAGCTAGTGTGTTCCATTATGCTCACAGCAGTAA']}\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": "775ca674",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "21184f03",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T03:51:39.817420Z",
     "iopub.status.busy": "2025-03-25T03:51:39.817297Z",
     "iopub.status.idle": "2025-03-25T03:51:39.949445Z",
     "shell.execute_reply": "2025-03-25T03:51:39.949114Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of genes after mapping: 18488\n",
      "First 10 gene symbols:\n",
      "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A2LD1', 'A2M', 'A2ML1', 'A4GALT', 'A4GNT',\n",
      "       'AAAS', 'AACS'],\n",
      "      dtype='object', name='Gene')\n"
     ]
    }
   ],
   "source": [
    "# 1. Determine which columns in gene_annotation contain probe IDs and gene symbols\n",
    "# From the preview output, we can see:\n",
    "# - 'ID' column contains identifiers like A_23_P100001 (matching gene_data index)\n",
    "# - 'GENE_SYMBOL' column contains human gene symbols like FAM174B, AP3S2, etc.\n",
    "\n",
    "# 2. Get a gene mapping dataframe with the ID and gene symbol columns\n",
    "mapping_data = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')\n",
    "\n",
    "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n",
    "# This handles many-to-many mapping by distributing expression values appropriately\n",
    "gene_data = apply_gene_mapping(gene_data, mapping_data)\n",
    "\n",
    "# Print the number of genes and preview first few gene symbols\n",
    "print(f\"Number of genes after mapping: {gene_data.shape[0]}\")\n",
    "print(\"First 10 gene symbols:\")\n",
    "print(gene_data.index[:10])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35471ee7",
   "metadata": {},
   "source": [
    "### Step 7: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0ba8b487",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T03:51:39.950825Z",
     "iopub.status.busy": "2025-03-25T03:51:39.950714Z",
     "iopub.status.idle": "2025-03-25T03:51:46.040703Z",
     "shell.execute_reply": "2025-03-25T03:51:46.040167Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clinical data preview:\n",
      "{'GSM7034090': [1.0], 'GSM7034091': [1.0], 'GSM7034092': [1.0], 'GSM7034093': [1.0], 'GSM7034094': [1.0], 'GSM7034095': [1.0], 'GSM7034096': [1.0], 'GSM7034097': [1.0], 'GSM7034098': [1.0], 'GSM7034099': [1.0], 'GSM7034100': [1.0], 'GSM7034101': [1.0], 'GSM7034102': [1.0], 'GSM7034103': [1.0], 'GSM7034104': [1.0], 'GSM7034105': [1.0], 'GSM7034106': [1.0], 'GSM7034107': [1.0], 'GSM7034108': [1.0], 'GSM7034109': [1.0], 'GSM7034110': [1.0], 'GSM7034111': [1.0], 'GSM7034112': [1.0], 'GSM7034113': [1.0], 'GSM7034114': [1.0], 'GSM7034115': [1.0], 'GSM7034116': [1.0], 'GSM7034117': [1.0], 'GSM7034118': [1.0], 'GSM7034119': [1.0]}\n",
      "Clinical data saved to ../../output/preprocess/Rheumatoid_Arthritis/clinical_data/GSE224842.csv\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalized gene data saved to ../../output/preprocess/Rheumatoid_Arthritis/gene_data/GSE224842.csv\n",
      "Linked data shape: (30, 18489)\n",
      "Linked data preview:\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Rheumatoid_Arthritis': [1.0, 1.0, 1.0, 1.0, 1.0], 'A1BG': [-5.672276699999999, -5.7897815999999995, -6.910050399999999, -7.37900833, -6.85140124], 'A1BG-AS1': [-0.82573605, -0.5873003, -1.2211533, -1.1551151, -0.93727255], 'A1CF': [-13.8762918, -12.7947989, -13.7370762, -13.416313200000001, -10.8981619], 'A2LD1': [-0.41900063, 1.0252638, -0.21993828, -0.5222225, -0.8553972], 'A2M': [-4.400196, -4.0229826, -4.515613, -3.1656747, -4.5899105], 'A2ML1': [-0.48994827, -0.4254818, -1.1093178, -1.1353436, -1.2472539], 'A4GALT': [-7.4506717, -7.31589, -5.7703366, -6.598217, -5.0818677], 'A4GNT': [-7.218234, -7.0256987, -7.2700396, -6.986816, -7.2179604], 'AAAS': [-0.7432537, -0.5085478, -1.1884365, -0.9100504, -0.6118984], 'AACS': [-1.0635576, -0.34570217, -0.81950283, -0.2558813, -0.58977795], 'AADAC': [-7.545908, -7.38232, -7.52981, -7.2697544, -7.6395655], 'AADACL2': [-7.503484, -7.356191, -5.2046757, -7.269441, -7.6019454], 'AADAT': [-7.527377, -7.371703, -7.4991765, -4.376906, -6.504797], 'AAGAB': [2.2582073, 2.548541, 2.1341095, 2.5100403, 2.169055], 'AAK1': [-10.1140018, -10.66734, -8.8441229, -9.5454646, -11.114736], 'AAMP': [1.0216742, 1.7772751, 1.4360008, 1.7343493, 2.1862898], 'AANAT': [-2.407835, -2.600957, -3.1172256, -3.2139435, -2.1840324], 'AARS': [-1.3784218, -0.96384525, -1.1997204, 0.41259098, -0.87807274], 'AARS2': [1.2421246, 0.7138815, 0.71047974, 0.61293507, 0.6756878], 'AARSD1': [-6.34245004, -4.5921581300000005, -5.38046676, -4.74188747, -6.92157907], 'AASDH': [-8.96104619, -9.192639254, -8.82581376, -9.39817388, -9.8033094], 'AASDHPPT': [2.96489045, 2.4781103030000002, 2.0863609600000004, 2.82212354, 0.722592354], 'AASS': [-4.0427446, -5.587759, -3.4349966, -5.0994215, -4.392365], 'AATF': [4.5565377, 4.2980756, 3.6240091999999997, 4.5149498, 4.7296314], 'AATK': [-9.334624000000002, -6.873260070000001, -8.384986099999999, -8.87256865, -0.9261999999999999], 'ABAT': [-3.24603217, -2.80136063, -4.10673325, -2.59025958, -2.0031033000000003], 'ABCA1': [-12.757077800000001, -15.3560429, -12.903388, -13.3188338, -9.8142328], 'ABCA10': [-4.9155107, -5.8032866, -4.626173, -5.405068, -5.4405556], 'ABCA11P': [-2.422851, -3.131978, -2.0128284, -2.0210571, -4.3289123], 'ABCA12': [-7.486428, -7.318147, -7.531383, -6.988555, -2.822732], 'ABCA13': [-5.3295507, -7.3612285, -3.1822505, -5.0947323, -7.604577], 'ABCA2': [-1.8076773, -1.2912087, -1.6370692, -1.9587884, -1.7081199], 'ABCA3': [-1.4380689, -1.2448292, -1.3428173, -1.9600391, -1.5856733], 'ABCA4': [-6.471012, -7.217036, -6.6104593, -6.978163, -7.485575], 'ABCA5': [0.6779327599999999, 0.11842156000000004, 0.5958519, -1.2487487800000001, -0.4253073], 'ABCA6': [-9.472969599999999, -11.8636318, -11.823563, -10.8864856, -10.546861700000001], 'ABCA7': [1.9915819, 1.6059198, 1.7085752, 1.7209673, 2.1078033], 'ABCA8': [-7.4203796, -7.2688956, -7.521812, -7.227166, -7.5249386], 'ABCA9': [-14.653060799999999, -12.769157700000001, -14.9767066, -14.3128815, -14.4369796], 'ABCB1': [-2.0282202, -2.4826593, -1.0026102, -3.126443, -2.0018563], 'ABCB10': [-8.624274230000001, -8.578881599999999, -7.84577654, -7.9238329499999995, -12.04703233], 'ABCB11': [-7.1828556, -7.0696793, -6.7747784, -7.0420246, -7.2673364], 'ABCB4': [-7.164705, -7.348943, -5.5546684, -7.239685, -6.467076], 'ABCB5': [-7.2002506, -7.0846815, -4.0676703, -6.8182974, -6.5319233], 'ABCB6': [-2.67638685, -2.3826008, -3.685645, -0.09470175999999997, -2.9620061], 'ABCB7': [1.3340616, 1.4737597, 1.0411892, 1.0278149, 0.88613796], 'ABCB8': [-4.140383, -3.5827246, -4.197866, -3.7845197, -3.9600024], 'ABCB9': [-13.7507846, -9.289794, -12.280249099999999, -6.978387, -11.3399894], 'ABCC1': [-1.8489552, -2.0801287, -1.8150349, -1.6443453, -1.7558122], 'ABCC10': [-0.8678589, -0.34912872, -0.64244556, -0.18570137, -0.07838249], 'ABCC11': [-7.3919, -7.274523, -7.5247974, -7.236232, -7.5175114], 'ABCC12': [-7.177505, -7.0590277, -7.3686857, -7.0309725, -7.2513103], 'ABCC13': [-21.2657442, -21.1505015, -21.4832578, -14.935285499999999, -22.6532829], 'ABCC2': [-6.5338074, -6.9857444, -5.733757499999999, -6.5686412, -5.5115806], 'ABCC3': [-0.5467434, -0.33122063, -0.5763979, -0.44857216, -1.3619113], 'ABCC4': [-20.212616699999998, -18.6689857, -15.8695898, -16.5618327, -20.1354993], 'ABCC5': [-2.6609211999999998, -3.7586982000000004, -2.3223719, -3.3235003999999995, -1.8228191], 'ABCC6': [-7.9646101, -9.4157849, -8.8081841, -13.3019816, -7.717088400000001], 'ABCC8': [-5.1685414, -5.8076735, -5.9039516, -7.0703325, -6.0444765], 'ABCC9': [-21.764257, -21.394194, -21.630094, -20.8896146, -22.1087536], 'ABCD1': [-1.8755527, -1.8623714, -2.5637264, -2.343752, -1.6781306], 'ABCD2': [-3.4833298, -4.6907625, -4.2587023, -4.2959223, -5.8055077], 'ABCD3': [-5.7499561, -5.670706300000001, -4.3278193, -5.4211168, -5.0843983], 'ABCD4': [0.81443214, 0.61604214, 0.84440994, 0.45913792, 0.6721153], 'ABCE1': [-2.9135966, -2.91890238, -2.9767418300000004, -2.66954706, -3.5395136], 'ABCF1': [1.8778639, 1.9378271, 1.5987844, 2.3812885, 2.2160187], 'ABCF2': [-2.62760008, -2.638169813, -2.510194734, -1.16964, -1.66941155], 'ABCF3': [1.3612194, 1.2426577, 1.0398817, 1.1960793, 1.3382578], 'ABCG1': [-0.99579906, -1.9206533, -1.3551111, -1.9619017, -1.0773997], 'ABCG2': [-3.2962637, -4.401633, -4.4412265, -3.2782435, -3.7415028], 'ABCG4': [-7.5338736, -7.376561, -7.5302525, -7.2711535, -7.6219125], 'ABCG5': [-6.657258, -7.250321, -7.403962, -7.2010193, -7.4992304], 'ABCG8': [-7.2271833, -7.124341, -7.467396, -7.0909815, -7.3497014], 'ABHD1': [-6.762836999999999, -8.5736408, -9.8026127, -10.290669900000001, -8.9762783], 'ABHD10': [-8.799778400000001, -8.775035800000001, -9.401433919999999, -9.140755720000001, -9.49976203], 'ABHD11': [-2.4761997, -1.6473356000000001, -3.6900701600000003, -0.8848332600000001, -1.4365778], 'ABHD12': [-3.0476747499999997, -3.2087121, -2.9060482600000004, -2.3387251, -2.61919447], 'ABHD12B': [-7.267728, -7.15442, -7.491486, -7.1545515, -7.3993483], 'ABHD13': [-8.2465113, -8.2902551, -7.6255898, -9.5016803, -7.83000665], 'ABHD14A': [-0.18257809, 0.023521423, -0.37325954, -0.05340004, -0.443964], 'ABHD14B': [-2.70071652, -2.7552423499999996, -2.886884247, -2.50080918, -2.0672236], 'ABHD15': [0.15834427, -0.64338684, -0.50092983, -1.2246332, -1.2972102], 'ABHD16A': [-0.6656771, -0.397007, -0.92480373, -0.96979046, -0.22925949], 'ABHD16B': [-3.7478871, -3.364874, -2.8352184, -2.8809175, -3.4663444], 'ABHD2': [-16.2790142, -15.82895994, -10.5943938, -12.13850693, -11.67165071], 'ABHD3': [0.13170528, -0.2957859, 0.1852827, -0.12103939, 0.27666855], 'ABHD4': [-0.7260065, -1.0659947, -1.3100948, -0.86526203, -0.48190308], 'ABHD5': [0.44874, 0.6871443, 0.34294033, 0.21835995, 1.5371256], 'ABHD6': [0.44564724, 0.48078156, -0.118780136, -0.18615627, -0.5337801], 'ABHD8': [-2.0172424, -1.4165812, -2.2404466, -2.109457, -1.8998551], 'ABI1': [2.0388355999999996, 2.1790695, 1.6608687, 1.5719729, 2.0651226200000004], 'ABI2': [-3.8268227, -3.9822534999999997, -3.1611133000000002, -3.0800023, -3.3106175], 'ABI3': [2.046051, 2.768569, 1.9696617, 2.6781359, 1.8798723], 'ABI3BP': [-12.584055, -14.3518403, -14.8202193, -12.7740476, -14.8524752], 'ABL1': [-18.05591475, -18.36841663, -16.075487380000002, -17.16053067, -16.80604232], 'ABL2': [-9.151920800000001, -10.674274, -9.60929394, -11.8544745, -8.7107896], 'ABLIM1': [4.5914726, 4.1526575, 4.179431, 3.8035822, 4.6776524], 'ABLIM2': [-2.7442427, -3.0166035, -2.927566, -3.532939, -2.308198], 'ABLIM3': [-8.3152331, -6.39541076, -8.7026693, -4.7626633, -9.064794], 'ABO': [-7.3071556, -7.146287, -7.4039717, -7.1021605, -7.426547], 'ABP1': [-5.043387, -5.5227637, -6.479496, -6.621311, -3.5038342], 'ABR': [4.1594563, 4.395466, 4.3334055, 3.85252, 4.547434], 'ABRA': [-7.5288954, -5.0816774, -6.6456428, -7.2622585, -7.6311307], 'ABT1': [-1.2014389, -1.6133046, -1.1205416, -1.7967935, -1.46207], 'ABTB1': [2.1097794, 2.5995178, 2.40991593, 1.6793689569999999, 4.688216199999999], 'ABTB2': [-2.3811474, -2.359599, -2.533659, -1.6266832, -1.5705929], 'ACAA1': [4.6205206, 4.2452879, 3.73272516, 4.4472141, 5.295465500000001], 'ACAA2': [1.2371387, 1.2707539, 1.9868469, 2.1454563, 1.2646112], 'ACACA': [-11.342644, -11.9572687, -11.871556, -8.4872532, -11.0053837], 'ACACB': [-5.6445055, -5.9599175, -4.49924, -4.660137, -4.173883], 'ACAD10': [-9.178745469999999, -10.13194556, -8.809484099999999, -8.846339766, -7.928208784000001], 'ACAD11': [-1.7105169, -2.0726829, -1.3038492, -1.5708971, -2.049047], 'ACAD8': [-2.37114045, -2.7611013, -1.8885279000000001, -3.2005038999999997, -2.46819977], 'ACAD9': [2.0959654, 2.675339654, 2.2586020999999996, 3.0270967, 2.3611125470000003], 'ACADL': [-6.538789, -6.4952345, -5.5564976, -6.8421926, -6.8785048], 'ACADM': [-1.0278292, -0.8900089, -0.04274273, 0.5626459, -1.1752162], 'ACADS': [-3.1051044, -2.7642422, -3.105947, -1.6386485, -2.1175466], 'ACADSB': [-4.98120118, -6.7336068, -5.1612573, -5.412092660000001, -6.3190217], 'ACADVL': [4.5316467, 4.8567753, 4.3535566, 4.7093363, 5.045844], 'ACAN': [-5.181829, -4.773378, -5.258699, -5.9512634, -5.290766], 'ACAP1': [-1.16984986, -0.8138245999999999, -1.2016487000000002, -0.8507890399999999, -0.9950757000000001], 'ACAP2': [1.12271214, 0.802750578, 2.72405823, 0.90293887, 2.4363136], 'ACAP3': [-12.2100244, -10.6799294, -12.066364700000001, -11.467922699999999, -10.5877032], 'ACAT1': [0.7947096999999999, 1.0266851999999997, 1.55346297, 2.8571472, 0.06352710000000017], 'ACAT2': [1.63053322, 1.3659477500000001, 0.96924586, 2.483555805, 0.9836378000000001], 'ACBD3': [-0.3144627, -1.0357976, -1.7451067, -0.27079149999999985, -1.6651744499999999], 'ACBD4': [-4.0879316, -2.724246, -0.39037469999999996, 0.05358639999999992, -2.5280304], 'ACBD5': [-5.79491051, -6.85836501, -5.40001874, -4.7711992, -5.1585912800000004], 'ACBD6': [1.8423347, 2.0644531, 2.0107822, 2.03154, 2.3613071], 'ACBD7': [-3.0800323, -2.2434773, -2.2077913, -2.2273965, -1.4388709], 'ACCN1': [-4.360352, -5.3642793, -5.157859, -6.0670023, -4.8050423], 'ACCN2': [-3.995082, -3.5359678, -3.3330522, -1.7383995, -1.3894367], 'ACCN3': [-4.706303, -4.257624, -4.9579554, -3.271546, -3.8409743], 'ACCN4': [-7.4128046, -7.290801, -7.524151, -7.2136765, -6.860148], 'ACCN5': [-7.009403, -6.841119, -7.325863, -6.880547, -7.1541514], 'ACCS': [0.23649216, 1.4211206, 1.0881166, -0.5122328, 0.8612385], 'ACD': [0.5862436, 0.6035557, 0.19909477, 0.52528, 0.3198347], 'ACE': [-13.547852800000001, -17.4194617, -16.5371519, -17.740468, -13.943834200000001], 'ACE2': [-7.23131, -7.119723, -7.4764404, -7.1291866, -7.366466], 'ACER1': [-4.896104, -3.8794484, -5.319996, -5.237687, -4.9432716], 'ACER3': [-6.6761112, -8.2445415, -7.27013495, -9.4878195, -10.092452600000001], 'ACHE': [-3.8559752, -3.9458804, -4.583341, -4.3558445, -3.378273], 'ACIN1': [0.7603836, 0.99685764, 0.8268814, 1.7168608, -0.079520226], 'ACLY': [0.38022804, 0.412138, 0.08678627, 0.147089, 0.4066553], 'ACMSD': [-7.1809416, -5.843359, -7.451509, -7.0912375, -7.338464], 'ACN9': [1.1430626, 0.7836666, 0.9518442, 0.51330376, -0.021832466], 'ACO1': [-1.40317197, -1.8843564940000002, -2.24013423, -2.8537455, -1.833668265], 'ACO2': [5.2178001, 5.5096644, 4.2981871, 6.511754, 5.9552527], 'ACOT1': [0.7868767, 0.8424082, 0.75664234, 1.2970495, 1.2177763], 'ACOT11': [-16.408636100000003, -14.790592199999999, -17.982816200000002, -18.0501052, -16.5575398], 'ACOT12': [-7.0225816, -6.8067093, -7.133717, -6.763191, -7.0988083], 'ACOT13': [1.1285734, 1.1647539, 0.89674854, 1.7105026, 0.74302197], 'ACOT2': [-2.1154037, -2.5177779, -2.44614984, -1.4156661000000001, -1.1240697], 'ACOT4': [-1.79389, -1.5896764, -1.3459873, -1.3705006, -1.6034584], 'ACOT7': [-1.6337776, -1.2804909, -1.2576017, 0.9806242, -0.90421104], 'ACOT8': [-0.29053974, 0.041664124, -0.49155045, -0.15271378, -0.34722137], 'ACOT9': [2.7252636, 2.52243797, 2.04512406, 2.2083330500000002, 2.4873247], 'ACOX1': [-7.766797862000001, -7.211201600000001, -8.62706211, -8.14803854, -7.59882087], 'ACOX2': [-3.3249192, -3.3608236, -4.105567, -3.9730997, -3.745706], 'ACOX3': [-2.6122059, -2.6702690000000002, -3.3615551, -3.3754831000000003, -2.8767762], 'ACOXL': [-7.2625484, -7.1502724, -7.427355, -3.0364175, -7.385391], 'ACP1': [-5.1440669, -5.2066701, -5.3446321, -5.5635848, -5.4709596], 'ACP2': [-7.8325366999999995, -7.903704599999999, -8.759673, -4.3251777, -6.2901712], 'ACP5': [3.2569914, 2.759904, 3.3639917, 4.447199, 3.4829054], 'ACP6': [-3.2169528, -3.0801902, -3.4940128, -3.1373043, -2.6426635], 'ACPL2': [0.50189495, 1.3311968, -0.084204674, 0.05305481, -0.24776363], 'ACPP': [-6.1080341, -5.7067457, -4.7667212, -8.8658736, -5.8529135], 'ACPT': [-3.6121526, -4.053385, -4.4567885, -4.6687727, -4.4847374], 'ACR': [-10.281091700000001, -10.6681005, -10.9398676, -10.967090500000001, -10.552845], 'ACRBP': [1.2464142, 1.7671185, 0.8749714, 1.8071194, 0.72149944], 'ACRC': [1.0909805, 0.52371407, 0.1806364, 0.48336792, 0.20383072], 'ACRV1': [-5.674367, -7.1800632, -7.4921412, -7.1311216, -7.4125724], 'ACSBG1': [-11.4070084, -10.587768, -11.2596709, -9.2162844, -12.045697700000002], 'ACSBG2': [-4.8861513, -5.7379456, -6.3619385, -6.0682516, -6.3843765], 'ACSF2': [-0.99878407, -1.4018011, -1.4597378, -1.2621794, -0.81375504], 'ACSF3': [-0.28560069999999993, -1.40667204, -0.20403479999999996, -0.11452867, 0.24322510000000008], 'ACSL1': [0.9514036, 2.413104, 1.998766, 0.99235344, 2.6377115], 'ACSL3': [-1.5468415800000002, -1.7404795000000002, -2.1830596499999997, -2.0155115, -2.8115629999999996], 'ACSL4': [-0.5112896, -0.056881905, -0.41926956, -0.75004864, -0.41520977], 'ACSL5': [-0.596859, -0.7243061, -0.52190685, -0.2998848, -0.599843], 'ACSL6': [-11.3485674, -14.5851996, -13.39652443, -14.845241600000001, -15.0063285], 'ACSM1': [-4.7167435, -5.0712967, -4.7472897, -5.193263, -4.5040483], 'ACSM2A': [-19.1286755, -19.546621000000002, -20.4018622, -19.5713956, -19.737284799999998], 'ACSM2B': [-5.927985, -5.3562965, -7.2507296, -7.2405396, -6.303212], 'ACSM3': [-4.344808, -4.1558995, -3.9980268, -4.500384, -4.041515], 'ACSM5': [-21.1506905, -21.1800815, -21.9647515, -21.0945042, -21.7255961], 'ACSS1': [-0.8348091999999998, -0.9425234999999996, -0.4974660999999998, 0.14134780000000013, -0.2674646000000003], 'ACSS2': [-1.8410072499999999, -0.8970055800000001, -1.31744959, -2.8160043, 0.9462614], 'ACSS3': [-7.3240376, -7.2490735, -7.5138493, -7.177437, -7.488361], 'ACTA1': [-4.424113, -4.785349, -5.5042844, -5.605706, -4.7932854], 'ACTA2': [-0.82315254, -1.4225492, -0.6007261, -0.51765156, -1.1715164], 'ACTB': [23.4101327, 24.8861091, 25.2548084, 23.179679699999998, 26.09254], 'ACTBL2': [-0.50547314, 0.03707218, 0.25015354, -0.032699585, 1.204628], 'ACTC1': [-1.74121, -1.6949024, -1.4414039, -2.2562386, 0.42049740000000035], 'ACTG1': [18.8395413, 18.4781458, 18.836751800000002, 19.509356500000003, 18.1723604], 'ACTG2': [-4.083104, -4.354103, -7.2761574, -3.5374637, -5.5926127], 'ACTL6A': [1.6886635, 1.5176992, 1.2817659, 1.7075338, 1.1334505], 'ACTL6B': [-3.3464222, -3.53649, -2.9222069, -3.34277, -3.7558079], 'ACTL7A': [-6.169523, -5.52555, -6.5140753, -6.038963, -6.365119]}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linked data shape after handling missing values: (30, 18489)\n",
      "Quartiles for 'Rheumatoid_Arthritis':\n",
      "  25%: 1.0\n",
      "  50% (Median): 1.0\n",
      "  75%: 1.0\n",
      "Min: 1.0\n",
      "Max: 1.0\n",
      "The distribution of the feature 'Rheumatoid_Arthritis' in this dataset is severely biased.\n",
      "\n",
      "Linked data was not usable and was not saved.\n"
     ]
    }
   ],
   "source": [
    "# 1. First, we need to extract clinical features since we missed this step earlier\n",
    "selected_clinical_data = geo_select_clinical_features(\n",
    "    clinical_data, \n",
    "    trait, \n",
    "    trait_row, \n",
    "    convert_trait,\n",
    "    age_row, \n",
    "    convert_age,\n",
    "    gender_row, \n",
    "    convert_gender\n",
    ")\n",
    "\n",
    "print(\"Clinical data preview:\")\n",
    "print(preview_df(selected_clinical_data))\n",
    "\n",
    "# Save the clinical data\n",
    "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "selected_clinical_data.to_csv(out_clinical_data_file)\n",
    "print(f\"Clinical data saved to {out_clinical_data_file}\")\n",
    "\n",
    "# 2. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n",
    "# Note: Already normalized in step 7\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\"Normalized gene data saved to {out_gene_data_file}\")\n",
    "\n",
    "# 3. 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_data, gene_data)\n",
    "print(f\"Linked data shape: {linked_data.shape}\")\n",
    "print(\"Linked data preview:\")\n",
    "print(preview_df(linked_data))\n",
    "\n",
    "# 4. Handle missing values in the linked data\n",
    "linked_data = handle_missing_values(linked_data, trait)\n",
    "print(f\"Linked data shape after handling missing values: {linked_data.shape}\")\n",
    "\n",
    "# 5. Determine whether the trait and some demographic features are severely biased, and remove biased features.\n",
    "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n",
    "\n",
    "# 6. 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=\"Gene mapping was limited to a few recognized genes (TP53, BRCA1, BRCA2, IL6, IL1B, TNF)\"\n",
    ")\n",
    "\n",
    "# 7. 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\"Usable linked data saved to {out_data_file}\")\n",
    "else:\n",
    "    print(\"Linked data was not usable and was not 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
}