File size: 29,604 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
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a116024b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:15:15.098589Z",
     "iopub.status.busy": "2025-03-25T05:15:15.098483Z",
     "iopub.status.idle": "2025-03-25T05:15:15.263296Z",
     "shell.execute_reply": "2025-03-25T05:15:15.262943Z"
    }
   },
   "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 = \"Essential_Thrombocythemia\"\n",
    "cohort = \"GSE12295\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Essential_Thrombocythemia\"\n",
    "in_cohort_dir = \"../../input/GEO/Essential_Thrombocythemia/GSE12295\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Essential_Thrombocythemia/GSE12295.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Essential_Thrombocythemia/gene_data/GSE12295.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Essential_Thrombocythemia/clinical_data/GSE12295.csv\"\n",
    "json_path = \"../../output/preprocess/Essential_Thrombocythemia/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f87c00ef",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "70f656be",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:15:15.264744Z",
     "iopub.status.busy": "2025-03-25T05:15:15.264604Z",
     "iopub.status.idle": "2025-03-25T05:15:15.282299Z",
     "shell.execute_reply": "2025-03-25T05:15:15.282012Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Class prediction models of thrombocytosis using genetic biomarkers\"\n",
      "!Series_summary\t\"Using custom spotted oligonucelotide platelet-focused arrays, platelet samples were compared. 48 health controls, 23 reactive thrombocytosis (RT) and 24 essential thrombocythemia (ET) samples were compared. An 11-biomarker gene subset was identified that discriminated among the three cohorts with 86.3% accuracy.\"\n",
      "!Series_overall_design\t\"70 mer oligonucleotides were spotted in quadruplicate and hybridized versus reference RNA in two color method. Spiked control RNAs were also included.\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['Essential thrombocythemia Patient Platelet', 'Reactive Thrombocytosis Patient platelets', 'Normal Patient Platelets']}\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": "55ea216e",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f78c13c9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:15:15.283240Z",
     "iopub.status.busy": "2025-03-25T05:15:15.283137Z",
     "iopub.status.idle": "2025-03-25T05:15:15.292458Z",
     "shell.execute_reply": "2025-03-25T05:15:15.292184Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preview of clinical features:\n",
      "{'GSM309072': [1.0], 'GSM309073': [1.0], 'GSM309074': [1.0], 'GSM309075': [1.0], 'GSM309076': [1.0], 'GSM309077': [1.0], 'GSM309078': [1.0], 'GSM309079': [1.0], 'GSM309080': [1.0], 'GSM309081': [1.0], 'GSM309082': [1.0], 'GSM309083': [1.0], 'GSM309084': [1.0], 'GSM309085': [1.0], 'GSM309086': [1.0], 'GSM309087': [1.0], 'GSM309088': [1.0], 'GSM309089': [0.0], 'GSM309090': [1.0], 'GSM309091': [0.0], 'GSM309092': [1.0], 'GSM309093': [1.0], 'GSM309094': [1.0], 'GSM309095': [0.0], 'GSM309096': [0.0], 'GSM309097': [0.0], 'GSM309098': [0.0], 'GSM309099': [0.0], 'GSM309100': [0.0], 'GSM309101': [0.0], 'GSM309102': [0.0], 'GSM309103': [0.0], 'GSM309104': [0.0], 'GSM309105': [0.0], 'GSM309106': [0.0], 'GSM309107': [0.0], 'GSM309108': [0.0], 'GSM309109': [0.0], 'GSM309110': [0.0], 'GSM309111': [0.0], 'GSM309112': [0.0], 'GSM309113': [0.0], 'GSM309114': [0.0], 'GSM309115': [0.0], 'GSM309116': [0.0], 'GSM309117': [0.0], 'GSM309118': [0.0], 'GSM309119': [0.0], 'GSM309120': [0.0], 'GSM309121': [0.0], 'GSM309122': [0.0], 'GSM309123': [0.0], 'GSM309124': [0.0], 'GSM309125': [0.0], 'GSM309126': [0.0], 'GSM309127': [0.0], 'GSM309128': [0.0], 'GSM309129': [0.0], 'GSM309130': [0.0], 'GSM309131': [0.0], 'GSM309132': [0.0], 'GSM309133': [0.0], 'GSM309134': [0.0], 'GSM309135': [0.0], 'GSM309136': [0.0], 'GSM309137': [0.0], 'GSM309138': [0.0], 'GSM309139': [0.0], 'GSM309140': [0.0], 'GSM309141': [0.0], 'GSM309142': [0.0], 'GSM309143': [0.0], 'GSM309144': [0.0], 'GSM309145': [0.0], 'GSM309146': [0.0], 'GSM309147': [0.0], 'GSM309148': [0.0], 'GSM309149': [1.0], 'GSM309150': [1.0], 'GSM309151': [0.0], 'GSM309152': [0.0], 'GSM309153': [0.0], 'GSM309154': [0.0], 'GSM309155': [0.0], 'GSM309156': [0.0], 'GSM309157': [0.0], 'GSM309158': [0.0], 'GSM309159': [0.0], 'GSM309160': [0.0], 'GSM309161': [0.0], 'GSM309162': [0.0], 'GSM309163': [0.0], 'GSM309164': [0.0], 'GSM309165': [0.0], 'GSM309166': [1.0]}\n",
      "Clinical data saved to ../../output/preprocess/Essential_Thrombocythemia/clinical_data/GSE12295.csv\n"
     ]
    }
   ],
   "source": [
    "# Step 1: Determine gene expression data availability\n",
    "# Based on series title and overall design, this appears to be a gene expression dataset\n",
    "# using oligonucleotide arrays with platelet samples\n",
    "is_gene_available = True\n",
    "\n",
    "# Step 2: Clinical feature availability and type conversion\n",
    "# From sample characteristics, we can see that row 0 contains information about patient type\n",
    "# which includes \"Essential thrombocythemia\", \"Reactive Thrombocytosis\", and \"Normal\"\n",
    "# This is the trait information we need for Essential_Thrombocythemia\n",
    "\n",
    "# 2.1 Data Availability\n",
    "trait_row = 0  # The trait information is in row 0\n",
    "age_row = None  # No age information found\n",
    "gender_row = None  # No gender information found\n",
    "\n",
    "# 2.2 Data Type Conversion\n",
    "def convert_trait(value):\n",
    "    \"\"\"Convert trait information to binary format (0 or 1)\"\"\"\n",
    "    if isinstance(value, str):\n",
    "        # Extract value after colon if it exists\n",
    "        if ':' in value:\n",
    "            value = value.split(':', 1)[1].strip()\n",
    "        \n",
    "        # Check if \"Essential thrombocythemia\" is in the value\n",
    "        if \"Essential thrombocythemia\" in value:\n",
    "            return 1\n",
    "        elif \"Normal\" in value or \"Reactive Thrombocytosis\" in value:\n",
    "            return 0\n",
    "    return None\n",
    "\n",
    "def convert_age(value):\n",
    "    \"\"\"Convert age information to numeric format\"\"\"\n",
    "    # Not needed since age data is not available\n",
    "    return None\n",
    "\n",
    "def convert_gender(value):\n",
    "    \"\"\"Convert gender information to binary format (0 for female, 1 for male)\"\"\"\n",
    "    # Not needed since gender data is not available\n",
    "    return None\n",
    "\n",
    "# Step 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",
    "# Step 4: Clinical Feature Extraction (if trait data is available)\n",
    "if trait_row is not None:\n",
    "    # Load the clinical data (assuming clinical_data is already loaded)\n",
    "    try:\n",
    "        clinical_data\n",
    "    except NameError:\n",
    "        print(\"Clinical data not loaded yet. Loading it now.\")\n",
    "        # This is a placeholder. In a real scenario, clinical_data would be loaded from a previous step\n",
    "        # Since it's not provided, we'll create a placeholder to avoid errors\n",
    "        clinical_data = pd.DataFrame()\n",
    "    \n",
    "    # Extract clinical features\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",
    "    # Preview the dataframe\n",
    "    preview = preview_df(selected_clinical_df)\n",
    "    print(\"Preview of clinical features:\")\n",
    "    print(preview)\n",
    "    \n",
    "    # Save to CSV\n",
    "    os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "    selected_clinical_df.to_csv(out_clinical_data_file, index=True)\n",
    "    print(f\"Clinical data saved to {out_clinical_data_file}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a09001b",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d7869e98",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:15:15.293473Z",
     "iopub.status.busy": "2025-03-25T05:15:15.293368Z",
     "iopub.status.idle": "2025-03-25T05:15:15.308089Z",
     "shell.execute_reply": "2025-03-25T05:15:15.307789Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First 20 gene/probe identifiers:\n",
      "Index(['1001', '1002', '1003', '1004', '1005', '1006', '1007', '1008', '1009',\n",
      "       '1010', '1011', '1012', '1013', '1014', '1015', '1016', '1017', '1018',\n",
      "       '1019', '1020'],\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": "4a760222",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea0f74d6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:15:15.309053Z",
     "iopub.status.busy": "2025-03-25T05:15:15.308943Z",
     "iopub.status.idle": "2025-03-25T05:15:15.310666Z",
     "shell.execute_reply": "2025-03-25T05:15:15.310381Z"
    }
   },
   "outputs": [],
   "source": [
    "# These identifiers ('1001', '1002', etc.) are not human gene symbols.\n",
    "# They appear to be numeric probe IDs that need to be mapped to gene symbols.\n",
    "# Human gene symbols typically look like BRCA1, TP53, IL6, etc.\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2871c51",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3477c132",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:15:15.311677Z",
     "iopub.status.busy": "2025-03-25T05:15:15.311575Z",
     "iopub.status.idle": "2025-03-25T05:15:15.387618Z",
     "shell.execute_reply": "2025-03-25T05:15:15.387302Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene annotation preview:\n",
      "{'ID': ['1001', '1002', '1003', '1004', '1005'], 'ProbeID': ['1405_i_at', '1773_at', '200024_at', '200033_at', '200063_s_at'], 'Length': ['69', '69', '70', '70', '70'], 'TM': [76.668, 81.422, 78.843, 74.743, 74.743], 'Conc. (uM)': [50.0, 50.0, 50.0, 50.0, 50.0], 'Vol. (uL)': [200.0, 200.0, 200.0, 200.0, 200.0], 'SEQUENCE': ['AAAAGCTTCCCCAACTAAAGCCTAGAAGAGCTTCTGAGGCGCTGCTTTGTCAAAAGGAAGTCTCTAGGT', 'AGCTTAAGGATGAGACATCGGCAGAGCCTGCAACCGACTAGAGGACCTGGGTCCCGGCAGCTCTTTGCT', 'TCCTCGAACTCCTATGCCATTAAGAAGAAGGACGAGCTGGAGCGTGTGGCCAAGTCCAACCGCTGATTTT', 'TGCTACTGCAGCTGCACCTATGATTGGTTATCCAATGCCAACAGGATATTCCCAATAAGACTTTAGAAGT', 'AGAGTGAGAACTTTCCCTACCGTGTTTGATAAATGTTGTCCAGGTTCTATTGCCAAGAATGTGTTGTCCA'], 'Gene Symbol': ['CCL5', 'FNTB', '---', 'DDX5', 'NPM1'], 'ORF': ['CCL5', 'FNTB', nan, 'DDX5', 'NPM1'], 'SPOT_ID': [nan, nan, '200024_at', nan, nan]}\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": "752a4010",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3cf14881",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:15:15.388958Z",
     "iopub.status.busy": "2025-03-25T05:15:15.388686Z",
     "iopub.status.idle": "2025-03-25T05:15:15.411828Z",
     "shell.execute_reply": "2025-03-25T05:15:15.411537Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene mapping preview:\n",
      "{'ID': ['1001', '1002', '1003', '1004', '1005'], 'Gene': ['CCL5', 'FNTB', '---', 'DDX5', 'NPM1']}\n",
      "Gene expression data preview after mapping:\n",
      "{'GSM309072': [0.3613, 1.2385, 3.8373999999999997, 1.0297, 1.1287], 'GSM309073': [2.2002, 0.1765, 0.9791000000000001, -0.5281, 1.7099], 'GSM309074': [0.2529, 1.2249, 1.4834, 0.3542, 0.6599], 'GSM309075': [0.222, 1.194, 1.5643, 0.2396, 0.629], 'GSM309076': [0.0, -0.2831, 0.7523000000000001, 1.5443, 0.4921], 'GSM309077': [0.0, 0.371, -2.1963, 0.0623, -0.1802], 'GSM309078': [0.0, -0.9493, -1.4156, -1.1107, 0.0], 'GSM309079': [-3.2136, -0.3648, -0.7977, 0.0715, -0.5518], 'GSM309080': [0.0, -1.5601, -7.0526, -2.5894, -1.1948], 'GSM309081': [0.0, 1.6709, 4.8239, 0.8932, 1.7434], 'GSM309082': [-0.0265, 0.6641, 1.0421, -1.0281, -0.6279], 'GSM309083': [0.7839, -0.8244, -2.0099, -0.3996, 1.149], 'GSM309084': [-3.1558, -1.4091, -3.7744, -1.3303, -1.4615], 'GSM309085': [0.0, 0.0, 0.0, 1.0552, 0.0], 'GSM309086': [0.0, 0.0, 2.016, 0.0699, 0.0], 'GSM309087': [0.0, 0.1945, 0.0, 0.4222, 0.0], 'GSM309088': [0.0, 0.0, -1.0885, -0.0391, -0.2265], 'GSM309089': [-1.519, 0.9638, -0.6178000000000001, 0.0696, 0.6705], 'GSM309090': [0.0, -1.1378, -1.213, 0.5458, 0.2208], 'GSM309091': [0.0, 0.5391, 2.1229999999999998, 1.556, 1.3992], 'GSM309092': [0.0, 0.8574, 1.766, 0.5056, -0.2356], 'GSM309093': [0.0, -0.1333, 1.5826, 0.0189, 0.0], 'GSM309094': [0.0, 1.0775, 1.3574, 0.1331, -1.6419], 'GSM309095': [0.0, 1.3461, 1.5451000000000001, -0.2872, 1.3072], 'GSM309096': [0.0, 0.0297, -1.4036000000000002, 0.0649, -0.6549], 'GSM309097': [0.0, 0.5596, 3.9409, 0.3991, 1.7206], 'GSM309098': [0.6893, 1.3763, 3.7793, -0.2381, 1.9205], 'GSM309099': [0.0, 1.3028, 5.6052, 1.2953, 2.8148], 'GSM309100': [0.0, -0.0847, -5.5847, -1.8389, 0.7864], 'GSM309101': [0.0, 0.3395, 3.6105, 0.9164, 0.0], 'GSM309102': [0.0, -1.2011, -2.7195, -0.5296, 0.2284], 'GSM309103': [0.0, -0.6915, -3.7036, -3.9667, -0.3136], 'GSM309104': [0.0, -1.294, -1.0558, -0.9046, 0.0], 'GSM309105': [0.0, 1.551, 0.0, 1.259, 2.8663], 'GSM309106': [0.0, -0.0214, 1.0021, -0.2993, -0.7268], 'GSM309107': [0.0, -0.7853, -0.6104, -1.9839, -0.6835], 'GSM309108': [-1.4821, -0.0309, -2.4136, -0.6209, 0.2303], 'GSM309109': [-0.6151, 0.1763, 1.9387, 1.0081, -2.5828], 'GSM309110': [-0.1323, 0.6175, 0.7125000000000001, -0.1722, -0.0692], 'GSM309111': [1.3508, 1.148, 4.1967, 1.1691, 0.7933], 'GSM309112': [0.0409, 1.0696, 2.2758, 0.3195, 0.7553], 'GSM309113': [-0.3511, 0.8563, -0.4036, 1.012, 0.8532], 'GSM309114': [0.0, 1.7623, 0.0, 0.018, 0.0], 'GSM309115': [0.0, 0.076, -2.5332, 1.846, 1.2725], 'GSM309116': [-0.9815, -0.302, 0.0, 0.0, 0.0], 'GSM309117': [0.0, 0.6294, 4.6808, 1.1432, 2.0719], 'GSM309118': [0.0, 2.4793, 4.9186, 0.0, 1.8317], 'GSM309119': [0.0, 1.5272, 3.3418, 1.4698, 1.0476], 'GSM309120': [0.0, 0.2517, -0.5357, -0.7192, -1.7276], 'GSM309121': [0.0, -1.4377, -2.2374, -1.9243, -2.5887], 'GSM309122': [0.0, -1.7137, -3.3864, -3.8124, -3.3397], 'GSM309123': [0.0, 1.656, 3.4787999999999997, 0.5698, 1.5321], 'GSM309124': [0.0, 2.3282, 1.4104, 1.6687, 2.0998], 'GSM309125': [0.0, 0.8636, 2.8536, 1.07, 0.8455], 'GSM309126': [0.0, -1.4459, -1.0336, -0.5223, -2.1063], 'GSM309127': [0.0, 1.4965, 3.6075, 0.129, 3.1556], 'GSM309128': [0.1379, 1.3935, 2.7613, 0.9427, 2.1999], 'GSM309129': [1.2156, 2.8242, 6.8843, 2.2627, 4.7448], 'GSM309130': [0.0, -0.7558, 1.5112, -1.5102, 0.0], 'GSM309131': [0.3525, 1.8534, 1.1275, 1.844, 0.0], 'GSM309132': [0.0, 1.7093, 2.6828000000000003, 1.0324, 1.7875], 'GSM309133': [0.0, 1.3018, 1.491, 0.5426, -0.117], 'GSM309134': [-0.2891, 1.1009, 1.9446999999999999, -0.375, -0.0111], 'GSM309135': [0.0, 0.9167, 2.5248999999999997, -0.7523, -0.063], 'GSM309136': [0.0, -0.6571, 0.0, -0.8067, -0.9832], 'GSM309137': [0.0, 0.8455, -0.3629, -1.0066, 0.3992], 'GSM309138': [0.0, 0.9634, 0.814, 0.6151, 2.3526], 'GSM309139': [0.0, -0.9418, -1.0356, -1.767, -0.0553], 'GSM309140': [-0.1069, -1.387, -2.0241, -0.5792, -1.6081], 'GSM309141': [0.0, -0.0727, 0.1575, -1.3271, -0.3351], 'GSM309142': [1.8101, 2.6405, 6.577400000000001, 3.0272, 3.1111], 'GSM309143': [0.0, -1.2786, -2.9162, -2.8368, -1.7426], 'GSM309144': [0.0, 0.658, 1.3676, 0.5045, -1.0619], 'GSM309145': [0.0, -1.1593, -0.585, -0.2536, -0.4637], 'GSM309146': [0.0, -1.1261, 2.6986, -0.8031, -0.6314], 'GSM309147': [0.0, 0.0, 0.0, -1.2803, -0.5364], 'GSM309148': [0.0, -3.3032, 1.5988, -1.1619, -0.4155], 'GSM309149': [0.0, 0.0, -3.7561999999999998, -2.354, -1.8862], 'GSM309150': [-2.0645, -5.4597, -1.2686, -3.0706, 0.0], 'GSM309151': [0.274, 0.0, -3.7332, -0.4449, -0.0831], 'GSM309152': [0.0, 0.0, -1.8303, -2.0458, 0.0], 'GSM309153': [0.0, 0.0, -3.3146, -3.7171, -2.2778], 'GSM309154': [0.0, 0.0, 0.0, -0.8025, -0.8618], 'GSM309155': [0.0, -2.155, -3.4703, -2.039, -0.96], 'GSM309156': [0.0, 0.0, 0.5435, 2.3734, 1.6223], 'GSM309157': [0.0, -0.5322, -0.8053999999999999, 0.4497, -0.7048], 'GSM309158': [0.0, -0.0007, 1.813, 1.2003, 1.9106], 'GSM309159': [0.0, -0.1732, -0.1768, 0.1513, 0.3777], 'GSM309160': [0.0, -0.5567, -1.0189, 0.5674, 0.4684], 'GSM309161': [0.0742, -2.559, -3.0284, -2.3005, -1.3133], 'GSM309162': [0.0, -1.1636, -1.3293, -2.0252, -0.4163], 'GSM309163': [0.7325, 0.1887, 0.19659999999999997, -0.053, 1.1867], 'GSM309164': [-1.295, -1.1098, -1.2967, -0.0183, 1.2539], 'GSM309165': [0.6059, -1.4014, -0.6243, -0.8428, -1.1621], 'GSM309166': [0.0, -0.1921, 1.275, 0.5519, 1.2219]}\n",
      "Shape of gene expression data: (421, 95)\n"
     ]
    }
   ],
   "source": [
    "# 1. Identify the columns for gene identifiers and gene symbols in the annotation data\n",
    "# From the preview, we can see that:\n",
    "# - 'ID' in gene_annotation corresponds to gene identifiers in gene_data\n",
    "# - 'Gene Symbol' contains the corresponding gene symbols\n",
    "\n",
    "# 2. Get a gene mapping dataframe by extracting these two columns\n",
    "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene Symbol')\n",
    "\n",
    "# Print a preview of the mapping to verify\n",
    "print(\"Gene mapping preview:\")\n",
    "print(preview_df(gene_mapping))\n",
    "\n",
    "# 3. Apply the gene mapping to convert probe-level measurements to gene-level expression\n",
    "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n",
    "\n",
    "# Print the first few rows of the resulting gene expression data\n",
    "print(\"Gene expression data preview after mapping:\")\n",
    "print(preview_df(gene_data))\n",
    "\n",
    "# Print the shape of the resulting gene expression dataframe\n",
    "print(\"Shape of gene expression data:\", gene_data.shape)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a4e2e09",
   "metadata": {},
   "source": [
    "### Step 7: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a2c156a9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:15:15.412941Z",
     "iopub.status.busy": "2025-03-25T05:15:15.412833Z",
     "iopub.status.idle": "2025-03-25T05:15:15.612103Z",
     "shell.execute_reply": "2025-03-25T05:15:15.611767Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalizing gene symbols...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene data shape after normalization: (400, 95)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalized gene data saved to ../../output/preprocess/Essential_Thrombocythemia/gene_data/GSE12295.csv\n",
      "Loading the original clinical data...\n",
      "Extracting clinical features...\n",
      "Clinical data preview:\n",
      "{'GSM309072': [1.0], 'GSM309073': [1.0], 'GSM309074': [1.0], 'GSM309075': [1.0], 'GSM309076': [1.0], 'GSM309077': [1.0], 'GSM309078': [1.0], 'GSM309079': [1.0], 'GSM309080': [1.0], 'GSM309081': [1.0], 'GSM309082': [1.0], 'GSM309083': [1.0], 'GSM309084': [1.0], 'GSM309085': [1.0], 'GSM309086': [1.0], 'GSM309087': [1.0], 'GSM309088': [1.0], 'GSM309089': [0.0], 'GSM309090': [1.0], 'GSM309091': [0.0], 'GSM309092': [1.0], 'GSM309093': [1.0], 'GSM309094': [1.0], 'GSM309095': [0.0], 'GSM309096': [0.0], 'GSM309097': [0.0], 'GSM309098': [0.0], 'GSM309099': [0.0], 'GSM309100': [0.0], 'GSM309101': [0.0], 'GSM309102': [0.0], 'GSM309103': [0.0], 'GSM309104': [0.0], 'GSM309105': [0.0], 'GSM309106': [0.0], 'GSM309107': [0.0], 'GSM309108': [0.0], 'GSM309109': [0.0], 'GSM309110': [0.0], 'GSM309111': [0.0], 'GSM309112': [0.0], 'GSM309113': [0.0], 'GSM309114': [0.0], 'GSM309115': [0.0], 'GSM309116': [0.0], 'GSM309117': [0.0], 'GSM309118': [0.0], 'GSM309119': [0.0], 'GSM309120': [0.0], 'GSM309121': [0.0], 'GSM309122': [0.0], 'GSM309123': [0.0], 'GSM309124': [0.0], 'GSM309125': [0.0], 'GSM309126': [0.0], 'GSM309127': [0.0], 'GSM309128': [0.0], 'GSM309129': [0.0], 'GSM309130': [0.0], 'GSM309131': [0.0], 'GSM309132': [0.0], 'GSM309133': [0.0], 'GSM309134': [0.0], 'GSM309135': [0.0], 'GSM309136': [0.0], 'GSM309137': [0.0], 'GSM309138': [0.0], 'GSM309139': [0.0], 'GSM309140': [0.0], 'GSM309141': [0.0], 'GSM309142': [0.0], 'GSM309143': [0.0], 'GSM309144': [0.0], 'GSM309145': [0.0], 'GSM309146': [0.0], 'GSM309147': [0.0], 'GSM309148': [0.0], 'GSM309149': [1.0], 'GSM309150': [1.0], 'GSM309151': [0.0], 'GSM309152': [0.0], 'GSM309153': [0.0], 'GSM309154': [0.0], 'GSM309155': [0.0], 'GSM309156': [0.0], 'GSM309157': [0.0], 'GSM309158': [0.0], 'GSM309159': [0.0], 'GSM309160': [0.0], 'GSM309161': [0.0], 'GSM309162': [0.0], 'GSM309163': [0.0], 'GSM309164': [0.0], 'GSM309165': [0.0], 'GSM309166': [1.0]}\n",
      "Clinical data saved to ../../output/preprocess/Essential_Thrombocythemia/clinical_data/GSE12295.csv\n",
      "Linking clinical and genetic data...\n",
      "Linked data shape: (95, 401)\n",
      "Handling missing values...\n",
      "Linked data shape after handling missing values: (95, 401)\n",
      "Checking for bias in trait distribution...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For the feature 'Essential_Thrombocythemia', the least common label is '1.0' with 24 occurrences. This represents 25.26% of the dataset.\n",
      "The distribution of the feature 'Essential_Thrombocythemia' in this dataset is fine.\n",
      "\n",
      "Dataset usability: True\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linked data saved to ../../output/preprocess/Essential_Thrombocythemia/GSE12295.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 patients with Essential Thrombocythemia (ET), Polycythemia Vera (PV), and Primary Myelofibrosis (PMF).\"\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
}