File size: 31,207 Bytes
92d2f89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "61b25fec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:26:08.890358Z",
     "iopub.status.busy": "2025-03-25T06:26:08.890254Z",
     "iopub.status.idle": "2025-03-25T06:26:09.048716Z",
     "shell.execute_reply": "2025-03-25T06:26:09.048286Z"
    }
   },
   "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 = \"Alzheimers_Disease\"\n",
    "cohort = \"GSE132903\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Alzheimers_Disease\"\n",
    "in_cohort_dir = \"../../input/GEO/Alzheimers_Disease/GSE132903\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Alzheimers_Disease/GSE132903.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Alzheimers_Disease/gene_data/GSE132903.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Alzheimers_Disease/clinical_data/GSE132903.csv\"\n",
    "json_path = \"../../output/preprocess/Alzheimers_Disease/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "735a22d8",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7d76ea28",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:26:09.049973Z",
     "iopub.status.busy": "2025-03-25T06:26:09.049829Z",
     "iopub.status.idle": "2025-03-25T06:26:09.485141Z",
     "shell.execute_reply": "2025-03-25T06:26:09.484788Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Transcriptome changes in the Alzheimer's middle temporal gyrus: importance of RNA metabolism and mitochondria-associated membrane (MAM) genes\"\n",
      "!Series_summary\t\"We used Illumina Human HT-12 v4 arrays to compare RNA expression of middle temporal gyrus (MTG; BA21) in Alzheimer’s Disease (AD = 97) and non-demented controls (ND = 98).  A total of 938 transcripts were highly differentially expressed (adj p < 0.01; log2 Fold Change (FC) ≥ |0.500|, with 411 overexpressed and 527 underexpressed in AD.  Our results correlated with expression profiling in neurons from AD and ND obtained by Laser Capture Microscopy in MTG  from an independent dataset (log2 FC correlation: r = 0.504; p = 2.2e-16).  Additionally selected effects were validated by qPCR. ANOVA analysis yielded no difference between genders in response to AD, but some gender specific genes were detected (e.g: IL8 and AGRN in males, and HSPH1 and GRM1 in females).  Several transcripts were associated with Braak Staging (e.g AEBP1 and DNALI1), ante-mortem MMSE (e.g. AEBP1 and GFAP) and Tangle density (eg. RNU1G2, and DNALI1). At the pathway level we detected enrichment of Synaptic Vesicle Processes and GABAergic transmission genes. Finally, applying the Weighted Correlation Network Analysis (WGCNA) we identified 4 expression modules enriched for neuronal and synaptic genes, mitochondria-associated membrane (MAM), chemical stimulus and olfactory receptor and non-coding RNA metabolism genes.    Our results represent an extensive description of MTG mRNA profiling in a large sample of AD and ND.  These data provide a list of genes associated with AD, and correlated to neurofibrillary tangles density. In addition, these data emphasize the importance of mitochondrial membranes and transcripts related to olfactory receptors in AD.\"\n",
      "!Series_overall_design\t\"We compared RNA expression of middle temporal gyrus (MTG; BA21) between Alzheimer’s Disease (AD = 97) and non-demented controls (ND = 98) using Illumina Human HT-12 v4 arrays\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['tissue: middle temporal gyrus'], 1: ['Sex: female', 'Sex: male'], 2: ['expired_age (years): 90+', 'expired_age (years): 82', 'expired_age (years): 88', 'expired_age (years): 92', 'expired_age (years): 91', 'expired_age (years): 87', 'expired_age (years): 86', 'expired_age (years): 78', 'expired_age (years): 79', 'expired_age (years): 77', 'expired_age (years): 85', 'expired_age (years): 95', 'expired_age (years): 102', 'expired_age (years): 89', 'expired_age (years): 70', 'expired_age (years): 73', 'expired_age (years): 94', 'expired_age (years): 96', 'expired_age (years): 84', 'expired_age (years): 83', 'expired_age (years): 98', 'expired_age (years): 100', 'expired_age (years): 75', 'expired_age (years): 80', 'expired_age (years): 74', 'expired_age (years): 76', 'expired_age (years): 71', 'expired_age (years): 97', 'expired_age (years): 81', 'expired_age (years): 72'], 3: ['diagnosis: ND', 'diagnosis: AD']}\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": "e75cac64",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b6117b31",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:26:09.486316Z",
     "iopub.status.busy": "2025-03-25T06:26:09.486203Z",
     "iopub.status.idle": "2025-03-25T06:26:09.491074Z",
     "shell.execute_reply": "2025-03-25T06:26:09.490748Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Trait row identified: 3\n",
      "Age row identified: 2\n",
      "Gender row identified: 1\n",
      "The actual clinical data extraction requires the full clinical dataset.\n",
      "We've identified the relevant rows and created the conversion functions.\n",
      "Metadata saved to ../../output/preprocess/Alzheimers_Disease/cohort_info.json, indicating trait data is available: True\n"
     ]
    }
   ],
   "source": [
    "# 1. Determine gene expression data availability\n",
    "# From the background info, we see Illumina Human HT-12 v4 arrays were used\n",
    "# for RNA expression, which indicates gene expression data.\n",
    "is_gene_available = True\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "# 2.1 Data Availability\n",
    "\n",
    "# For trait (Alzheimer's Disease), we can use the 'diagnosis' field\n",
    "trait_row = 3  # 'diagnosis: ND', 'diagnosis: AD'\n",
    "\n",
    "# For age, we have 'expired_age (years)' field\n",
    "age_row = 2  # Contains ages of participants\n",
    "\n",
    "# For gender, we have 'Sex' field\n",
    "gender_row = 1  # 'Sex: female', 'Sex: male'\n",
    "\n",
    "# 2.2 Data Type Conversion\n",
    "\n",
    "def convert_trait(value):\n",
    "    \"\"\"Convert diagnosis to binary trait value.\"\"\"\n",
    "    if not value or ':' not in value:\n",
    "        return None\n",
    "    \n",
    "    diagnosis = value.split(':', 1)[1].strip()\n",
    "    if diagnosis == 'AD':\n",
    "        return 1  # Alzheimer's Disease\n",
    "    elif diagnosis == 'ND':\n",
    "        return 0  # Non-demented control\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "def convert_age(value):\n",
    "    \"\"\"Convert age string to continuous numeric value.\"\"\"\n",
    "    if not value or ':' not in value:\n",
    "        return None\n",
    "    \n",
    "    age_str = value.split(':', 1)[1].strip()\n",
    "    try:\n",
    "        if age_str.endswith('+'):\n",
    "            # For 90+, use 90 as the base age\n",
    "            return float(age_str.replace('+', ''))\n",
    "        else:\n",
    "            return float(age_str)\n",
    "    except ValueError:\n",
    "        return None\n",
    "\n",
    "def convert_gender(value):\n",
    "    \"\"\"Convert gender string to binary.\"\"\"\n",
    "    if not value or ':' not in value:\n",
    "        return None\n",
    "    \n",
    "    gender = value.split(':', 1)[1].strip().lower()\n",
    "    if gender == 'female':\n",
    "        return 0\n",
    "    elif gender == 'male':\n",
    "        return 1\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "# 3. Save metadata\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\n",
    "# We have only the sample characteristics dictionary showing unique values,\n",
    "# but we need the actual clinical data to perform the extraction.\n",
    "# We'll print the information we've gathered for debugging purposes\n",
    "if trait_row is not None:\n",
    "    print(f\"Trait row identified: {trait_row}\")\n",
    "    print(f\"Age row identified: {age_row}\")\n",
    "    print(f\"Gender row identified: {gender_row}\")\n",
    "    print(\"The actual clinical data extraction requires the full clinical dataset.\")\n",
    "    print(\"We've identified the relevant rows and created the conversion functions.\")\n",
    "    \n",
    "    # Since we can't perform the actual extraction without the clinical data,\n",
    "    # we'll just note that we've saved the metadata indicating the dataset\n",
    "    # has the necessary trait information\n",
    "    print(f\"Metadata saved to {json_path}, indicating trait data is available: {is_trait_available}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "105081e9",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2e2b60b1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:26:09.492223Z",
     "iopub.status.busy": "2025-03-25T06:26:09.491949Z",
     "iopub.status.idle": "2025-03-25T06:26:10.306417Z",
     "shell.execute_reply": "2025-03-25T06:26:10.305970Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "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_1651249', 'ILMN_1651254', 'ILMN_1651259',\n",
      "       'ILMN_1651260', 'ILMN_1651262', 'ILMN_1651268', 'ILMN_1651278'],\n",
      "      dtype='object', name='ID')\n"
     ]
    }
   ],
   "source": [
    "# 1. First get the file paths again to access the matrix file\n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "\n",
    "# 2. Use the get_genetic_data function from the library to get the gene_data from the matrix_file\n",
    "gene_data = get_genetic_data(matrix_file)\n",
    "\n",
    "# 3. Print the first 20 row IDs (gene or probe identifiers) for future observation\n",
    "print(\"First 20 gene/probe identifiers:\")\n",
    "print(gene_data.index[:20])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d212a12",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "767abff7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:26:10.307753Z",
     "iopub.status.busy": "2025-03-25T06:26:10.307641Z",
     "iopub.status.idle": "2025-03-25T06:26:10.309642Z",
     "shell.execute_reply": "2025-03-25T06:26:10.309340Z"
    }
   },
   "outputs": [],
   "source": [
    "# Analyzing the gene identifiers\n",
    "# The identifiers follow the \"ILMN_\" prefix pattern, which indicates they are Illumina probe IDs\n",
    "# These are not human gene symbols but Illumina BeadArray probe identifiers that need to be mapped to gene symbols\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c73d7a1a",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "101fd4c3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:26:10.310760Z",
     "iopub.status.busy": "2025-03-25T06:26:10.310656Z",
     "iopub.status.idle": "2025-03-25T06:26:25.249620Z",
     "shell.execute_reply": "2025-03-25T06:26:25.248954Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene annotation preview:\n",
      "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Species': [nan, nan, nan, nan, nan], 'Source': [nan, nan, nan, nan, nan], 'Search_Key': [nan, nan, nan, nan, nan], 'Transcript': [nan, nan, nan, nan, nan], 'ILMN_Gene': [nan, nan, nan, nan, nan], 'Source_Reference_ID': [nan, nan, nan, nan, nan], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Unigene_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': [nan, nan, nan, nan, nan], 'Symbol': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB'], 'Protein_Product': [nan, nan, nan, nan, 'thrB'], 'Probe_Id': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5090180.0, 6510136.0, 7560739.0, 1450438.0, 1240647.0], 'Probe_Type': [nan, nan, nan, nan, nan], 'Probe_Start': [nan, nan, nan, nan, nan], 'SEQUENCE': ['GAATAAAGAACAATCTGCTGATGATCCCTCCGTGGATCTGATTCGTGTAA', 'CCATGTGATACGAGGGCGCGTAGTTTGCATTATCGTTTTTATCGTTTCAA', 'CCGACAGATGTATGTAAGGCCAACGTGCTCAAATCTTCATACAGAAAGAT', 'TCTGTCACTGTCAGGAAAGTGGTAAAACTGCAACTCAATTACTGCAATGC', 'CTTGTGCCTGAGCTGTCAAAAGTAGAGCACGTCGCCGAGATGAAGGGCGC'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, nan, nan, nan], 'Definition': [nan, nan, nan, nan, nan], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan]}\n"
     ]
    }
   ],
   "source": [
    "# 1. First get the file paths using geo_get_relevant_filepaths function\n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "\n",
    "# 2. 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",
    "# 3. 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": "5954ce72",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "47b5fa78",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:26:25.251668Z",
     "iopub.status.busy": "2025-03-25T06:26:25.251512Z",
     "iopub.status.idle": "2025-03-25T06:26:25.816918Z",
     "shell.execute_reply": "2025-03-25T06:26:25.816282Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene mapping (first 5 rows):\n",
      "             ID                     Gene\n",
      "0  ILMN_1343048      phage_lambda_genome\n",
      "1  ILMN_1343049      phage_lambda_genome\n",
      "2  ILMN_1343050  phage_lambda_genome:low\n",
      "3  ILMN_1343052  phage_lambda_genome:low\n",
      "4  ILMN_1343059                     thrB\n",
      "\n",
      "Gene expression data after mapping (first 5 genes):\n",
      "Index(['A1BG', 'A1CF', 'A26C3', 'A2BP1', 'A2M'], dtype='object', name='Gene')\n",
      "Total number of genes after mapping: 19788\n"
     ]
    }
   ],
   "source": [
    "# 1. Determine which columns to use for gene identifier mapping\n",
    "# From the gene annotation preview, 'ID' column contains the probe identifiers matching the gene expression data index\n",
    "# The 'Symbol' column contains the gene symbols we want to map to\n",
    "probe_col = 'ID'\n",
    "gene_col = 'Symbol'\n",
    "\n",
    "# 2. Get the gene mapping dataframe by extracting the identifier and symbol columns\n",
    "gene_mapping = get_gene_mapping(gene_annotation, probe_col, gene_col)\n",
    "\n",
    "# Print first few rows of the mapping\n",
    "print(\"Gene mapping (first 5 rows):\")\n",
    "print(gene_mapping.head())\n",
    "\n",
    "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n",
    "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n",
    "\n",
    "# Print the first few genes \n",
    "print(\"\\nGene expression data after mapping (first 5 genes):\")\n",
    "print(gene_data.index[:5])\n",
    "print(f\"Total number of genes after mapping: {len(gene_data)}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25c65742",
   "metadata": {},
   "source": [
    "### Step 7: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fa6d05f5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:26:25.818815Z",
     "iopub.status.busy": "2025-03-25T06:26:25.818689Z",
     "iopub.status.idle": "2025-03-25T06:26:45.510809Z",
     "shell.execute_reply": "2025-03-25T06:26:45.510103Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalizing gene symbols...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene data shape after normalization: (18799, 195)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalized gene data saved to ../../output/preprocess/Alzheimers_Disease/gene_data/GSE132903.csv\n",
      "Loading the original clinical data...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting clinical features...\n",
      "Clinical data preview:\n",
      "{'GSM3895951': [0.0, 90.0, 0.0], 'GSM3895952': [0.0, 82.0, 1.0], 'GSM3895953': [0.0, 88.0, 0.0], 'GSM3895954': [0.0, 92.0, 0.0], 'GSM3895955': [0.0, 91.0, 1.0], 'GSM3895956': [0.0, 87.0, 0.0], 'GSM3895957': [0.0, 86.0, 1.0], 'GSM3895958': [0.0, 78.0, 1.0], 'GSM3895959': [0.0, 87.0, 1.0], 'GSM3895960': [0.0, 79.0, 1.0], 'GSM3895961': [0.0, 77.0, 0.0], 'GSM3895962': [0.0, 77.0, 1.0], 'GSM3895963': [0.0, 88.0, 0.0], 'GSM3895964': [0.0, 85.0, 1.0], 'GSM3895965': [0.0, 95.0, 0.0], 'GSM3895966': [0.0, 102.0, 0.0], 'GSM3895967': [0.0, 89.0, 1.0], 'GSM3895968': [0.0, 70.0, 1.0], 'GSM3895969': [0.0, 82.0, 0.0], 'GSM3895970': [0.0, 73.0, 0.0], 'GSM3895971': [0.0, 90.0, 1.0], 'GSM3895972': [0.0, 94.0, 1.0], 'GSM3895973': [0.0, 96.0, 0.0], 'GSM3895974': [0.0, 85.0, 0.0], 'GSM3895975': [0.0, 84.0, 1.0], 'GSM3895976': [0.0, 83.0, 1.0], 'GSM3895977': [0.0, 90.0, 0.0], 'GSM3895978': [0.0, 87.0, 0.0], 'GSM3895979': [0.0, 85.0, 1.0], 'GSM3895980': [0.0, 83.0, 0.0], 'GSM3895981': [0.0, 84.0, 1.0], 'GSM3895982': [0.0, 88.0, 1.0], 'GSM3895983': [0.0, 98.0, 0.0], 'GSM3895984': [0.0, 85.0, 1.0], 'GSM3895985': [0.0, 86.0, 0.0], 'GSM3895986': [0.0, 87.0, 0.0], 'GSM3895987': [0.0, 89.0, 1.0], 'GSM3895988': [0.0, 92.0, 1.0], 'GSM3895989': [0.0, 78.0, 0.0], 'GSM3895990': [0.0, 77.0, 0.0], 'GSM3895991': [0.0, 91.0, 1.0], 'GSM3895992': [0.0, 100.0, 1.0], 'GSM3895993': [0.0, 82.0, 1.0], 'GSM3895994': [0.0, 87.0, 0.0], 'GSM3895995': [0.0, 73.0, 1.0], 'GSM3895996': [0.0, 75.0, 1.0], 'GSM3895997': [0.0, 82.0, 1.0], 'GSM3895998': [0.0, 90.0, 0.0], 'GSM3895999': [0.0, 96.0, 0.0], 'GSM3896000': [0.0, 84.0, 0.0], 'GSM3896001': [0.0, 80.0, 1.0], 'GSM3896002': [0.0, 86.0, 1.0], 'GSM3896003': [0.0, 91.0, 0.0], 'GSM3896004': [0.0, 91.0, 0.0], 'GSM3896005': [0.0, 94.0, 0.0], 'GSM3896006': [0.0, 87.0, 1.0], 'GSM3896007': [0.0, 75.0, 0.0], 'GSM3896008': [0.0, 74.0, 1.0], 'GSM3896009': [0.0, 76.0, 1.0], 'GSM3896010': [0.0, 71.0, 1.0], 'GSM3896011': [0.0, 87.0, 1.0], 'GSM3896012': [0.0, 90.0, 1.0], 'GSM3896013': [0.0, 80.0, 1.0], 'GSM3896014': [0.0, 84.0, 1.0], 'GSM3896015': [0.0, 80.0, 1.0], 'GSM3896016': [0.0, 89.0, 1.0], 'GSM3896017': [0.0, 86.0, 0.0], 'GSM3896018': [0.0, 80.0, 0.0], 'GSM3896019': [0.0, 92.0, 1.0], 'GSM3896020': [0.0, 83.0, 0.0], 'GSM3896021': [0.0, 86.0, 0.0], 'GSM3896022': [0.0, 91.0, 0.0], 'GSM3896023': [0.0, 95.0, 0.0], 'GSM3896024': [0.0, 95.0, 0.0], 'GSM3896025': [0.0, 82.0, 0.0], 'GSM3896026': [0.0, 85.0, 0.0], 'GSM3896027': [0.0, 87.0, 0.0], 'GSM3896028': [0.0, 95.0, 1.0], 'GSM3896029': [0.0, 85.0, 0.0], 'GSM3896030': [0.0, 91.0, 0.0], 'GSM3896031': [0.0, 89.0, 0.0], 'GSM3896032': [1.0, 80.0, 1.0], 'GSM3896033': [1.0, 87.0, 0.0], 'GSM3896034': [1.0, 92.0, 0.0], 'GSM3896035': [1.0, 77.0, 0.0], 'GSM3896036': [1.0, 84.0, 0.0], 'GSM3896037': [1.0, 91.0, 0.0], 'GSM3896038': [1.0, 87.0, 0.0], 'GSM3896039': [1.0, 97.0, 0.0], 'GSM3896040': [1.0, 87.0, 0.0], 'GSM3896041': [1.0, 78.0, 1.0], 'GSM3896042': [1.0, 76.0, 1.0], 'GSM3896043': [1.0, 81.0, 1.0], 'GSM3896044': [1.0, 80.0, 1.0], 'GSM3896045': [1.0, 86.0, 0.0], 'GSM3896046': [1.0, 81.0, 0.0], 'GSM3896047': [1.0, 79.0, 1.0], 'GSM3896048': [1.0, 91.0, 0.0], 'GSM3896049': [1.0, 91.0, 0.0], 'GSM3896050': [1.0, 89.0, 0.0], 'GSM3896051': [1.0, 82.0, 0.0], 'GSM3896052': [1.0, 92.0, 0.0], 'GSM3896053': [1.0, 86.0, 1.0], 'GSM3896054': [1.0, 82.0, 0.0], 'GSM3896055': [1.0, 86.0, 0.0], 'GSM3896056': [1.0, 80.0, 1.0], 'GSM3896057': [1.0, 87.0, 0.0], 'GSM3896058': [1.0, 92.0, 1.0], 'GSM3896059': [1.0, 90.0, 0.0], 'GSM3896060': [1.0, 88.0, 0.0], 'GSM3896061': [1.0, 90.0, 1.0], 'GSM3896062': [1.0, 90.0, 1.0], 'GSM3896063': [1.0, 72.0, 1.0], 'GSM3896064': [1.0, 87.0, 1.0], 'GSM3896065': [1.0, 75.0, 1.0], 'GSM3896066': [1.0, 86.0, 0.0], 'GSM3896067': [1.0, 95.0, 0.0], 'GSM3896068': [1.0, 95.0, 1.0], 'GSM3896069': [1.0, 88.0, 0.0], 'GSM3896070': [1.0, 87.0, 1.0], 'GSM3896071': [1.0, 81.0, 0.0], 'GSM3896072': [1.0, 83.0, 1.0], 'GSM3896073': [1.0, 85.0, 0.0], 'GSM3896074': [1.0, 95.0, 0.0], 'GSM3896075': [1.0, 81.0, 1.0], 'GSM3896076': [1.0, 83.0, 1.0], 'GSM3896077': [1.0, 85.0, 1.0], 'GSM3896078': [1.0, 85.0, 0.0], 'GSM3896079': [1.0, 94.0, 1.0], 'GSM3896080': [1.0, 97.0, 1.0], 'GSM3896081': [1.0, 82.0, 0.0], 'GSM3896082': [1.0, 91.0, 1.0], 'GSM3896083': [1.0, 92.0, 1.0], 'GSM3896084': [1.0, 70.0, 1.0], 'GSM3896085': [1.0, 84.0, 1.0], 'GSM3896086': [1.0, 86.0, 1.0], 'GSM3896087': [1.0, 95.0, 0.0], 'GSM3896088': [1.0, 88.0, 1.0], 'GSM3896089': [1.0, 79.0, 1.0], 'GSM3896090': [1.0, 87.0, 1.0], 'GSM3896091': [1.0, 73.0, 0.0], 'GSM3896092': [1.0, 90.0, 0.0], 'GSM3896093': [1.0, 83.0, 1.0], 'GSM3896094': [1.0, 85.0, 0.0], 'GSM3896095': [1.0, 74.0, 1.0], 'GSM3896096': [1.0, 71.0, 1.0], 'GSM3896097': [1.0, 78.0, 0.0], 'GSM3896098': [1.0, 82.0, 1.0], 'GSM3896099': [1.0, 85.0, 1.0], 'GSM3896100': [1.0, 96.0, 0.0], 'GSM3896101': [1.0, 70.0, 0.0], 'GSM3896102': [1.0, 78.0, 0.0], 'GSM3896103': [1.0, 77.0, 0.0], 'GSM3896104': [1.0, 87.0, 0.0], 'GSM3896105': [1.0, 84.0, 1.0], 'GSM3896106': [1.0, 98.0, 1.0], 'GSM3896107': [1.0, 75.0, 1.0], 'GSM3896108': [1.0, 76.0, 1.0], 'GSM3896109': [1.0, 94.0, 0.0], 'GSM3896110': [1.0, 84.0, 1.0], 'GSM3896111': [1.0, 75.0, 0.0], 'GSM3896112': [0.0, 75.0, 1.0], 'GSM3896113': [0.0, 92.0, 0.0], 'GSM3896114': [0.0, 81.0, 0.0], 'GSM3896115': [0.0, 77.0, 0.0], 'GSM3896116': [1.0, 88.0, 1.0], 'GSM3896117': [1.0, 87.0, 0.0], 'GSM3896118': [1.0, 77.0, 0.0], 'GSM3896119': [1.0, 93.0, 1.0], 'GSM3896120': [1.0, 97.0, 0.0], 'GSM3896121': [1.0, 89.0, 1.0], 'GSM3896122': [1.0, 88.0, 1.0], 'GSM3896123': [1.0, 73.0, 1.0], 'GSM3896124': [1.0, 91.0, 0.0], 'GSM3896125': [1.0, 91.0, 0.0], 'GSM3896126': [0.0, 78.0, 0.0], 'GSM3896127': [1.0, 89.0, 1.0], 'GSM3896128': [1.0, 78.0, 0.0], 'GSM3896129': [1.0, 90.0, 0.0], 'GSM3896130': [1.0, 85.0, 1.0], 'GSM3896131': [1.0, 85.0, 0.0], 'GSM3896132': [1.0, 82.0, 1.0], 'GSM3896133': [1.0, 82.0, 1.0], 'GSM3896134': [0.0, 72.0, 1.0], 'GSM3896135': [0.0, 82.0, 0.0], 'GSM3896136': [0.0, 81.0, 0.0], 'GSM3896137': [0.0, 81.0, 1.0], 'GSM3896138': [0.0, 79.0, 1.0], 'GSM3896139': [0.0, 91.0, 1.0], 'GSM3896140': [0.0, 81.0, 1.0], 'GSM3896141': [0.0, 70.0, 0.0], 'GSM3896142': [0.0, 76.0, 1.0], 'GSM3896143': [0.0, 90.0, 1.0], 'GSM3896144': [0.0, 83.0, 1.0], 'GSM3896145': [0.0, 83.0, 1.0]}\n",
      "Clinical data saved to ../../output/preprocess/Alzheimers_Disease/clinical_data/GSE132903.csv\n",
      "Linking clinical and genetic data...\n",
      "Linked data shape: (195, 18802)\n",
      "Handling missing values...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linked data shape after handling missing values: (195, 18802)\n",
      "Checking for bias in trait distribution...\n",
      "For the feature 'Alzheimers_Disease', the least common label is '1.0' with 97 occurrences. This represents 49.74% of the dataset.\n",
      "The distribution of the feature 'Alzheimers_Disease' in this dataset is fine.\n",
      "\n",
      "Quartiles for 'Age':\n",
      "  25%: 80.5\n",
      "  50% (Median): 85.0\n",
      "  75%: 90.0\n",
      "Min: 70.0\n",
      "Max: 102.0\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 96 occurrences. This represents 49.23% of the dataset.\n",
      "The distribution of the feature 'Gender' in this dataset is fine.\n",
      "\n",
      "Dataset usability: True\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linked data saved to ../../output/preprocess/Alzheimers_Disease/GSE132903.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Normalize gene symbols in the gene expression data\n",
    "print(\"Normalizing gene symbols...\")\n",
    "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n",
    "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n",
    "\n",
    "# Save the normalized gene data to a CSV file\n",
    "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
    "normalized_gene_data.to_csv(out_gene_data_file)\n",
    "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n",
    "\n",
    "# 2. Link the clinical and genetic data\n",
    "print(\"Loading the original clinical data...\")\n",
    "# Get the matrix file again to ensure we have the proper data\n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n",
    "\n",
    "print(\"Extracting clinical features...\")\n",
    "# Use the clinical_data obtained directly from the matrix file\n",
    "selected_clinical_df = geo_select_clinical_features(\n",
    "    clinical_df=clinical_data,\n",
    "    trait=trait,\n",
    "    trait_row=trait_row,\n",
    "    convert_trait=convert_trait,\n",
    "    age_row=age_row,\n",
    "    convert_age=convert_age,\n",
    "    gender_row=gender_row,\n",
    "    convert_gender=convert_gender\n",
    ")\n",
    "\n",
    "print(\"Clinical data preview:\")\n",
    "print(preview_df(selected_clinical_df))\n",
    "\n",
    "# Save the clinical data to a CSV file\n",
    "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "selected_clinical_df.to_csv(out_clinical_data_file)\n",
    "print(f\"Clinical data saved to {out_clinical_data_file}\")\n",
    "\n",
    "# Link clinical and genetic data using the normalized gene data\n",
    "print(\"Linking clinical and genetic data...\")\n",
    "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n",
    "print(f\"Linked data shape: {linked_data.shape}\")\n",
    "\n",
    "# 3. Handle missing values in the linked data\n",
    "print(\"Handling missing values...\")\n",
    "linked_data = handle_missing_values(linked_data, trait)\n",
    "print(f\"Linked data shape after handling missing values: {linked_data.shape}\")\n",
    "\n",
    "# 4. Check if trait is biased\n",
    "print(\"Checking for bias in trait distribution...\")\n",
    "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n",
    "\n",
    "# 5. Final validation\n",
    "note = \"Dataset contains gene expression data from bronchial brushings from control individuals and patients with asthma after rhinovirus infection in vivo, as described in the study 'Rhinovirus-induced epithelial RIG-I inflammasome suppresses antiviral immunity and promotes inflammation in asthma and COVID-19'.\"\n",
    "is_usable = validate_and_save_cohort_info(\n",
    "    is_final=True,\n",
    "    cohort=cohort,\n",
    "    info_path=json_path,\n",
    "    is_gene_available=is_gene_available,\n",
    "    is_trait_available=is_trait_available,\n",
    "    is_biased=is_biased,\n",
    "    df=linked_data,\n",
    "    note=note\n",
    ")\n",
    "\n",
    "print(f\"Dataset usability: {is_usable}\")\n",
    "\n",
    "# 6. Save 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 trait-gene association studies due to bias or other issues.\")"
   ]
  }
 ],
 "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
}