File size: 26,149 Bytes
58f02a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b98e8110",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:21:09.081716Z",
     "iopub.status.busy": "2025-03-25T06:21:09.081617Z",
     "iopub.status.idle": "2025-03-25T06:21:09.247422Z",
     "shell.execute_reply": "2025-03-25T06:21:09.247082Z"
    }
   },
   "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 = \"Adrenocortical_Cancer\"\n",
    "cohort = \"GSE49278\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Adrenocortical_Cancer\"\n",
    "in_cohort_dir = \"../../input/GEO/Adrenocortical_Cancer/GSE49278\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Adrenocortical_Cancer/GSE49278.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Adrenocortical_Cancer/gene_data/GSE49278.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Adrenocortical_Cancer/clinical_data/GSE49278.csv\"\n",
    "json_path = \"../../output/preprocess/Adrenocortical_Cancer/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04223ae3",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "85d0172f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:21:09.248875Z",
     "iopub.status.busy": "2025-03-25T06:21:09.248731Z",
     "iopub.status.idle": "2025-03-25T06:21:09.416153Z",
     "shell.execute_reply": "2025-03-25T06:21:09.415796Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Expression profiling by array of 44 adrenocortical carcinomas\"\n",
      "!Series_summary\t\"Gene expression profiles of adrenocortical carcinomas were analyzed using Affymetrix Human Gene 2.0 ST Array to identify homogeneous molecular subgroups\"\n",
      "!Series_overall_design\t\"Gene expression profiles of 44 adrenocortical carcinomas were hybridized using Affymetrix Human Gene 2.0 ST Array\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['age (years): 70', 'age (years): 26', 'age (years): 53', 'age (years): 73', 'age (years): 15', 'age (years): 51', 'age (years): 63', 'age (years): 29', 'age (years): 79', 'age (years): 45', 'age (years): 43', 'age (years): 41', 'age (years): 37', 'age (years): 81', 'age (years): 68', 'age (years): 42', 'age (years): 59', 'age (years): 39', 'age (years): 25', 'age (years): 36', 'age (years): 24', 'age (years): 49', 'age (years): 75', 'age (years): 48', 'age (years): 54', 'age (years): 28', 'age (years): 40', 'age (years): 44', 'age (years): 52', 'age (years): 30'], 1: ['gender: F', 'gender: M'], 2: ['cell type: Adrenocortical carcinoma']}\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": "50fa822c",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c93bd0ed",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:21:09.417294Z",
     "iopub.status.busy": "2025-03-25T06:21:09.417188Z",
     "iopub.status.idle": "2025-03-25T06:21:09.427771Z",
     "shell.execute_reply": "2025-03-25T06:21:09.427489Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clinical DataFrame Preview:\n",
      "{'GSM1196511': [1.0, 70.0, 0.0], 'GSM1196512': [1.0, 26.0, 0.0], 'GSM1196513': [1.0, 53.0, 0.0], 'GSM1196514': [1.0, 73.0, 1.0], 'GSM1196515': [1.0, 15.0, 0.0], 'GSM1196516': [1.0, 51.0, 0.0], 'GSM1196517': [1.0, 63.0, 1.0], 'GSM1196518': [1.0, 26.0, 0.0], 'GSM1196519': [1.0, 29.0, 1.0], 'GSM1196520': [1.0, 79.0, 0.0], 'GSM1196521': [1.0, 45.0, 0.0], 'GSM1196522': [1.0, 43.0, 0.0], 'GSM1196523': [1.0, 53.0, 0.0], 'GSM1196524': [1.0, 45.0, 0.0], 'GSM1196525': [1.0, 41.0, 0.0], 'GSM1196526': [1.0, 37.0, 0.0], 'GSM1196527': [1.0, 81.0, 0.0], 'GSM1196528': [1.0, 68.0, 1.0], 'GSM1196529': [1.0, 42.0, 0.0], 'GSM1196530': [1.0, 59.0, 0.0], 'GSM1196531': [1.0, 39.0, 0.0], 'GSM1196532': [1.0, 25.0, 0.0], 'GSM1196533': [1.0, 41.0, 0.0], 'GSM1196534': [1.0, 36.0, 0.0], 'GSM1196535': [1.0, 24.0, 0.0], 'GSM1196536': [1.0, 49.0, 0.0], 'GSM1196537': [1.0, 75.0, 0.0], 'GSM1196538': [1.0, 37.0, 0.0], 'GSM1196539': [1.0, 26.0, 0.0], 'GSM1196540': [1.0, 48.0, 0.0], 'GSM1196541': [1.0, 15.0, 0.0], 'GSM1196542': [1.0, 49.0, 0.0], 'GSM1196543': [1.0, 54.0, 1.0], 'GSM1196544': [1.0, 39.0, 1.0], 'GSM1196545': [1.0, 79.0, 0.0], 'GSM1196546': [1.0, 28.0, 0.0], 'GSM1196547': [1.0, 40.0, 0.0], 'GSM1196548': [1.0, 44.0, 0.0], 'GSM1196549': [1.0, 28.0, 0.0], 'GSM1196550': [1.0, 53.0, 0.0], 'GSM1196551': [1.0, 28.0, 1.0], 'GSM1196552': [1.0, 52.0, 1.0], 'GSM1196553': [1.0, 30.0, 0.0], 'GSM1196554': [1.0, 46.0, 0.0]}\n",
      "Clinical data saved to ../../output/preprocess/Adrenocortical_Cancer/clinical_data/GSE49278.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Gene Expression Data Availability\n",
    "# From the background information: \"Expression profiling by array\" and \"Gene expression profiles...using Affymetrix Human Gene 2.0 ST Array\"\n",
    "# This indicates the dataset contains gene expression data\n",
    "is_gene_available = True\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "# 2.1 Data Availability\n",
    "\n",
    "# Trait (Adrenocortical Cancer)\n",
    "# From characteristics dictionary, key 2 contains 'cell type: Adrenocortical carcinoma'\n",
    "# This is constant across all samples, but since it's the trait we're studying, we'll use it\n",
    "trait_row = 2\n",
    "\n",
    "# Age\n",
    "# From characteristics dictionary, key 0 contains age data\n",
    "age_row = 0\n",
    "\n",
    "# Gender\n",
    "# From characteristics dictionary, key 1 contains gender data\n",
    "gender_row = 1\n",
    "\n",
    "# 2.2 Data Type Conversion\n",
    "\n",
    "def convert_trait(value):\n",
    "    \"\"\"Convert trait value to binary (1 for adrenocortical carcinoma, 0 for normal)\"\"\"\n",
    "    if not value or ':' not in value:\n",
    "        return None\n",
    "    \n",
    "    value = value.split(':', 1)[1].strip().lower()\n",
    "    \n",
    "    if 'adrenocortical carcinoma' in value:\n",
    "        return 1\n",
    "    else:\n",
    "        return None  # We don't have controls in this dataset\n",
    "\n",
    "def convert_age(value):\n",
    "    \"\"\"Convert age value to continuous\"\"\"\n",
    "    if not value or ':' not in value:\n",
    "        return None\n",
    "    \n",
    "    try:\n",
    "        age_str = value.split(':', 1)[1].strip()\n",
    "        return float(age_str)\n",
    "    except (ValueError, TypeError):\n",
    "        return None\n",
    "\n",
    "def convert_gender(value):\n",
    "    \"\"\"Convert gender value to binary (0 for female, 1 for male)\"\"\"\n",
    "    if not value or ':' not in value:\n",
    "        return None\n",
    "    \n",
    "    gender = value.split(':', 1)[1].strip().upper()\n",
    "    \n",
    "    if gender == 'F':\n",
    "        return 0\n",
    "    elif gender == '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 and save metadata\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_df = geo_select_clinical_features(\n",
    "        clinical_df=clinical_data,  # This variable should be defined in previous steps\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 dataframe\n",
    "    preview = preview_df(clinical_df)\n",
    "    print(\"Clinical DataFrame Preview:\")\n",
    "    print(preview)\n",
    "    \n",
    "    # Save to CSV\n",
    "    os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "    clinical_df.to_csv(out_clinical_data_file, index=False)\n",
    "    print(f\"Clinical data saved to {out_clinical_data_file}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "820392fa",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6aecc97c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:21:09.428778Z",
     "iopub.status.busy": "2025-03-25T06:21:09.428675Z",
     "iopub.status.idle": "2025-03-25T06:21:09.695629Z",
     "shell.execute_reply": "2025-03-25T06:21:09.695285Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First 20 gene/probe identifiers:\n",
      "Index(['16650001', '16650003', '16650005', '16650007', '16650009', '16650011',\n",
      "       '16650013', '16650015', '16650017', '16650019', '16650021', '16650023',\n",
      "       '16650025', '16650027', '16650029', '16650031', '16650033', '16650035',\n",
      "       '16650037', '16650041'],\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": "67d98e66",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b02afc8b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:21:09.696854Z",
     "iopub.status.busy": "2025-03-25T06:21:09.696736Z",
     "iopub.status.idle": "2025-03-25T06:21:09.698968Z",
     "shell.execute_reply": "2025-03-25T06:21:09.698672Z"
    }
   },
   "outputs": [],
   "source": [
    "# Examining the gene identifiers to determine if they are human gene symbols\n",
    "# These identifiers (e.g., '16650001') appear to be numeric probe IDs, not human gene symbols\n",
    "# Human gene symbols typically have alphanumeric patterns like \"BRCA1\", \"TP53\", etc.\n",
    "# These appear to be probeset IDs from a microarray platform that need to be mapped to gene symbols\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebc843e2",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "69a85a70",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:21:09.700020Z",
     "iopub.status.busy": "2025-03-25T06:21:09.699908Z",
     "iopub.status.idle": "2025-03-25T06:21:12.626191Z",
     "shell.execute_reply": "2025-03-25T06:21:12.625811Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene annotation preview:\n",
      "{'ID': ['16657436', '16657440', '16657445', '16657447', '16657450'], 'RANGE_STRAND': ['+', '+', '+', '+', '+'], 'RANGE_START': [12190.0, 29554.0, 69091.0, 160446.0, 317811.0], 'RANGE_END': [13639.0, 31109.0, 70008.0, 161525.0, 328581.0], 'total_probes': [25.0, 28.0, 8.0, 13.0, 36.0], 'GB_ACC': ['NR_046018', nan, nan, nan, 'NR_024368'], 'SPOT_ID': ['chr1:12190-13639', 'chr1:29554-31109', 'chr1:69091-70008', 'chr1:160446-161525', 'chr1:317811-328581'], 'RANGE_GB': ['NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10']}\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": "ef7a893f",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6f714bd5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:21:12.627469Z",
     "iopub.status.busy": "2025-03-25T06:21:12.627346Z",
     "iopub.status.idle": "2025-03-25T06:21:13.943641Z",
     "shell.execute_reply": "2025-03-25T06:21:13.943259Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original gene expression data shape: (53617, 44)\n",
      "\n",
      "Using probe IDs as gene identifiers (first 5 rows):\n",
      "          GSM1196511  GSM1196512  GSM1196513  GSM1196514  GSM1196515  \\\n",
      "Gene                                                                   \n",
      "16650001    3.114460    2.761934    3.191700    2.981038    3.113831   \n",
      "16650003    2.070307    1.831540    2.303189    2.430376    1.507325   \n",
      "16650005    2.532754    3.371765    2.264750    2.647668    2.559651   \n",
      "16650007    1.968311    2.229541    1.762466    2.827752    1.626150   \n",
      "16650009    1.418189    1.314710    1.571579    1.233351    1.753973   \n",
      "\n",
      "          GSM1196516  GSM1196517  GSM1196518  GSM1196519  GSM1196520  ...  \\\n",
      "Gene                                                                  ...   \n",
      "16650001    2.687413    3.468881    2.411585    3.761057    2.974074  ...   \n",
      "16650003    2.382929    2.808405    2.031501    2.797925    2.567698  ...   \n",
      "16650005    3.508271    1.959297    2.764491    2.655120    1.712738  ...   \n",
      "16650007    2.184046    1.214179    1.664709    1.559880    2.373817  ...   \n",
      "16650009    1.033928    1.259945    1.239220    1.104874    1.285327  ...   \n",
      "\n",
      "          GSM1196545  GSM1196546  GSM1196547  GSM1196548  GSM1196549  \\\n",
      "Gene                                                                   \n",
      "16650001    2.440173    2.954796    3.445082    3.388275    2.450530   \n",
      "16650003    1.833832    2.025689    2.493108    2.313400    1.594192   \n",
      "16650005    2.165530    4.164357    3.455904    4.223868    2.515237   \n",
      "16650007    3.381329    2.235444    2.027248    1.226888    1.948129   \n",
      "16650009    1.765765    0.843827    1.600953    1.369317    0.956487   \n",
      "\n",
      "          GSM1196550  GSM1196551  GSM1196552  GSM1196553  GSM1196554  \n",
      "Gene                                                                  \n",
      "16650001    2.293126    3.136449    2.748609    3.587116    3.194252  \n",
      "16650003    2.106784    3.733405    2.427485    3.297156    1.920650  \n",
      "16650005    2.956488    3.047515    1.870629    2.264684    4.401433  \n",
      "16650007    1.840212    2.096553    2.489499    2.316459    1.641595  \n",
      "16650009    1.137052    1.658009    1.689291    1.196682    1.994568  \n",
      "\n",
      "[5 rows x 44 columns]\n",
      "\n",
      "Number of genes/probes: 53617\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene data saved to ../../output/preprocess/Adrenocortical_Cancer/gene_data/GSE49278.csv\n",
      "\n",
      "NOTE: For this dataset, probe IDs are being used directly as gene identifiers\n",
      "because appropriate gene mapping information was not available in the provided annotation.\n"
     ]
    }
   ],
   "source": [
    "# Since we're facing difficulties in mapping the probe IDs to gene symbols,\n",
    "# we'll take a different approach for this platform (Affymetrix Human Gene 2.0 ST Array)\n",
    "\n",
    "print(\"Original gene expression data shape:\", gene_data.shape)\n",
    "\n",
    "# For this Affymetrix platform, we could try to load a proper annotation,\n",
    "# but as an alternative approach, we'll use the probe IDs directly as gene identifiers\n",
    "# This is a valid fallback when proper mapping isn't available\n",
    "\n",
    "# Clean and prepare the gene data\n",
    "gene_data.index.name = 'Gene'  # Rename index to 'Gene' to maintain consistency with later steps\n",
    "\n",
    "# Since we're using probe IDs as gene identifiers, no mapping is applied\n",
    "# but we need to ensure the data is in the right format for subsequent steps\n",
    "\n",
    "# Preview the gene data\n",
    "print(\"\\nUsing probe IDs as gene identifiers (first 5 rows):\")\n",
    "print(gene_data.head())\n",
    "\n",
    "# Count the number of genes\n",
    "print(f\"\\nNumber of genes/probes: {len(gene_data)}\")\n",
    "\n",
    "# Create the output directory if it doesn't exist\n",
    "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
    "\n",
    "# Save the gene data to a CSV file\n",
    "gene_data.to_csv(out_gene_data_file)\n",
    "print(f\"Gene data saved to {out_gene_data_file}\")\n",
    "\n",
    "# Add a note about the gene mapping approach\n",
    "print(\"\\nNOTE: For this dataset, probe IDs are being used directly as gene identifiers\")\n",
    "print(\"because appropriate gene mapping information was not available in the provided annotation.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a992b1e8",
   "metadata": {},
   "source": [
    "### Step 7: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1b016c96",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:21:13.944949Z",
     "iopub.status.busy": "2025-03-25T06:21:13.944832Z",
     "iopub.status.idle": "2025-03-25T06:21:43.828158Z",
     "shell.execute_reply": "2025-03-25T06:21:43.827839Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading original gene expression data...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene expression data saved to ../../output/preprocess/Adrenocortical_Cancer/gene_data/GSE49278.csv\n",
      "Gene data shape: (53617, 44)\n",
      "Clinical data columns: ['GSM1196511', 'GSM1196512', 'GSM1196513', 'GSM1196514', 'GSM1196515', 'GSM1196516', 'GSM1196517', 'GSM1196518', 'GSM1196519', 'GSM1196520', 'GSM1196521', 'GSM1196522', 'GSM1196523', 'GSM1196524', 'GSM1196525', 'GSM1196526', 'GSM1196527', 'GSM1196528', 'GSM1196529', 'GSM1196530', 'GSM1196531', 'GSM1196532', 'GSM1196533', 'GSM1196534', 'GSM1196535', 'GSM1196536', 'GSM1196537', 'GSM1196538', 'GSM1196539', 'GSM1196540', 'GSM1196541', 'GSM1196542', 'GSM1196543', 'GSM1196544', 'GSM1196545', 'GSM1196546', 'GSM1196547', 'GSM1196548', 'GSM1196549', 'GSM1196550', 'GSM1196551', 'GSM1196552', 'GSM1196553', 'GSM1196554']\n",
      "Transposed clinical data shape: (43, 3)\n",
      "Linked data shape: (44, 53620)\n",
      "Linked data columns (first 5): ['Adrenocortical_Cancer', 'Age', 'Gender', '16650001', '16650003']\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After handling missing values, linked data shape: (43, 53620)\n",
      "Quartiles for 'Adrenocortical_Cancer':\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 'Adrenocortical_Cancer' in this dataset is severely biased.\n",
      "\n",
      "Quartiles for 'Age':\n",
      "  25%: 29.5\n",
      "  50% (Median): 43.0\n",
      "  75%: 53.0\n",
      "Min: 15.0\n",
      "Max: 81.0\n",
      "The distribution of the feature 'Age' in this dataset is fine.\n",
      "\n",
      "For the feature 'Gender', the least common label is '1.0' with 8 occurrences. This represents 18.60% of the dataset.\n",
      "The distribution of the feature 'Gender' in this dataset is fine.\n",
      "\n",
      "Is trait biased: True\n",
      "Dataset is not usable for trait-gene association studies.\n"
     ]
    }
   ],
   "source": [
    "# 1. Since normalization of gene symbols failed (no IDs recognized), let's use the original gene data\n",
    "print(\"Loading original gene expression data...\")\n",
    "gene_data = get_genetic_data(matrix_file)\n",
    "\n",
    "# Save the gene data with original probe IDs\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",
    "print(f\"Gene data shape: {gene_data.shape}\")\n",
    "\n",
    "# 2. Load previously saved clinical data and link with genetic data\n",
    "clinical_data_path = out_clinical_data_file\n",
    "clinical_data = pd.read_csv(clinical_data_path)\n",
    "print(f\"Clinical data columns: {clinical_data.columns.tolist()}\")\n",
    "\n",
    "# The clinical data CSV likely has columns named by sample IDs from transpose\n",
    "# We need to set the columns as index before linking\n",
    "clinical_data = clinical_data.set_index(clinical_data.columns[0])\n",
    "\n",
    "# Transpose clinical data to have samples as rows and features as columns\n",
    "clinical_data = clinical_data.T\n",
    "print(f\"Transposed clinical data shape: {clinical_data.shape}\")\n",
    "\n",
    "# Rename the columns to standard names\n",
    "if len(clinical_data.columns) >= 3:\n",
    "    clinical_data.columns = [trait, 'Age', 'Gender']\n",
    "elif len(clinical_data.columns) == 2:\n",
    "    clinical_data.columns = [trait, 'Age']\n",
    "elif len(clinical_data.columns) == 1:\n",
    "    clinical_data.columns = [trait]\n",
    "\n",
    "# Transpose gene data to have samples as rows and genes as columns\n",
    "gene_data_t = gene_data.T\n",
    "\n",
    "# Merge clinical and genetic data\n",
    "linked_data = pd.concat([clinical_data, gene_data_t], axis=1)\n",
    "print(f\"Linked data shape: {linked_data.shape}\")\n",
    "\n",
    "# Check for the trait column\n",
    "print(f\"Linked data columns (first 5): {linked_data.columns[:5].tolist()}\")\n",
    "\n",
    "# 3. Handle missing values in the linked data\n",
    "linked_data = handle_missing_values(linked_data, trait)\n",
    "print(f\"After handling missing values, linked data shape: {linked_data.shape}\")\n",
    "\n",
    "# 4. Determine whether the trait and demographic features are severely biased\n",
    "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n",
    "print(f\"Is trait biased: {is_biased}\")\n",
    "\n",
    "# 5. Conduct final quality validation and save cohort information\n",
    "note = \"Dataset containing gene expression profiles of adrenocortical carcinomas. All samples are cancer cases (no controls), making the trait binary variable biased.\"\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",
    "# 6. If the linked data is usable, save it\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.\")"
   ]
  }
 ],
 "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
}